step-timeline/Scripts/StepSwitches/CorrectActionStepSwitch.cs

152 lines
4.5 KiB
C#

using TriInspector;
using UnityEngine;
namespace R0bbie.Timeline
{
/// <summary>
/// Wait for an action to be completed, allowing to trigger an incorrect step
/// </summary>
[AddComponentMenu("Timeline/StepSwitches/Correct Action (Step Switch)")]
public class CorrectActionStepSwitch : StepSwitch
{
[System.Serializable]
struct ActionSwitch
{
public TimelineTrigger trigger;
public Step stepOption;
}
// Exposed Variables
[Title("Correct Action Step Switch")]
[SerializeField] ActionSwitch incorrectStep;
[SerializeField] bool keepPlaying;
[Space(10)]
[SerializeField] ActionSwitch correctStep;
// Private variables
bool incorrectPlayed;
bool correctPlayed;
/// <summary>
/// Init by 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
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;
}
/// <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
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;
}
public override void Play()
{
Debug.Log(name + " - Started");
isActive = true;
waitingForExternalTrigger = true;
// Wait for event from the player
activeMode = Mode.WaitingForEvent;
}
/// <summary>
/// Trigger command received from external TimelineTrigger
/// </summary>
public override void TriggerContinue(TimelineTrigger _trigger)
{
if (!isActive || activeMode != Mode.WaitingForEvent)
return;
if (_trigger == incorrectStep.trigger)
{
if (!incorrectPlayed || keepPlaying)
PlaySelectedStepOption(incorrectStep.stepOption);
incorrectPlayed = true;
}
else
{
// Change the status only if the correct action have been completed
// If is not completed we still want to be able to receive input from triggers
activeMode = Mode.PlayingChildStep;
correctPlayed = true;
PlaySelectedStepOption(correctStep.stepOption);
}
}
void PlaySelectedStepOption(Step _option)
{
activeStepOption = _option;
// Play the selected option, unless it's null (in which case just play next step in timeline)
if (activeStepOption)
{
activeStepOption.Play();
}
else
{
Debug.Log("The option selected by the step switch was null, so just proceeding to next step in timeline.");
Continue();
}
}
public override void Continue()
{
if (!correctPlayed)
return;
// If the correct step have been played, then continue to the next step
base.Continue();
}
}
}