为系统循环创建高性能组件池

tot*_*ner 7 c# entity-component-system

我正在设置一个非常小的Entity-Component-System示例,并且组件池存在一些问题.

目前我的实体只是ID(GUID),这很好.

每个系统都必须实现该ISystem接口

internal interface ISystem
{
    void Run();
}
Run Code Online (Sandbox Code Playgroud)

并存储在KeyedByTypeCollection.此集合确保每个系统都是唯一的.

每个组件都必须实现该IComponent接口.

internal interface IComponent
{
}
Run Code Online (Sandbox Code Playgroud)

通过这样做,我可以将所有不同的组件类型存储到其匹配的组件池中.每个游泳池都是Dictionary<Guid, IComponent>.键表示实体的ID,值是该实体的组件.每个池都存储在一个池中KeyedByTypeCollection,以确保组件池是唯一的.

目前我的EntityManager类包含核心逻辑.我不知道它是否需要静态但目前我的系统需要从中获取一些信息.

处理组件池的重要核心方法是:

internal static class EntityManager
{
    public static List<Guid> activeEntities = new List<Guid>();

    private static KeyedByTypeCollection<Dictionary<Guid, IComponent>> componentPools = new KeyedByTypeCollection<Dictionary<Guid, IComponent>>();

    public static KeyedByTypeCollection<ISystem> systems = new KeyedByTypeCollection<ISystem>(); // Hold unique Systems

    public static Guid CreateEntity() // Add a new GUID and return it
    {
        Guid entityId = Guid.NewGuid();
        activeEntities.Add(entityId);
        return entityId;
    }

    public static void DestroyEntity(Guid entityId) // Remove the entity from every component pool
    {
        activeEntities.Remove(entityId);

        for (int i = 0; i < componentPools.Count; i++)
        {
            componentPools[i].Remove(entityId);
        }
    }

    public static Dictionary<Guid, TComponent> GetComponentPool<TComponent>() where TComponent : IComponent // get a component pool by its component type
    {
        return componentPools[typeof(TComponent)];
    }

    public static void AddComponentPool<TComponent>() where TComponent : IComponent // add a new pool by its component type
    {
        componentPools.Add(new Dictionary<Guid, TComponent>());
    }

    public static void RemoveComponentPool<TComponent>() where TComponent : IComponent // remove a pool by its component type
    {
        componentPools.Remove(typeof(TComponent));
    }

    public static void AddComponentToEntity(Guid entityId, IComponent component) // add a component to an entity by the component type
    {
        componentPools[component.GetType()].Add(entityId, component);
    }

    public static void RemoveComponentFromEntity<TComponent>(Guid entityId) where TComponent : IComponent // remove a component from an entity by the component type
    {
        componentPools[typeof(TComponent)].Remove(entityId);
    }
}
Run Code Online (Sandbox Code Playgroud)

我为测试目的创建了一个小型移动系统:

internal class Movement : ISystem
{
    public void Run()
    {
        for (int i = 0; i < EntityManager.activeEntities.Count; i++)
        {
            Guid entityId = EntityManager.activeEntities[i];

            if (EntityManager.GetComponentPool<Position>().TryGetValue(entityId, out Position positionComponent)) // Get the position component
            {
                positionComponent.X++; // Move one to the right and one up ...
                positionComponent.Y++;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

由于字典查找,这应该没问题.我不确定我是否可以优化它,但我认为我必须先遍历所有实体.

这是问题所在:

我无法使用,KeyedByTypeCollection<Dictionary<Guid, IComponent>>因为我需要有类似的东西

KeyedByTypeCollection<Dictionary<Guid, Type where Type : IComponent>>

GetComponentPoolAddComponentPool方法抛出错误.

GetComponentPool:无法将IComponent隐式转换为TComponent

在调用时GetComponentPool我必须将字典值转换IComponentTComponent.

AddComponentPool:无法从TComponent转换为IComponent

致电时AddComponentPool我会投TComponentIComponent.

我不认为铸造是一种选择,因为这似乎会降低性能.

有人会介意帮我修复类型问题吗?


更新:

在调试时,我使用此代码来测试整个ECS

internal class Program
{
    private static void Main(string[] args)
    {
        EntityManager.AddComponentPool<Position>();
        EntityManager.AddComponentPool<MovementSpeed>();

        EntityManager.Systems.Add(new Movement());

        Guid playerId = EntityManager.CreateEntity();
        EntityManager.AddComponentToEntity(playerId, new Position());
        EntityManager.AddComponentToEntity(playerId, new MovementSpeed(1));

        foreach (ISystem system in EntityManager.Systems)
        {
            system.Run();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经展示了Movement系统,这里是缺少的组件

internal class Position : IComponent
{
    public Position(float x = 0, float y = 0)
    {
        X = x;
        Y = y;
    }

    public float X { get; set; }
    public float Y { get; set; }
}

internal class MovementSpeed : IComponent
{
    public MovementSpeed(float value = 0)
    {
        Value = value;
    }

    public float Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

usr*_*usr 5

问题的核心是,Dictionary<Guid, TComponent>并且Dictionary<Guid, IComponent>完全不相关的类型.他们不能相互转换.这是因为可以读写字典.如果你可以将a转换List<Giraffe>为a,List<Animal>那么你可以存储一个Tiger会打破类型安全的东西.如果你可以转换另一种方式,你可以读取Tiger实例并将它们视为Giraffe再次破坏类型安全性.您可以在网上搜索.net covariance contravariance以查找更多信息.


一个简单的解决方案是改变

KeyedByTypeCollection<Dictionary<Guid, IComponent>> componentPools
Run Code Online (Sandbox Code Playgroud)

Dictionary<Type, object> componentPools
Run Code Online (Sandbox Code Playgroud)

.

这使您可以Dictionary<Guid, TComponent>在之前无法存储的集合中存储.缺点是你需要现在施法.但是你只需要转换相当便宜的字典实例.您不需要在O(N)时间内转换整个字典.

访问者:

public static Dictionary<Guid, TComponent> GetComponentPool<TComponent>() where TComponent : IComponent // get a component pool by its component type
{
    return (Dictionary<Guid, TComponent>)componentPools[typeof(TComponent)]; //cast inserted
}

public static void AddComponentPool<TComponent>() where TComponent : IComponent // add a new pool by its component type
{
    componentPools.Add(typeof(TComponent), (object)new Dictionary<Guid, TComponent>()); //unneeded cast inserted for clarity
}
Run Code Online (Sandbox Code Playgroud)

也许你不喜欢我们需要施放的东西.我无法理解这一点.实践经验表明,当您使用类型系统(泛型,方差)进行详细说明时,您将获得更糟糕的解决方案.所以不要担心.做有用的.