accidentally destroyed every pooled dictionary when an entity was destroyed LOOOL

pull/5/head
Evan Hemsley 2019-08-01 23:09:41 -07:00
parent fa27ed850a
commit dbe6cc4f53
7 changed files with 65 additions and 41 deletions

View File

@ -69,6 +69,7 @@ namespace Encompass
return componentID;
}
internal IEnumerable<Guid> GetComponentIDsByEntityID(Guid entityID)
{
HashSet<Guid> idSet;
@ -201,10 +202,7 @@ namespace Encompass
{
entityIDToComponentIDs.Remove(entityID);
foreach (var pooledDictionary in entityIDToComponentTypeToComponentIDs.Values)
{
pooledDictionary.Dispose();
}
entityIDToComponentTypeToComponentIDs[entityID].Dispose();
entityIDToComponentTypeToComponentIDs.Remove(entityID);
}
}

View File

@ -17,6 +17,25 @@ namespace Encompass
private readonly Dictionary<Entity, PooledDictionary<Type, HashSet<Guid>>> entityToTypeToPendingComponentIDs = new Dictionary<Entity, PooledDictionary<Type, HashSet<Guid>>>();
private readonly Dictionary<Entity, PooledDictionary<Type, HashSet<Guid>>> entityToTypeToComponentIDs = new Dictionary<Entity, PooledDictionary<Type, HashSet<Guid>>>();
internal void RegisterEntity(Entity entity)
{
entityToTypeToComponentIDs[entity] = new PooledDictionary<Type, HashSet<Guid>>();
entityToTypeToPendingComponentIDs[entity] = new PooledDictionary<Type, HashSet<Guid>>();
entityToTypeToExistingComponentIDs[entity] = new PooledDictionary<Type, HashSet<Guid>>();
}
internal void RegisterDestroyedEntity(Entity entity)
{
entityToTypeToComponentIDs[entity].Dispose();
entityToTypeToComponentIDs.Remove(entity);
entityToTypeToPendingComponentIDs[entity].Dispose();
entityToTypeToPendingComponentIDs.Remove(entity);
entityToTypeToExistingComponentIDs[entity].Dispose();
entityToTypeToExistingComponentIDs.Remove(entity);
}
internal void ClearMessages()
{
componentIDToComponent.Clear();
@ -61,27 +80,6 @@ namespace Encompass
}
}
internal void RegisterDestroyedEntity(Entity entity)
{
foreach (var pooledDictionary in entityToTypeToComponentIDs.Values)
{
pooledDictionary.Dispose();
}
entityToTypeToComponentIDs.Remove(entity);
foreach (var pooledDictionary in entityToTypeToPendingComponentIDs.Values)
{
pooledDictionary.Dispose();
}
entityToTypeToPendingComponentIDs.Remove(entity);
foreach (var pooledDictionary in entityToTypeToExistingComponentIDs.Values)
{
pooledDictionary.Dispose();
}
entityToTypeToExistingComponentIDs.Remove(entity);
}
internal void AddExistingComponentMessage<TComponent>(ComponentMessage<TComponent> componentMessage) where TComponent : struct, IComponent
{
RegisterExistingOrPendingComponentMessage(componentMessage.entity, componentMessage.componentID, componentMessage.component);
@ -93,10 +91,6 @@ namespace Encompass
componentMessageTypeToExistingComponentIDs[typeof(TComponent)].Add(componentMessage.componentID);
if (!entityToTypeToExistingComponentIDs.ContainsKey(componentMessage.entity))
{
entityToTypeToExistingComponentIDs.Add(componentMessage.entity, new PooledDictionary<Type, HashSet<Guid>>());
}
if (!entityToTypeToExistingComponentIDs[componentMessage.entity].ContainsKey(typeof(TComponent)))
{
entityToTypeToExistingComponentIDs[componentMessage.entity].Add(typeof(TComponent), new HashSet<Guid>());
@ -116,10 +110,6 @@ namespace Encompass
componentMessageTypeToPendingComponentIDs[typeof(TComponent)].Add(pendingComponentMessage.componentID);
if (!entityToTypeToPendingComponentIDs.ContainsKey(pendingComponentMessage.entity))
{
entityToTypeToPendingComponentIDs.Add(pendingComponentMessage.entity, new PooledDictionary<Type, HashSet<Guid>>());
}
if (!entityToTypeToPendingComponentIDs[pendingComponentMessage.entity].ContainsKey(typeof(TComponent)))
{
entityToTypeToPendingComponentIDs[pendingComponentMessage.entity].Add(typeof(TComponent), new HashSet<Guid>());
@ -138,10 +128,6 @@ namespace Encompass
}
componentMessageTypeToComponentIDs[typeof(TComponent)].Add(componentID);
if (!entityToTypeToComponentIDs.ContainsKey(entity))
{
entityToTypeToComponentIDs.Add(entity, new PooledDictionary<Type, HashSet<Guid>>());
}
if (!entityToTypeToComponentIDs[entity].ContainsKey(typeof(TComponent)))
{
entityToTypeToComponentIDs[entity].Add(typeof(TComponent), new HashSet<Guid>());

View File

@ -78,6 +78,7 @@ namespace Encompass
var layer = componentIDToLayerIndex[id];
layerIndexToComponentIDs[layer].Remove(id);
}
componentIDToLayerIndex.Remove(id);
}
public void AdjustComponentLayer(Guid id, int layer)

View File

@ -140,7 +140,7 @@ namespace Encompass
{
var componentID = componentManager.NextID();
componentManager.AddDrawComponent(entity, componentID, component);
componentManager.AddDrawComponent(entity, componentID, component, layer);
if (sendTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
{

View File

@ -24,6 +24,7 @@ namespace Encompass
var entity = new Entity(id);
IDToEntity[id] = entity;
componentManager.RegisterEntity(id);
componentMessageManager.RegisterEntity(entity);
return entity;
}

View File

@ -438,14 +438,14 @@ namespace Tests
}
}
static IEnumerable<ValueTuple<Guid, MockComponent>> results;
static List<(Guid, MockComponent)> results;
[Reads(typeof(MockComponent))]
class ReaderEngine : Engine
{
public override void Update(double dt)
{
results = ReadComponents<MockComponent>();
results = ReadComponents<MockComponent>().ToList();
}
}
@ -458,6 +458,7 @@ namespace Tests
var entity = worldBuilder.CreateEntity();
var entityB = worldBuilder.CreateEntity();
var entityC = worldBuilder.CreateEntity();
DestroyerComponent destroyerComponent;
MockComponent mockComponent;
@ -470,12 +471,16 @@ namespace Tests
worldBuilder.AddComponent(entityB, destroyerComponent);
var componentBID = worldBuilder.AddComponent(entityB, mockComponent);
var componentCID = worldBuilder.AddComponent(entityC, mockComponent);
var world = worldBuilder.Build();
world.Update(0.01f);
world.Update(0.01);
world.Update(0.01);
Assert.That(results, Does.Not.Contain((componentID, mockComponent)));
Assert.That(results, Does.Not.Contain((componentBID, mockComponent)));
Assert.That(results, Does.Contain((componentCID, mockComponent)));
}
[Reads(typeof(DestroyerComponent), typeof(MockComponent))]

View File

@ -64,5 +64,38 @@ namespace Tests
Assert.IsTrue(calledOnDraw);
resultComponent.Should().BeEquivalentTo((testDrawComponentID, testDrawComponent));
}
[Reads(typeof(TestDrawComponent))]
class DestroyerEngine : Engine
{
public override void Update(double dt)
{
foreach (var (componentID, component) in ReadComponents<TestDrawComponent>())
{
Destroy(GetEntityIDByComponentID(componentID));
}
}
}
[Test]
public void RenderMethodNotCalledAfterDestroy()
{
calledOnDraw = false;
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new DestroyerEngine());
var renderer = worldBuilder.AddOrderedRenderer(new CalledRenderer());
TestDrawComponent testDrawComponent;
var entity = worldBuilder.CreateEntity();
var testDrawComponentID = worldBuilder.AddDrawComponent(entity, testDrawComponent, 1);
var world = worldBuilder.Build();
world.Update(0.01);
world.Draw();
Assert.IsFalse(calledOnDraw);
}
}
}