EerieVillage/Assets/Scripts/Mechanics/MonsterRandomizer.cs

58 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
namespace Platformer.Mechanics
{
/// <summary>
/// 몬스터 종류 랜덤 영역 + 수동 idle animation.
/// PD 지시 (2026-05-10): 6 종 (M001~M006) random + animation 영역.
/// Animator 영역 영역 (sprite 자동 영역 영역 X) — 4 frame idle loop 수동 영역.
/// </summary>
public class MonsterRandomizer : MonoBehaviour
{
[Tooltip("6 종 × 4 frame idle sprite (24 sprite·6 group). Inspector 영역 영역 — 0~3=M001·4~7=M002·...")]
public Sprite[] idleFrames;
[Tooltip("frame 영역 (초). 0.15s = 1 frame")]
public float frameInterval = 0.15f;
const int FramesPerMonster = 4;
int _monsterIdx;
int _frame;
float _elapsed;
SpriteRenderer _sr;
void Awake()
{
_sr = GetComponent<SpriteRenderer>();
var anim = GetComponent<Animator>();
if (anim != null) anim.enabled = false; // sprite 자동 영역 영역
int monsters = (idleFrames != null) ? idleFrames.Length / FramesPerMonster : 0;
if (monsters <= 0) return;
_monsterIdx = Random.Range(0, monsters);
_frame = 0;
_elapsed = 0f;
ApplyFrame();
}
void Update()
{
if (_sr == null || idleFrames == null) return;
_elapsed += Time.deltaTime;
if (_elapsed >= frameInterval)
{
_elapsed = 0f;
_frame = (_frame + 1) % FramesPerMonster;
ApplyFrame();
}
}
void ApplyFrame()
{
int idx = _monsterIdx * FramesPerMonster + _frame;
if (idx >= 0 && idx < idleFrames.Length && idleFrames[idx] != null)
_sr.sprite = idleFrames[idx];
}
}
}