step-timeline/Scripts/StepSwitches/CounterStepSwitch.cs

150 lines
4.5 KiB
C#

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<CountSwitch> countSwitches = new List<CountSwitch>();
[SerializeField] TimelineTrigger externalTrigger;
[SerializeField] bool repeatStepsInSwitch;
int count;
int activeStepIndex = -1;
/// <summary>
/// Init switch with Timeline
/// </summary>
/// <param name="_stepTimeline"></param>
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;
}
/// <summary>
/// Init switch by StepController
/// </summary>
/// <param name="_controller"></param>
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;
}
/// <summary>
/// Play this StepSwitch, in turn playing whichever step its conditions point to
/// </summary>
public override void Play()
{
Debug.Log(name + " - Started");
isActive = true;
waitingForExternalTrigger = true;
// Subscribe to trigger
externalTrigger.onTrigger.AddListener(TriggerContinue);
activeMode = Mode.WaitingForEvent;
}
/// <summary>
/// Increase count each time triggered
/// </summary>
/// <returns></returns>
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
}
/// <summary>
/// When the external watched trigger, triggers!
/// </summary>
/// <returns></returns>
public override void TriggerContinue(TimelineTrigger _trigger)
{
if (!isActive || activeMode != Mode.WaitingForEvent)
return;
base.Continue();
}
}
}