HeavenStudio/Assets/Scripts/Util/BeatAction.cs
minenice55 60d29f19c6
Timekeeping Improvements and Small Optimizations (#544)
* make BeatActions coroutines instead of componentrs

* pooled scheduled sounds

implement S' entity seek

* remove debug prints from last two changes

* implement absolute time tracking

implement DSP time resyncing

* optimize GameManager

* update TMPro

* update IDE packages

* fix dsp sync making the drift worse

* fix issue with the JSL dll

* relocate debug print

* make scheduled pitch setter functional

* any cpu
2023-09-11 22:28:04 +00:00

67 lines
1.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace HeavenStudio.Util
{
public class BeatAction
{
private int index;
private List<Action> actions = new List<Action>();
private Coroutine coroutine;
private MonoBehaviour behaviour;
public delegate void EventCallback();
public class Action
{
public double beat { get; set; }
public EventCallback function { get; set; }
public Action(double beat, EventCallback function)
{
this.beat = beat;
this.function = function;
}
}
public static BeatAction New(MonoBehaviour behaviour, List<Action> actions)
{
if (behaviour == null)
{
Debug.LogWarning("Starting a BeatAction with no assigned behaviour. The Conductor will be used instead.");
behaviour = Conductor.instance;
}
BeatAction beatAction = new BeatAction();
beatAction.actions = actions;
beatAction.behaviour = behaviour;
beatAction.coroutine = behaviour.StartCoroutine(beatAction.BeatActionRoutine());
return beatAction;
}
IEnumerator BeatActionRoutine()
{
int idx = 0;
WaitUntil waitUntil = new WaitUntil(() => Conductor.instance.songPositionInBeatsAsDouble >= actions[idx].beat || !Conductor.instance.isPlaying);
while (idx < actions.Count)
{
yield return waitUntil;
if (!Conductor.instance.isPlaying)
yield break;
actions[idx].function.Invoke();
idx++;
}
this.actions = null;
yield break;
}
public void Delete()
{
behaviour.StopCoroutine(coroutine);
this.actions = null;
}
}
}