189 lines
5.2 KiB
C#
189 lines
5.2 KiB
C#
using UnityEngine;
|
|
|
|
public class Note : BaseNote
|
|
{
|
|
private KeyCode keyToPress;
|
|
private string noteColor;
|
|
private AnimationController anim;
|
|
private NoteController controller;
|
|
private bool isJudged = false;
|
|
|
|
[Header("判定区间配置")]
|
|
public NoteJudgeConfig judgeConfig; // 判定区间配置,需在预制体或生成时赋值
|
|
|
|
private void Awake()
|
|
{
|
|
anim = GetComponent<AnimationController>();
|
|
controller = GetComponent<NoteController>();
|
|
}
|
|
|
|
public void Setup(KeyCode key, int trackIndex, float speed, float hitTime, string color, NoteJudgeConfig judgeConfig)
|
|
{
|
|
keyToPress = key;
|
|
noteColor = color;
|
|
TrackIndex = trackIndex;
|
|
Speed = speed;
|
|
this.hitTime = hitTime;
|
|
isJudged = false;
|
|
this.judgeConfig = judgeConfig;
|
|
|
|
if (controller != null)
|
|
{
|
|
controller.SetSpeed(speed);
|
|
}
|
|
|
|
InputManager.OnKeyPressed += HandlePress;
|
|
|
|
// 判定区间配置检查
|
|
if (judgeConfig == null)
|
|
Debug.LogError($"NoteJudgeConfig is null! 判定区间配置未传入!track={trackIndex}");
|
|
else
|
|
Debug.Log($"NoteJudgeConfig: perfect={judgeConfig.perfectRange}, great={judgeConfig.greatRange}, good={judgeConfig.goodRange}, miss={judgeConfig.missRange}");
|
|
}
|
|
|
|
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);
|
|
string judgeResult = null;
|
|
|
|
if (judgeConfig != null)
|
|
{
|
|
if (timeDifference <= judgeConfig.perfectRange)
|
|
{
|
|
Debug.Log($"{keyToPress}: Perfect");
|
|
judgeResult = "Perfect";
|
|
}
|
|
else if (timeDifference <= judgeConfig.greatRange)
|
|
{
|
|
Debug.Log($"{keyToPress}: Great");
|
|
judgeResult = "Great";
|
|
}
|
|
else if (timeDifference <= judgeConfig.goodRange)
|
|
{
|
|
Debug.Log($"{keyToPress}: Good");
|
|
judgeResult = "Good";
|
|
}
|
|
else if (timeDifference <= judgeConfig.missRange)
|
|
{
|
|
Debug.Log($"{keyToPress}: Miss");
|
|
judgeResult = "Miss";
|
|
InputManager.Instance?.ShowJudgeResult(TrackIndex, judgeResult);
|
|
JudgeMiss();
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"{keyToPress}: Miss");
|
|
judgeResult = "Miss";
|
|
InputManager.Instance?.ShowJudgeResult(TrackIndex, judgeResult);
|
|
JudgeMiss();
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 兼容旧逻辑
|
|
if (timeDifference <= 0.08f)
|
|
{
|
|
Debug.Log($"{keyToPress}: Perfect");
|
|
judgeResult = "Perfect";
|
|
}
|
|
else if (timeDifference <= 0.15f)
|
|
{
|
|
Debug.Log($"{keyToPress}: Great");
|
|
judgeResult = "Great";
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"{keyToPress}: Miss");
|
|
judgeResult = "Miss";
|
|
InputManager.Instance?.ShowJudgeResult(TrackIndex, judgeResult);
|
|
JudgeMiss();
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 显示判定文本
|
|
if (!string.IsNullOrEmpty(judgeResult))
|
|
{
|
|
InputManager.Instance?.ShowJudgeResult(TrackIndex, judgeResult);
|
|
}
|
|
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)
|
|
{
|
|
anim.PlayDestroyAnimation(noteColor);
|
|
}
|
|
|
|
}
|
|
|
|
public void JudgeMiss()
|
|
{
|
|
if (isJudged) return;
|
|
isJudged = true;
|
|
Debug.Log($"{keyToPress} Miss");
|
|
// InputManager.Instance?.ShowJudgeResult(TrackIndex, "Miss"); // 已在HandlePress中调用
|
|
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();
|
|
}
|
|
}
|
|
}
|