From 5cdd044154504c98d659d9280caf4c1139ea9427 Mon Sep 17 00:00:00 2001 From: Pengu123 Date: Wed, 4 May 2022 20:05:51 +0200 Subject: [PATCH] Added a list to keep track of all the scheduled inputs of a minigame ...Along with an Util function to return the closest scheduled input --- Assets/Scripts/Games/Minigame.cs | 41 +++++++++++++++++++++-- Assets/Scripts/Games/PlayerActionEvent.cs | 5 +++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/Games/Minigame.cs b/Assets/Scripts/Games/Minigame.cs index 8df41dfb..0499e450 100644 --- a/Assets/Scripts/Games/Minigame.cs +++ b/Assets/Scripts/Games/Minigame.cs @@ -22,12 +22,14 @@ namespace HeavenStudio.Games } + public List scheduledInputs = new List(); + /** * Schedule an Input for a later time in the minigame. Executes the methods put in parameters * * float startBeat : When the scheduling started (In beats) - * float timer : How many beats later should the input be recorded - * InputType inputType : The type of the input that's expected (Press, release, A, B, Directions..) + * float timer : How many beats later should the input be expected + * InputType inputType : The type of the input that's expected (Press, release, A, B, Directions..) (Check InputType class for a list) * ActionEventCallbackState OnHit : Method to run if the Input has been Hit * ActionEventCallback OnMiss : Method to run if the Input has been Missed * ActionEventCallback OnBlank : Method to run whenever there's an Input while this is Scheduled (Shouldn't be used too much) @@ -52,17 +54,50 @@ namespace HeavenStudio.Games evt.OnMiss = OnMiss; evt.OnBlank = OnBlank; + evt.OnDestroy = RemoveScheduledInput; + evt.canHit = true; evt.enabled = true; evt.transform.parent = this.transform.parent; - //Instantiate(evtObj); evtObj.SetActive(true); + scheduledInputs.Add(evt); + return evt; } + public void RemoveScheduledInput(PlayerActionEvent evt) + { + scheduledInputs.Remove(evt); + } + + //Get the scheduled input that should happen the **Soonest** + //Can return null if there's no scheduled inputs + public PlayerActionEvent GetClosestScheduledInput() + { + PlayerActionEvent closest = null; + + foreach(PlayerActionEvent toCompare in scheduledInputs) + { + if(closest == null) + { + closest = toCompare; + } else + { + float t1 = closest.startBeat + closest.timer; + float t2 = toCompare.startBeat + toCompare.timer; + + Debug.Log("t1=" + t1 + " -- t2=" + t2); + + if (t2 < t1) closest = toCompare; + } + } + + return closest; + } + // hopefully these will fix the lowbpm problem public static float EarlyTime() { diff --git a/Assets/Scripts/Games/PlayerActionEvent.cs b/Assets/Scripts/Games/PlayerActionEvent.cs index 792ae997..3a6757a3 100644 --- a/Assets/Scripts/Games/PlayerActionEvent.cs +++ b/Assets/Scripts/Games/PlayerActionEvent.cs @@ -14,10 +14,14 @@ namespace HeavenStudio.Games public delegate void ActionEventCallback(); public delegate void ActionEventCallbackState(float state); + public delegate void ActionEventCallbackSelf(PlayerActionEvent evt); + public ActionEventCallbackState OnHit; //Function to trigger when an input has been done perfectly public ActionEventCallback OnMiss; //Function to trigger when an input has been missed public ActionEventCallback OnBlank; //Function to trigger when an input has been recorded while this is pending + public ActionEventCallbackSelf OnDestroy; //Function to trigger whenever this event gets destroyed. /!\ Shouldn't be used for a minigame! Use OnMiss instead /!\ + public float startBeat; public float timer; @@ -154,6 +158,7 @@ namespace HeavenStudio.Games public void CleanUp() { + OnDestroy(this); Destroy(this.gameObject); }