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(); private Dictionary noteEndTimes = new Dictionary(); private Dictionary _holdNoteStartTimes = new Dictionary(); // 长音符状态管理 private Dictionary _holdNoteValidity = new Dictionary(); private Dictionary _holdNoteKeyHeld = new Dictionary(); // 新增:记录按键按住状态 private Dictionary> multiKeyJudgeQueues = new Dictionary>(); public void RegisterNoteForMultiKey(KeyCode key, Note note) { if (!multiKeyJudgeQueues.ContainsKey(key)) multiKeyJudgeQueues[key] = new List(); if (!multiKeyJudgeQueues[key].Contains(note)) { multiKeyJudgeQueues[key].Add(note); } } public void JudgeAllNotesForMultiKey(KeyCode key) { if (multiKeyJudgeQueues.ContainsKey(key)) { // 对同一按键的所有音符进行判定 foreach (var note in multiKeyJudgeQueues[key]) { if (note != null && !note.IsJudged()) { note.Judge(); } } multiKeyJudgeQueues[key].Clear(); } } private void Awake() { if (Instance == null) { Instance = this; } else { Destroy(gameObject); return; } } #region 长音符状态管理 public bool IsHoldNoteValid(int holdNoteId) { return _holdNoteValidity.ContainsKey(holdNoteId) && _holdNoteValidity[holdNoteId]; } public void SetHoldNoteValidity(int holdNoteId, bool isValid) { _holdNoteValidity[holdNoteId] = isValid; //Debug.Log($"设置长音符有效性: ID={holdNoteId}, Valid={isValid}"); } public void RemoveHoldNoteValidity(int holdNoteId) { if (_holdNoteValidity.ContainsKey(holdNoteId)) { _holdNoteValidity.Remove(holdNoteId); } } // 新增:注册/更新按键按住状态 public void RegisterHoldNoteKeyHeld(int holdNoteId, bool isHeld) { _holdNoteKeyHeld[holdNoteId] = isHeld; } // 新增:检查按键是否按住 public bool IsHoldNoteKeyHeld(int holdNoteId) { return _holdNoteKeyHeld.ContainsKey(holdNoteId) && _holdNoteKeyHeld[holdNoteId]; } #endregion #region 长音符时间记录 public void RegisterScheduledEndTime(string noteId, float endTime) { noteEndTimes[noteId] = endTime; } public float GetScheduledEndTime(string noteId) { return noteEndTimes.ContainsKey(noteId) ? noteEndTimes[noteId] : 0f; } public void RegisterHoldNoteStart(string noteId, float startTime) { _holdNoteStartTimes[noteId] = startTime; //Debug.Log($"注册长音符开始: ID={noteId}, Time={startTime}"); } public bool IsHoldNoteActive(string noteId) { return _holdNoteStartTimes.ContainsKey(noteId) && (Time.time - _holdNoteStartTimes[noteId]) < 5f; } #endregion #region 音符判定状态 public void RegisterStartJudged(string noteID, bool state) { startJudgedNotes[noteID] = state; //Debug.Log($"[JudgeManager] RegisterStartJudged: {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]; } #endregion #region 短音符判定 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 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(); } } } #endregion #region 重置管理 public void ResetAllHoldNotes() { _holdNoteValidity.Clear(); _holdNoteStartTimes.Clear(); _holdNoteKeyHeld.Clear(); //Debug.Log("重置所有长音符状态"); } #endregion }