From 9eb4b947f0fee2b196131a3b1f1a9a5ed4dc3657 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 18 Mar 2022 12:50:59 -0700 Subject: [PATCH] WithEntity message getters --- src/MessageDepot.cs | 19 +++++++++++++++++-- src/MessageStorage.cs | 37 +++++++++++++++++++++++++++++++++++++ src/System.cs | 19 +++++++++++++++++-- 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/MessageDepot.cs b/src/MessageDepot.cs index ef91f6d..28e8cc9 100644 --- a/src/MessageDepot.cs +++ b/src/MessageDepot.cs @@ -24,16 +24,31 @@ internal class MessageDepot return Lookup().Some(); } - public ReadOnlySpan ReadAll() where TMessage : struct + public ReadOnlySpan All() where TMessage : struct { return Lookup().All(); } - public TMessage ReadFirst() where TMessage : struct + public TMessage First() where TMessage : struct { return Lookup().First(); } + public IEnumerable WithEntity(int entityID) where TMessage : struct, IHasEntity + { + return Lookup().WithEntity(entityID); + } + + public ref readonly TMessage FirstWithEntity(int entityID) where TMessage : struct, IHasEntity + { + return ref Lookup().FirstWithEntity(entityID); + } + + public bool SomeWithEntity(int entityID) where TMessage : struct, IHasEntity + { + return Lookup().SomeWithEntity(entityID); + } + public void Clear() { foreach (var storage in storages.Values) diff --git a/src/MessageStorage.cs b/src/MessageStorage.cs index 8bfe24f..9441500 100644 --- a/src/MessageStorage.cs +++ b/src/MessageStorage.cs @@ -10,6 +10,7 @@ internal class MessageStorage : MessageStorage where TMessage : struct private int count = 0; private int capacity = 128; private TMessage[] messages; + private Dictionary> entityToIndices = new Dictionary>(); public MessageStorage() { @@ -25,6 +26,17 @@ internal class MessageStorage : MessageStorage where TMessage : struct } 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; } @@ -43,8 +55,33 @@ internal class MessageStorage : MessageStorage where TMessage : struct return messages[0]; } + public IEnumerable WithEntity(int entityID) + { + if (entityToIndices.ContainsKey(entityID)) + { + foreach (var index in entityToIndices[entityID]) + { + yield return messages[index]; + } + } + } + + public ref readonly TMessage FirstWithEntity(int entityID) + { + return ref messages[entityToIndices[entityID][0]]; + } + + public bool SomeWithEntity(int entityID) + { + return entityToIndices.ContainsKey(entityID) && entityToIndices[entityID].Count > 0; + } + public override void Clear() { count = 0; + foreach (var set in entityToIndices.Values) + { + set.Clear(); + } } } diff --git a/src/System.cs b/src/System.cs index 1c166a1..c58c4e1 100644 --- a/src/System.cs +++ b/src/System.cs @@ -38,12 +38,12 @@ public abstract class System : EntityComponentReader protected ReadOnlySpan ReadMessages() where TMessage : struct { - return MessageDepot.ReadAll(); + return MessageDepot.All(); } protected TMessage ReadMessage() where TMessage : struct { - return MessageDepot.ReadFirst(); + return MessageDepot.First(); } protected bool SomeMessage() where TMessage : struct @@ -51,6 +51,21 @@ public abstract class System : EntityComponentReader return MessageDepot.Some(); } + protected IEnumerable ReadMessagesWithEntity(in Entity entity) where TMessage : struct, IHasEntity + { + return MessageDepot.WithEntity(entity.ID); + } + + protected ref readonly TMessage ReadMessageWithEntity(in Entity entity) where TMessage : struct, IHasEntity + { + return ref MessageDepot.FirstWithEntity(entity.ID); + } + + protected bool SomeMessageWithEntity(in Entity entity) where TMessage : struct, IHasEntity + { + return MessageDepot.SomeWithEntity(entity.ID); + } + protected void Send(in TMessage message) where TMessage : struct { MessageDepot.Add(message);