namespace Zinnia.Process.Moment
{
using UnityEngine;
using Zinnia.Extension;
///
/// Wrapper for an process that has a state to determine when it is to be processed.
///
public class MomentProcess : MonoBehaviour, IProcessable
{
[Tooltip("The source process to attach to the moment.")]
[SerializeField]
private ProcessContainer source;
///
/// The source process to attach to the moment.
///
public ProcessContainer Source
{
get
{
return source;
}
set
{
source = value;
}
}
[Tooltip("The process only executes if the GameObject is active and the Component is enabled.")]
[SerializeField]
private bool onlyProcessOnActiveAndEnabled = true;
///
/// The process only executes if the is active and the is enabled.
///
public bool OnlyProcessOnActiveAndEnabled
{
get
{
return onlyProcessOnActiveAndEnabled;
}
set
{
onlyProcessOnActiveAndEnabled = value;
}
}
[Tooltip("The interval in seconds defining how often to process the Process. Negative values will be clamped to zero.")]
[SerializeField]
private float interval;
///
/// The interval in seconds defining how often to process the . Negative values will be clamped to zero.
///
public float Interval
{
get
{
return interval;
}
set
{
interval = value;
if (this.IsMemberChangeAllowed())
{
OnAfterIntervalChange();
}
}
}
///
/// When to call the next time. Updated automatically based on after has been called.
///
public float NextProcessTime { get; set; }
///
/// Clears .
///
public virtual void ClearSource()
{
if (!this.IsValidState())
{
return;
}
Source = default;
}
///
/// Calls on if allows.
///
public virtual void Process()
{
if (NextProcessTime <= Time.time)
{
ProcessNow();
}
}
///
/// Calls on , ignoring whether allows.
///
public virtual void ProcessNow()
{
if (Source == null || (OnlyProcessOnActiveAndEnabled && !this.CheckIsActiveAndEnabled()))
{
return;
}
Source.Interface.Process();
UpdateNextProcessTime();
}
///
/// Sets to a random time between now and now plus .
///
public virtual void RandomizeNextProcessTime()
{
NextProcessTime = Time.time + (Random.value * Interval);
}
protected virtual void Awake()
{
RandomizeNextProcessTime();
}
protected virtual void OnEnable()
{
OnAfterIntervalChange();
}
///
/// Updates to adjust to the latest .
///
protected virtual void UpdateNextProcessTime()
{
NextProcessTime = Time.time + Interval;
}
///
/// Called after has been changed.
///
protected virtual void OnAfterIntervalChange()
{
interval = Mathf.Max(0f, Interval);
}
}
}