fix crash when components are added to an entity that will be destroyed

pull/5/head
thatcosmonaut 2019-08-19 18:11:15 -07:00
parent 3e7bba1e4c
commit 0447610b23
2 changed files with 13 additions and 10 deletions

View File

@ -44,16 +44,6 @@ namespace Encompass
var id = NextID();
componentAddData.Add((entity, typeof(TComponent), id, component));
// add these here so entity and component lookups dont break on pending components
IDToComponent[id] = component;
componentIDToEntityID[id] = entity.ID;
componentIDToType[id] = typeof(TComponent);
if (!typeToComponentIDs.ContainsKey(typeof(TComponent)))
{
typeToComponentIDs.Add(typeof(TComponent), new HashSet<Guid>());
}
typeToComponentIDs[typeof(TComponent)].Add(id);
entityIDToComponentIDs[entity.ID].Add(id);
return id;
}
@ -66,9 +56,20 @@ namespace Encompass
internal void AddComponent(Entity entity, Type type, Guid componentID, IComponent component)
{
if (!entityIDToComponentTypeToComponentID.ContainsKey(entity.ID)) { return; }
if (!entityIDToComponentTypeToComponentID[entity.ID].ContainsKey(type))
{
IDToComponent[componentID] = component;
componentIDToEntityID[componentID] = entity.ID;
componentIDToType[componentID] = type;
entityIDToComponentTypeToComponentID[entity.ID][type] = componentID;
if (!typeToComponentIDs.ContainsKey(type))
{
typeToComponentIDs.Add(type, new HashSet<Guid>());
}
typeToComponentIDs[type].Add(componentID);
entityIDToComponentIDs[entity.ID].Add(componentID);
}
else
{

View File

@ -112,6 +112,8 @@ namespace Encompass
return (TComponent)componentMessageManager.GetComponentByID(componentID);
}
// these next two are for the ComponentMessageEmitter only
internal IEnumerable<(Guid, TComponent)> ReadComponentsFromWorld<TComponent>() where TComponent : struct, IComponent
{
return componentManager.GetComponentsByType<TComponent>();