Switched to using a new Entity class
While likely less performant, it should provide good readability, and good explaination of how an entity is related in the engine
This commit is contained in:
parent
0b3553d46e
commit
6ada4110e1
@ -24,15 +24,15 @@ class ComponentArray{
|
|||||||
// The object data array
|
// The object data array
|
||||||
private List<Object> componentArray = new ArrayList<>();
|
private List<Object> componentArray = new ArrayList<>();
|
||||||
// The mappings between data and entity
|
// The mappings between data and entity
|
||||||
private Map<Integer, Integer> entityComponentDataMap = new HashMap<>();
|
private Map<Entity, Integer> entityComponentDataMap = new HashMap<>();
|
||||||
private Map<Integer, Integer> componentDataEntityMap = new HashMap<>();
|
private Map<Integer, Entity> componentDataEntityMap = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the data Object associated with the entity.
|
* Gets the data Object associated with the entity.
|
||||||
* @param entity the entity to find data for
|
* @param entity the entity to find data for
|
||||||
* @return the Object if the data exists, else null
|
* @return the Object if the data exists, else null
|
||||||
*/
|
*/
|
||||||
protected Object getData(int entity){
|
protected Object getData(Entity entity){
|
||||||
try{
|
try{
|
||||||
return componentArray.get(entityComponentDataMap.get(entity));
|
return componentArray.get(entityComponentDataMap.get(entity));
|
||||||
}
|
}
|
||||||
@ -52,7 +52,7 @@ class ComponentArray{
|
|||||||
* @param component the component data
|
* @param component the component data
|
||||||
* @return true if successful, false if the entity is already subscribed to the component
|
* @return true if successful, false if the entity is already subscribed to the component
|
||||||
*/
|
*/
|
||||||
protected boolean insertData(int entity, Object component){
|
protected boolean insertData(Entity entity, Object component){
|
||||||
if (entityComponentDataMap.containsKey(entity)){
|
if (entityComponentDataMap.containsKey(entity)){
|
||||||
ECS.writeErr("Entity is already subscribed to the component");
|
ECS.writeErr("Entity is already subscribed to the component");
|
||||||
return false;
|
return false;
|
||||||
@ -71,7 +71,7 @@ class ComponentArray{
|
|||||||
* @param destinationEntity
|
* @param destinationEntity
|
||||||
* @return 0 if successful, -1 if the object is null, -2 if a NullPointerException occurred, and -3 if inserting data failed
|
* @return 0 if successful, -1 if the object is null, -2 if a NullPointerException occurred, and -3 if inserting data failed
|
||||||
*/
|
*/
|
||||||
protected int moveData(int sourceEntity, int destinationEntity){
|
protected int moveData(Entity sourceEntity, Entity destinationEntity){
|
||||||
try{
|
try{
|
||||||
Object data = entityComponentDataMap.get(sourceEntity);
|
Object data = entityComponentDataMap.get(sourceEntity);
|
||||||
if (data == null) return -1;
|
if (data == null) return -1;
|
||||||
@ -92,7 +92,7 @@ class ComponentArray{
|
|||||||
* @param entity the entity to remove the data from
|
* @param entity the entity to remove the data from
|
||||||
* @return true if the data was removed. If the data isn't found then returns false
|
* @return true if the data was removed. If the data isn't found then returns false
|
||||||
*/
|
*/
|
||||||
protected boolean removeData(int entity){
|
protected boolean removeData(Entity entity){
|
||||||
if (!entityComponentDataMap.containsKey(entity)){
|
if (!entityComponentDataMap.containsKey(entity)){
|
||||||
ECS.writeErr("Attempted to remove non-existent entity");
|
ECS.writeErr("Attempted to remove non-existent entity");
|
||||||
return false;
|
return false;
|
||||||
@ -102,7 +102,7 @@ class ComponentArray{
|
|||||||
// Replace the removed component with the last component in the array
|
// Replace the removed component with the last component in the array
|
||||||
componentArray.set(removedComponentDataIndex, componentArray.get(componentArray.size() -1));
|
componentArray.set(removedComponentDataIndex, componentArray.get(componentArray.size() -1));
|
||||||
// update the data positions in the map
|
// update the data positions in the map
|
||||||
int lastEntity = componentDataEntityMap.get(componentArray.size()-1);
|
Entity lastEntity = componentDataEntityMap.get(componentArray.size()-1);
|
||||||
entityComponentDataMap.replace(lastEntity, removedComponentDataIndex);
|
entityComponentDataMap.replace(lastEntity, removedComponentDataIndex);
|
||||||
componentDataEntityMap.replace(removedComponentDataIndex, lastEntity);
|
componentDataEntityMap.replace(removedComponentDataIndex, lastEntity);
|
||||||
// Finally, remomve the last elements
|
// Finally, remomve the last elements
|
||||||
|
@ -24,7 +24,7 @@ class ComponentManager{
|
|||||||
* @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, int entity){
|
protected boolean addComponentToEntity(Type componentType, Object componentData, Entity entity){
|
||||||
return componentArrays.get(componentType).insertData(entity, componentData);
|
return componentArrays.get(componentType).insertData(entity, componentData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ 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(int entity){
|
public void entityDestroyed(Entity entity){
|
||||||
for (Type key : componentArrays.keySet()) {
|
for (Type key : componentArrays.keySet()) {
|
||||||
componentArrays.get(key).removeData(entity);
|
componentArrays.get(key).removeData(entity);
|
||||||
}
|
}
|
||||||
@ -44,7 +44,7 @@ class ComponentManager{
|
|||||||
* @param entity the entity to find data for
|
* @param entity the entity to find data for
|
||||||
* @return the Object data found, or null if it was not found
|
* @return the Object data found, or null if it was not found
|
||||||
*/
|
*/
|
||||||
public Object getComponent(Type componentType, int entity){
|
public Object getComponent(Type componentType, Entity entity){
|
||||||
return componentArrays.get(componentType).getData(entity);
|
return componentArrays.get(componentType).getData(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ class ComponentManager{
|
|||||||
* @param component the component class type to consider
|
* @param component the component class type to consider
|
||||||
* @return true if the component was moved successfully, else false
|
* @return true if the component was moved successfully, else false
|
||||||
*/
|
*/
|
||||||
protected boolean moveComponentData(int sourceEntity, int destinationEntity, Type component){
|
protected boolean moveComponentData(Entity sourceEntity, Entity destinationEntity, Type component){
|
||||||
if (componentArrays.get(component).moveData(sourceEntity, destinationEntity) == 0){
|
if (componentArrays.get(component).moveData(sourceEntity, destinationEntity) == 0){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -96,7 +96,7 @@ class ComponentManager{
|
|||||||
* @param destinationEntity the entity to move data to
|
* @param destinationEntity the entity to move data to
|
||||||
* @param sourceRegistrations the component registrations of the source entity
|
* @param sourceRegistrations the component registrations of the source entity
|
||||||
*/
|
*/
|
||||||
protected void moveAllComponentData(int sourceEntity, int destinationEntity, BitSet sourceRegistrations){
|
protected void moveAllComponentData(Entity sourceEntity, Entity destinationEntity, BitSet sourceRegistrations){
|
||||||
int result = sourceRegistrations.nextSetBit(0);
|
int result = sourceRegistrations.nextSetBit(0);
|
||||||
while (result != -1){
|
while (result != -1){
|
||||||
Type key = indexComponentType.get(result);
|
Type key = indexComponentType.get(result);
|
||||||
@ -126,7 +126,7 @@ class ComponentManager{
|
|||||||
* @param componentType the class type of the component to remove
|
* @param componentType the class type of the component to remove
|
||||||
* @param entity the entity to remove the component from
|
* @param entity the entity to remove the component from
|
||||||
*/
|
*/
|
||||||
public boolean removeComponentFromEntity(Type componentType, int entity){
|
public boolean removeComponentFromEntity(Type componentType, Entity entity){
|
||||||
return componentArrays.get(componentType).removeData(entity);
|
return componentArrays.get(componentType).removeData(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -56,9 +56,9 @@ public class ECS {
|
|||||||
* @return the index of the new entity
|
* @return the index of the new entity
|
||||||
* @throws IndexOutOfBoundsException
|
* @throws IndexOutOfBoundsException
|
||||||
*/
|
*/
|
||||||
Integer createEntity() throws IndexOutOfBoundsException{
|
Entity createEntity() throws IndexOutOfBoundsException{
|
||||||
int newEntity = entityManager.addEntity();
|
Entity newEntity = entityManager.addEntity();
|
||||||
if (newEntity == -1) throw new IndexOutOfBoundsException("Could not create a new entity");
|
if (newEntity == null) throw new IndexOutOfBoundsException("Could not create a new entity");
|
||||||
return newEntity;
|
return newEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ public class ECS {
|
|||||||
* Signals each manager to remove the specified entity
|
* Signals each manager to remove the specified entity
|
||||||
* @param entity the entity to destroy
|
* @param entity the entity to destroy
|
||||||
*/
|
*/
|
||||||
void destroyEntity(int entity){
|
void destroyEntity(Entity entity){
|
||||||
entityManager.removeEntity(entity);
|
entityManager.removeEntity(entity);
|
||||||
componentManager.entityDestroyed(entity);
|
componentManager.entityDestroyed(entity);
|
||||||
systemManager.entityDestroyed(entity);
|
systemManager.entityDestroyed(entity);
|
||||||
@ -99,7 +99,7 @@ public class ECS {
|
|||||||
* @param componentName the class name of the component to add
|
* @param componentName the class name of the component to add
|
||||||
* @param component the actual component data
|
* @param component the actual component data
|
||||||
*/
|
*/
|
||||||
boolean addComponent(int entity, Type componentName, Object component){
|
boolean addComponent(Entity entity, Type componentName, Object component){
|
||||||
if (entityManager.registerComponent(componentManager.getComponentIndex(componentName), entity))
|
if (entityManager.registerComponent(componentManager.getComponentIndex(componentName), entity))
|
||||||
{
|
{
|
||||||
systemManager.entityRegistrationsChanged(entity, entityManager.getRegistrations(entity));
|
systemManager.entityRegistrationsChanged(entity, entityManager.getRegistrations(entity));
|
||||||
@ -114,7 +114,7 @@ public class ECS {
|
|||||||
* @param entity the entity to remove the component from
|
* @param entity the entity to remove the component from
|
||||||
* @param componentName the class name of the component
|
* @param componentName the class name of the component
|
||||||
*/
|
*/
|
||||||
boolean removeComponent(int entity, Type componentType){
|
boolean removeComponent(Entity entity, Type componentType){
|
||||||
if (entityManager.unregisterComponent(componentManager.getComponentIndex(componentType), entity))
|
if (entityManager.unregisterComponent(componentManager.getComponentIndex(componentType), entity))
|
||||||
{
|
{
|
||||||
componentManager.removeComponentFromEntity(componentType, entity);
|
componentManager.removeComponentFromEntity(componentType, entity);
|
||||||
@ -131,7 +131,7 @@ public class ECS {
|
|||||||
* @param componentType the class type of the component
|
* @param componentType the class type of the component
|
||||||
* @return the component data Object associated with the entity
|
* @return the component data Object associated with the entity
|
||||||
*/
|
*/
|
||||||
Object getComponentData(int entity, Type componentType){
|
Object getComponentData(Entity entity, Type componentType){
|
||||||
return componentManager.getComponent(componentType, entity);
|
return componentManager.getComponent(componentType, entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ import java.util.Set;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
abstract class ECSSystem{
|
abstract class ECSSystem{
|
||||||
Set<Integer> entities = new HashSet<>();
|
Set<Entity> entities = new HashSet<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implement additional parameterised init() functions as required.
|
* Implement additional parameterised init() functions as required.
|
||||||
|
38
javaecs/src/main/java/nz/ac/massey/javaecs/Entity.java
Normal file
38
javaecs/src/main/java/nz/ac/massey/javaecs/Entity.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package nz.ac.massey.javaecs;
|
||||||
|
/**
|
||||||
|
* Entity class.
|
||||||
|
* Whilst an entity is just an Integer, and using Integer values
|
||||||
|
* would be more performant (no function call), it is encapsulated to help
|
||||||
|
* distinguish between entities and Integers.
|
||||||
|
*/
|
||||||
|
public class Entity {
|
||||||
|
private int value;
|
||||||
|
|
||||||
|
public Entity(int value){
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the int value wrapped as an entity.
|
||||||
|
* Used to provide a distinction between creating a new entity, and
|
||||||
|
* using an int value we assume is a valid entity.
|
||||||
|
*
|
||||||
|
* Functionally, this is no different to creating a new entity.
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Entity asEntity(int value){
|
||||||
|
return new Entity(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj)
|
||||||
|
{
|
||||||
|
return (this.getValue() == ((Entity)obj).getValue());
|
||||||
|
}
|
||||||
|
}
|
@ -21,7 +21,7 @@ import java.util.ArrayList;
|
|||||||
* I.e. Controls adding and removing entities, and registration and deregistration of components.
|
* I.e. Controls adding and removing entities, and registration and deregistration of components.
|
||||||
*/
|
*/
|
||||||
class EntityManager{
|
class EntityManager{
|
||||||
private Queue<Integer> unusedEntities;
|
private Queue<Entity> unusedEntities;
|
||||||
private List<BitSet> entityRegistrations;
|
private List<BitSet> entityRegistrations;
|
||||||
private int maxSize = 1024;
|
private int maxSize = 1024;
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ class EntityManager{
|
|||||||
entityRegistrations = new ArrayList<>();
|
entityRegistrations = new ArrayList<>();
|
||||||
|
|
||||||
for (int i = 0; i < maxSize; i++) {
|
for (int i = 0; i < maxSize; i++) {
|
||||||
unusedEntities.add(i);
|
unusedEntities.add(new Entity(i));
|
||||||
entityRegistrations.add(null); // Leave bitsets out as if they are null the entity isn't initialised properly
|
entityRegistrations.add(null); // Leave bitsets out as if they are null the entity isn't initialised properly
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,7 +48,7 @@ class EntityManager{
|
|||||||
entityRegistrations = new ArrayList<>();
|
entityRegistrations = new ArrayList<>();
|
||||||
|
|
||||||
for (int i = 0; i < maxEntities; i++) {
|
for (int i = 0; i < maxEntities; i++) {
|
||||||
unusedEntities.add(i);
|
unusedEntities.add(new Entity(i));
|
||||||
entityRegistrations.add(null);
|
entityRegistrations.add(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -57,13 +57,13 @@ class EntityManager{
|
|||||||
* Creates a new entity
|
* Creates a new entity
|
||||||
* @return the index of the new entity, or -1 if there is no more available entities
|
* @return the index of the new entity, or -1 if there is no more available entities
|
||||||
*/
|
*/
|
||||||
protected Integer addEntity(){
|
protected Entity addEntity(){
|
||||||
if (unusedEntities.size() == 0){
|
if (unusedEntities.size() == 0){
|
||||||
ECS.writeErr("No available space to create a new entity");
|
ECS.writeErr("No available space to create a new entity");
|
||||||
return -1;
|
return null;
|
||||||
}
|
}
|
||||||
int result = unusedEntities.remove();
|
Entity result = unusedEntities.remove();
|
||||||
entityRegistrations.set(result, new BitSet());
|
entityRegistrations.set(result.getValue(), new BitSet());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,12 +80,12 @@ class EntityManager{
|
|||||||
* @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
|
||||||
*/
|
*/
|
||||||
protected BitSet getRegistrations(int entity){
|
protected BitSet getRegistrations(Entity entity){
|
||||||
try{
|
try{
|
||||||
return entityRegistrations.get(entity);
|
return entityRegistrations.get(entity.getValue());
|
||||||
}
|
}
|
||||||
catch (IndexOutOfBoundsException e){
|
catch (IndexOutOfBoundsException e){
|
||||||
ECS.writeErr("Index out of bounds error getting registrations for " + entity + ";\nThe entity might not exist");
|
ECS.writeErr("Index out of bounds error getting registrations for " + entity.getValue() + ";\nThe 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,18 +96,18 @@ class EntityManager{
|
|||||||
* @param entity the entity to register
|
* @param entity the entity to register
|
||||||
* @return true if the operation was successful
|
* @return true if the operation was successful
|
||||||
*/
|
*/
|
||||||
protected boolean registerComponent(int component, int entity){
|
protected boolean registerComponent(int component, Entity entity){
|
||||||
if (entity >= maxSize){
|
if (entity.getValue() >= maxSize){
|
||||||
ECS.writeErr("Attempted to assign a component to non-existent entity: " + entity);
|
ECS.writeErr("Attempted to assign a component to non-existent entity: " + entity.getValue());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else if (entityRegistrations.get(entity).get(component))
|
else if (entityRegistrations.get(entity.getValue()).get(component))
|
||||||
{
|
{
|
||||||
ECS.writeErr("Entity is already assigned to the component");
|
ECS.writeErr("Entity is already assigned to the component");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
entityRegistrations.get(entity).set(component);
|
entityRegistrations.get(entity.getValue()).set(component);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -118,9 +118,9 @@ class EntityManager{
|
|||||||
* <b>Does not handle associated data</b> Use the method in ECS to remove entities cleanly
|
* <b>Does not handle associated data</b> Use the method in ECS to remove entities cleanly
|
||||||
* @param entity the entity to remove
|
* @param entity the entity to remove
|
||||||
*/
|
*/
|
||||||
protected void removeEntity(int entity){
|
protected void removeEntity(Entity entity){
|
||||||
unusedEntities.add(entity);
|
unusedEntities.add(entity);
|
||||||
entityRegistrations.set(entity, null);
|
entityRegistrations.set(entity.getValue(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -128,8 +128,8 @@ class EntityManager{
|
|||||||
* @param entity the entity to set
|
* @param entity the entity to set
|
||||||
* @param registrations the preset registrations
|
* @param registrations the preset registrations
|
||||||
*/
|
*/
|
||||||
protected void setRegistrations(int entity, BitSet registrations){
|
protected void setRegistrations(Entity entity, BitSet registrations){
|
||||||
entityRegistrations.set(entity, registrations);
|
entityRegistrations.set(entity.getValue(), registrations);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -140,9 +140,9 @@ class EntityManager{
|
|||||||
* @param entity the entity to remove
|
* @param entity the entity to remove
|
||||||
* @return true if successful
|
* @return true if successful
|
||||||
*/
|
*/
|
||||||
protected boolean unregisterComponent(int component, int entity){
|
protected boolean unregisterComponent(int component, Entity entity){
|
||||||
try{
|
try{
|
||||||
entityRegistrations.get(entity).clear(component);
|
entityRegistrations.get(entity.getValue()).clear(component);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (NullPointerException e){
|
catch (NullPointerException e){
|
||||||
@ -175,16 +175,17 @@ class EntityManager{
|
|||||||
// Consistency should be maintained.
|
// Consistency should be maintained.
|
||||||
// This is computationally expensive; we must re-order every assigned entity above newSize, if the newSize is smaller
|
// This is computationally expensive; we must re-order every assigned entity above newSize, if the newSize is smaller
|
||||||
if (newSize < maxSize){
|
if (newSize < maxSize){
|
||||||
List<Integer> outOfBounds = new ArrayList<>();
|
List<Entity> outOfBounds = new ArrayList<>();
|
||||||
for (int i = newSize; i < maxSize; i++) {
|
for (int i = newSize; i < maxSize; i++) {
|
||||||
if (!unusedEntities.remove(i)){
|
Entity entityI = Entity.asEntity(i);
|
||||||
|
if (!unusedEntities.remove(entityI)){
|
||||||
// Could not remove element, as it didn't exist (already assigned).
|
// Could not remove element, as it didn't exist (already assigned).
|
||||||
// must find it and reassign it a new in-bounds value
|
// must find it and reassign it a new in-bounds value
|
||||||
outOfBounds.add(i);
|
outOfBounds.add(entityI);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Integer integer : outOfBounds) {
|
for (Entity integer : outOfBounds) {
|
||||||
int newPos = addEntity();
|
Entity newPos = addEntity();
|
||||||
setRegistrations(newPos, getRegistrations(integer));
|
setRegistrations(newPos, getRegistrations(integer));
|
||||||
// Invoke an update in the SystemManager
|
// Invoke an update in the SystemManager
|
||||||
systemManager.entityDestroyed(integer);
|
systemManager.entityDestroyed(integer);
|
||||||
@ -200,7 +201,7 @@ class EntityManager{
|
|||||||
else{
|
else{
|
||||||
// Init unassigned values
|
// Init unassigned values
|
||||||
for (int i = maxSize; i < newSize; i++) {
|
for (int i = maxSize; i < newSize; i++) {
|
||||||
unusedEntities.add(i);
|
unusedEntities.add(new Entity(i));
|
||||||
entityRegistrations.add(new BitSet());
|
entityRegistrations.add(new BitSet());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ class SystemManager{
|
|||||||
* Removes the entity from each system's tracked entities
|
* Removes the entity from each system's tracked entities
|
||||||
* @param entity the destroyed entity
|
* @param entity the destroyed entity
|
||||||
*/
|
*/
|
||||||
protected void entityDestroyed(int entity){
|
protected void entityDestroyed(Entity entity){
|
||||||
for (Type key : systems.keySet()) {
|
for (Type key : systems.keySet()) {
|
||||||
systems.get(key).entities.remove(entity);
|
systems.get(key).entities.remove(entity);
|
||||||
}
|
}
|
||||||
@ -35,7 +35,7 @@ class SystemManager{
|
|||||||
* @param entity the entity that was modified
|
* @param entity the entity that was modified
|
||||||
* @param entityRegistrations the new registrations of the entity
|
* @param entityRegistrations the new registrations of the entity
|
||||||
*/
|
*/
|
||||||
protected void entityRegistrationsChanged(int entity, BitSet entityRegistrations){
|
protected void entityRegistrationsChanged(Entity entity, BitSet entityRegistrations){
|
||||||
for (Type key : systems.keySet()) {
|
for (Type key : systems.keySet()) {
|
||||||
// Check if the signature is null
|
// Check if the signature is null
|
||||||
if (!entityRegistrations.equals(null)){
|
if (!entityRegistrations.equals(null)){
|
||||||
|
@ -33,7 +33,7 @@ class AppTest {
|
|||||||
class TestSystem extends ECSSystem{
|
class TestSystem extends ECSSystem{
|
||||||
public void init(){}
|
public void init(){}
|
||||||
public void update() {
|
public void update() {
|
||||||
for (Integer entity : entities) {
|
for (Entity entity : entities) {
|
||||||
TestObject entityComponent = (TestObject)gameEngine.getComponentData(entity, TestObject.class);
|
TestObject entityComponent = (TestObject)gameEngine.getComponentData(entity, TestObject.class);
|
||||||
entityComponent.i++;
|
entityComponent.i++;
|
||||||
}
|
}
|
||||||
@ -44,7 +44,7 @@ class AppTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testAssignOne() {
|
void testAssignOne() {
|
||||||
int entity = gameEngine.createEntity();
|
Entity entity = gameEngine.createEntity();
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
||||||
}
|
}
|
||||||
@ -55,7 +55,7 @@ class AppTest {
|
|||||||
@Test
|
@Test
|
||||||
void testAssignMax(){
|
void testAssignMax(){
|
||||||
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
||||||
int entity = gameEngine.createEntity();
|
Entity entity = gameEngine.createEntity();
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
||||||
}
|
}
|
||||||
@ -72,7 +72,7 @@ class AppTest {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
int entity = gameEngine.createEntity();
|
Entity entity = gameEngine.createEntity();
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ class AppTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testRemoveOne(){
|
void testRemoveOne(){
|
||||||
int entity = gameEngine.createEntity();
|
Entity entity = gameEngine.createEntity();
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
gameEngine.destroyEntity(entity);
|
gameEngine.destroyEntity(entity);
|
||||||
assertNull(((TestObject)gameEngine.getComponentData(entity, TestObject.class)));
|
assertNull(((TestObject)gameEngine.getComponentData(entity, TestObject.class)));
|
||||||
@ -94,7 +94,7 @@ class AppTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testRunSystem(){
|
void testRunSystem(){
|
||||||
int entity = gameEngine.createEntity();
|
Entity entity = gameEngine.createEntity();
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
|
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
@ -110,7 +110,7 @@ class AppTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
void testResizeMinimal(){
|
void testResizeMinimal(){
|
||||||
int entity = gameEngine.createEntity();
|
Entity entity = gameEngine.createEntity();
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
|
|
||||||
gameEngine.resizeMaximum(10);
|
gameEngine.resizeMaximum(10);
|
||||||
@ -124,12 +124,12 @@ class AppTest {
|
|||||||
@Test
|
@Test
|
||||||
void testLargeCreateDelete(){
|
void testLargeCreateDelete(){
|
||||||
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
||||||
int entity = gameEngine.createEntity();
|
Entity entity = gameEngine.createEntity();
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
||||||
}
|
}
|
||||||
for (int i = 256; i < 512; i++) {
|
for (int i = 256; i < 512; i++) {
|
||||||
gameEngine.destroyEntity(i);
|
gameEngine.destroyEntity(Entity.asEntity(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,12 +139,12 @@ class AppTest {
|
|||||||
@Test
|
@Test
|
||||||
void testLargeCreateDeleteResize(){
|
void testLargeCreateDeleteResize(){
|
||||||
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
||||||
int entity = gameEngine.createEntity();
|
Entity entity = gameEngine.createEntity();
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
||||||
}
|
}
|
||||||
for (int i = 256; i < 512; i++) {
|
for (int i = 256; i < 512; i++) {
|
||||||
gameEngine.destroyEntity(i);
|
gameEngine.destroyEntity(Entity.asEntity(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
gameEngine.resizeMaximum(1024 - 255);
|
gameEngine.resizeMaximum(1024 - 255);
|
||||||
@ -156,12 +156,12 @@ class AppTest {
|
|||||||
@Test
|
@Test
|
||||||
void testLargeCreateDeleteResizeTooSmall(){
|
void testLargeCreateDeleteResizeTooSmall(){
|
||||||
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
||||||
int entity = gameEngine.createEntity();
|
Entity entity = gameEngine.createEntity();
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
||||||
}
|
}
|
||||||
for (int i = 256; i < 512; i++) {
|
for (int i = 256; i < 512; i++) {
|
||||||
gameEngine.destroyEntity(i);
|
gameEngine.destroyEntity(Entity.asEntity(i));
|
||||||
}
|
}
|
||||||
assertFalse(gameEngine.resizeMaximum(512));
|
assertFalse(gameEngine.resizeMaximum(512));
|
||||||
|
|
||||||
@ -175,12 +175,12 @@ class AppTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAssignToNonExistentEntity(){
|
void testAssignToNonExistentEntity(){
|
||||||
assertFalse(gameEngine.addComponent(1025, TestObject.class, new TestObject()));
|
assertFalse(gameEngine.addComponent(Entity.asEntity(1025), TestObject.class, new TestObject()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRemoveComponent(){
|
void testRemoveComponent(){
|
||||||
int entity = gameEngine.createEntity();
|
Entity entity = gameEngine.createEntity();
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
gameEngine.removeComponent(entity, TestObject.class);
|
gameEngine.removeComponent(entity, TestObject.class);
|
||||||
assertFalse(gameEngine.entityManager.getRegistrations(entity).get(gameEngine.getComponentIndex(TestObject.class)));
|
assertFalse(gameEngine.entityManager.getRegistrations(entity).get(gameEngine.getComponentIndex(TestObject.class)));
|
||||||
@ -214,12 +214,12 @@ class AppTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testRemoveNonExistentComponentData(){
|
void testRemoveNonExistentComponentData(){
|
||||||
assertFalse(gameEngine.removeComponent(1, TestObject.class));
|
assertFalse(gameEngine.removeComponent(Entity.asEntity(1), TestObject.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAssignComponentAlreadyAssigned(){
|
void testAssignComponentAlreadyAssigned(){
|
||||||
int entity = gameEngine.createEntity();
|
Entity entity = gameEngine.createEntity();
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||||
assertFalse(gameEngine.addComponent(entity, TestObject.class, new TestObject()));
|
assertFalse(gameEngine.addComponent(entity, TestObject.class, new TestObject()));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user