131 lines
2.9 KiB
C#
131 lines
2.9 KiB
C#
using UnityEngine;
|
|
|
|
public class Note : BaseNote
|
|
{
|
|
private KeyCode keyToPress;
|
|
private string noteColor;
|
|
private AnimationController anim;
|
|
private NoteController controller;
|
|
private bool isJudged = false;
|
|
|
|
private void Awake()
|
|
{
|
|
anim = GetComponent<AnimationController>();
|
|
controller = GetComponent<NoteController>();
|
|
}
|
|
|
|
public void Setup(KeyCode key, int trackIndex, float speed, float hitTime, string color)
|
|
{
|
|
keyToPress = key;
|
|
noteColor = color;
|
|
TrackIndex = trackIndex;
|
|
Speed = speed;
|
|
this.hitTime = hitTime;
|
|
isJudged = false;
|
|
|
|
if (controller != null)
|
|
{
|
|
controller.SetSpeed(speed);
|
|
}
|
|
|
|
InputManager.OnKeyPressed += HandlePress;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
InputManager.OnKeyPressed -= HandlePress;
|
|
}
|
|
|
|
private void HandlePress(KeyCode key)
|
|
{
|
|
// 确保只有匹配的按键触发,并且音符处于判定区域
|
|
if (isJudged || key != keyToPress || (controller != null && !controller.IsInJudgeZone()))
|
|
return;
|
|
|
|
float timeDifference = Mathf.Abs(Time.time - hitTime);
|
|
|
|
if (timeDifference <= 0.08f)
|
|
{
|
|
Debug.Log($"短音符 {keyToPress}: Perfect");
|
|
}
|
|
else if (timeDifference <= 0.15f)
|
|
{
|
|
Debug.Log($"短音符 {keyToPress}: Great");
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"短音符 {keyToPress} Miss");
|
|
JudgeMiss();
|
|
return;
|
|
}
|
|
|
|
Judge();
|
|
}
|
|
|
|
public bool IsJudged()
|
|
{
|
|
return isJudged;
|
|
}
|
|
|
|
public void Judge()
|
|
{
|
|
if (isJudged)
|
|
return;
|
|
isJudged = true;
|
|
|
|
if (controller != null)
|
|
{
|
|
controller.StopMovement();
|
|
controller.PlayHitEffect();
|
|
}
|
|
ReturnToPool();
|
|
if (anim !=null)
|
|
{
|
|
Debug.Log("执行");
|
|
anim.PlayDestroyAnimation(noteColor);
|
|
}
|
|
|
|
}
|
|
|
|
public void JudgeMiss()
|
|
{
|
|
if (isJudged) return;
|
|
isJudged = true;
|
|
Debug.Log($"短音符 {keyToPress} Miss");
|
|
ReturnToPool();
|
|
}
|
|
|
|
private void ReturnToPool()
|
|
{
|
|
InputManager.OnKeyPressed -= HandlePress;
|
|
if (controller != null)
|
|
{
|
|
controller.ResetState();
|
|
}
|
|
gameObject.SetActive(false);
|
|
NotePool.Instance.ReturnNote(gameObject, noteColor);
|
|
}
|
|
|
|
public int GetTrackIndex()
|
|
{
|
|
return TrackIndex;
|
|
}
|
|
|
|
public KeyCode GetKey()
|
|
{
|
|
return keyToPress;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 由 Controller 调用,通知 Note 是否在判定区域内
|
|
/// </summary>
|
|
public void SetJudgeZone(bool inZone)
|
|
{
|
|
// 如果音符离开判定区域且还未判定,则判为 Miss
|
|
if (!inZone && !isJudged)
|
|
{
|
|
JudgeMiss();
|
|
}
|
|
}
|
|
}
|