perf: regular events to custom events

This commit is contained in:
2025-05-30 12:58:08 +03:00
parent b1b5af94d3
commit 846aa75dd5
42 changed files with 342 additions and 198 deletions

View File

@@ -8,9 +8,9 @@ namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("Behaviour Count: {behaviours.Count}")]
public class BehaviourController : BaseEntity, IBehaviourController
{
public event IBehaviourController.BehaviourAddedEventHandler? OnBehaviourAdded = null;
public event IBehaviourController.BehaviourRemovedEventHandler? OnBehaviourRemoved = null;
public event IHasUniverseObject.UniverseObjectAssignedEventHandler? OnUniverseObjectAssigned = null;
public Event<IBehaviourController, IBehaviour> OnBehaviourAdded { get; private set; } = new();
public Event<IBehaviourController, IBehaviour> OnBehaviourRemoved { get; private set; } = new();
public Event<IHasUniverseObject> OnUniverseObjectAssigned { get; private set; } = new();
private readonly IList<IBehaviour> behaviours = new List<IBehaviour>(Constants.BEHAVIOURS_SIZE_INITIAL);
@@ -20,6 +20,9 @@ public class BehaviourController : BaseEntity, IBehaviourController
public int Count => behaviours.Count;
public IBehaviour this[Index index] => behaviours[index];
public int Count => behaviours.Count;
public IBehaviour this[Index index] => behaviours[index];
public T AddBehaviour<T>(T behaviour) where T : class, IBehaviour
{
InsertBehaviourByPriority(behaviour);
@@ -28,7 +31,7 @@ public class BehaviourController : BaseEntity, IBehaviourController
if (IsInitialized)
behaviour.Initialize();
behaviour.OnPriorityChanged += OnPriorityChange;
behaviour.OnPriorityChanged.AddListener(OnPriorityChange);
OnBehaviourAdded?.Invoke(this, behaviour);
return behaviour;
}
@@ -94,7 +97,7 @@ public class BehaviourController : BaseEntity, IBehaviourController
if (!behaviours.Contains(behaviour))
throw new Exception($"{behaviour.GetType().Name} does not exist in {UniverseObject.Name}'s {nameof(IBehaviourController)}.");
behaviour.OnPriorityChanged -= OnPriorityChange;
behaviour.OnPriorityChanged.RemoveListener(OnPriorityChange);
behaviour.Finalize();
behaviours.Remove(behaviour);
OnBehaviourRemoved?.Invoke(this, behaviour);