using System.Collections.Generic; using UnityEngine; public class JudgeManager : MonoBehaviour { public static JudgeManager Instance { get; private set; } private Dictionary> judgeQueues = new Dictionary>(); private Dictionary startJudgedNotes = new Dictionary(); private Dictionary releasedNotes = new Dictionary(); // 记录 End 段的 scheduledEndTime(防止 End 被回收后仍然能判定) private Dictionary noteEndTimes = new Dictionary(); private void Awake() { if (Instance == null) { Instance = this; } else { Destroy(gameObject); return; } } /// /// 记录某个颜色的长音符 End 段的 scheduledEndTime /// public void RegisterScheduledEndTime(string noteColor, float endTime) { noteEndTimes[noteColor] = endTime; } /// /// 获取某个颜色的 End 段的 scheduledEndTime /// public float GetScheduledEndTime(string noteColor) { return noteEndTimes.ContainsKey(noteColor) ? noteEndTimes[noteColor] : 0f; } public void RegisterStartJudged(string noteID, bool state) { startJudgedNotes[noteID] = state; } public bool IsStartJudged(string noteID) { return startJudgedNotes.ContainsKey(noteID) && startJudgedNotes[noteID]; } public void RegisterNoteReleased(string noteID, bool state) { releasedNotes[noteID] = state; Debug.Log($"[JudgeManager] RegisterNoteReleased: {noteID} = {state}"); } public bool HasNoteReleased(string noteID) { return releasedNotes.ContainsKey(noteID) && releasedNotes[noteID]; } /// /// 当音符进入判定区域时调用 /// public void RegisterNote(KeyCode key, Note note) { if (!judgeQueues.ContainsKey(key)) judgeQueues[key] = new Queue(); if (!judgeQueues[key].Contains(note)) { judgeQueues[key].Enqueue(note); } } /// /// 当音符离开判定区域时调用 /// public void UnregisterNote(KeyCode key, Note note) { if (!judgeQueues.ContainsKey(key)) return; Queue newQueue = new Queue(); while (judgeQueues[key].Count > 0) { Note n = judgeQueues[key].Dequeue(); if (n != note) { newQueue.Enqueue(n); } else { // **如果音符未被判定,则触发 Miss** if (!n.IsJudged()) { n.JudgeMiss(); } } } judgeQueues[key] = newQueue; } /// /// 按键按下时,判定队列中最早进入的音符 /// public void JudgeEarliestNote(KeyCode key) { if (judgeQueues.ContainsKey(key) && judgeQueues[key].Count > 0) { Note note = judgeQueues[key].Dequeue(); // 只取最早的音符 if (note != null && !note.IsJudged()) { note.Judge(); } } } }