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 TriInspector;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
namespace R0bbie.Timeline namespace R0bbie.Timeline
{ {
@ -46,11 +47,9 @@ namespace R0bbie.Timeline
// Init all step switch options // Init all step switch options
if (incorrectStep.stepOption) if (incorrectStep.stepOption)
incorrectStep.stepOption.Init(attachedStepTimeline, this); incorrectStep.stepOption.Init(attachedStepTimeline, this);
incorrectStep.trigger.AddStep(this);
if (correctStep.stepOption) if (correctStep.stepOption)
correctStep.stepOption.Init(attachedStepTimeline, this); correctStep.stepOption.Init(attachedStepTimeline, this);
correctStep.trigger.AddStep(this);
init = true; init = true;
} }
@ -71,16 +70,17 @@ namespace R0bbie.Timeline
// Init all step switch options // Init all step switch options
if (incorrectStep.stepOption) if (incorrectStep.stepOption)
incorrectStep.stepOption.Init(attachedController, this); incorrectStep.stepOption.Init(attachedController, this);
incorrectStep.trigger.AddStep(this);
if (correctStep.stepOption) if (correctStep.stepOption)
correctStep.stepOption.Init(attachedController, this); correctStep.stepOption.Init(attachedController, this);
correctStep.trigger.AddStep(this);
init = true; init = true;
} }
/// <summary>
/// Switch played by timeline
/// </summary>
public override void Play() public override void Play()
{ {
Debug.Log(name + " - Started"); Debug.Log(name + " - Started");
@ -90,6 +90,10 @@ namespace R0bbie.Timeline
// Wait for event from the player // Wait for event from the player
activeMode = Mode.WaitingForEvent; 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) void PlaySelectedStepOption(Step _option)
{ {
activeStepOption = _option; activeStepOption = _option;
@ -137,11 +145,19 @@ namespace R0bbie.Timeline
} }
/// <summary>
/// On continued
/// </summary>
/// <returns></returns>
public override void Continue() public override void Continue()
{ {
if (!correctPlayed) if (!correctPlayed)
return; 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 // If the correct step have been played, then continue to the next step
base.Continue(); base.Continue();
} }

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using TriInspector; using TriInspector;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
namespace R0bbie.Timeline namespace R0bbie.Timeline
{ {
@ -46,8 +47,6 @@ namespace R0bbie.Timeline
countSwitch.stepOption.Init(attachedStepTimeline, this); countSwitch.stepOption.Init(attachedStepTimeline, this);
} }
externalTrigger.AddStep(this);
init = true; init = true;
} }
@ -71,8 +70,6 @@ namespace R0bbie.Timeline
countSwitch.stepOption.Init(attachedController, this); countSwitch.stepOption.Init(attachedController, this);
} }
externalTrigger.AddStep(this);
init = true; init = true;
} }
@ -87,10 +84,17 @@ namespace R0bbie.Timeline
isActive = true; isActive = true;
waitingForExternalTrigger = true; waitingForExternalTrigger = true;
// Subscribe to trigger
externalTrigger.onTrigger.AddListener(TriggerContinue);
activeMode = Mode.WaitingForEvent; activeMode = Mode.WaitingForEvent;
} }
/// <summary>
/// Increase count each time triggered
/// </summary>
/// <returns></returns>
public void IncreaseCount() public void IncreaseCount()
{ {
if (!isActive) if (!isActive)
@ -113,9 +117,12 @@ namespace R0bbie.Timeline
// If it's the last one, disable the counter for now // If it's the last one, disable the counter for now
if (activeStepIndex >= countSwitches.Count) if (activeStepIndex >= countSwitches.Count)
{
externalTrigger.onTrigger.RemoveListener(TriggerContinue);
isActive = false; isActive = false;
} }
} }
}
public override void Continue() public override void Continue()
@ -125,6 +132,10 @@ namespace R0bbie.Timeline
} }
/// <summary>
/// When the external watched trigger, triggers!
/// </summary>
/// <returns></returns>
public override void TriggerContinue(TimelineTrigger _trigger) public override void TriggerContinue(TimelineTrigger _trigger)
{ {
if (!isActive || activeMode != Mode.WaitingForEvent) if (!isActive || activeMode != Mode.WaitingForEvent)

View File

@ -50,7 +50,6 @@ namespace R0bbie.Timeline
foreach (TriggerSwitch triggerSwitch in triggerSwitches) foreach (TriggerSwitch triggerSwitch in triggerSwitches)
{ {
triggerSwitch.stepOption.Init(attachedStepTimeline, this); triggerSwitch.stepOption.Init(attachedStepTimeline, this);
triggerSwitch.trigger.AddStep(this);
} }
init = true; init = true;
@ -72,7 +71,6 @@ namespace R0bbie.Timeline
foreach (TriggerSwitch triggerSwitch in triggerSwitches) foreach (TriggerSwitch triggerSwitch in triggerSwitches)
{ {
triggerSwitch.stepOption.Init(attachedController, this); triggerSwitch.stepOption.Init(attachedController, this);
triggerSwitch.trigger.AddStep(this);
} }
init = true; init = true;
@ -119,8 +117,13 @@ namespace R0bbie.Timeline
} }
} }
// If we've got here then we want to wait for an external trigger to send instruction to this switch.. // 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) if (!isActive || activeMode != Mode.WaitingForEvent)
return; 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 // Find this trigger in the list, and set the corresponding child step as active
int switchIndexInList = triggerSwitches.FindIndex(x => x.trigger == _trigger); int switchIndexInList = triggerSwitches.FindIndex(x => x.trigger == _trigger);

View File

@ -87,6 +87,9 @@ namespace R0bbie.Timeline
} }
/// <summary>
/// Reset the timer
/// </summary>
void ResetTimer() void ResetTimer()
{ {
timer = 0; 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() void PlaySetStepOption()
{ {
// Play the selected option, unless it's null (in which case just play next step in timeline) // 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) if (timer > waitForSeconds)
timedOut = true; timedOut = true;
// If we've timed out..
if (timedOut) if (timedOut)
{ {
// Play our assist step
activeStepOption = assistAfterDelayStep; activeStepOption = assistAfterDelayStep;
PlaySetStepOption(); PlaySetStepOption();
return; return;

View File

@ -125,8 +125,6 @@ namespace R0bbie.Timeline
// Get refs to all commands attached to this step // Get refs to all commands attached to this step
commands = GetComponents<StepCmd>().ToList(); commands = GetComponents<StepCmd>().ToList();
InitializeContinueConditions();
init = true; init = true;
} }
@ -171,8 +169,6 @@ namespace R0bbie.Timeline
// Get refs to all commands attached to this step // Get refs to all commands attached to this step
commands = GetComponents<StepCmd>().ToList(); commands = GetComponents<StepCmd>().ToList();
InitializeContinueConditions();
init = true; init = true;
} }
@ -203,24 +199,6 @@ namespace R0bbie.Timeline
} }
/// <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> /// <summary>
/// Update loop used for tracking timers /// Update loop used for tracking timers
/// </summary> /// </summary>
@ -333,6 +311,9 @@ namespace R0bbie.Timeline
// Otherwise, we'll wait for the external trigger to tell the step to continue.. // Otherwise, we'll wait for the external trigger to tell the step to continue..
waitingForExternalTrigger = true; waitingForExternalTrigger = true;
// Watch for the trigger
externalContinueTrigger.onTrigger.AddListener(TriggerContinue);
} }
else else
{ {
@ -458,6 +439,9 @@ namespace R0bbie.Timeline
Debug.Log(name + " - Continue from trigger"); Debug.Log(name + " - Continue from trigger");
// Remove listener
externalContinueTrigger.onTrigger.RemoveListener(TriggerContinue);
waitingForExternalTrigger = false; waitingForExternalTrigger = false;
// Continue immediately, unless a minimum time limit was set that we've not met yet // 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;
using UnityEngine.Events;
namespace R0bbie.Timeline namespace R0bbie.Timeline
{ {
/// <summary> /// <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> /// </summary>
public class TimelineTrigger : MonoBehaviour 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; [SerializeField] string description;
// Private variables // Provide public subscribable trigger event
public UnityEvent<TimelineTrigger> onTrigger;
List<Step> triggerContinueOnSteps = new List<Step>();
// Properties
// Track whether previously triggered
public bool triggered { get; protected set; } public bool triggered { get; protected set; }
/// <summary>
public void AddStep(Step _stepToTrigger) /// On trigger
{ /// </summary>
// 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);
}
///
public void Trigger() public void Trigger()
{ {
triggered = true; triggered = true;
// Add message to trigger continue on all attached steps onTrigger.Invoke(this);
foreach (Step triggerContinueOnStep in triggerContinueOnSteps)
{
triggerContinueOnStep.TriggerContinue(this);
}
} }