using System; using System.Collections.Generic; using System.Linq; using Demo.Platformer.Entities.Components; using Demo.Platformer.Entities.Systems; using Microsoft.Xna.Framework; namespace Demo.Platformer.Entities { public class EntityComponentSystem { public EntityComponentSystem() { _entities = new List(); _components = new List(); _systems = new List(); _nextEntityId = 1; } private readonly List _systems; private readonly List _entities; private readonly List _components; private long _nextEntityId; public void RegisterSystem(ComponentSystem system) { if (system.EntityComponentSystem != null) throw new InvalidOperationException("Component system already registered"); system.EntityComponentSystem = this; _systems.Add(system); } public Entity CreateEntity(string name) { var entity = new Entity(this, _nextEntityId, name); _entities.Add(entity); _nextEntityId++; return entity; } public Entity FindEntity(string name) { return _entities.FirstOrDefault(e => e.Name == name); } public void DestroyEntity(Entity entity) { _entities.Remove(entity); } public void AttachComponent(EntityComponent component) { _components.Add(component); } public void DetachComponent(EntityComponent component) { _components.Remove(component); } internal IEnumerable GetComponents() { return _components.OfType(); } public void Update(GameTime gameTime) { foreach (var componentSystem in _systems.OfType()) componentSystem.Update(gameTime); } public void Draw(GameTime gameTime) { foreach (var componentSystem in _systems.OfType()) componentSystem.Draw(gameTime); } } }