引用类之外的字典值

时间:2019-11-27 13:43:26

标签: c# dictionary entity-component-system data-oriented-design

基本上,我正在尝试克隆各种统一的ECS系统,而我当前的问题是实体管理器查询系统,我必须从该类,查询的组件列表或要传递给包含对字典列表中组件的引用。

using System;
using System.Collections.Generic;

namespace Ecs
{
    //contains objects that have a variable amount of components
    public struct Entity : IComponent
    {
        public IComponent[] Components { get; set; }
        public Entity(params IComponent[] components)
        {
            Components = components;
        }
    }

    //makes entity data queryable by a per datatype basis (ie. all "locations" in one clean list that's fast to scim through)
    public class EntityManager
    {
        public List<SortedDictionary<Guid, IComponent>> EntityComponentPool = new List<SortedDictionary<Guid, IComponent>>();

        public EntityManager()
        {

        }

        /// <summary>
        /// turns entity into lists of it's components
        /// </summary>
        /// <param name="entity"></param>
        public void AddEntity(Entity entity)
        {
            Guid entityId = new Guid();
            foreach(IComponent component in entity.Components)
            {
                int index = EntityComponentPool.FindIndex(x => x.GetType().GenericTypeArguments[1].GetType() == component.GetType());

                if(index > 0)
                {
                    SortedDictionary<Guid, IComponent> newType = new SortedDictionary<Guid, IComponent>
                    {
                        { entityId, component }
                    };

                    EntityComponentPool.Add(newType);
                }
                else
                {
                    EntityComponentPool[index].Add(entityId, component);
                }
            }
        }

        public void AddEntityList(List<Entity> entityList)
        { 
            foreach(Entity entity in entityList)
            {
                AddEntity(entity);
            }
        }

        public ref IComponent[] QueryEntityRefrence(System.Type type)
        {
            List<IComponent> returned = new List<IComponent>();
            int index = EntityComponentPool.FindIndex(x => x.GetType().GenericTypeArguments[1].GetType() == type);

            //now somehow return a reference to the dictionary slot
            return ref 
        }
    }

    public interface IComponent
    {
    }
}

到目前为止,我在这里所做的只是将实体的数据拆分为包含相同类型的多个列表。从理论上讲应该可以,但是我需要了解的是如何通过查询查询来更改组件列表的值。

字典是到目前为止我唯一尝试过的事情,我可以做任何优雅的系统来存储数据组件之间的关系,例如值元组列表或某种类似的东西,只要我现在可以在代码外部执行即可查询具有某些组件类型(并排除某些类型)的实体,并对它们进行数学运算,以便它是对EntityComponentPool中组件的引用。由于仅返回已修改其数据的实体列表可能效率低下,并且令人讨厌代码...

0 个答案:

没有答案