168 lines
5.0 KiB
C#
168 lines
5.0 KiB
C#
using TriInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
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);
|
|
|
|
if (correctStep.stepOption)
|
|
correctStep.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
|
|
if (incorrectStep.stepOption)
|
|
incorrectStep.stepOption.Init(attachedController, this);
|
|
|
|
if (correctStep.stepOption)
|
|
correctStep.stepOption.Init(attachedController, this);
|
|
|
|
init = true;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Switch played by timeline
|
|
/// </summary>
|
|
public override void Play()
|
|
{
|
|
Debug.Log(name + " - Started");
|
|
|
|
isActive = true;
|
|
waitingForExternalTrigger = true;
|
|
|
|
// Wait for event from the player
|
|
activeMode = Mode.WaitingForEvent;
|
|
|
|
// Listen for triggers
|
|
correctStep.trigger.onTrigger.AddListener(TriggerContinue);
|
|
incorrectStep.trigger.onTrigger.AddListener(TriggerContinue);
|
|
}
|
|
|
|
|
|
/// <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);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Play the step based on which action was performed
|
|
/// </summary>
|
|
/// <param name="_option"></param>
|
|
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();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// On continued
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override void Continue()
|
|
{
|
|
if (!correctPlayed)
|
|
return;
|
|
|
|
// Remove listeners
|
|
correctStep.trigger.onTrigger.RemoveListener(TriggerContinue);
|
|
incorrectStep.trigger.onTrigger.RemoveListener(TriggerContinue);
|
|
|
|
// If the correct step have been played, then continue to the next step
|
|
base.Continue();
|
|
}
|
|
|
|
|
|
}
|
|
}
|