diff --git a/src/ComponentDepot.cs b/src/ComponentDepot.cs index cfe0e65..18057fe 100644 --- a/src/ComponentDepot.cs +++ b/src/ComponentDepot.cs @@ -55,16 +55,19 @@ internal class ComponentDepot entityComponentMap.Add(entityID, new HashSet()); } - var alreadyExists = entityComponentMap[entityID].Add(typeof(TComponent)); + var notFound = entityComponentMap[entityID].Add(typeof(TComponent)); // update filters - if (!alreadyExists) + if (notFound) { if (typeToFilterSignatures.TryGetValue(typeof(TComponent), out var filterSignatures)) { foreach (var filterSignature in filterSignatures) { - CheckFilter(filterSignature, entityID); + if (CheckFilter(filterSignature, entityID)) + { + filterSignatureToEntityIDs[filterSignature].Add(entityID); + } } } } @@ -93,7 +96,10 @@ internal class ComponentDepot { foreach (var filterSignature in filterSignatures) { - CheckFilter(filterSignature, entityID); + if (!CheckFilter(filterSignature, entityID)) + { + filterSignatureToEntityIDs[filterSignature].Remove(entityID); + } } } } diff --git a/src/System.cs b/src/System.cs index 125de07..12e8dfb 100644 --- a/src/System.cs +++ b/src/System.cs @@ -10,8 +10,18 @@ public abstract class System : EntityComponentReader MessageDepot = messageDepot; } + public System(World world) + { + world.AddSystem(this); + } + public abstract void Update(TimeSpan delta); + public virtual void InitializeFilters() + { + + } + protected Entity CreateEntity() { return EntityStorage.Create(); diff --git a/src/World.cs b/src/World.cs index 2eddb2a..9b53b8f 100644 --- a/src/World.cs +++ b/src/World.cs @@ -2,25 +2,40 @@ public class World { - private readonly List Systems; - private readonly List Renderers; - private readonly EntityStorage EntityStorage; - private readonly ComponentDepot ComponentDepot; - private MessageDepot MessageDepot; + private readonly List Systems = new List(); + private readonly List Renderers = new List(); + private readonly EntityStorage EntityStorage = new EntityStorage(); + private readonly ComponentDepot ComponentDepot = new ComponentDepot(); + private MessageDepot MessageDepot = new MessageDepot(); - internal World( - List systems, - List renderers, - ComponentDepot componentDepot, - EntityStorage entityStorage, - MessageDepot messageDepot - ) + internal void AddSystem(System system) { - Systems = systems; - Renderers = renderers; - ComponentDepot = componentDepot; - EntityStorage = entityStorage; - MessageDepot = messageDepot; + system.RegisterEntityStorage(EntityStorage); + system.RegisterComponentDepot(ComponentDepot); + system.RegisterMessageDepot(MessageDepot); + Systems.Add(system); + } + + internal void AddRenderer(Renderer renderer) + { + renderer.RegisterEntityStorage(EntityStorage); + renderer.RegisterComponentDepot(ComponentDepot); + Renderers.Add(renderer); + } + + public Entity CreateEntity() + { + return EntityStorage.Create(); + } + + public void Set(Entity entity, in TComponent component) where TComponent : struct + { + ComponentDepot.Set(entity.ID, component); + } + + public void Send(in TMessage message) where TMessage : struct + { + MessageDepot.Add(message); } public void Update(TimeSpan delta) diff --git a/src/WorldBuilder.cs b/src/WorldBuilder.cs deleted file mode 100644 index d184f1c..0000000 --- a/src/WorldBuilder.cs +++ /dev/null @@ -1,59 +0,0 @@ -namespace MoonTools.ECS; - -public class WorldBuilder -{ - private ComponentDepot componentDepot; - private EntityStorage entityStorage; - private MessageDepot messageDepot; - - private readonly List systems = new List(); - private readonly List renderers = new List(); - - public WorldBuilder() - { - componentDepot = new ComponentDepot(); - entityStorage = new EntityStorage(); - messageDepot = new MessageDepot(); - } - - public void AddSystem(System system) - { - system.RegisterEntityStorage(entityStorage); - system.RegisterComponentDepot(componentDepot); - system.RegisterMessageDepot(messageDepot); - systems.Add(system); - } - - public void AddRenderer(Renderer renderer) - { - renderer.RegisterEntityStorage(entityStorage); - renderer.RegisterComponentDepot(componentDepot); - renderers.Add(renderer); - } - - public Entity CreateEntity() - { - return entityStorage.Create(); - } - - public void Set(Entity entity, in TComponent component) where TComponent : struct - { - componentDepot.Set(entity.ID, component); - } - - public void Send(in TMessage message) where TMessage : struct - { - messageDepot.Add(message); - } - - public World Build() - { - return new World( - systems, - renderers, - componentDepot, - entityStorage, - messageDepot - ); - } -}