From f7d4fcdee74bc214e4ce17646576ba17b8cd7bac Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 24 May 2023 12:46:27 -0700 Subject: [PATCH] add Manipulator, remove Spawner, remove Template --- src/EntityComponentReader.cs | 1 - src/Manipulator.cs | 17 ++++++++++ src/Spawner.cs | 13 -------- src/System.cs | 57 ++------------------------------ src/Template.cs | 13 -------- src/TemplateStorage.cs | 34 ------------------- src/World.cs | 64 ++++++++++++++++++++---------------- 7 files changed, 56 insertions(+), 143 deletions(-) create mode 100644 src/Manipulator.cs delete mode 100644 src/Spawner.cs delete mode 100644 src/Template.cs delete mode 100644 src/TemplateStorage.cs diff --git a/src/EntityComponentReader.cs b/src/EntityComponentReader.cs index b752d43..976d1c1 100644 --- a/src/EntityComponentReader.cs +++ b/src/EntityComponentReader.cs @@ -13,7 +13,6 @@ namespace MoonTools.ECS internal FilterStorage FilterStorage => World.FilterStorage; internal TypeIndices ComponentTypeIndices => World.ComponentTypeIndices; internal TypeIndices RelationTypeIndices => World.RelationTypeIndices; - internal TemplateStorage TemplateStorage => World.TemplateStorage; internal ComponentDepot TemplateComponentDepot => World.TemplateComponentDepot; public EntityComponentReader(World world) diff --git a/src/Manipulator.cs b/src/Manipulator.cs new file mode 100644 index 0000000..4b336c1 --- /dev/null +++ b/src/Manipulator.cs @@ -0,0 +1,17 @@ +namespace MoonTools.ECS +{ + public abstract class Manipulator : EntityComponentReader + { + public Manipulator(World world) : base(world) + { + } + + protected Entity CreateEntity() => World.CreateEntity(); + protected void Set(in Entity entity, in TComponent component) where TComponent : unmanaged => World.Set(entity, component); + protected void Remove(in Entity entity) where TComponent : unmanaged => World.Remove(entity); + protected void Relate(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged => World.Relate(entityA, entityB, relationData); + protected void Unrelate(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged => World.Unrelate(entityA, entityB); + protected void UnrelateAll(in Entity entity) where TRelationKind : unmanaged => World.UnrelateAll(entity); + protected void Destroy(in Entity entity) => World.Destroy(entity); + } +} diff --git a/src/Spawner.cs b/src/Spawner.cs deleted file mode 100644 index b0c414f..0000000 --- a/src/Spawner.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace MoonTools.ECS -{ - public abstract class Spawner : EntityComponentReader - { - public Spawner(World world) : base(world) - { - } - - protected Entity CreateEntity() => World.CreateEntity(); - protected void Set(in Entity entity, in TComponent component) where TComponent : unmanaged => World.Set(entity, component); - protected void Relate(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged => World.Relate(entityA, entityB, relationData); - } -} diff --git a/src/System.cs b/src/System.cs index eb87a46..5cc10c8 100644 --- a/src/System.cs +++ b/src/System.cs @@ -1,9 +1,8 @@ using System; -using System.Collections.Generic; namespace MoonTools.ECS { - public abstract class System : EntityComponentReader + public abstract class System : Manipulator { internal MessageDepot MessageDepot => World.MessageDepot; @@ -11,25 +10,6 @@ namespace MoonTools.ECS public abstract void Update(TimeSpan delta); - protected Entity CreateEntity() => World.CreateEntity(); - - protected void Set(in Entity entity, in TComponent component) where TComponent : unmanaged => World.Set(entity, component); - - protected void Remove(in Entity entity) where TComponent : unmanaged - { - if (EntityStorage.RemoveComponent(entity.ID, ComponentTypeIndices.GetIndex())) - { - // Run filter storage update first so that the entity state is still valid in the remove callback. - FilterStorage.Check(entity.ID); - ComponentDepot.Remove(entity.ID); - } - } - - protected void Set(in Template template, in TComponent component) where TComponent : unmanaged => World.Set(template, component); - - // This feature is EXPERIMENTAL. USe at your own risk!! - protected Entity Instantiate(in Template template) => World.Instantiate(template); - protected ReadOnlySpan ReadMessages() where TMessage : unmanaged { return MessageDepot.All(); @@ -60,39 +40,8 @@ namespace MoonTools.ECS return MessageDepot.SomeWithEntity(entity.ID); } - protected void Send(in TMessage message) where TMessage : unmanaged - { - MessageDepot.Add(message); - } + protected void Send(in TMessage message) where TMessage : unmanaged => World.Send(message); - protected void Send(in Entity entity, in TMessage message) where TMessage : unmanaged - { - MessageDepot.Add(entity.ID, message); - } - - protected void Relate(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged => World.Relate(entityA, entityB, relationData); - - protected void Unrelate(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged - { - var (aEmpty, bEmpty) = RelationDepot.Remove(entityA, entityB); - - if (aEmpty) - { - EntityStorage.RemoveRelation(entityA.ID, RelationTypeIndices.GetIndex()); - } - - if (bEmpty) - { - EntityStorage.RemoveRelation(entityB.ID, RelationTypeIndices.GetIndex()); - } - } - - protected void UnrelateAll(in Entity entity) where TRelationKind : unmanaged - { - RelationDepot.UnrelateAll(entity.ID); - EntityStorage.RemoveRelation(entity.ID, RelationTypeIndices.GetIndex()); - } - - protected void Destroy(in Entity entity) => World.Destroy(entity); + protected void Send(in Entity entity, in TMessage message) where TMessage : unmanaged => World.Send(entity, message); } } diff --git a/src/Template.cs b/src/Template.cs deleted file mode 100644 index 7654b7a..0000000 --- a/src/Template.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace MoonTools.ECS -{ - // This feature is EXPERIMENTAL. Use at your own risk!! - public struct Template - { - public int ID { get; } - - internal Template(int id) - { - ID = id; - } - } -} diff --git a/src/TemplateStorage.cs b/src/TemplateStorage.cs deleted file mode 100644 index fb9dfa0..0000000 --- a/src/TemplateStorage.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; - -namespace MoonTools.ECS -{ - public class TemplateStorage - { - private int nextID = 0; - - private Dictionary> TemplateToComponentTypeIndices = new Dictionary>(); - - public Template Create() - { - TemplateToComponentTypeIndices.Add(nextID, new HashSet()); - return new Template(NextID()); - } - - public bool SetComponent(int templateID, int componentTypeIndex) - { - return TemplateToComponentTypeIndices[templateID].Add(componentTypeIndex); - } - - public HashSet ComponentTypeIndices(int templateID) - { - return TemplateToComponentTypeIndices[templateID]; - } - - private int NextID() - { - var id = nextID; - nextID += 1; - return id; - } - } -} diff --git a/src/World.cs b/src/World.cs index 6a04493..43c26c8 100644 --- a/src/World.cs +++ b/src/World.cs @@ -13,7 +13,6 @@ namespace MoonTools.ECS internal readonly FilterStorage FilterStorage; public FilterBuilder FilterBuilder => new FilterBuilder(FilterStorage, ComponentTypeIndices); - internal readonly TemplateStorage TemplateStorage = new TemplateStorage(); internal readonly ComponentDepot TemplateComponentDepot; public World() @@ -46,36 +45,14 @@ namespace MoonTools.ECS } } - public Template CreateTemplate() + public void Remove(in Entity entity) where TComponent : unmanaged { - return TemplateStorage.Create(); - } - - public void Set(in Template template, in TComponent component) where TComponent : unmanaged - { - var componentTypeIndex = ComponentTypeIndices.GetIndex(); - TemplateStorage.SetComponent(template.ID, componentTypeIndex); - TemplateComponentDepot.Set(template.ID, component); - ComponentDepot.Register(componentTypeIndex); - } - - public unsafe Entity Instantiate(in Template template) - { - var entity = EntityStorage.Create(); - - foreach (var componentTypeIndex in TemplateStorage.ComponentTypeIndices(template.ID)) + if (EntityStorage.RemoveComponent(entity.ID, ComponentTypeIndices.GetIndex())) { - EntityStorage.SetComponent(entity.ID, componentTypeIndex); - FilterStorage.Check(entity.ID, componentTypeIndex); - ComponentDepot.Set(entity.ID, componentTypeIndex, TemplateComponentDepot.UntypedGet(template.ID, componentTypeIndex)); + // Run filter storage update first so that the entity state is still valid in the remove callback. + FilterStorage.Check(entity.ID); + ComponentDepot.Remove(entity.ID); } - - return entity; - } - - public void Send(in TMessage message) where TMessage : unmanaged - { - MessageDepot.Add(message); } public void Relate(in Entity entityA, in Entity entityB, TRelationKind relationData) where TRelationKind : unmanaged @@ -86,6 +63,37 @@ namespace MoonTools.ECS EntityStorage.AddRelationKind(entityB.ID, relationTypeIndex); } + public void Unrelate(in Entity entityA, in Entity entityB) where TRelationKind : unmanaged + { + var (aEmpty, bEmpty) = RelationDepot.Remove(entityA, entityB); + + if (aEmpty) + { + EntityStorage.RemoveRelation(entityA.ID, RelationTypeIndices.GetIndex()); + } + + if (bEmpty) + { + EntityStorage.RemoveRelation(entityB.ID, RelationTypeIndices.GetIndex()); + } + } + + public void UnrelateAll(in Entity entity) where TRelationKind : unmanaged + { + RelationDepot.UnrelateAll(entity.ID); + EntityStorage.RemoveRelation(entity.ID, RelationTypeIndices.GetIndex()); + } + + public void Send(in TMessage message) where TMessage : unmanaged + { + MessageDepot.Add(message); + } + + public void Send(in Entity entity, in TMessage message) where TMessage : unmanaged + { + MessageDepot.Add(entity.ID, message); + } + public void Destroy(in Entity entity) { foreach (var componentTypeIndex in EntityStorage.ComponentTypeIndices(entity.ID))