From 0c12f8d38bbf323d82cda9cc5f784e393938bd7c Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Sat, 27 Mar 2021 13:42:35 +1300 Subject: [PATCH] Added create and destroy functions --- .../ECS.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/javaecs/src/main/java/nz/ac/massey/programming_project_159333_s1_2021/ECS.java b/javaecs/src/main/java/nz/ac/massey/programming_project_159333_s1_2021/ECS.java index 88a9f5e..fe275d9 100644 --- a/javaecs/src/main/java/nz/ac/massey/programming_project_159333_s1_2021/ECS.java +++ b/javaecs/src/main/java/nz/ac/massey/programming_project_159333_s1_2021/ECS.java @@ -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 components = new ArrayList<>(); + List> 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); + } } } \ No newline at end of file