world transfer determinism

pull/5/head
cosmonaut 2023-10-10 11:59:28 -07:00
parent 1ef141422c
commit a2a81bf477
4 changed files with 36 additions and 16 deletions

View File

@ -13,12 +13,9 @@ namespace MoonTools.ECS
{ {
} }
protected IEnumerable<dynamic> Debug_GetAllComponents(Entity entity) protected ComponentEnumerator Debug_GetAllComponents(Entity entity)
{ {
foreach (var typeIndex in EntityStorage.ComponentTypeIndices(entity.ID)) return new ComponentEnumerator(ComponentDepot, entity, EntityStorage.ComponentTypeIndices(entity.ID));
{
yield return ComponentDepot.Debug_Get(entity.ID, typeIndex);
}
} }
protected IEnumerable<Entity> Debug_GetEntities(Type componentType) protected IEnumerable<Entity> Debug_GetEntities(Type componentType)
@ -39,6 +36,28 @@ namespace MoonTools.ECS
} }
} }
} }
public ref struct ComponentEnumerator
{
private ComponentDepot ComponentDepot;
private Entity Entity;
private ReverseSpanEnumerator<int> ComponentTypeIndices;
public ComponentEnumerator GetEnumerator() => this;
internal ComponentEnumerator(
ComponentDepot componentDepot,
Entity entity,
Collections.IndexableSet<int> componentTypeIndices
) {
ComponentDepot = componentDepot;
Entity = entity;
ComponentTypeIndices = componentTypeIndices.GetEnumerator();
}
public bool MoveNext() => ComponentTypeIndices.MoveNext();
public object Current => ComponentDepot.Debug_Get(Entity.ID, ComponentTypeIndices.Current);
}
} }
} }
#endif #endif

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using MoonTools.ECS.Collections;
namespace MoonTools.ECS namespace MoonTools.ECS
{ {
@ -10,8 +11,8 @@ namespace MoonTools.ECS
// FIXME: this is only needed in debug mode // FIXME: this is only needed in debug mode
private readonly HashSet<int> availableIDHash = new HashSet<int>(); private readonly HashSet<int> availableIDHash = new HashSet<int>();
private Dictionary<int, HashSet<int>> EntityToComponentTypeIndices = new Dictionary<int, HashSet<int>>(); private Dictionary<int, IndexableSet<int>> EntityToComponentTypeIndices = new Dictionary<int, IndexableSet<int>>();
private Dictionary<int, HashSet<int>> EntityToRelationTypeIndices = new Dictionary<int, HashSet<int>>(); private Dictionary<int, IndexableSet<int>> EntityToRelationTypeIndices = new Dictionary<int, IndexableSet<int>>();
public int Count => nextID - availableIDs.Count; public int Count => nextID - availableIDs.Count;
@ -23,12 +24,12 @@ namespace MoonTools.ECS
if (!EntityToComponentTypeIndices.ContainsKey(entity.ID)) if (!EntityToComponentTypeIndices.ContainsKey(entity.ID))
{ {
EntityToComponentTypeIndices.Add(entity.ID, new HashSet<int>()); EntityToComponentTypeIndices.Add(entity.ID, new IndexableSet<int>());
} }
if (!EntityToRelationTypeIndices.ContainsKey(entity.ID)) if (!EntityToRelationTypeIndices.ContainsKey(entity.ID))
{ {
EntityToRelationTypeIndices.Add(entity.ID, new HashSet<int>()); EntityToRelationTypeIndices.Add(entity.ID, new IndexableSet<int>());
} }
Tags[entity.ID] = tag; Tags[entity.ID] = tag;
@ -86,12 +87,12 @@ namespace MoonTools.ECS
return Tags[entityID]; return Tags[entityID];
} }
public HashSet<int> ComponentTypeIndices(int entityID) public IndexableSet<int> ComponentTypeIndices(int entityID)
{ {
return EntityToComponentTypeIndices[entityID]; return EntityToComponentTypeIndices[entityID];
} }
public HashSet<int> RelationTypeIndices(int entityID) public IndexableSet<int> RelationTypeIndices(int entityID)
{ {
return EntityToRelationTypeIndices[entityID]; return EntityToRelationTypeIndices[entityID];
} }

View File

@ -22,8 +22,6 @@ namespace MoonTools.ECS
public bool Equals(FilterSignature other) public bool Equals(FilterSignature other)
{ {
// workaround for HashSet<T>.SetEquals generating garbage
// maybe fixed in .NET 8?
foreach (var included in Included) foreach (var included in Included)
{ {
if (!other.Included.Contains(included)) if (!other.Included.Contains(included))

View File

@ -148,12 +148,14 @@ namespace MoonTools.ECS
} }
} }
filterSignatureToEntityIDs[filterSignature].Add(entityID); if (filterSignatureToEntityIDs[filterSignature].Add(entityID))
{
if (addCallbacks.TryGetValue(filterSignature, out var addCallback)) if (addCallbacks.TryGetValue(filterSignature, out var addCallback))
{ {
addCallback(entityID); addCallback(entityID);
} }
} }
}
public void RemoveEntity(int entityID, int componentTypeIndex) public void RemoveEntity(int entityID, int componentTypeIndex)
{ {