step-timeline/Scripts/StepSwitches/StepPoolSwitch.cs

100 lines
2.8 KiB
C#

using System.Collections.Generic;
using TriInspector;
using UnityEngine;
namespace R0bbie.Timeline
{
/// <summary>
/// Trigger the child steps in order, increasing index each time is played
/// </summary>
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<Step> childSteps = new List<Step>();
int activeIndex;
/// <summary>
/// Initialise StepPool via controller
/// </summary>
/// <param name="_controller"></param>
public override void Init(StepController _controller)
{
attachedController = _controller;
InitPool();
}
/// <summary>
/// Initialise StepPool via controller, if child of StepGroup
/// </summary>
/// <param name="_controller"></param>
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<Step>();
if (childStep)
{
childSteps.Add(childStep);
childStep.Init(attachedController, this);
}
}
init = true;
}
/// <summary>
/// Play this StepGroup with the current active step index
/// </summary>
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);
}
}
}