Files
bansonic_beta_main/Assets/scripts/gamePlay_gameplay/JudgeManager.cs
T

197 lines
5.8 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class JudgeManager : MonoBehaviour
{
public static JudgeManager Instance { get; private set; }
private Dictionary<KeyCode, Queue<Note>> judgeQueues = new Dictionary<KeyCode, Queue<Note>>();
private Dictionary<string, bool> startJudgedNotes = new Dictionary<string, bool>();
private Dictionary<string, bool> releasedNotes = new Dictionary<string, bool>();
private Dictionary<string, float> noteEndTimes = new Dictionary<string, float>();
private Dictionary<string, float> _holdNoteStartTimes = new Dictionary<string, float>();
// 长音符状态管理
private Dictionary<int, bool> _holdNoteValidity = new Dictionary<int, bool>();
private Dictionary<int, bool> _holdNoteKeyHeld = new Dictionary<int, bool>(); // 新增:记录按键按住状态
private Dictionary<KeyCode, List<Note>> multiKeyJudgeQueues = new Dictionary<KeyCode, List<Note>>();
public void RegisterNoteForMultiKey(KeyCode key, Note note)
{
if (!multiKeyJudgeQueues.ContainsKey(key))
multiKeyJudgeQueues[key] = new List<Note>();
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<Note>();
if (!judgeQueues[key].Contains(note))
{
judgeQueues[key].Enqueue(note);
}
}
public void UnregisterNote(KeyCode key, Note note)
{
if (!judgeQueues.ContainsKey(key)) return;
Queue<Note> newQueue = new Queue<Note>();
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
}