From d86c07352a169676b77b3903b66726df8dbcc116 Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Wed, 9 Jun 2021 12:25:22 +1200 Subject: [PATCH] Refactored ECS to Engine --- javaecs/pom.xml | 12 +++++++++++ .../nz/ac/massey/javaecs/ComponentArray.java | 8 +++---- .../ac/massey/javaecs/ComponentManager.java | 2 +- .../massey/javaecs/{ECS.java => Engine.java} | 21 +++++++++++-------- .../nz/ac/massey/javaecs/EntityManager.java | 14 ++++++------- .../nz/ac/massey/javaecs/SystemManager.java | 2 +- .../java/nz/ac/massey/javaecs/AppTest.java | 14 ++++++------- 7 files changed, 44 insertions(+), 29 deletions(-) rename javaecs/src/main/java/nz/ac/massey/javaecs/{ECS.java => Engine.java} (92%) diff --git a/javaecs/pom.xml b/javaecs/pom.xml index a39c26a..1f1f28b 100644 --- a/javaecs/pom.xml +++ b/javaecs/pom.xml @@ -60,6 +60,18 @@ + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + + + + org.apache.maven.plugins maven-enforcer-plugin diff --git a/javaecs/src/main/java/nz/ac/massey/javaecs/ComponentArray.java b/javaecs/src/main/java/nz/ac/massey/javaecs/ComponentArray.java index 876f1f2..e000ec6 100644 --- a/javaecs/src/main/java/nz/ac/massey/javaecs/ComponentArray.java +++ b/javaecs/src/main/java/nz/ac/massey/javaecs/ComponentArray.java @@ -37,11 +37,11 @@ class ComponentArray{ return componentArray.get(entityComponentDataMap.get(entity)); } catch (NullPointerException ex){ - ECS.writeErr("Attempted to retrieve non-existent data"); + Engine.writeErr("Attempted to retrieve non-existent data"); return null; } catch (IndexOutOfBoundsException e){ - ECS.writeErr("Index out-of-bounds"); + Engine.writeErr("Index out-of-bounds"); return null; } } @@ -54,7 +54,7 @@ class ComponentArray{ */ protected boolean insertData(Entity entity, Object component){ if (entityComponentDataMap.containsKey(entity)){ - ECS.writeErr("Entity is already subscribed to the component"); + Engine.writeErr("Entity is already subscribed to the component"); return false; } // Put data at the end of the componentArray @@ -94,7 +94,7 @@ class ComponentArray{ */ protected boolean removeData(Entity entity){ if (!entityComponentDataMap.containsKey(entity)){ - ECS.writeErr("Attempted to remove non-existent entity"); + Engine.writeErr("Attempted to remove non-existent entity"); return false; } // Get the componentData index of the entity diff --git a/javaecs/src/main/java/nz/ac/massey/javaecs/ComponentManager.java b/javaecs/src/main/java/nz/ac/massey/javaecs/ComponentManager.java index 0689694..e472173 100644 --- a/javaecs/src/main/java/nz/ac/massey/javaecs/ComponentManager.java +++ b/javaecs/src/main/java/nz/ac/massey/javaecs/ComponentManager.java @@ -112,7 +112,7 @@ class ComponentManager{ */ protected boolean registerComponent(Type type){ if (componentArrays.containsKey(type)){ - ECS.writeErr("Component " + type.getTypeName() + " is already registered"); + Engine.writeErr("Component " + type.getTypeName() + " is already registered"); return false; } componentArrays.put(type, new ComponentArray()); diff --git a/javaecs/src/main/java/nz/ac/massey/javaecs/ECS.java b/javaecs/src/main/java/nz/ac/massey/javaecs/Engine.java similarity index 92% rename from javaecs/src/main/java/nz/ac/massey/javaecs/ECS.java rename to javaecs/src/main/java/nz/ac/massey/javaecs/Engine.java index 13ae3fd..941ad56 100644 --- a/javaecs/src/main/java/nz/ac/massey/javaecs/ECS.java +++ b/javaecs/src/main/java/nz/ac/massey/javaecs/Engine.java @@ -21,7 +21,7 @@ import java.util.BitSet; * See https://git.software.kauripeak.co.nz/BrychanD/JavaECS * for documentation and more information. */ -public class ECS { +public class Engine { // All internal functions write error messages to errorStream; which defaults to System.err // Can be set to a different PrintStream, to allow errors to be logged elsewhere protected static PrintStream errorStream = System.err; @@ -34,7 +34,7 @@ public class ECS { *

* Maximum 1024 enitites default */ - public ECS(){ + public Engine(){ entityManager = new EntityManager(); componentManager = new ComponentManager(); systemManager = new SystemManager(); @@ -44,7 +44,7 @@ public class ECS { * Initialises the ECS with the specified value * @param maxEntities the maximum number of entities to allow */ - public ECS(int maxEntities){ + public Engine(int maxEntities){ entityManager = new EntityManager(maxEntities); componentManager = new ComponentManager(); systemManager = new SystemManager(); @@ -54,7 +54,7 @@ public class ECS { * /** * Creates a new entity * @return the index of the new entity - * @throws IndexOutOfBoundsException + * @throws IndexOutOfBoundsException if there are no more entities available */ public Entity createEntity() throws IndexOutOfBoundsException{ Entity newEntity = entityManager.addEntity(); @@ -83,7 +83,8 @@ public class ECS { /** * Registers the specified name in the component manager - * @param name the name to register. Should be the component class name or a suitable name for primitive types + * @param type the name to register. Should be the component class name or a suitable name for primitive types + * @return true if the component type was registered successfully */ public boolean registerComponent(Type type){ return componentManager.registerComponent(type); @@ -98,6 +99,7 @@ public class ECS { * @param entity the entity to add the component to * @param componentName the class name of the component to add * @param component the actual component data + * @return true if the compnent was added to the entity */ public boolean addComponent(Entity entity, Type componentName, Object component){ if (entityManager.registerComponent(componentManager.getComponentIndex(componentName), entity)) @@ -112,7 +114,8 @@ public class ECS { /** * Removes the component from the specified entity * @param entity the entity to remove the component from - * @param componentName the class name of the component + * @param componentType the class type of the component + * @return true if the component was removed */ public boolean removeComponent(Entity entity, Type componentType){ if (entityManager.unregisterComponent(componentManager.getComponentIndex(componentType), entity)) @@ -136,8 +139,8 @@ public class ECS { } /** - * - * @param systemName + * Registers the system to + * @param systemType * @param action */ public boolean registerSystem(Type systemType, ECSSystem action){ @@ -197,7 +200,7 @@ public class ECS { public ComponentManager getComponentManager() { return componentManager; } - + public EntityManager getEntityManager() { return entityManager; } diff --git a/javaecs/src/main/java/nz/ac/massey/javaecs/EntityManager.java b/javaecs/src/main/java/nz/ac/massey/javaecs/EntityManager.java index 1140d4c..93cfd95 100644 --- a/javaecs/src/main/java/nz/ac/massey/javaecs/EntityManager.java +++ b/javaecs/src/main/java/nz/ac/massey/javaecs/EntityManager.java @@ -59,7 +59,7 @@ class EntityManager{ */ protected Entity addEntity(){ if (unusedEntities.size() == 0){ - ECS.writeErr("No available space to create a new entity"); + Engine.writeErr("No available space to create a new entity"); return null; } Entity result = unusedEntities.remove(); @@ -85,7 +85,7 @@ class EntityManager{ return entityRegistrations.get(entity.getValue()); } catch (IndexOutOfBoundsException e){ - ECS.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() + ";\nThe entity might not exist"); return new BitSet(); // Using a blank BitSet will retain data safety (that is, no data will be modified) } } @@ -98,12 +98,12 @@ class EntityManager{ */ protected boolean registerComponent(int component, Entity entity){ if (entity.getValue() >= maxSize){ - ECS.writeErr("Attempted to assign a component to non-existent entity: " + entity.getValue()); + Engine.writeErr("Attempted to assign a component to non-existent entity: " + entity.getValue()); return false; } else if (entityRegistrations.get(entity.getValue()).get(component)) { - ECS.writeErr("Entity is already assigned to the component"); + Engine.writeErr("Entity is already assigned to the component"); return false; } else{ @@ -146,7 +146,7 @@ class EntityManager{ return true; } catch (NullPointerException e){ - ECS.writeErr("Entity not initialised"); + Engine.writeErr("Entity not initialised"); return false; } catch (IndexOutOfBoundsException e) @@ -164,11 +164,11 @@ class EntityManager{ */ protected boolean resize(int newSize, SystemManager systemManager, ComponentManager componentManager){ if (newSize < maxSize - unusedEntities.size()){ - ECS.writeErr("Attempted to resize the maximum entity count to a number smaller than the current assigned entity count."); + Engine.writeErr("Attempted to resize the maximum entity count to a number smaller than the current assigned entity count."); return false; } else if (newSize == maxSize){ - ECS.writeErr("Attempted to set the newSize to the current size"); + Engine.writeErr("Attempted to set the newSize to the current size"); return true; } else{ diff --git a/javaecs/src/main/java/nz/ac/massey/javaecs/SystemManager.java b/javaecs/src/main/java/nz/ac/massey/javaecs/SystemManager.java index 9edd3ce..0b00a35 100644 --- a/javaecs/src/main/java/nz/ac/massey/javaecs/SystemManager.java +++ b/javaecs/src/main/java/nz/ac/massey/javaecs/SystemManager.java @@ -59,7 +59,7 @@ class SystemManager{ */ protected boolean registerSystem(Type systemType, ECSSystem system){ if (systems.containsKey(systemType)){ - ECS.writeErr("System already registered"); + Engine.writeErr("System already registered"); return false; } systems.put(systemType, system); diff --git a/javaecs/src/test/java/nz/ac/massey/javaecs/AppTest.java b/javaecs/src/test/java/nz/ac/massey/javaecs/AppTest.java index 2028840..53ec351 100644 --- a/javaecs/src/test/java/nz/ac/massey/javaecs/AppTest.java +++ b/javaecs/src/test/java/nz/ac/massey/javaecs/AppTest.java @@ -19,7 +19,7 @@ import org.junit.jupiter.api.BeforeEach; */ class AppTest { - ECS gameEngine; + Engine gameEngine; TestSystem system; class TestObject{ @@ -169,7 +169,7 @@ class AppTest { @Test void testEntityManagerConstructor(){ - gameEngine = new ECS(5); + gameEngine = new Engine(5); assertEquals(5, gameEngine.getMaxEntities()); } @@ -189,15 +189,15 @@ class AppTest { @Test void testEqualResize(){ ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - PrintStream orig = ECS.getErr(); + PrintStream orig = Engine.getErr(); PrintStream newErr = new PrintStream(bytes); - ECS.setErr(newErr); + Engine.setErr(newErr); assertTrue(gameEngine.resizeMaximum(1024)); - ECS.setErr(orig); + Engine.setErr(orig); newErr.flush(); // ensure the bytes are recieved byte[] errBytes = bytes.toByteArray(); String result = new String(errBytes); - ECS.writeErr("Captured in redirect: " + result); + Engine.writeErr("Captured in redirect: " + result); assertTrue(result.trim().equals("Attempted to set the newSize to the current size")); } @@ -234,7 +234,7 @@ class AppTest { */ @BeforeEach void testSetup(){ - gameEngine = new ECS(); + gameEngine = new Engine(); gameEngine.registerComponent(TestObject.class); system = new TestSystem();