using System; using System.Collections.Generic; using System.Linq; namespace Encompass { public class Entity { public readonly static List Empty = new List(); public readonly int id; private readonly Dictionary> componentBag = new Dictionary>(); private readonly Dictionary> activeComponents = new Dictionary>(); public Entity(int id) { this.id = id; } public TComponent AddComponent() where TComponent : Component, new() { TComponent component = new TComponent(); if (!componentBag.ContainsKey(typeof(TComponent))) { var componentList = new List(); var activeComponentList = new List(); componentBag.Add(typeof(TComponent), componentList); activeComponents.Add(typeof(TComponent), activeComponentList); componentList.Add(component); activeComponentList.Add(component); } else { componentBag[typeof(TComponent)].Add(component); activeComponents[typeof(TComponent)].Add(component); } return component; } public IEnumerable GetComponents() where TComponent : Component { if (activeComponents.ContainsKey(typeof(TComponent))) { return activeComponents[typeof(TComponent)].Cast(); } else { return Enumerable.Empty(); } } public TComponent GetComponent() where TComponent : Component { return GetComponents().First(); } public bool HasComponent() where TComponent : Component { return activeComponents.ContainsKey(typeof(TComponent)) && activeComponents[typeof(TComponent)].Count != 0; } } }