40 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
 | 
						|
namespace 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);
 | 
						|
}
 |