step-timeline/Scripts/Commands/WatchForEventCmd.cs

72 lines
1.5 KiB
C#

using TriInspector;
using UnityEngine;
using UnityEngine.Events;
using R0bbie.Timeline.Events;
namespace R0bbie.Timeline
{
/// <summary>
/// Just watches for custom GameEvent being raised, then auto-completes
/// </summary>
[AddComponentMenu("Timeline/Commands/Watch for Event (Step Command)")]
[RequireComponent(typeof(Step))]
public class WatchForEventCmd : StepCmd, IGameEventListener
{
// Exposed Variables
[Title("Event Reference")]
[SerializeField] GameEvent gameEventToWatchFor;
bool registered;
/// <summary>
/// Initialise command (called before Activate)
/// </summary>
protected override void Init()
{
init = true;
}
public override void Activate(Step _parentStep)
{
base.Activate(_parentStep);
// Start watching for GameEvent...
if (!registered)
Register();
}
void Register()
{
gameEventToWatchFor.RegisterListener(this);
registered = true;
}
void Unregister()
{
gameEventToWatchFor.UnregisterListener(this);
registered = false;
}
public void OnEventRaised()
{
Unregister();
Complete();
}
protected override void Cleanup()
{
active = false;
}
}
}