diff --git a/MoonTools.ECS.csproj b/MoonTools.ECS.csproj
index bafd05b..b9251e9 100644
--- a/MoonTools.ECS.csproj
+++ b/MoonTools.ECS.csproj
@@ -2,7 +2,6 @@
net6.0
- enable
enable
diff --git a/src/ComponentDepot.cs b/src/ComponentDepot.cs
index dae13fb..38eaef6 100644
--- a/src/ComponentDepot.cs
+++ b/src/ComponentDepot.cs
@@ -1,259 +1,263 @@
-namespace MoonTools.ECS;
+using System;
+using System.Collections.Generic;
-internal class ComponentDepot
+namespace MoonTools.ECS
{
- private Dictionary storages = new Dictionary();
-
- private Dictionary> filterSignatureToEntityIDs = new Dictionary>();
-
- private Dictionary> typeToFilterSignatures = new Dictionary>();
-
- private Dictionary> entityComponentMap = new Dictionary>();
-
- #if DEBUG
- private Dictionary singleComponentFilters = new Dictionary();
- #endif
-
- internal void Register() where TComponent : struct
+ internal class ComponentDepot
{
- if (!storages.ContainsKey(typeof(TComponent)))
+ private Dictionary storages = new Dictionary();
+
+ private Dictionary> filterSignatureToEntityIDs = new Dictionary>();
+
+ private Dictionary> typeToFilterSignatures = new Dictionary>();
+
+ private Dictionary> entityComponentMap = new Dictionary>();
+
+#if DEBUG
+ private Dictionary singleComponentFilters = new Dictionary();
+#endif
+
+ internal void Register() where TComponent : struct
{
- storages.Add(typeof(TComponent), new ComponentStorage());
- #if DEBUG
- singleComponentFilters.Add(typeof(TComponent), CreateFilter(new HashSet() { typeof(TComponent) }, new HashSet()));
- #endif
- }
- }
-
- private ComponentStorage Lookup(Type type)
- {
- return storages[type];
- }
-
- private ComponentStorage Lookup() where TComponent : struct
- {
- // TODO: is it possible to optimize this?
- Register();
- return storages[typeof(TComponent)] as ComponentStorage;
- }
-
- public bool Some() where TComponent : struct
- {
- return Lookup().Any();
- }
-
- public bool Has(int entityID) where TComponent : struct
- {
- return Lookup().Has(entityID);
- }
-
- private bool Has(Type type, int entityID)
- {
- return Lookup(type).Has(entityID);
- }
-
- public ref readonly TComponent Get(int entityID) where TComponent : struct
- {
- return ref Lookup().Get(entityID);
- }
-
- public ref readonly TComponent Get() where TComponent : struct
- {
- return ref Lookup().Get();
- }
-
- public void Set(int entityID, in TComponent component) where TComponent : struct
- {
- Lookup().Set(entityID, component);
-
- if (!entityComponentMap.ContainsKey(entityID))
- {
- entityComponentMap.Add(entityID, new HashSet());
- }
-
- var notFound = entityComponentMap[entityID].Add(typeof(TComponent));
-
- // update filters
- if (notFound)
- {
- if (typeToFilterSignatures.TryGetValue(typeof(TComponent), out var filterSignatures))
+ if (!storages.ContainsKey(typeof(TComponent)))
{
- foreach (var filterSignature in filterSignatures)
+ storages.Add(typeof(TComponent), new ComponentStorage());
+#if DEBUG
+ singleComponentFilters.Add(typeof(TComponent), CreateFilter(new HashSet() { typeof(TComponent) }, new HashSet()));
+#endif
+ }
+ }
+
+ private ComponentStorage Lookup(Type type)
+ {
+ return storages[type];
+ }
+
+ private ComponentStorage Lookup() where TComponent : struct
+ {
+ // TODO: is it possible to optimize this?
+ Register();
+ return storages[typeof(TComponent)] as ComponentStorage;
+ }
+
+ public bool Some() where TComponent : struct
+ {
+ return Lookup().Any();
+ }
+
+ public bool Has(int entityID) where TComponent : struct
+ {
+ return Lookup().Has(entityID);
+ }
+
+ private bool Has(Type type, int entityID)
+ {
+ return Lookup(type).Has(entityID);
+ }
+
+ public ref readonly TComponent Get(int entityID) where TComponent : struct
+ {
+ return ref Lookup().Get(entityID);
+ }
+
+ public ref readonly TComponent Get() where TComponent : struct
+ {
+ return ref Lookup().Get();
+ }
+
+ public void Set(int entityID, in TComponent component) where TComponent : struct
+ {
+ Lookup().Set(entityID, component);
+
+ if (!entityComponentMap.ContainsKey(entityID))
+ {
+ entityComponentMap.Add(entityID, new HashSet());
+ }
+
+ var notFound = entityComponentMap[entityID].Add(typeof(TComponent));
+
+ // update filters
+ if (notFound)
+ {
+ if (typeToFilterSignatures.TryGetValue(typeof(TComponent), out var filterSignatures))
{
- CheckFilter(filterSignature, entityID);
+ foreach (var filterSignature in filterSignatures)
+ {
+ CheckFilter(filterSignature, entityID);
+ }
}
}
}
- }
- public Entity GetSingletonEntity() where TComponent : struct
- {
- return Lookup().FirstEntity();
- }
-
- public ReadOnlySpan ReadComponents() where TComponent : struct
- {
- return Lookup().AllComponents();
- }
-
- private void Remove(Type type, int entityID)
- {
- Lookup(type).Remove(entityID);
-
- var found = entityComponentMap[entityID].Remove(type);
-
- // update filters
- if (found)
+ public Entity GetSingletonEntity() where TComponent : struct
{
- if (typeToFilterSignatures.TryGetValue(type, out var filterSignatures))
+ return Lookup().FirstEntity();
+ }
+
+ public ReadOnlySpan ReadComponents() where TComponent : struct
+ {
+ return Lookup().AllComponents();
+ }
+
+ private void Remove(Type type, int entityID)
+ {
+ Lookup(type).Remove(entityID);
+
+ var found = entityComponentMap[entityID].Remove(type);
+
+ // update filters
+ if (found)
{
- foreach (var filterSignature in filterSignatures)
+ if (typeToFilterSignatures.TryGetValue(type, out var filterSignatures))
{
- CheckFilter(filterSignature, entityID);
+ foreach (var filterSignature in filterSignatures)
+ {
+ CheckFilter(filterSignature, entityID);
+ }
}
}
}
- }
- public void Remove(int entityID) where TComponent : struct
- {
- Lookup().Remove(entityID);
-
- var found = entityComponentMap[entityID].Remove(typeof(TComponent));
-
- // update filters
- if (found)
+ public void Remove(int entityID) where TComponent : struct
{
- if (typeToFilterSignatures.TryGetValue(typeof(TComponent), out var filterSignatures))
+ Lookup().Remove(entityID);
+
+ var found = entityComponentMap[entityID].Remove(typeof(TComponent));
+
+ // update filters
+ if (found)
{
- foreach (var filterSignature in filterSignatures)
+ if (typeToFilterSignatures.TryGetValue(typeof(TComponent), out var filterSignatures))
{
- CheckFilter(filterSignature, entityID);
+ foreach (var filterSignature in filterSignatures)
+ {
+ CheckFilter(filterSignature, entityID);
+ }
}
}
}
- }
- public void OnEntityDestroy(int entityID)
- {
- if (entityComponentMap.ContainsKey(entityID))
+ public void OnEntityDestroy(int entityID)
{
- foreach (var type in entityComponentMap[entityID])
+ if (entityComponentMap.ContainsKey(entityID))
{
- Remove(type, entityID);
- }
-
- entityComponentMap.Remove(entityID);
- }
- }
-
- public Filter CreateFilter(HashSet included, HashSet excluded)
- {
- var filterSignature = new FilterSignature(included, excluded);
- if (!filterSignatureToEntityIDs.ContainsKey(filterSignature))
- {
- filterSignatureToEntityIDs.Add(filterSignature, new IndexableSet());
-
- foreach (var type in included)
- {
- if (!typeToFilterSignatures.ContainsKey(type))
+ foreach (var type in entityComponentMap[entityID])
{
- typeToFilterSignatures.Add(type, new HashSet());
+ Remove(type, entityID);
}
- typeToFilterSignatures[type].Add(filterSignature);
+ entityComponentMap.Remove(entityID);
}
+ }
- foreach (var type in excluded)
+ public Filter CreateFilter(HashSet included, HashSet excluded)
+ {
+ var filterSignature = new FilterSignature(included, excluded);
+ if (!filterSignatureToEntityIDs.ContainsKey(filterSignature))
{
- if (!typeToFilterSignatures.ContainsKey(type))
+ filterSignatureToEntityIDs.Add(filterSignature, new IndexableSet());
+
+ foreach (var type in included)
{
- typeToFilterSignatures.Add(type, new HashSet());
+ if (!typeToFilterSignatures.ContainsKey(type))
+ {
+ typeToFilterSignatures.Add(type, new HashSet());
+ }
+
+ typeToFilterSignatures[type].Add(filterSignature);
}
- typeToFilterSignatures[type].Add(filterSignature);
+ foreach (var type in excluded)
+ {
+ if (!typeToFilterSignatures.ContainsKey(type))
+ {
+ typeToFilterSignatures.Add(type, new HashSet());
+ }
+
+ typeToFilterSignatures[type].Add(filterSignature);
+ }
}
+ return new Filter(this, included, excluded);
}
- return new Filter(this, included, excluded);
- }
- // FIXME: this dictionary should probably just store entities
- public IEnumerable FilterEntities(Filter filter)
- {
- foreach (var id in filterSignatureToEntityIDs[filter.Signature])
+ // FIXME: this dictionary should probably just store entities
+ public IEnumerable FilterEntities(Filter filter)
{
- yield return new Entity(id);
- }
- }
-
- public IEnumerable FilterEntitiesRandom(Filter filter)
- {
- foreach (var index in RandomGenerator.LinearCongruentialGenerator(FilterCount(filter)))
- {
- yield return new Entity(filterSignatureToEntityIDs[filter.Signature][index]);
- }
- }
-
- public Entity FilterRandomEntity(Filter filter)
- {
- var randomIndex = RandomGenerator.Next(FilterCount(filter));
- return new Entity(filterSignatureToEntityIDs[filter.Signature][randomIndex]);
- }
-
- public int FilterCount(Filter filter)
- {
- return filterSignatureToEntityIDs[filter.Signature].Count;
- }
-
- private void CheckFilter(FilterSignature filterSignature, int entityID)
- {
- foreach (var type in filterSignature.Included)
- {
- if (!Has(type, entityID))
+ foreach (var id in filterSignatureToEntityIDs[filter.Signature])
{
- filterSignatureToEntityIDs[filterSignature].Remove(entityID);
- return;
+ yield return new Entity(id);
}
}
- foreach (var type in filterSignature.Excluded)
+ public IEnumerable FilterEntitiesRandom(Filter filter)
{
- if (Has(type, entityID))
+ foreach (var index in RandomGenerator.LinearCongruentialGenerator(FilterCount(filter)))
{
- filterSignatureToEntityIDs[filterSignature].Remove(entityID);
- return;
+ yield return new Entity(filterSignatureToEntityIDs[filter.Signature][index]);
}
}
- filterSignatureToEntityIDs[filterSignature].Add(entityID);
- }
-
- #if DEBUG
- public IEnumerable