From 1438188dca5ebb3a1d206612934d84e9fed7e654 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 26 Jan 2023 16:34:15 -0800 Subject: [PATCH] add filter callback system --- src/Filter.cs | 10 ++++++++++ src/FilterStorage.cs | 29 +++++++++++++++++++++++++++++ src/World.cs | 4 ++-- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/Filter.cs b/src/Filter.cs index 4124428..d08e1ca 100644 --- a/src/Filter.cs +++ b/src/Filter.cs @@ -23,5 +23,15 @@ namespace MoonTools.ECS // WARNING: this WILL crash if the index is out of range! public Entity NthEntity(int index) => FilterStorage.FilterNthEntity(Signature, index); + + public void RegisterAddCallback(Action callback) + { + FilterStorage.RegisterAddCallback(Signature, callback); + } + + public void RegisterRemoveCallback(Action callback) + { + FilterStorage.RegisterRemoveCallback(Signature, callback); + } } } diff --git a/src/FilterStorage.cs b/src/FilterStorage.cs index b8ea8f3..5a466a8 100644 --- a/src/FilterStorage.cs +++ b/src/FilterStorage.cs @@ -10,6 +10,9 @@ namespace MoonTools.ECS private Dictionary> filterSignatureToEntityIDs = new Dictionary>(); private Dictionary> typeToFilterSignatures = new Dictionary>(); + private Dictionary> addCallbacks = new Dictionary>(); + private Dictionary> removeCallbacks = new Dictionary>(); + public FilterStorage(EntityStorage entityStorage, TypeIndices componentTypeIndices) { EntityStorage = entityStorage; @@ -116,6 +119,10 @@ namespace MoonTools.ECS if (!EntityStorage.HasComponent(entityID, type)) { filterSignatureToEntityIDs[filterSignature].Remove(entityID); + if (removeCallbacks.TryGetValue(filterSignature, out var removeCallback)) + { + removeCallback(entityID); + } return; } } @@ -125,11 +132,19 @@ namespace MoonTools.ECS if (EntityStorage.HasComponent(entityID, type)) { filterSignatureToEntityIDs[filterSignature].Remove(entityID); + if (removeCallbacks.TryGetValue(filterSignature, out var removeCallback)) + { + removeCallback(entityID); + } return; } } filterSignatureToEntityIDs[filterSignature].Add(entityID); + if (addCallbacks.TryGetValue(filterSignature, out var addCallback)) + { + addCallback(entityID); + } } public void RemoveEntity(int entityID, int componentTypeIndex) @@ -139,8 +154,22 @@ namespace MoonTools.ECS foreach (var filterSignature in filterSignatures) { filterSignatureToEntityIDs[filterSignature].Remove(entityID); + if (removeCallbacks.TryGetValue(filterSignature, out var removeCallback)) + { + removeCallback(entityID); + } } } } + + public void RegisterAddCallback(FilterSignature filterSignature, Action callback) + { + addCallbacks.Add(filterSignature, callback); + } + + public void RegisterRemoveCallback(FilterSignature filterSignature, Action callback) + { + removeCallbacks.Add(filterSignature, callback); + } } } diff --git a/src/World.cs b/src/World.cs index 17f487f..481900e 100644 --- a/src/World.cs +++ b/src/World.cs @@ -38,12 +38,12 @@ namespace MoonTools.ECS throw new InvalidOperationException("This entity is not valid!"); } #endif + ComponentDepot.Set(entity.ID, component); + if (EntityStorage.SetComponent(entity.ID, ComponentTypeIndices.GetIndex())) { FilterStorage.Check(entity.ID); } - - ComponentDepot.Set(entity.ID, component); } public Template CreateTemplate()