Update TimelineTrigger to provide a UnityEvent to listen to instead

This commit is contained in:
Robbie Cargill 2024-02-28 18:16:58 +00:00
parent 3a7faa930f
commit b7af95fad1
6 changed files with 85 additions and 61 deletions

View File

@ -1,5 +1,6 @@
using TriInspector;
using UnityEngine;
using UnityEngine.Events;
namespace R0bbie.Timeline
{
@ -46,11 +47,9 @@ namespace R0bbie.Timeline
// 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;
}
@ -71,16 +70,17 @@ namespace R0bbie.Timeline
// 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;
}
/// <summary>
/// Switch played by timeline
/// </summary>
public override void Play()
{
Debug.Log(name + " - Started");
@ -90,6 +90,10 @@ namespace R0bbie.Timeline
// Wait for event from the player
activeMode = Mode.WaitingForEvent;
// Listen for triggers
correctStep.trigger.onTrigger.AddListener(TriggerContinue);
incorrectStep.trigger.onTrigger.AddListener(TriggerContinue);
}
@ -120,6 +124,10 @@ namespace R0bbie.Timeline
}
/// <summary>
/// Play the step based on which action was performed
/// </summary>
/// <param name="_option"></param>
void PlaySelectedStepOption(Step _option)
{
activeStepOption = _option;
@ -137,11 +145,19 @@ namespace R0bbie.Timeline
}
/// <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();
}

View File

@ -1,6 +1,7 @@
using System.Collections.Generic;
using TriInspector;
using UnityEngine;
using UnityEngine.Events;
namespace R0bbie.Timeline
{
@ -46,8 +47,6 @@ namespace R0bbie.Timeline
countSwitch.stepOption.Init(attachedStepTimeline, this);
}
externalTrigger.AddStep(this);
init = true;
}
@ -71,8 +70,6 @@ namespace R0bbie.Timeline
countSwitch.stepOption.Init(attachedController, this);
}
externalTrigger.AddStep(this);
init = true;
}
@ -86,11 +83,18 @@ namespace R0bbie.Timeline
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)
@ -113,7 +117,10 @@ namespace R0bbie.Timeline
// If it's the last one, disable the counter for now
if (activeStepIndex >= countSwitches.Count)
{
externalTrigger.onTrigger.RemoveListener(TriggerContinue);
isActive = false;
}
}
}
@ -125,6 +132,10 @@ namespace R0bbie.Timeline
}
/// <summary>
/// When the external watched trigger, triggers!
/// </summary>
/// <returns></returns>
public override void TriggerContinue(TimelineTrigger _trigger)
{
if (!isActive || activeMode != Mode.WaitingForEvent)

View File

@ -50,7 +50,6 @@ namespace R0bbie.Timeline
foreach (TriggerSwitch triggerSwitch in triggerSwitches)
{
triggerSwitch.stepOption.Init(attachedStepTimeline, this);
triggerSwitch.trigger.AddStep(this);
}
init = true;
@ -72,7 +71,6 @@ namespace R0bbie.Timeline
foreach (TriggerSwitch triggerSwitch in triggerSwitches)
{
triggerSwitch.stepOption.Init(attachedController, this);
triggerSwitch.trigger.AddStep(this);
}
init = true;
@ -118,9 +116,14 @@ namespace R0bbie.Timeline
activeStepOption.Play();
}
}
// Listen for triggers..
foreach (TriggerSwitch triggerSwitch in triggerSwitches)
{
triggerSwitch.trigger.onTrigger.AddListener(TriggerContinue);
}
// If we've got here then we want to wait for an external trigger to send instruction to this switch..
}
@ -139,6 +142,12 @@ namespace R0bbie.Timeline
if (!isActive || activeMode != Mode.WaitingForEvent)
return;
// Remove listeners
foreach (TriggerSwitch triggerSwitch in triggerSwitches)
{
triggerSwitch.trigger.onTrigger.RemoveListener(TriggerContinue);
}
// Find this trigger in the list, and set the corresponding child step as active
int switchIndexInList = triggerSwitches.FindIndex(x => x.trigger == _trigger);

View File

@ -87,6 +87,9 @@ namespace R0bbie.Timeline
}
/// <summary>
/// Reset the timer
/// </summary>
void ResetTimer()
{
timer = 0;
@ -128,9 +131,21 @@ namespace R0bbie.Timeline
}
}
}
/*else
{
if (watchForEvent == WatchForEvent.OnExternalTrigger && watchTrigger)
{
// Listen for trigger
watchTrigger.onTrigger.AddListener(TriggerContinue);
}
}*/
}
/// <summary>
/// Play the actively set step option
/// </summary>
/// <returns></returns>
void PlaySetStepOption()
{
// Play the selected option, unless it's null (in which case just play next step in timeline)
@ -160,10 +175,11 @@ namespace R0bbie.Timeline
if (timer > waitForSeconds)
timedOut = true;
// If we've timed out..
if (timedOut)
{
// Play our assist step
activeStepOption = assistAfterDelayStep;
PlaySetStepOption();
return;

View File

@ -125,8 +125,6 @@ namespace R0bbie.Timeline
// Get refs to all commands attached to this step
commands = GetComponents<StepCmd>().ToList();
InitializeContinueConditions();
init = true;
}
@ -171,8 +169,6 @@ namespace R0bbie.Timeline
// Get refs to all commands attached to this step
commands = GetComponents<StepCmd>().ToList();
InitializeContinueConditions();
init = true;
}
@ -202,24 +198,6 @@ namespace R0bbie.Timeline
Init(_controller);
}
/// <summary>
/// Initialize any reference needed for the continue condition
/// </summary>
void InitializeContinueConditions()
{
// Only need to initialize the external trigger
if (continueCondition == ContinueCondition.OnExternalTrigger)
{
// Assign this step to the trigger
if (externalContinueTrigger != null)
externalContinueTrigger.AddStep(this);
else
Debug.LogWarning("No external trigger assigned to step: " + name);
}
}
/// <summary>
/// Update loop used for tracking timers
@ -282,7 +260,7 @@ namespace R0bbie.Timeline
// Invoke event
onStepPlayed?.Invoke();
// ACTIVATE ATTACHED COMMANDS (some command types can have unlimited instances on a step, others can only have one instance on a step)
foreach (StepCmd command in commands)
{
@ -333,6 +311,9 @@ namespace R0bbie.Timeline
// Otherwise, we'll wait for the external trigger to tell the step to continue..
waitingForExternalTrigger = true;
// Watch for the trigger
externalContinueTrigger.onTrigger.AddListener(TriggerContinue);
}
else
{
@ -458,6 +439,9 @@ namespace R0bbie.Timeline
Debug.Log(name + " - Continue from trigger");
// Remove listener
externalContinueTrigger.onTrigger.RemoveListener(TriggerContinue);
waitingForExternalTrigger = false;
// Continue immediately, unless a minimum time limit was set that we've not met yet

View File

@ -1,46 +1,34 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace R0bbie.Timeline
{
/// <summary>
/// Generic TimelineTrigger which simply calls TriggerContinue on all attached steps
/// Generic TimelineTrigger which invokes an event, and keeps track if it was previously triggered
/// </summary>
public class TimelineTrigger : MonoBehaviour
{
// No functional impact - but allow giving this trigger a description so its clearer what it relates to on the component itself
[SerializeField] string description;
// Private variables
List<Step> triggerContinueOnSteps = new List<Step>();
// Provide public subscribable trigger event
public UnityEvent<TimelineTrigger> onTrigger;
// Properties
// Track whether previously triggered
public bool triggered { get; protected set; }
public void AddStep(Step _stepToTrigger)
{
// Only add step to the list if it's not already in it..
if (triggerContinueOnSteps.Contains(_stepToTrigger))
return;
// Add step to list
triggerContinueOnSteps.Add(_stepToTrigger);
}
///
/// <summary>
/// On trigger
/// </summary>
public void Trigger()
{
triggered = true;
// Add message to trigger continue on all attached steps
foreach (Step triggerContinueOnStep in triggerContinueOnSteps)
{
triggerContinueOnStep.TriggerContinue(this);
}
onTrigger.Invoke(this);
}