#if GRAPH_DESIGNER /// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions.Utility { using Opsive.GraphDesigner.Runtime.Variables; using UnityEngine; [Opsive.Shared.Utility.Category("Utility")] [Opsive.Shared.Utility.Description("Increments or decrements a counter and finishes when the threshold is reached.")] public class Counter : Action { [Tooltip("The counter value that should be modified.")] [SerializeField] protected SharedVariable m_Counter; [Tooltip("The amount to increment or decrement the counter by each update.")] [SerializeField] protected SharedVariable m_IncrementAmount = 1; [Tooltip("The threshold value that the counter should reach before finishing.")] [SerializeField] protected SharedVariable m_Threshold; [Tooltip("Should the counter be incremented (true) or decremented (false)?")] [SerializeField] protected SharedVariable m_Increment = true; [Tooltip("Should the counter be reset to 0 when the action starts?")] [SerializeField] protected SharedVariable m_ResetOnStart = false; /// /// Called when the action is started. /// public override void OnStart() { base.OnStart(); if (m_ResetOnStart.Value) { m_Counter.Value = 0; } } /// /// Executes the action logic. /// /// The status of the action. public override TaskStatus OnUpdate() { // Update the counter. if (m_Increment.Value) { m_Counter.Value += m_IncrementAmount.Value; } else { m_Counter.Value -= m_IncrementAmount.Value; } // Check if threshold is reached. if (m_Increment.Value) { if (m_Counter.Value >= m_Threshold.Value) { return TaskStatus.Success; } } else { if (m_Counter.Value <= m_Threshold.Value) { return TaskStatus.Success; } } return TaskStatus.Running; } /// /// Resets the action values back to their default. /// public override void Reset() { base.Reset(); m_Counter = 0; m_IncrementAmount = 1; m_Threshold = 10; m_Increment = true; m_ResetOnStart = false; } } } #endif