using System.Collections.Generic; using TriInspector; using UnityEngine; using UnityEngine.Events; namespace R0bbie.Timeline { [AddComponentMenu("Timeline/StepSwitches/Counter (Step Switch)")] public class CounterStepSwitch : StepSwitch { // Assign the step, and how long should the timeline wait before trigger the next step [System.Serializable] struct CountSwitch { [SerializeField] public Step stepOption; [SerializeField] public int triggerAfter; } // Private Variables [Title("Switch Options")] [SerializeField] List countSwitches = new List(); [SerializeField] TimelineTrigger externalTrigger; [SerializeField] bool repeatStepsInSwitch; int count; int activeStepIndex = -1; /// /// Init switch with Timeline /// /// public override void Init(StepTimeline _stepTimeline) { // Setup necessary refs such as parent timeline attachedStepTimeline = _stepTimeline; // Inactive by default activeMode = Mode.Inactive; // Init all step switch options foreach (CountSwitch countSwitch in countSwitches) { if (countSwitch.stepOption) countSwitch.stepOption.Init(attachedStepTimeline, this); } init = true; } /// /// Init switch by StepController /// /// public override void Init(StepController _controller) { // Setup necessary refs such as parent controller attachedController = _controller; // Inactive by default activeMode = Mode.Inactive; // Init all step switch options foreach (CountSwitch countSwitch in countSwitches) { if (countSwitch.stepOption) countSwitch.stepOption.Init(attachedController, this); } init = true; } /// /// Play this StepSwitch, in turn playing whichever step its conditions point to /// public override void Play() { Debug.Log(name + " - Started"); isActive = true; waitingForExternalTrigger = true; // Subscribe to trigger externalTrigger.onTrigger.AddListener(TriggerContinue); activeMode = Mode.WaitingForEvent; } /// /// Increase count each time triggered /// /// public void IncreaseCount() { if (!isActive) return; count++; // Reset step index, or exit this method if all steps in the switch have already been performed if (repeatStepsInSwitch) { if (countSwitches.Count <= activeStepIndex + 1) activeStepIndex = -1; else return; } if (count >= countSwitches[activeStepIndex + 1].triggerAfter && (activeStepIndex + 1) <= countSwitches.Count) { activeStepIndex++; countSwitches[activeStepIndex].stepOption.Play(); // If it's the last one, disable the counter for now if (activeStepIndex >= countSwitches.Count) { externalTrigger.onTrigger.RemoveListener(TriggerContinue); isActive = false; } } } public override void Continue() { // Leave empty as we want to wait until the player completes the actual task // And the steps are triggered through increase of the count tries } /// /// When the external watched trigger, triggers! /// /// public override void TriggerContinue(TimelineTrigger _trigger) { if (!isActive || activeMode != Mode.WaitingForEvent) return; base.Continue(); } } }