59 lines
1.5 KiB
C#
59 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class TrackKeyManager : MonoBehaviour
|
|
{
|
|
public static TrackKeyManager Instance { get; private set; }
|
|
|
|
private Dictionary<int, Queue<KeyCode>> trackKeyMappings = new Dictionary<int, Queue<KeyCode>>();
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当音符进入判定区域时,添加按键映射
|
|
/// </summary>
|
|
public void RegisterKey(int trackIndex, KeyCode key)
|
|
{
|
|
if (!trackKeyMappings.ContainsKey(trackIndex))
|
|
trackKeyMappings[trackIndex] = new Queue<KeyCode>();
|
|
|
|
trackKeyMappings[trackIndex].Enqueue(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当音符离开判定区域时,移除按键映射
|
|
/// </summary>
|
|
public void UnregisterKey(int trackIndex, KeyCode key)
|
|
{
|
|
if (!trackKeyMappings.ContainsKey(trackIndex) || trackKeyMappings[trackIndex].Count == 0)
|
|
return;
|
|
|
|
if (trackKeyMappings[trackIndex].Peek() == key)
|
|
{
|
|
trackKeyMappings[trackIndex].Dequeue();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取轨道当前的按键(队列头)
|
|
/// </summary>
|
|
public KeyCode GetCurrentKey(int trackIndex)
|
|
{
|
|
if (trackKeyMappings.ContainsKey(trackIndex) && trackKeyMappings[trackIndex].Count > 0)
|
|
{
|
|
return trackKeyMappings[trackIndex].Peek();
|
|
}
|
|
return KeyCode.None; // 无按键
|
|
}
|
|
}
|