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
40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Syntriax.Engine.Core.Serialization;
|
|
|
|
public class EntityRegistry
|
|
{
|
|
public Event<EntityRegistry, EntityRegisteredArguments> OnEntityRegistered = null!;
|
|
|
|
private readonly Dictionary<string, Action<IEntity>?> assignCallbacks = [];
|
|
private readonly Dictionary<string, IEntity> registeredEntities = [];
|
|
public IReadOnlyDictionary<string, IEntity> RegisteredEntities => registeredEntities;
|
|
|
|
public void Add(IEntity entity)
|
|
{
|
|
if (registeredEntities.TryAdd(entity.Id, entity))
|
|
OnEntityRegistered?.Invoke(this, new(entity));
|
|
}
|
|
|
|
public void QueueAssign(string id, Action<IEntity> setMethod)
|
|
{
|
|
assignCallbacks.TryAdd(id, null);
|
|
assignCallbacks[id] = assignCallbacks[id] + setMethod;
|
|
}
|
|
|
|
public void AssignAll()
|
|
{
|
|
foreach ((string id, Action<IEntity>? action) in assignCallbacks)
|
|
action?.Invoke(registeredEntities[id]);
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
assignCallbacks.Clear();
|
|
registeredEntities.Clear();
|
|
}
|
|
|
|
public readonly record struct EntityRegisteredArguments(IEntity Entity);
|
|
}
|