HeavenStudio/Assets/Scripts/Util/BeatAction.cs

77 lines
2.6 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cysharp.Threading.Tasks;
using System.Threading;
2022-03-14 14:21:05 +00:00
namespace HeavenStudio.Util
{
public class BeatAction
{
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 CancellationTokenSource New(MonoBehaviour behaviour, List<Action> actions)
{
if (behaviour == null)
{
Mass Text Update (#615) * Air Rally Text Update * Blue Bear Text Update * Board Meeting Text Update * Built To Scale DS Text Update also changed Air Rally's assetbundle tag from "normal" to "keep" * Catchy Tune Text Update also changed some minor wording in Board Meeting and Built To Scale DS * Cheer Readers Text Update * The Clappy Trio Text Update * Coin Toss Text Update * Crop Stomp Text Update * DJ School Text Update * Dog Ninja Text Update * Double Date Text Update * Drumming Practice Text Update * Fan Club Text Update * Fireworks Text Update * Second Contact Text Update * Flipper-Flop Text Update also fix an error in Catchy Tune * Fork Lifter Text Update * Glee Club Text Update * Karate Man Text Update also minor updates to other games * Kitties! Text Update * Launch Party Text Update * Lockstep Text Update * Marching Orders Text Update * Meat Grinder Text Update also fixed an error in Second Contact * Mr. Upbeat Text Update * Munchy Monk Text Update * Octopus Machine Text Update * Pajama Party Text Update * Quiz Show Text Update also changed some wording in meat grinder * Rhythm Rally Text Update * Rhythm Somen Text Update that was easy * Rhythm Tweezers Text Update * Ringside Text Update * Rockers Text Update this sucked * Samurai Slice DS Text Update * See Saw Text Update * Sneaky Spirits Text Update * Spaceball Text Update * Space Dance Text Update * Space Soccer Text Update * Splashdown Text Update * Tambourine Text Update * Tap Trial Text Update * Tap Troupe Text Update * The Dazzles Text Update * Toss Boys Text Update * Tram & Pauline Text Update also added translation for Fireworks * Tunnel Text Update * Wizard's Waltz Text Update * Working Dough Text Update * fix compiler errors * fix editor offset bug(?) * fix missing param in second contact * Ball Redispense text * remove space soccer swing * Trick on the Class Text Update * Non-Game Text Update * fix pre-function sorting * camera shake ease * remove a bunch of prints * rhythm tweezers bug fix * Update Credits.txt * ssds nop samurai bop * swap order of shake properties * Update FirstContact.cs --------- Co-authored-by: minenice55 <star.elementa@gmail.com>
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;
}
CancellationTokenSource cancelToken = new CancellationTokenSource();
RunAsync(behaviour, actions, cancelToken.Token).Forget();
return cancelToken;
}
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)
{
Conductor conductor = Conductor.instance;
int idx = 0;
while (idx < actions.Count)
{
await UniTask.WaitUntil(() => (conductor.songPositionInBeatsAsDouble >= actions[idx].beat && !conductor.WaitingForDsp) || !(conductor.isPlaying || conductor.isPaused) || behaviour == null, cancellationToken: token);
if (behaviour == null || !(conductor.isPlaying || conductor.isPaused))
return;
while (conductor.songPositionInBeatsAsDouble >= actions[idx].beat)
{
try
{
actions[idx].function.Invoke();
}
catch (System.Exception e)
{
Debug.LogError($"Exception thrown while executing BeatAction: {e}");
}
idx++;
if (idx >= actions.Count) return;
}
}
}
}
}