perf!: events refactored throughout all the project to use Event<> class

All delegate events are refactored to use the Event<TSender> and Event<TSender, TArgument> for performance issues regarding delegate events creating garbage, also this gives us better control on event invocation since C# Delegates did also create unnecessary garbage during Delegate.DynamicInvoke
This commit is contained in:
2025-05-31 00:14:43 +03:00
parent 996e61d0ad
commit 61e2761580
58 changed files with 637 additions and 485 deletions

View File

@@ -6,11 +6,11 @@ namespace Syntriax.Engine.Systems.StateMachine;
public class State : BaseEntity, IState
{
public event IState.StateUpdateEventHandler? OnStateUpdate = null;
public event IState.StateTransitionedFromEventHandler? OnStateTransitionedFrom = null;
public event IState.StateTransitionedToEventHandler? OnStateTransitionedTo = null;
public event IState.StateTransitionReadyEventHandler? OnStateTransitionReady = null;
public event INameable.NameChangedEventHandler? OnNameChanged = null;
public Event<IState> OnStateUpdate { get; } = new();
public Event<IState, IState.StateTransitionedFromArguments> OnStateTransitionedFrom { get; } = new();
public Event<IState, IState.StateTransitionedToArguments> OnStateTransitionedTo { get; } = new();
public Event<IState, IState.StateTransitionReadyArguments> OnStateTransitionReady { get; } = new();
public Event<INameable, INameable.NameChangedArguments> OnNameChanged { get; } = new();
private readonly List<StateTransition> transitions = [];
private readonly Dictionary<string, StateTransition> possibleTransitions = [];
@@ -28,7 +28,7 @@ public class State : BaseEntity, IState
string previousName = _name;
_name = value;
OnNameChanged?.Invoke(this, previousName);
OnNameChanged?.Invoke(this, new(previousName));
}
}
@@ -53,12 +53,12 @@ public class State : BaseEntity, IState
public void Update()
{
if (GetNextState() is IState transitionState)
OnStateTransitionReady?.Invoke(this, transitionState);
OnStateTransitionReady?.Invoke(this, new(transitionState));
OnStateUpdate?.Invoke(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?.Invoke(this, new(from));
public void TransitionFrom(IState to) => OnStateTransitionedFrom?.Invoke(this, new(to));
public IState? GetNextState()
{