36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Syntriax.Engine.Core.Serialization;
|
|
|
|
public record class EntityReference(string? Id = null);
|
|
|
|
public class EntityRegistry
|
|
{
|
|
public event EntityRegisteredEventHandler? 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?.InvokeSafe(this, 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?.InvokeSafe(registeredEntities[id]);
|
|
}
|
|
|
|
public delegate void EntityRegisteredEventHandler(EntityRegistry sender, IEntity entity);
|
|
}
|