Entity Component System, можно ли доработать класс Entity - C#

Узнай цену своей работы

Формулировка задачи:

Здравствуйте, сделал свою реализацию Entity Component System, но хотелось бы узнать ваше мнение по поводу класса Entity. Можно ли ускорить его работу, сократить код для доступа к его компонентам?
    public class Entity 
    {
        private Dictionary<Type, Component> _components = new Dictionary<Type, Component>();
 
        public void Add(Component component)
        {
            _components.Add(component.GetType(), component);
            component.Entity = this;
        }
        public void Remove(Type componentType)
        {
            _components[componentType].Entity = null;
            _components.Remove(componentType);
 
        }
 
        public T Get<T>() where T : class
        {
            if (_components.ContainsKey(typeof(T))) return _components[typeof(T)] as T;
 
            return null;
        }
 
    }
 
    public abstract class Component
    {
        public Entity Entity { get; set; }
 
        public T Get<T>() where T : class
        {
            return Entity.Get<T>();
        }
 
    }

Решение задачи: «Entity Component System, можно ли доработать класс Entity»

textual
Листинг программы
public T Get<T>() where T : class
{
    if (_components.ContainsKey(typeof(T))) return _components[typeof(T)] as T;
 
    return null;
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4 из 5
Похожие ответы