Worked on tests, fixed issues with entities

Implemented hashValue() for the entity so equality can be compared correctly.
I.e. hashValue is the actual value of the entity, as they *should* be unique
This commit is contained in:
Brychan Dempsey 2021-06-10 15:57:58 +12:00
parent 86ef1e30e3
commit edd8a3faa7
7 changed files with 36 additions and 15 deletions

View File

@ -37,11 +37,11 @@ class ComponentArray{
return componentArray.get(entityComponentDataMap.get(entity)); return componentArray.get(entityComponentDataMap.get(entity));
} }
catch (NullPointerException ex){ catch (NullPointerException ex){
Engine.writeErr("Attempted to retrieve non-existent data"); Engine.writeErr("Attempted to retrieve non-existent data for unassigned entity: " + entity.getValue());
return null; return null;
} }
catch (IndexOutOfBoundsException e){ catch (IndexOutOfBoundsException e){
Engine.writeErr("Index out-of-bounds"); Engine.writeErr("Index out-of-bounds for entity: " + entity.getValue());
return null; return null;
} }
} }
@ -94,7 +94,7 @@ class ComponentArray{
*/ */
protected boolean removeData(Entity entity){ protected boolean removeData(Entity entity){
if (!entityComponentDataMap.containsKey(entity)){ if (!entityComponentDataMap.containsKey(entity)){
Engine.writeErr("Attempted to remove non-existent entity"); Engine.writeErr("Attempted to remove non-existent entity: " + entity.getValue());
return false; return false;
} }
// Get the componentData index of the entity // Get the componentData index of the entity

View File

@ -17,7 +17,6 @@ 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.

View File

@ -97,18 +97,21 @@ public class Engine {
/** /**
* Adds an exisiting component to an exisiting entity * Adds an exisiting component to an exisiting entity
* @param entity the entity to add the component to * @param entity the entity to add the component to
* @param componentName the class name of the component to add * @param componentType the class name of the component to add
* @param component the actual component data * @param component the actual component data
* @return true if the compnent was added to the entity * @return true if the compnent was added to the entity
*/ */
public boolean addComponent(Entity entity, Type componentName, Object component){ public boolean addComponent(Entity entity, Type componentType, Object component){
if (entityManager.registerComponent(componentManager.getComponentIndex(componentName), entity)) if (entityManager.registerComponent(componentManager.getComponentIndex(componentType), entity))
{ {
systemManager.entityRegistrationsChanged(entity, entityManager.getRegistrations(entity)); systemManager.entityRegistrationsChanged(entity, entityManager.getRegistrations(entity));
componentManager.addComponentToEntity(componentName, component, entity); componentManager.addComponentToEntity(componentType, component, entity);
return true; return true;
} }
else return false; else {
writeErr("(" + componentType.getTypeName() + ")");
return false;
}
} }
/** /**

View File

@ -35,4 +35,9 @@ public class Entity {
{ {
return (this.getValue() == ((Entity)obj).getValue()); return (this.getValue() == ((Entity)obj).getValue());
} }
@Override
public int hashCode(){
return value;
}
} }

View File

@ -76,16 +76,23 @@ class EntityManager{
} }
/** /**
* Gets the BitSet containing the registrations of the entity * Gets the BitSet containing the registrations of the entity.
* @param entity the entity whose BitSet to retrieve * @param entity the entity whose BitSet to retrieve
* @return the BitSet of the provided entity * @return the BitSet of the provided entity, or a new, empty BitSet if the result was null or out of bounds
*/ */
protected BitSet getRegistrations(Entity entity){ protected BitSet getRegistrations(Entity entity){
try{ try{
return entityRegistrations.get(entity.getValue()); BitSet registrations = entityRegistrations.get(entity.getValue());
if (registrations != null){
return registrations;
}
else{
Engine.writeErr("Registrations not initialised for entity: " + entity.getValue() + "; The entity does not exist");
return new BitSet();
}
} }
catch (IndexOutOfBoundsException e){ catch (IndexOutOfBoundsException e){
Engine.writeErr("Index out of bounds error getting registrations for " + entity.getValue() + ";\nThe entity might not exist"); Engine.writeErr("Index out of bounds error getting registrations for " + entity.getValue() + "; The entity might not exist");
return new BitSet(); // Using a blank BitSet will retain data safety (that is, no data will be modified) return new BitSet(); // Using a blank BitSet will retain data safety (that is, no data will be modified)
} }
} }
@ -103,7 +110,7 @@ class EntityManager{
} }
else if (entityRegistrations.get(entity.getValue()).get(component)) else if (entityRegistrations.get(entity.getValue()).get(component))
{ {
Engine.writeErr("Entity is already assigned to the component"); Engine.writeErr("Entity: " + entity.getValue() + " is already assigned to component " + component);
return false; return false;
} }
else{ else{

View File

@ -59,7 +59,7 @@ class SystemManager{
*/ */
protected boolean registerSystem(Type systemType, ECSSystem system){ protected boolean registerSystem(Type systemType, ECSSystem system){
if (systems.containsKey(systemType)){ if (systems.containsKey(systemType)){
Engine.writeErr("System already registered"); Engine.writeErr("System \'" + systemType.getTypeName() + "\' already registered");
return false; return false;
} }
systems.put(systemType, system); systems.put(systemType, system);

View File

@ -131,6 +131,7 @@ class AppTest {
for (int i = 256; i < 512; i++) { for (int i = 256; i < 512; i++) {
gameEngine.destroyEntity(Entity.asEntity(i)); gameEngine.destroyEntity(Entity.asEntity(i));
} }
int k = 0;
} }
/** /**
@ -231,6 +232,12 @@ class AppTest {
@Test @Test
void testGetRegistrationsOutOfRange(){ void testGetRegistrationsOutOfRange(){
Entity entity = Entity.asEntity(-1);
assertEquals(new BitSet(), gameEngine.entityManager.getRegistrations(entity));
}
@Test
void testGetRegistrationsUnassignedEntity(){
Entity entity = Entity.asEntity(25); Entity entity = Entity.asEntity(25);
assertEquals(new BitSet(), gameEngine.entityManager.getRegistrations(entity)); assertEquals(new BitSet(), gameEngine.entityManager.getRegistrations(entity));
} }