2022-01-21 07:09:32 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2023-10-29 19:44:47 +00:00
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
using System.Threading;
|
2022-01-21 07:09:32 +00:00
|
|
|
|
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
|
|
|
{
|
|
|
|
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-10-29 19:44:47 +00:00
|
|
|
public static CancellationTokenSource 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)
|
|
|
|
{
|
2024-01-15 02:04:10 +00:00
|
|
|
Debug.LogWarning("Starting a BeatAction with no assigned behaviour. The Game Manager will be used instead.");
|
|
|
|
behaviour = GameManager.instance;
|
2023-09-11 22:28:04 +00:00
|
|
|
}
|
2023-10-29 19:44:47 +00:00
|
|
|
CancellationTokenSource cancelToken = new CancellationTokenSource();
|
|
|
|
RunAsync(behaviour, actions, cancelToken.Token).Forget();
|
2023-01-15 04:33:37 +00:00
|
|
|
|
2023-10-29 19:44:47 +00:00
|
|
|
return cancelToken;
|
2022-01-21 07:09:32 +00:00
|
|
|
}
|
|
|
|
|
2023-10-29 19:44:47 +00:00
|
|
|
static async UniTask RunAsync(MonoBehaviour behaviour, List<Action> actions, CancellationToken token)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await BeatActionAsync(behaviour, actions, token);
|
|
|
|
}
|
|
|
|
catch (System.OperationCanceledException)
|
|
|
|
{
|
|
|
|
Debug.Log("BeatAction cancelled.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async UniTask BeatActionAsync(MonoBehaviour behaviour, List<Action> actions, CancellationToken token)
|
2022-01-21 07:09:32 +00:00
|
|
|
{
|
2024-01-17 23:43:26 +00:00
|
|
|
Conductor conductor = Conductor.instance;
|
2023-09-11 22:28:04 +00:00
|
|
|
int idx = 0;
|
|
|
|
while (idx < actions.Count)
|
2022-01-21 07:09:32 +00:00
|
|
|
{
|
2024-01-17 23:43:26 +00:00
|
|
|
await UniTask.WaitUntil(() => (conductor.songPositionInBeatsAsDouble >= actions[idx].beat && !conductor.WaitingForDsp) || !(conductor.isPlaying || conductor.isPaused) || behaviour == null, cancellationToken: token);
|
2023-09-11 22:28:04 +00:00
|
|
|
|
2024-01-17 23:43:26 +00:00
|
|
|
if (behaviour == null || !(conductor.isPlaying || conductor.isPaused))
|
2023-10-29 19:44:47 +00:00
|
|
|
return;
|
2023-09-11 22:28:04 +00:00
|
|
|
|
|
|
|
actions[idx].function.Invoke();
|
|
|
|
idx++;
|
2022-01-21 07:09:32 +00:00
|
|
|
}
|
2023-01-15 04:33:37 +00:00
|
|
|
}
|
2022-01-21 07:09:32 +00:00
|
|
|
}
|
|
|
|
}
|