Improved entity destruction handling

This commit is contained in:
Brychan Dempsey 2021-06-10 22:45:12 +12:00
parent edd8a3faa7
commit 4f4e1bec7b
4 changed files with 22 additions and 5 deletions

View File

@ -39,10 +39,20 @@ class ComponentManager{
* Signals to the ComponentManager the entity was destroyed. All component data references should be removed. * Signals to the ComponentManager the entity was destroyed. All component data references should be removed.
* @param entity the entity that was destroyed. * @param entity the entity that was destroyed.
*/ */
public void entityDestroyed(Entity entity){ public void entityDestroyed(Entity entity, BitSet entityRegistrations){
for (Type key : componentArrays.keySet()) { // HashMap lookups take time, use the known bitstates
componentArrays.get(key).removeData(entity); int index = entityRegistrations.nextSetBit(0);
while(index != -1){
if (!componentArrays.get(indexComponentType.get(index)).removeData(entity)){
Engine.writeErr(indexComponentType.get(index).getTypeName());
} }
index = entityRegistrations.nextSetBit(index+1);
}
/*for (Type key : componentArrays.keySet()) {
if (!componentArrays.get(key).removeData(entity)){
Engine.writeErr(key.getTypeName());
}
}*/
} }
/** /**

View File

@ -76,8 +76,8 @@ public class Engine {
* @param entity the entity to destroy * @param entity the entity to destroy
*/ */
public void destroyEntity(Entity entity){ public void destroyEntity(Entity entity){
componentManager.entityDestroyed(entity, entityManager.getRegistrations(entity));
entityManager.removeEntity(entity); entityManager.removeEntity(entity);
componentManager.entityDestroyed(entity);
systemManager.entityDestroyed(entity); systemManager.entityDestroyed(entity);
} }

View File

@ -33,8 +33,14 @@ public class Entity {
@Override @Override
public boolean equals(Object obj) public boolean equals(Object obj)
{ {
if (obj instanceof Entity){
return (this.getValue() == ((Entity)obj).getValue()); return (this.getValue() == ((Entity)obj).getValue());
} }
else if (obj instanceof Integer){
return (this.value == (Integer)obj);
}
else return false;
}
@Override @Override
public int hashCode(){ public int hashCode(){

View File

@ -24,6 +24,7 @@ class SystemManager{
* @param entity the destroyed entity * @param entity the destroyed entity
*/ */
protected void entityDestroyed(Entity entity){ protected void entityDestroyed(Entity entity){
// Unlike components, this isn't simply indexed; makes more sense just to search the systems
for (Type key : systems.keySet()) { for (Type key : systems.keySet()) {
systems.get(key).entities.remove(entity); systems.get(key).entities.remove(entity);
} }