step-timeline/Scripts/StepSwitches/StepSwitch.cs

137 lines
4.4 KiB
C#
Raw Permalink Normal View History

using UnityEngine;
using TriInspector;
namespace R0bbie.Timeline
{
/// <summary>
/// Allow the selection of the next step based on a player action
/// </summary>
public abstract class StepSwitch : Step
{
protected enum Mode
{
Inactive,
WaitingForEvent,
PlayingChildStep
}
protected Mode activeMode;
protected Step activeStepOption;
/// <summary>
/// Tell timeline to continue once step option associated with this Switch has completed
/// </summary>
public override void Continue()
{
Debug.Log(name + " - Completed");
isActive = false;
activeMode = Mode.Inactive;
wasCompleted = true;
// Tell the timeline to play the next step (or step group, or controller)
if (parentStepGroup)
{
parentStepGroup.Continue();
}
else if (parentStepSwitch)
{
parentStepSwitch.Continue();
}
else
{
if (attachedStepTimeline != null)
attachedStepTimeline.PlayNextStep();
else
attachedController.StepCompleted();
}
}
/// <summary>
/// When a child step is played using GoToStep, if that step was nested within multiple step groups and/or switches, this function may be called by a child step group to tell this switch that a step within it somewhere is active
/// </summary>
public void SetActiveOnGoToChild(Step _childStep)
{
// If group wasn't already active, we'll set it active now
if (!isActive)
{
isActive = true;
}
// Set the active option
activeStepOption = _childStep;
// Then set any other parent step groups active too (all the way up the hierarchy, till we make it to the timeline, then till timeline to mark highest level group as active)
if (parentStepGroup)
parentStepGroup.SetActiveOnGoToChild(this);
if (parentStepSwitch)
parentStepSwitch.SetActiveOnGoToChild(this);
else
attachedStepTimeline.SetSpecifiedHighestStepActive(this);
}
#if UNITY_EDITOR
// ODIN INSPECTOR BUTTONS
[Title("Add Option to Switch")]
[Button("Add Step")]
private void Editor_AddStepOption()
{
// Create new GameObject, and reparent it as a child of the StepSwitch parent object
GameObject stepGo = new GameObject();
stepGo.transform.parent = transform;
// Add Step component to this new GameObject
Step step = stepGo.AddComponent<Step>();
// Rename object
int parentStepNo = transform.GetSiblingIndex() + 1;
int childStepNo = step.transform.GetSiblingIndex() + 1;
step.gameObject.name = "#" + parentStepNo.ToString("00") + "." + childStepNo.ToString("00") + " - Switch Option - *ADD STEP NAME*";
}
[Button("Add GoTo Step")]
private void Editor_AddGoToStepOption()
{
// Create new GameObject, and reparent it as a child of the StepSwitch parent object
GameObject stepGo = new GameObject();
stepGo.transform.parent = transform;
// Add GoToStep component to this new GameObject
GoToStep step = stepGo.AddComponent<GoToStep>();
// Rename object
int parentStepNo = transform.GetSiblingIndex() + 1;
int childStepNo = step.transform.GetSiblingIndex() + 1;
step.gameObject.name = "#" + parentStepNo.ToString("00") + "." + childStepNo.ToString("00") + " - GOTO - Switch Option - *ADD STEP NAME*";
}
[Title("Editor Functions")]
[Button("Rename Child Steps")]
private void Editor_RenameChildren()
{
RenameChildren();
}
// OTHER EDITOR HELPER FUNCTIONS
public void RenameChildren()
{
foreach (Transform child in transform)
{
if (child.GetComponent<Step>())
child.GetComponent<Step>().UpdateStepName();
}
}
#endif
}
}