feat: safe delegate invocation helper added
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.StateMachine;
|
||||
|
||||
public class State : IState
|
||||
@@ -35,12 +37,12 @@ public class State : IState
|
||||
public void Update()
|
||||
{
|
||||
if (GetNextState() is IState transitionState)
|
||||
OnStateTransitionReady?.Invoke(this, transitionState);
|
||||
OnStateUpdate?.Invoke(this);
|
||||
OnStateTransitionReady?.InvokeSafe(this, transitionState);
|
||||
OnStateUpdate?.InvokeSafe(this);
|
||||
}
|
||||
|
||||
public void TransitionTo(IState from) => OnStateTransitionedTo?.Invoke(this, from);
|
||||
public void TransitionFrom(IState to) => OnStateTransitionedFrom?.Invoke(this, to);
|
||||
public void TransitionTo(IState from) => OnStateTransitionedTo?.InvokeSafe(this, from);
|
||||
public void TransitionFrom(IState to) => OnStateTransitionedFrom?.InvokeSafe(this, to);
|
||||
|
||||
public IState? GetNextState()
|
||||
{
|
||||
|
@@ -16,21 +16,21 @@ public abstract class StateBehaviourBase : Behaviour, IState
|
||||
public void Update()
|
||||
{
|
||||
OnUpdateState();
|
||||
OnStateUpdate?.Invoke(this);
|
||||
OnStateUpdate?.InvokeSafe(this);
|
||||
}
|
||||
|
||||
protected virtual void OnTransitionedToState(IState from) { }
|
||||
public void TransitionTo(IState from)
|
||||
{
|
||||
OnTransitionedToState(from);
|
||||
OnStateTransitionedTo?.Invoke(this, from);
|
||||
OnStateTransitionedTo?.InvokeSafe(this, from);
|
||||
}
|
||||
|
||||
protected virtual void OnTransitionedFromState(IState to) { }
|
||||
public void TransitionFrom(IState to)
|
||||
{
|
||||
OnTransitionedFromState(to);
|
||||
OnStateTransitionedFrom?.Invoke(this, to);
|
||||
OnStateTransitionedFrom?.InvokeSafe(this, to);
|
||||
}
|
||||
|
||||
public abstract IState? GetNextState();
|
||||
|
@@ -21,7 +21,7 @@ public class StateMachine : Behaviour
|
||||
_state = value;
|
||||
previousState.TransitionFrom(value);
|
||||
value.TransitionTo(_state);
|
||||
OnStateChanged?.Invoke(this, previousState, value);
|
||||
OnStateChanged?.InvokeSafe(this, previousState, value);
|
||||
|
||||
value.OnStateTransitionReady += OnStateTransitionReady;
|
||||
}
|
||||
|
@@ -36,7 +36,7 @@ public class StopwatchBehaviour : Behaviour, IStopwatch
|
||||
shouldBeTicking = false;
|
||||
|
||||
State = TimerState.Stopped;
|
||||
OnStopped?.Invoke(this);
|
||||
OnStopped?.InvokeSafe(this);
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
@@ -47,7 +47,7 @@ public class StopwatchBehaviour : Behaviour, IStopwatch
|
||||
double delta = GameManager.Time.DeltaSpan.TotalSeconds;
|
||||
|
||||
Time += delta;
|
||||
OnDelta?.Invoke(this, delta);
|
||||
OnDelta?.InvokeSafe(this, delta);
|
||||
}
|
||||
|
||||
protected override void OnEnteredHierarchy(IGameManager gameManager)
|
||||
@@ -72,13 +72,13 @@ public class StopwatchBehaviour : Behaviour, IStopwatch
|
||||
public virtual void Pause()
|
||||
{
|
||||
State = TimerState.Paused;
|
||||
OnPaused?.Invoke(this);
|
||||
OnPaused?.InvokeSafe(this);
|
||||
}
|
||||
|
||||
public virtual void Resume()
|
||||
{
|
||||
State = TimerState.Ticking;
|
||||
OnResumed?.Invoke(this);
|
||||
OnResumed?.InvokeSafe(this);
|
||||
}
|
||||
|
||||
private void StartStopwatch()
|
||||
@@ -86,7 +86,7 @@ public class StopwatchBehaviour : Behaviour, IStopwatch
|
||||
hasStartedTickingBefore = true;
|
||||
|
||||
State = TimerState.Ticking;
|
||||
OnStarted?.Invoke(this);
|
||||
OnStarted?.InvokeSafe(this);
|
||||
}
|
||||
|
||||
protected override void OnFinalize()
|
||||
|
@@ -1,3 +1,5 @@
|
||||
using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Systems.Time;
|
||||
|
||||
public class TickerBehaviour : StopwatchBehaviour, ITicker
|
||||
@@ -24,7 +26,7 @@ public class TickerBehaviour : StopwatchBehaviour, ITicker
|
||||
{
|
||||
nextTick += Period;
|
||||
TickCounter++;
|
||||
OnTick?.Invoke(this);
|
||||
OnTick?.InvokeSafe(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -51,7 +51,7 @@ public class TimerBehaviour : Behaviour, ITimer
|
||||
shouldBeTicking = false;
|
||||
|
||||
State = TimerState.Stopped;
|
||||
OnStopped?.Invoke(this);
|
||||
OnStopped?.InvokeSafe(this);
|
||||
}
|
||||
|
||||
protected override void OnUpdate()
|
||||
@@ -62,7 +62,7 @@ public class TimerBehaviour : Behaviour, ITimer
|
||||
double delta = GameManager.Time.DeltaSpan.TotalSeconds;
|
||||
|
||||
Remaining -= delta;
|
||||
OnDelta?.Invoke(this, delta);
|
||||
OnDelta?.InvokeSafe(this, delta);
|
||||
|
||||
if (Remaining <= .0f)
|
||||
Stop();
|
||||
@@ -90,13 +90,13 @@ public class TimerBehaviour : Behaviour, ITimer
|
||||
public virtual void Pause()
|
||||
{
|
||||
State = TimerState.Paused;
|
||||
OnPaused?.Invoke(this);
|
||||
OnPaused?.InvokeSafe(this);
|
||||
}
|
||||
|
||||
public virtual void Resume()
|
||||
{
|
||||
State = TimerState.Ticking;
|
||||
OnResumed?.Invoke(this);
|
||||
OnResumed?.InvokeSafe(this);
|
||||
}
|
||||
|
||||
private void StartTimer()
|
||||
@@ -104,7 +104,7 @@ public class TimerBehaviour : Behaviour, ITimer
|
||||
hasStartedTickingBefore = true;
|
||||
|
||||
State = TimerState.Ticking;
|
||||
OnStarted?.Invoke(this);
|
||||
OnStarted?.InvokeSafe(this);
|
||||
}
|
||||
|
||||
protected override void OnFinalize()
|
||||
|
@@ -26,14 +26,14 @@ internal class Tween : ITween
|
||||
_state = value;
|
||||
switch (value)
|
||||
{
|
||||
case TweenState.Completed: OnCompleted?.Invoke(this); OnEnded?.Invoke(this); break;
|
||||
case TweenState.Cancelled: OnCancelled?.Invoke(this); OnEnded?.Invoke(this); break;
|
||||
case TweenState.Paused: OnPaused?.Invoke(this); break;
|
||||
case TweenState.Completed: OnCompleted?.InvokeSafe(this); OnEnded?.InvokeSafe(this); break;
|
||||
case TweenState.Cancelled: OnCancelled?.InvokeSafe(this); OnEnded?.InvokeSafe(this); break;
|
||||
case TweenState.Paused: OnPaused?.InvokeSafe(this); break;
|
||||
case TweenState.Playing:
|
||||
if (previousState == TweenState.Idle)
|
||||
OnStarted?.Invoke(this);
|
||||
OnStarted?.InvokeSafe(this);
|
||||
else
|
||||
OnResumed?.Invoke(this);
|
||||
OnResumed?.InvokeSafe(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -58,9 +58,9 @@ internal class Tween : ITween
|
||||
|
||||
_counter = value.Min(Duration).Max(0f);
|
||||
Progress = Counter / Duration;
|
||||
OnUpdated?.Invoke(this);
|
||||
OnUpdated?.InvokeSafe(this);
|
||||
|
||||
OnDeltaUpdated?.Invoke(this, Easing.Evaluate(Progress) - Easing.Evaluate(previousProgress));
|
||||
OnDeltaUpdated?.InvokeSafe(this, Easing.Evaluate(Progress) - Easing.Evaluate(previousProgress));
|
||||
|
||||
if (_counter >= Duration)
|
||||
State = TweenState.Completed;
|
||||
|
@@ -14,7 +14,7 @@ public class TweenManager : HierarchyObject
|
||||
public ITween StartTween(float duration, TweenSetCallback? setCallback = null)
|
||||
{
|
||||
Tween tween = new(duration);
|
||||
tween.OnUpdated += tween => setCallback?.Invoke(tween.Value);
|
||||
tween.OnUpdated += tween => setCallback?.InvokeSafe(tween.Value);
|
||||
runningCoroutines.Add(tween, coroutineManager.StartCoroutine(RunTween(tween)));
|
||||
return tween;
|
||||
}
|
||||
|
Reference in New Issue
Block a user