diff --git a/Scripts/StepSwitches/CorrectActionStepSwitch.cs b/Scripts/StepSwitches/CorrectActionStepSwitch.cs index ef559f8..84f830b 100644 --- a/Scripts/StepSwitches/CorrectActionStepSwitch.cs +++ b/Scripts/StepSwitches/CorrectActionStepSwitch.cs @@ -1,5 +1,6 @@ using TriInspector; using UnityEngine; +using UnityEngine.Events; namespace R0bbie.Timeline { @@ -46,11 +47,9 @@ namespace R0bbie.Timeline // Init all step switch options if (incorrectStep.stepOption) incorrectStep.stepOption.Init(attachedStepTimeline, this); - incorrectStep.trigger.AddStep(this); if (correctStep.stepOption) correctStep.stepOption.Init(attachedStepTimeline, this); - correctStep.trigger.AddStep(this); init = true; } @@ -71,16 +70,17 @@ namespace R0bbie.Timeline // Init all step switch options if (incorrectStep.stepOption) incorrectStep.stepOption.Init(attachedController, this); - incorrectStep.trigger.AddStep(this); if (correctStep.stepOption) correctStep.stepOption.Init(attachedController, this); - correctStep.trigger.AddStep(this); init = true; } + /// + /// Switch played by timeline + /// public override void Play() { Debug.Log(name + " - Started"); @@ -90,6 +90,10 @@ namespace R0bbie.Timeline // Wait for event from the player activeMode = Mode.WaitingForEvent; + + // Listen for triggers + correctStep.trigger.onTrigger.AddListener(TriggerContinue); + incorrectStep.trigger.onTrigger.AddListener(TriggerContinue); } @@ -120,6 +124,10 @@ namespace R0bbie.Timeline } + /// + /// Play the step based on which action was performed + /// + /// void PlaySelectedStepOption(Step _option) { activeStepOption = _option; @@ -137,11 +145,19 @@ namespace R0bbie.Timeline } + /// + /// On continued + /// + /// public override void Continue() { if (!correctPlayed) return; + // Remove listeners + correctStep.trigger.onTrigger.RemoveListener(TriggerContinue); + incorrectStep.trigger.onTrigger.RemoveListener(TriggerContinue); + // If the correct step have been played, then continue to the next step base.Continue(); } diff --git a/Scripts/StepSwitches/CounterStepSwitch.cs b/Scripts/StepSwitches/CounterStepSwitch.cs index 5625a31..8045b87 100644 --- a/Scripts/StepSwitches/CounterStepSwitch.cs +++ b/Scripts/StepSwitches/CounterStepSwitch.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using TriInspector; using UnityEngine; +using UnityEngine.Events; namespace R0bbie.Timeline { @@ -46,8 +47,6 @@ namespace R0bbie.Timeline countSwitch.stepOption.Init(attachedStepTimeline, this); } - externalTrigger.AddStep(this); - init = true; } @@ -71,8 +70,6 @@ namespace R0bbie.Timeline countSwitch.stepOption.Init(attachedController, this); } - externalTrigger.AddStep(this); - init = true; } @@ -86,11 +83,18 @@ namespace R0bbie.Timeline isActive = true; waitingForExternalTrigger = true; + + // Subscribe to trigger + externalTrigger.onTrigger.AddListener(TriggerContinue); activeMode = Mode.WaitingForEvent; } + /// + /// Increase count each time triggered + /// + /// public void IncreaseCount() { if (!isActive) @@ -113,7 +117,10 @@ namespace R0bbie.Timeline // If it's the last one, disable the counter for now if (activeStepIndex >= countSwitches.Count) + { + externalTrigger.onTrigger.RemoveListener(TriggerContinue); isActive = false; + } } } @@ -125,6 +132,10 @@ namespace R0bbie.Timeline } + /// + /// When the external watched trigger, triggers! + /// + /// public override void TriggerContinue(TimelineTrigger _trigger) { if (!isActive || activeMode != Mode.WaitingForEvent) diff --git a/Scripts/StepSwitches/ExternalTriggerStepSwitch.cs b/Scripts/StepSwitches/ExternalTriggerStepSwitch.cs index 16d22bb..4d98edd 100644 --- a/Scripts/StepSwitches/ExternalTriggerStepSwitch.cs +++ b/Scripts/StepSwitches/ExternalTriggerStepSwitch.cs @@ -50,7 +50,6 @@ namespace R0bbie.Timeline foreach (TriggerSwitch triggerSwitch in triggerSwitches) { triggerSwitch.stepOption.Init(attachedStepTimeline, this); - triggerSwitch.trigger.AddStep(this); } init = true; @@ -72,7 +71,6 @@ namespace R0bbie.Timeline foreach (TriggerSwitch triggerSwitch in triggerSwitches) { triggerSwitch.stepOption.Init(attachedController, this); - triggerSwitch.trigger.AddStep(this); } init = true; @@ -118,9 +116,14 @@ namespace R0bbie.Timeline activeStepOption.Play(); } } + + // Listen for triggers.. + foreach (TriggerSwitch triggerSwitch in triggerSwitches) + { + triggerSwitch.trigger.onTrigger.AddListener(TriggerContinue); + } // If we've got here then we want to wait for an external trigger to send instruction to this switch.. - } @@ -139,6 +142,12 @@ namespace R0bbie.Timeline if (!isActive || activeMode != Mode.WaitingForEvent) return; + // Remove listeners + foreach (TriggerSwitch triggerSwitch in triggerSwitches) + { + triggerSwitch.trigger.onTrigger.RemoveListener(TriggerContinue); + } + // Find this trigger in the list, and set the corresponding child step as active int switchIndexInList = triggerSwitches.FindIndex(x => x.trigger == _trigger); diff --git a/Scripts/StepSwitches/WatchAndAssistStepSwitch.cs b/Scripts/StepSwitches/WatchAndAssistStepSwitch.cs index 366f63d..2233d90 100644 --- a/Scripts/StepSwitches/WatchAndAssistStepSwitch.cs +++ b/Scripts/StepSwitches/WatchAndAssistStepSwitch.cs @@ -87,6 +87,9 @@ namespace R0bbie.Timeline } + /// + /// Reset the timer + /// void ResetTimer() { timer = 0; @@ -128,9 +131,21 @@ namespace R0bbie.Timeline } } } + /*else + { + if (watchForEvent == WatchForEvent.OnExternalTrigger && watchTrigger) + { + // Listen for trigger + watchTrigger.onTrigger.AddListener(TriggerContinue); + } + }*/ } + /// + /// Play the actively set step option + /// + /// void PlaySetStepOption() { // Play the selected option, unless it's null (in which case just play next step in timeline) @@ -160,10 +175,11 @@ namespace R0bbie.Timeline if (timer > waitForSeconds) timedOut = true; + // If we've timed out.. if (timedOut) { + // Play our assist step activeStepOption = assistAfterDelayStep; - PlaySetStepOption(); return; diff --git a/Scripts/Steps/Step.cs b/Scripts/Steps/Step.cs index 4c28198..9d03794 100644 --- a/Scripts/Steps/Step.cs +++ b/Scripts/Steps/Step.cs @@ -125,8 +125,6 @@ namespace R0bbie.Timeline // Get refs to all commands attached to this step commands = GetComponents().ToList(); - InitializeContinueConditions(); - init = true; } @@ -171,8 +169,6 @@ namespace R0bbie.Timeline // Get refs to all commands attached to this step commands = GetComponents().ToList(); - InitializeContinueConditions(); - init = true; } @@ -202,24 +198,6 @@ namespace R0bbie.Timeline Init(_controller); } - - /// - /// Initialize any reference needed for the continue condition - /// - void InitializeContinueConditions() - { - // Only need to initialize the external trigger - if (continueCondition == ContinueCondition.OnExternalTrigger) - { - // Assign this step to the trigger - if (externalContinueTrigger != null) - externalContinueTrigger.AddStep(this); - else - Debug.LogWarning("No external trigger assigned to step: " + name); - } - - } - /// /// Update loop used for tracking timers @@ -282,7 +260,7 @@ namespace R0bbie.Timeline // Invoke event onStepPlayed?.Invoke(); - + // ACTIVATE ATTACHED COMMANDS (some command types can have unlimited instances on a step, others can only have one instance on a step) foreach (StepCmd command in commands) { @@ -333,6 +311,9 @@ namespace R0bbie.Timeline // Otherwise, we'll wait for the external trigger to tell the step to continue.. waitingForExternalTrigger = true; + + // Watch for the trigger + externalContinueTrigger.onTrigger.AddListener(TriggerContinue); } else { @@ -458,6 +439,9 @@ namespace R0bbie.Timeline Debug.Log(name + " - Continue from trigger"); + // Remove listener + externalContinueTrigger.onTrigger.RemoveListener(TriggerContinue); + waitingForExternalTrigger = false; // Continue immediately, unless a minimum time limit was set that we've not met yet diff --git a/Scripts/Triggers/TimelineTrigger.cs b/Scripts/Triggers/TimelineTrigger.cs index 7b82b31..50bf435 100644 --- a/Scripts/Triggers/TimelineTrigger.cs +++ b/Scripts/Triggers/TimelineTrigger.cs @@ -1,46 +1,34 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using UnityEngine; +using UnityEngine.Events; namespace R0bbie.Timeline { /// - /// Generic TimelineTrigger which simply calls TriggerContinue on all attached steps + /// Generic TimelineTrigger which invokes an event, and keeps track if it was previously triggered /// public class TimelineTrigger : MonoBehaviour { + // No functional impact - but allow giving this trigger a description so its clearer what it relates to on the component itself [SerializeField] string description; - // Private variables - - List triggerContinueOnSteps = new List(); + // Provide public subscribable trigger event + public UnityEvent onTrigger; - // Properties - + + // Track whether previously triggered public bool triggered { get; protected set; } - - public void AddStep(Step _stepToTrigger) - { - // Only add step to the list if it's not already in it.. - if (triggerContinueOnSteps.Contains(_stepToTrigger)) - return; - - // Add step to list - triggerContinueOnSteps.Add(_stepToTrigger); - } - - - /// + /// + /// On trigger + /// public void Trigger() { triggered = true; - - // Add message to trigger continue on all attached steps - foreach (Step triggerContinueOnStep in triggerContinueOnSteps) - { - triggerContinueOnStep.TriggerContinue(this); - } + + onTrigger.Invoke(this); }