diff --git a/Engine.Core/ActiveBehaviourCollector.cs b/Engine.Core/ActiveBehaviourCollector.cs index f7472aa..cb894b8 100644 --- a/Engine.Core/ActiveBehaviourCollector.cs +++ b/Engine.Core/ActiveBehaviourCollector.cs @@ -63,12 +63,12 @@ public class ActiveBehaviourCollector : IBehaviourCollector where T : clas { activeBehaviours.Add(behaviour); OnBehaviourAdd(behaviour); - OnCollected?.Invoke(this, behaviour); + OnCollected?.InvokeSafe(this, behaviour); } else if (activeBehaviours.Remove(behaviour)) { OnBehaviourRemove(behaviour); - OnRemoved?.Invoke(this, behaviour); + OnRemoved?.InvokeSafe(this, behaviour); } } @@ -85,7 +85,7 @@ public class ActiveBehaviourCollector : IBehaviourCollector where T : clas if (activeBehaviours.Remove(tBehaviour)) { OnBehaviourRemove(tBehaviour); - OnRemoved?.Invoke(this, tBehaviour); + OnRemoved?.InvokeSafe(this, tBehaviour); } } @@ -101,7 +101,7 @@ public class ActiveBehaviourCollector : IBehaviourCollector where T : clas gameManager.OnHierarchyObjectUnRegistered += OnHierarchyObjectUnregistered; GameManager = gameManager; - OnGameManagerAssigned?.Invoke(this); + OnGameManagerAssigned?.InvokeSafe(this); return true; } @@ -118,7 +118,7 @@ public class ActiveBehaviourCollector : IBehaviourCollector where T : clas GameManager.OnHierarchyObjectUnRegistered -= OnHierarchyObjectUnregistered; GameManager = null!; - OnUnassigned?.Invoke(this); + OnUnassigned?.InvokeSafe(this); return true; } diff --git a/Engine.Core/BaseEntity.cs b/Engine.Core/BaseEntity.cs index 66db6b0..a575bac 100644 --- a/Engine.Core/BaseEntity.cs +++ b/Engine.Core/BaseEntity.cs @@ -33,7 +33,7 @@ public abstract class BaseEntity : IEntity string previousId = _id; _id = value; - OnIdChanged?.Invoke(this, previousId); + OnIdChanged?.InvokeSafe(this, previousId); } } @@ -47,9 +47,9 @@ public abstract class BaseEntity : IEntity _initialized = value; if (value) - OnInitialized?.Invoke(this); + OnInitialized?.InvokeSafe(this); else - OnFinalized?.Invoke(this); + OnFinalized?.InvokeSafe(this); } } @@ -62,7 +62,7 @@ public abstract class BaseEntity : IEntity _stateEnable = stateEnable; _stateEnable.Assign(this); OnAssign(stateEnable); - OnStateEnableAssigned?.Invoke(this); + OnStateEnableAssigned?.InvokeSafe(this); return true; } @@ -76,7 +76,7 @@ public abstract class BaseEntity : IEntity _stateEnable = null!; _stateEnable.Unassign(); - OnUnassigned?.Invoke(this); + OnUnassigned?.InvokeSafe(this); return true; } diff --git a/Engine.Core/BehaviourBase.cs b/Engine.Core/BehaviourBase.cs index 72d0fe0..7f76c95 100644 --- a/Engine.Core/BehaviourBase.cs +++ b/Engine.Core/BehaviourBase.cs @@ -25,7 +25,7 @@ public abstract class BehaviourBase : BaseEntity, IBehaviour int previousPriority = _priority; _priority = value; - OnPriorityChanged?.Invoke(this, previousPriority); + OnPriorityChanged?.InvokeSafe(this, previousPriority); } } @@ -43,7 +43,7 @@ public abstract class BehaviourBase : BaseEntity, IBehaviour behaviourController.OnHierarchyObjectAssigned += OnHierarchyObjectAssigned; if (behaviourController.HierarchyObject is not null) OnHierarchyObjectAssigned(behaviourController); - OnBehaviourControllerAssigned?.Invoke(this); + OnBehaviourControllerAssigned?.InvokeSafe(this); return true; } @@ -83,6 +83,6 @@ public abstract class BehaviourBase : BaseEntity, IBehaviour _isActive = StateEnable.Enabled && _behaviourController.HierarchyObject.IsActive; if (previousActive != IsActive) - OnActiveChanged?.Invoke(this, previousActive); + OnActiveChanged?.InvokeSafe(this, previousActive); } } diff --git a/Engine.Core/BehaviourCollector.cs b/Engine.Core/BehaviourCollector.cs index b586523..46f1be4 100644 --- a/Engine.Core/BehaviourCollector.cs +++ b/Engine.Core/BehaviourCollector.cs @@ -50,7 +50,7 @@ public class BehaviourCollector : IBehaviourCollector where T : class behaviours.Add(tBehaviour); OnBehaviourAdd(behaviour); - OnCollected?.Invoke(this, tBehaviour); + OnCollected?.InvokeSafe(this, tBehaviour); } protected virtual void OnBehaviourRemove(IBehaviour behaviour) { } @@ -63,7 +63,7 @@ public class BehaviourCollector : IBehaviourCollector where T : class return; OnBehaviourRemove(behaviour); - OnRemoved?.Invoke(this, tBehaviour); + OnRemoved?.InvokeSafe(this, tBehaviour); } protected virtual void OnAssign(IGameManager gameManager) { } @@ -80,7 +80,7 @@ public class BehaviourCollector : IBehaviourCollector where T : class GameManager = gameManager; OnAssign(gameManager); - OnGameManagerAssigned?.Invoke(this); + OnGameManagerAssigned?.InvokeSafe(this); return true; } @@ -97,7 +97,7 @@ public class BehaviourCollector : IBehaviourCollector where T : class GameManager.OnHierarchyObjectUnRegistered -= OnHierarchyObjectUnregistered; GameManager = null!; - OnUnassigned?.Invoke(this); + OnUnassigned?.InvokeSafe(this); return true; } diff --git a/Engine.Core/BehaviourController.cs b/Engine.Core/BehaviourController.cs index 0d80b1d..ad226d9 100644 --- a/Engine.Core/BehaviourController.cs +++ b/Engine.Core/BehaviourController.cs @@ -41,9 +41,9 @@ public class BehaviourController : IBehaviourController _initialized = value; if (value) - OnInitialized?.Invoke(this); + OnInitialized?.InvokeSafe(this); else - OnFinalized?.Invoke(this); + OnFinalized?.InvokeSafe(this); } } @@ -56,7 +56,7 @@ public class BehaviourController : IBehaviourController behaviour.Initialize(); behaviour.OnPriorityChanged += OnPriorityChange; - OnBehaviourAdded?.Invoke(this, behaviour); + OnBehaviourAdded?.InvokeSafe(this, behaviour); return behaviour; } @@ -121,7 +121,7 @@ public class BehaviourController : IBehaviourController behaviour.OnPriorityChanged -= OnPriorityChange; behaviour.Finalize(); behaviours.Remove(behaviour); - OnBehaviourRemoved?.Invoke(this, behaviour); + OnBehaviourRemoved?.InvokeSafe(this, behaviour); } protected virtual void OnAssign(IHierarchyObject hierarchyObject) { } @@ -132,7 +132,7 @@ public class BehaviourController : IBehaviourController _hierarchyObject = hierarchyObject; OnAssign(hierarchyObject); - OnHierarchyObjectAssigned?.Invoke(this); + OnHierarchyObjectAssigned?.InvokeSafe(this); return true; } @@ -168,7 +168,7 @@ public class BehaviourController : IBehaviourController return false; _hierarchyObject = null!; - OnUnassigned?.Invoke(this); + OnUnassigned?.InvokeSafe(this); return true; } @@ -177,8 +177,8 @@ public class BehaviourController : IBehaviourController if (!HierarchyObject.StateEnable.Enabled) return; - OnPreUpdate?.Invoke(this); - OnUpdate?.Invoke(this); + OnPreUpdate?.InvokeSafe(this); + OnUpdate?.InvokeSafe(this); } public void UpdatePreDraw() @@ -186,7 +186,7 @@ public class BehaviourController : IBehaviourController if (!HierarchyObject.StateEnable.Enabled) return; - OnPreDraw?.Invoke(this); + OnPreDraw?.InvokeSafe(this); } public BehaviourController() { } diff --git a/Engine.Core/GameManager.cs b/Engine.Core/GameManager.cs index f90ba75..781f162 100644 --- a/Engine.Core/GameManager.cs +++ b/Engine.Core/GameManager.cs @@ -49,7 +49,7 @@ public class GameManager : BaseEntity, IGameManager if (!hierarchyObject.EnterHierarchy(this)) throw new Exception($"{hierarchyObject.Name} can't enter the hierarchy"); - OnHierarchyObjectRegistered?.Invoke(this, hierarchyObject); + OnHierarchyObjectRegistered?.InvokeSafe(this, hierarchyObject); } public T InstantiateHierarchyObject(params object?[]? args) where T : class, IHierarchyObject @@ -84,7 +84,7 @@ public class GameManager : BaseEntity, IGameManager if (!hierarchyObject.Finalize()) throw new Exception($"{hierarchyObject.Name} can't be finalized"); - OnHierarchyObjectUnRegistered?.Invoke(this, hierarchyObject); + OnHierarchyObjectUnRegistered?.InvokeSafe(this, hierarchyObject); } protected override void InitializeInternal() @@ -107,12 +107,12 @@ public class GameManager : BaseEntity, IGameManager UnscaledTime = engineTime; Time = new(TimeSpan.FromTicks((long)(Time.TimeSinceStart.Ticks + engineTime.DeltaSpan.Ticks * TimeScale)), TimeSpan.FromTicks((long)(engineTime.DeltaSpan.Ticks * TimeScale))); - OnPreUpdate?.Invoke(this, Time); + OnPreUpdate?.InvokeSafe(this, Time); for (int i = 0; i < HierarchyObjects.Count; i++) HierarchyObjects[i].BehaviourController.Update(); - OnUpdate?.Invoke(this, Time); + OnUpdate?.InvokeSafe(this, Time); } public void PreDraw() @@ -122,7 +122,7 @@ public class GameManager : BaseEntity, IGameManager for (int i = 0; i < HierarchyObjects.Count; i++) HierarchyObjects[i].BehaviourController.UpdatePreDraw(); - OnPreDraw?.Invoke(this); + OnPreDraw?.InvokeSafe(this); } private void OnHierarchyObjectFinalize(IInitializable initializable) diff --git a/Engine.Core/Helpers/DelegateHelpers.cs b/Engine.Core/Helpers/DelegateHelpers.cs new file mode 100644 index 0000000..11af496 --- /dev/null +++ b/Engine.Core/Helpers/DelegateHelpers.cs @@ -0,0 +1,20 @@ +using System; + +namespace Syntriax.Engine.Core; + +public static class DelegateHelpers +{ + public static void InvokeSafe(this Delegate @delegate, params object[] args) + { + if (@delegate is null) + return; + + foreach (Delegate invocation in @delegate.GetInvocationList()) + try { invocation.DynamicInvoke(args); } + catch (Exception exception) + { + string methodCallRepresentation = $"{invocation.Method.DeclaringType?.FullName}.{invocation.Method.Name}({string.Join(", ", args)})"; + Console.WriteLine($"Unexpected exception on invocation of method {methodCallRepresentation}:{Environment.NewLine}{exception.InnerException}"); + } + } +} diff --git a/Engine.Core/HierarchyObject.cs b/Engine.Core/HierarchyObject.cs index 6dcaba7..5d75a03 100644 --- a/Engine.Core/HierarchyObject.cs +++ b/Engine.Core/HierarchyObject.cs @@ -39,7 +39,7 @@ public class HierarchyObject : BaseEntity, IHierarchyObject string previousName = _name; _name = value; - OnNameChanged?.Invoke(this, previousName); + OnNameChanged?.InvokeSafe(this, previousName); } } @@ -52,7 +52,7 @@ public class HierarchyObject : BaseEntity, IHierarchyObject _gameManager = gameManager; UpdateActive(); OnEnteringHierarchy(gameManager); - OnEnteredHierarchy?.Invoke(this, gameManager); + OnEnteredHierarchy?.InvokeSafe(this, gameManager); return true; } @@ -64,7 +64,7 @@ public class HierarchyObject : BaseEntity, IHierarchyObject OnExitingHierarchy(gameManager); _gameManager = null!; - OnExitedHierarchy?.Invoke(this, gameManager); + OnExitedHierarchy?.InvokeSafe(this, gameManager); return true; } @@ -95,7 +95,7 @@ public class HierarchyObject : BaseEntity, IHierarchyObject } UpdateActive(); - OnParentChanged?.Invoke(this, previousParent, parent); + OnParentChanged?.InvokeSafe(this, previousParent, parent); } public void AddChild(IHierarchyObject parent) @@ -105,7 +105,7 @@ public class HierarchyObject : BaseEntity, IHierarchyObject _children.Add(parent); parent.SetParent(this); - OnChildrenAdded?.Invoke(this, parent); + OnChildrenAdded?.InvokeSafe(this, parent); } public void RemoveChild(IHierarchyObject child) @@ -114,7 +114,7 @@ public class HierarchyObject : BaseEntity, IHierarchyObject return; child.SetParent(null); - OnChildrenRemoved?.Invoke(this, child); + OnChildrenRemoved?.InvokeSafe(this, child); } protected virtual void OnAssign(IBehaviourController behaviourController) { } @@ -125,7 +125,7 @@ public class HierarchyObject : BaseEntity, IHierarchyObject _behaviourController = behaviourController; OnAssign(behaviourController); - OnBehaviourControllerAssigned?.Invoke(this); + OnBehaviourControllerAssigned?.InvokeSafe(this); return true; } @@ -145,7 +145,7 @@ public class HierarchyObject : BaseEntity, IHierarchyObject _isActive = StateEnable.Enabled && (Parent?.IsActive ?? true); if (previousActive != IsActive) - OnActiveChanged?.Invoke(this, previousActive); + OnActiveChanged?.InvokeSafe(this, previousActive); } protected override void UnassignInternal() diff --git a/Engine.Core/StateEnable.cs b/Engine.Core/StateEnable.cs index 1bfa884..cb4d08d 100644 --- a/Engine.Core/StateEnable.cs +++ b/Engine.Core/StateEnable.cs @@ -23,7 +23,7 @@ public class StateEnable : IStateEnable bool previousState = _enabled; _enabled = value; - OnEnabledChanged?.Invoke(this, previousState); + OnEnabledChanged?.InvokeSafe(this, previousState); } } @@ -35,7 +35,7 @@ public class StateEnable : IStateEnable _entity = entity; OnAssign(entity); - OnEntityAssigned?.Invoke(this); + OnEntityAssigned?.InvokeSafe(this); return true; } @@ -45,7 +45,7 @@ public class StateEnable : IStateEnable return false; _entity = null!; - OnUnassigned?.Invoke(this); + OnUnassigned?.InvokeSafe(this); return true; } } diff --git a/Engine.Core/Transform2D.cs b/Engine.Core/Transform2D.cs index b2a1eb1..a5ebb46 100644 --- a/Engine.Core/Transform2D.cs +++ b/Engine.Core/Transform2D.cs @@ -31,7 +31,7 @@ public class Transform2D : Behaviour, ITransform2D _position = value; UpdateLocalPosition(); - OnPositionChanged?.Invoke(this, _position); + OnPositionChanged?.InvokeSafe(this, _position); } } @@ -47,7 +47,7 @@ public class Transform2D : Behaviour, ITransform2D _scale = value; UpdateLocalScale(); - OnScaleChanged?.Invoke(this, previousScale); + OnScaleChanged?.InvokeSafe(this, previousScale); } } @@ -63,7 +63,7 @@ public class Transform2D : Behaviour, ITransform2D _rotation = value; UpdateLocalRotation(); - OnRotationChanged?.Invoke(this, previousRotation); + OnRotationChanged?.InvokeSafe(this, previousRotation); } } @@ -79,7 +79,7 @@ public class Transform2D : Behaviour, ITransform2D _localPosition = value; UpdatePosition(); - OnPositionChanged?.Invoke(this, previousPosition); + OnPositionChanged?.InvokeSafe(this, previousPosition); } } @@ -97,8 +97,8 @@ public class Transform2D : Behaviour, ITransform2D UpdateScale(); UpdatePosition(); - OnScaleChanged?.Invoke(this, previousScale); - OnPositionChanged?.Invoke(this, previousPosition); + OnScaleChanged?.InvokeSafe(this, previousScale); + OnPositionChanged?.InvokeSafe(this, previousPosition); } } @@ -114,7 +114,7 @@ public class Transform2D : Behaviour, ITransform2D _localRotation = value; UpdateRotation(); - OnRotationChanged?.Invoke(this, previousRotation); + OnRotationChanged?.InvokeSafe(this, previousRotation); } } @@ -125,7 +125,7 @@ public class Transform2D : Behaviour, ITransform2D UpdatePosition(); - OnPositionChanged?.Invoke(this, previousPosition); + OnPositionChanged?.InvokeSafe(this, previousPosition); } private void RecalculateScale(ITransform2D _, Vector2D previousScale) @@ -138,8 +138,8 @@ public class Transform2D : Behaviour, ITransform2D UpdateScale(); UpdatePosition(); - OnScaleChanged?.Invoke(this, previousScale); - OnPositionChanged?.Invoke(this, previousPosition); + OnScaleChanged?.InvokeSafe(this, previousScale); + OnPositionChanged?.InvokeSafe(this, previousPosition); } private void RecalculateRotation(ITransform2D _, float previousRotation) @@ -152,8 +152,8 @@ public class Transform2D : Behaviour, ITransform2D UpdateRotation(); UpdatePosition(); - OnRotationChanged?.Invoke(this, previousRotation); - OnPositionChanged?.Invoke(this, previousPosition); + OnRotationChanged?.InvokeSafe(this, previousRotation); + OnPositionChanged?.InvokeSafe(this, previousPosition); } private void UpdateLocalPosition() @@ -247,9 +247,9 @@ public class Transform2D : Behaviour, ITransform2D UpdateLocalScale(); UpdateLocalRotation(); - OnPositionChanged?.Invoke(this, Position); - OnScaleChanged?.Invoke(this, Scale); - OnRotationChanged?.Invoke(this, Rotation); + OnPositionChanged?.InvokeSafe(this, Position); + OnScaleChanged?.InvokeSafe(this, Scale); + OnRotationChanged?.InvokeSafe(this, Rotation); } private void LookForTransform2D(IBehaviourController sender, IBehaviour behaviourAdded) diff --git a/Engine.Physics2D/Collider2DBehaviourBase.cs b/Engine.Physics2D/Collider2DBehaviourBase.cs index f5cc052..7bd8384 100644 --- a/Engine.Physics2D/Collider2DBehaviourBase.cs +++ b/Engine.Physics2D/Collider2DBehaviourBase.cs @@ -71,7 +71,7 @@ public abstract class Collider2DBehaviourBase : Behaviour2D, ICollider2D Transform.OnRotationChanged -= SetNeedsRecalculationFromRotation; } - public void Detect(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionDetected?.Invoke(this, collisionDetectionInformation); - public void Resolve(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionResolved?.Invoke(this, collisionDetectionInformation); - public void Trigger(ICollider2D initiator) => OnTriggered?.Invoke(this, initiator); + public void Detect(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionDetected?.InvokeSafe(this, collisionDetectionInformation); + public void Resolve(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionResolved?.InvokeSafe(this, collisionDetectionInformation); + public void Trigger(ICollider2D initiator) => OnTriggered?.InvokeSafe(this, initiator); } diff --git a/Engine.Physics2D/PhysicsEngine2D.cs b/Engine.Physics2D/PhysicsEngine2D.cs index 5c05f3f..d823277 100644 --- a/Engine.Physics2D/PhysicsEngine2D.cs +++ b/Engine.Physics2D/PhysicsEngine2D.cs @@ -91,13 +91,13 @@ public class PhysicsEngine2D : HierarchyObject, IPhysicsEngine2D } } - OnPhysicsIteration?.Invoke(this, intervalDeltaTime); + OnPhysicsIteration?.InvokeSafe(this, intervalDeltaTime); } foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector) physicsUpdate.PhysicsUpdate(deltaTime); - OnPhysicsStep?.Invoke(this, deltaTime); + OnPhysicsStep?.InvokeSafe(this, deltaTime); } private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime) diff --git a/Engine.Physics2D/PhysicsEngine2DStandalone.cs b/Engine.Physics2D/PhysicsEngine2DStandalone.cs index c4d1a69..129ce14 100644 --- a/Engine.Physics2D/PhysicsEngine2DStandalone.cs +++ b/Engine.Physics2D/PhysicsEngine2DStandalone.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; +using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D.Abstract; @@ -106,9 +107,9 @@ public class PhysicsEngine2DStandalone : IPhysicsEngine2D } } } - OnPhysicsIteration?.Invoke(this, intervalDeltaTime); + OnPhysicsIteration?.InvokeSafe(this, intervalDeltaTime); } - OnPhysicsStep?.Invoke(this, deltaTime); + OnPhysicsStep?.InvokeSafe(this, deltaTime); } private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime) diff --git a/Engine.Systems/StateMachine/State.cs b/Engine.Systems/StateMachine/State.cs index 5ebe352..d72e76c 100644 --- a/Engine.Systems/StateMachine/State.cs +++ b/Engine.Systems/StateMachine/State.cs @@ -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() { diff --git a/Engine.Systems/StateMachine/StateBehaviourBase.cs b/Engine.Systems/StateMachine/StateBehaviourBase.cs index d51f888..9a1b49b 100644 --- a/Engine.Systems/StateMachine/StateBehaviourBase.cs +++ b/Engine.Systems/StateMachine/StateBehaviourBase.cs @@ -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(); diff --git a/Engine.Systems/StateMachine/StateMachine.cs b/Engine.Systems/StateMachine/StateMachine.cs index 4d5bda9..06930b8 100644 --- a/Engine.Systems/StateMachine/StateMachine.cs +++ b/Engine.Systems/StateMachine/StateMachine.cs @@ -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; } diff --git a/Engine.Systems/Time/StopwatchBehaviour.cs b/Engine.Systems/Time/StopwatchBehaviour.cs index 1731c04..1905ac6 100644 --- a/Engine.Systems/Time/StopwatchBehaviour.cs +++ b/Engine.Systems/Time/StopwatchBehaviour.cs @@ -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() diff --git a/Engine.Systems/Time/TickerBehaviour.cs b/Engine.Systems/Time/TickerBehaviour.cs index 6f928e4..7c02c53 100644 --- a/Engine.Systems/Time/TickerBehaviour.cs +++ b/Engine.Systems/Time/TickerBehaviour.cs @@ -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); } } diff --git a/Engine.Systems/Time/TimerBehaviour.cs b/Engine.Systems/Time/TimerBehaviour.cs index f88b5d4..df83629 100644 --- a/Engine.Systems/Time/TimerBehaviour.cs +++ b/Engine.Systems/Time/TimerBehaviour.cs @@ -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() diff --git a/Engine.Systems/Tween/Tween.cs b/Engine.Systems/Tween/Tween.cs index a9fb89d..a93448d 100644 --- a/Engine.Systems/Tween/Tween.cs +++ b/Engine.Systems/Tween/Tween.cs @@ -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; diff --git a/Engine.Systems/Tween/TweenManager.cs b/Engine.Systems/Tween/TweenManager.cs index e6df866..5ec7fdd 100644 --- a/Engine.Systems/Tween/TweenManager.cs +++ b/Engine.Systems/Tween/TweenManager.cs @@ -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; }