2022-01-21 07:09:32 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
2022-03-14 14:21:05 +00:00
|
|
|
namespace HeavenStudio.Util
|
2022-01-21 07:09:32 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
public class BeatAction
|
2022-01-21 07:09:32 +00:00
|
|
|
{
|
|
|
|
private int index;
|
|
|
|
private List<Action> actions = new List<Action>();
|
2023-09-11 22:28:04 +00:00
|
|
|
private Coroutine coroutine;
|
|
|
|
private MonoBehaviour behaviour;
|
2022-01-21 07:09:32 +00:00
|
|
|
|
|
|
|
public delegate void EventCallback();
|
|
|
|
|
|
|
|
public class Action
|
|
|
|
{
|
2023-01-15 04:33:37 +00:00
|
|
|
public double beat { get; set; }
|
2022-01-21 07:09:32 +00:00
|
|
|
public EventCallback function { get; set; }
|
|
|
|
|
2023-01-15 04:33:37 +00:00
|
|
|
public Action(double beat, EventCallback function)
|
2022-01-21 07:09:32 +00:00
|
|
|
{
|
|
|
|
this.beat = beat;
|
|
|
|
this.function = function;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
public static BeatAction New(MonoBehaviour behaviour, List<Action> actions)
|
2022-01-21 07:09:32 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
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();
|
2022-01-21 07:09:32 +00:00
|
|
|
beatAction.actions = actions;
|
2023-09-11 22:28:04 +00:00
|
|
|
beatAction.behaviour = behaviour;
|
|
|
|
beatAction.coroutine = behaviour.StartCoroutine(beatAction.BeatActionRoutine());
|
2023-01-15 04:33:37 +00:00
|
|
|
|
|
|
|
return beatAction;
|
2022-01-21 07:09:32 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 22:28:04 +00:00
|
|
|
IEnumerator BeatActionRoutine()
|
2022-01-21 07:09:32 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
int idx = 0;
|
|
|
|
WaitUntil waitUntil = new WaitUntil(() => Conductor.instance.songPositionInBeatsAsDouble >= actions[idx].beat || !Conductor.instance.isPlaying);
|
|
|
|
while (idx < actions.Count)
|
2022-01-21 07:09:32 +00:00
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
yield return waitUntil;
|
|
|
|
|
|
|
|
if (!Conductor.instance.isPlaying)
|
|
|
|
yield break;
|
|
|
|
|
|
|
|
actions[idx].function.Invoke();
|
|
|
|
idx++;
|
2022-01-21 07:09:32 +00:00
|
|
|
}
|
2023-09-11 22:28:04 +00:00
|
|
|
this.actions = null;
|
|
|
|
yield break;
|
2022-01-21 07:09:32 +00:00
|
|
|
}
|
2023-01-15 04:33:37 +00:00
|
|
|
|
|
|
|
public void Delete()
|
|
|
|
{
|
2023-09-11 22:28:04 +00:00
|
|
|
behaviour.StopCoroutine(coroutine);
|
|
|
|
this.actions = null;
|
2023-01-15 04:33:37 +00:00
|
|
|
}
|
2022-01-21 07:09:32 +00:00
|
|
|
}
|
|
|
|
}
|