Added create and destroy functions

This commit is contained in:
Brychan Dempsey 2021-03-27 13:42:35 +13:00
parent 85c3470569
commit 0c12f8d38b

View File

@ -13,6 +13,7 @@ public class ECS {
// Components are either primitive types or class/struct definitions, with an additional list associating
// the entities the component is assigned to
List<Object> components = new ArrayList<>();
List<List<Object>> componentAssociations = new ArrayList<>();
// Systems are user-defined functions that are called by the game engine, usually at a regular interval such
// as between every render frame. It must be able to find all instances of a specific component, in order
// to update/read its data. A component
@ -23,8 +24,35 @@ public class ECS {
int componentIndex = 0;
int systemIndex = 0;
/**
* Takes the next element off the list of unassigned entities
* @return the value of the new entity
*/
Integer createEntity(){
int newEntity = entities.remove(0);
if (componentAssociations.size() <= newEntity){
for (int i = componentAssociations.size(); i < newEntity+1 - componentAssociations.size(); i++) {
componentAssociations.add(i, new ArrayList<>());
}
}
componentAssociations.add(newEntity, element);
return newEntity;
}
/**
* Adds the entity back to the list of unassigned entities,
* and empty all component associations
* @param entity
*/
void destroyEntity(int entity){
entities.add(entity);
componentAssociations.set(entity, new ArrayList<>());
}
void init(int maxEntities){
for (int i = 0; i < maxEntities; i++) {
entities.add(i);
}
}
}