From 01a9211ff48336a4f613357b4e5819916bc7a0d3 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 9 Dec 2022 11:29:43 -0800 Subject: [PATCH] remove IHasEntity --- src/IHasEntity.cs | 7 ------- src/MessageDepot.cs | 21 +++++++++++++-------- src/MessageStorage.cs | 24 ++++++++++++------------ src/System.cs | 11 ++++++++--- 4 files changed, 33 insertions(+), 30 deletions(-) delete mode 100644 src/IHasEntity.cs diff --git a/src/IHasEntity.cs b/src/IHasEntity.cs deleted file mode 100644 index 35a676f..0000000 --- a/src/IHasEntity.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace MoonTools.ECS -{ - public interface IHasEntity - { - Entity Entity { get; } - } -} diff --git a/src/MessageDepot.cs b/src/MessageDepot.cs index 5f925ff..5af175f 100644 --- a/src/MessageDepot.cs +++ b/src/MessageDepot.cs @@ -7,7 +7,7 @@ namespace MoonTools.ECS { private Dictionary storages = new Dictionary(); - private MessageStorage Lookup() where TMessage : struct + private MessageStorage Lookup() where TMessage : unmanaged { if (!storages.ContainsKey(typeof(TMessage))) { @@ -17,37 +17,42 @@ namespace MoonTools.ECS return storages[typeof(TMessage)] as MessageStorage; } - public void Add(in TMessage message) where TMessage : struct + public void Add(in TMessage message) where TMessage : unmanaged { Lookup().Add(message); } - public bool Some() where TMessage : struct + public void Add(int entityID, in TMessage message) where TMessage : unmanaged + { + Lookup().Add(entityID, message); + } + + public bool Some() where TMessage : unmanaged { return Lookup().Some(); } - public ReadOnlySpan All() where TMessage : struct + public ReadOnlySpan All() where TMessage : unmanaged { return Lookup().All(); } - public TMessage First() where TMessage : struct + public TMessage First() where TMessage : unmanaged { return Lookup().First(); } - public IEnumerable WithEntity(int entityID) where TMessage : struct, IHasEntity + public IEnumerable WithEntity(int entityID) where TMessage : unmanaged { return Lookup().WithEntity(entityID); } - public ref readonly TMessage FirstWithEntity(int entityID) where TMessage : struct, IHasEntity + public ref readonly TMessage FirstWithEntity(int entityID) where TMessage : unmanaged { return ref Lookup().FirstWithEntity(entityID); } - public bool SomeWithEntity(int entityID) where TMessage : struct, IHasEntity + public bool SomeWithEntity(int entityID) where TMessage : unmanaged { return Lookup().SomeWithEntity(entityID); } diff --git a/src/MessageStorage.cs b/src/MessageStorage.cs index 0e29a1a..545da25 100644 --- a/src/MessageStorage.cs +++ b/src/MessageStorage.cs @@ -8,7 +8,7 @@ namespace MoonTools.ECS public abstract void Clear(); } - internal class MessageStorage : MessageStorage where TMessage : struct + internal class MessageStorage : MessageStorage where TMessage : unmanaged { private int count = 0; private int capacity = 128; @@ -29,20 +29,20 @@ namespace MoonTools.ECS } messages[count] = message; - - if (message is IHasEntity entityMessage) - { - if (!entityToIndices.ContainsKey(entityMessage.Entity.ID)) - { - entityToIndices.Add(entityMessage.Entity.ID, new List()); - } - - entityToIndices[entityMessage.Entity.ID].Add(count); - } - count += 1; } + public void Add(int entityID, in TMessage message) + { + if (!entityToIndices.ContainsKey(entityID)) + { + entityToIndices.Add(entityID, new List()); + } + entityToIndices[entityID].Add(count); + + Add(message); + } + public bool Some() { return count > 0; diff --git a/src/System.cs b/src/System.cs index f903de7..9e98e6a 100644 --- a/src/System.cs +++ b/src/System.cs @@ -43,17 +43,17 @@ namespace MoonTools.ECS return MessageDepot.Some(); } - protected IEnumerable ReadMessagesWithEntity(in Entity entity) where TMessage : unmanaged, IHasEntity + protected IEnumerable ReadMessagesWithEntity(in Entity entity) where TMessage : unmanaged { return MessageDepot.WithEntity(entity.ID); } - protected ref readonly TMessage ReadMessageWithEntity(in Entity entity) where TMessage : unmanaged, IHasEntity + protected ref readonly TMessage ReadMessageWithEntity(in Entity entity) where TMessage : unmanaged { return ref MessageDepot.FirstWithEntity(entity.ID); } - protected bool SomeMessageWithEntity(in Entity entity) where TMessage : unmanaged, IHasEntity + protected bool SomeMessageWithEntity(in Entity entity) where TMessage : unmanaged { return MessageDepot.SomeWithEntity(entity.ID); } @@ -63,6 +63,11 @@ namespace MoonTools.ECS MessageDepot.Add(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 { RelationDepot.Set(new Relation(entityA, entityB), relationData);