Files
ichni_Creator_Studio/Assets/Scripts/Manager/NoteManager.cs

74 lines
2.5 KiB
C#
Raw Normal View History

2025-10-03 06:46:05 -04:00
using System.Collections;
using System.Collections.Generic;
using UniRx;
2025-10-03 06:46:05 -04:00
using UnityEngine;
namespace Ichni.RhythmGame
{
public class NoteManager : MonoBehaviour
{
public List<(NoteBase note, float activationTime, float finishTime)> pendingNotes = new List<(NoteBase, float, float)>();
2025-10-03 06:46:05 -04:00
private int nextNoteIndex = 0;
private List<(NoteBase note1, bool isActive, float activationTime)> ProcessingNotes = new List<(NoteBase, bool, float)>();
2025-10-03 06:46:05 -04:00
public void RegisterNote(NoteBase note, float activationTime, float finishTime)
{
pendingNotes.Add((note, activationTime, finishTime));
AllNotesRegistered();
print($"Registered note {note.elementName} with activation time {activationTime} and finish time {finishTime}");
2025-10-03 06:46:05 -04:00
}
// 在所有物体注册完毕后,对列表进行一次排序
public void AllNotesRegistered()
{
pendingNotes.Sort((a, b) => a.activationTime.CompareTo(b.activationTime));
}
void Update()
2025-10-03 06:46:05 -04:00
{
float currentTime = EditorManager.instance.songInformation.songTime;
foreach (var item in ProcessingNotes)
{
var (note, isActive, activationTime) = item;
if (!isActive) note.Update();
note.gameObject.SetActive(isActive);
if (isActive)
{
note.Update();
if (currentTime < activationTime)
{
note.noteVisual?.Recover();
}
else if (note is Hold hold && currentTime >= hold.holdEndTime)
{
hold.SetFinishEffects();
}
}
}
ProcessingNotes.Clear();
// 一次性移除所有 null 项
pendingNotes.RemoveAll(item => item.note == null);
foreach (var item in pendingNotes)
2025-10-03 06:46:05 -04:00
{
var (note, activationTime, finishTime) = item;
bool shouldBeActive = currentTime >= activationTime && currentTime <= finishTime;
bool isActive = note.gameObject.activeSelf;
if (shouldBeActive && !isActive)
{
ProcessingNotes.Add((note, true, activationTime));
2025-10-03 06:46:05 -04:00
}
else if (!shouldBeActive && isActive)
2025-10-03 06:46:05 -04:00
{
ProcessingNotes.Add((note, false, activationTime));
2025-10-03 06:46:05 -04:00
}
}
2025-10-03 06:46:05 -04:00
}
}
}