Changed component data handling

Adding empty (null) component data defaults to false
Also added getNumEntities() function
Added a test for getRegistrationsOutOfRange
This commit is contained in:
Brychan Dempsey 2021-06-10 15:28:15 +12:00
parent 0571986059
commit 86ef1e30e3
4 changed files with 23 additions and 1 deletions

View File

@ -17,14 +17,22 @@ class ComponentManager{
private Map<Type, ComponentArray> componentArrays = new HashMap<>(); private Map<Type, ComponentArray> componentArrays = new HashMap<>();
private Map<Type, Integer> componentPosIndex = new HashMap<>(); private Map<Type, Integer> componentPosIndex = new HashMap<>();
private Map<Integer, Type> indexComponentType = new HashMap<>(); private Map<Integer, Type> indexComponentType = new HashMap<>();
private BitSet ignoreEmpty = new BitSet(); // Ignore empty values when deleting data of the index type.
/** /**
* Adds the specified component to the provided entity * Adds the specified component to the provided entity.
* <p>
* Providing a null Object defaults the value to boolean false
* @param componentType the class type of the component to add * @param componentType the class type of the component to add
* @param componentData the component data to associate * @param componentData the component data to associate
* @param entity the entity to associate data to * @param entity the entity to associate data to
*/ */
protected boolean addComponentToEntity(Type componentType, Object componentData, Entity entity){ protected boolean addComponentToEntity(Type componentType, Object componentData, Entity entity){
if (componentData == null){
// In the case of null component data, insert the boolean false
// (preserves structure by associating data with a null component)
return componentArrays.get(componentType).insertData(entity, false);
}
return componentArrays.get(componentType).insertData(entity, componentData); return componentArrays.get(componentType).insertData(entity, componentData);
} }

View File

@ -208,4 +208,8 @@ public class Engine {
public SystemManager getSystemManager() { public SystemManager getSystemManager() {
return systemManager; return systemManager;
} }
public int getNumEntities(){
return entityManager.getNumEntities();
}
} }

View File

@ -210,4 +210,8 @@ class EntityManager{
return true; return true;
} }
} }
protected int getNumEntities(){
return maxSize - unusedEntities.size();
}
} }

View File

@ -229,6 +229,12 @@ class AppTest {
assertFalse(gameEngine.registerSystem(TestSystem.class, new TestSystem())); assertFalse(gameEngine.registerSystem(TestSystem.class, new TestSystem()));
} }
@Test
void testGetRegistrationsOutOfRange(){
Entity entity = Entity.asEntity(25);
assertEquals(new BitSet(), gameEngine.entityManager.getRegistrations(entity));
}
/** /**
* Establish a simple ECS, with a single system and component * Establish a simple ECS, with a single system and component
*/ */