using System.Collections.Generic; using TriInspector; using UnityEngine; namespace R0bbie.Timeline { /// /// Trigger the child steps in order, increasing index each time is played /// public class StepPoolSwitch : StepSwitch { // Exposed Variables [Title("Pool Settings")] [InfoBox("Only compatible with steps inside a StepController", TriMessageType.Warning)] [SerializeField] bool storeIndexOnDevice; // Private Variables List childSteps = new List(); int activeIndex; /// /// Initialise StepPool via controller /// /// public override void Init(StepController _controller) { attachedController = _controller; InitPool(); } /// /// Initialise StepPool via controller, if child of StepGroup /// /// public override void Init(StepController _controller, StepGroup _stepGroup) { parentStepGroup = _stepGroup; Init(_controller); } void InitPool() { // If getting the store from memory, use the step name as the key, otherwise start at value 0 if (storeIndexOnDevice) activeIndex = PlayerPrefs.GetInt(shortName); // Setup necessary refs to parent timeline, and child steps (only 1 level deep to avoid getting children of StepSwitches etc) foreach (Transform t in transform) { Step childStep = t.GetComponent(); if (childStep) { childSteps.Add(childStep); childStep.Init(attachedController, this); } } init = true; } /// /// Play this StepGroup with the current active step index /// public override void Play() { // Set the index to start at the first step Debug.Log(name + " - Started"); isActive = true; // Only play the active step Step activeChildStep = childSteps[activeIndex]; activeChildStep.Play(); activeIndex++; // If index out of range, reset it if (activeIndex >= childSteps.Count) activeIndex = 0; if (storeIndexOnDevice) PlayerPrefs.SetInt(shortName, activeIndex); } } }