Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal class EntityManager
- {
- #region Constructor
- public EntityManager()
- {
- activeEntities = new List<Guid>();
- componentPools = new Dictionary<Type, object>();
- Systems = new KeyedByTypeCollection<ISystem>();
- }
- #endregion Constructor
- #region Variables
- private List<Guid> activeEntities;
- private Dictionary<Type, object> componentPools;
- #endregion Variables
- #region Properties
- public KeyedByTypeCollection<ISystem> Systems { get; }
- public IReadOnlyList<Guid> ActiveEntities { get { return activeEntities; } }
- #endregion Properties
- #region Methods
- #region Core Methods
- #region Entity Management
- public Guid CreateEntity()
- {
- Guid entityId = Guid.NewGuid();
- activeEntities.Add(entityId);
- foreach (ISystem system in Systems)
- {
- if (system.ComponentRequirements.Count < 1)
- {
- system.EntityCache.Add(entityId);
- }
- }
- return entityId;
- }
- public void DestroyEntity(Guid entityId)
- {
- activeEntities.Remove(entityId);
- foreach (KeyValuePair<Type, object> componentPool in componentPools)
- {
- Dictionary<Guid, object> typedTargetPool = (Dictionary<Guid, object>)componentPool.Value;
- typedTargetPool.Remove(entityId);
- }
- foreach (ISystem system in Systems)
- {
- system.EntityCache.Remove(entityId);
- }
- }
- public void AddComponentToEntity<TComponent>(Guid entityId, TComponent component) where TComponent : IComponent
- {
- GetComponentPoolByType<TComponent>().Add(entityId, component);
- foreach (ISystem system in Systems)
- {
- Type[] componentTypes = ; // MISSING PART HERE
- if (system.ComponentRequirements.ValidateEntity(componentTypes))
- {
- system.EntityCache.Add(entityId);
- }
- }
- }
- public void RemoveComponentFromEntity<TComponent>(Guid entityId) where TComponent : IComponent
- {
- GetComponentPoolByType<TComponent>().Remove(entityId);
- foreach (ISystem system in Systems)
- {
- if (system.ComponentRequirements.RequiresComponent<TComponent>())
- {
- system.EntityCache.Remove(entityId);
- }
- }
- }
- #endregion Entity Management
- #region Pool Management
- public Dictionary<Guid, TComponent> GetComponentPool<TComponent>() where TComponent : IComponent
- {
- return GetComponentPoolByType<TComponent>();
- }
- public void AddComponentPool<TComponent>() where TComponent : IComponent
- {
- componentPools.Add(typeof(TComponent), new Dictionary<Guid, TComponent>());
- }
- public void RemoveComponentPool<TComponent>() where TComponent : IComponent
- {
- if (!Systems.Any(system => system.ComponentRequirements.RequiresComponent<TComponent>()))
- {
- List<Guid> entitiesToRemoveFromSystemCaches = GetComponentPoolByType<TComponent>().Keys.ToList();
- componentPools.Remove(typeof(TComponent));
- foreach (ISystem system in Systems)
- {
- if (system.ComponentRequirements.RequiresComponent<TComponent>())
- {
- for (int i = 0; i < entitiesToRemoveFromSystemCaches.Count; i++)
- {
- system.EntityCache.Remove(entitiesToRemoveFromSystemCaches[i]);
- }
- }
- }
- }
- }
- #endregion Pool Management
- #endregion Core Methods
- #region Helper Methods
- private Dictionary<Guid, TComponent> GetComponentPoolByType<TComponent>() where TComponent : IComponent
- {
- object targetPool = componentPools[typeof(TComponent)];
- return (Dictionary<Guid, TComponent>)targetPool;
- }
- #endregion Helper Methods
- #endregion Methods
- }
Advertisement
Add Comment
Please, Sign In to add comment