Refactored ECS to Engine

This commit is contained in:
Brychan Dempsey 2021-06-09 12:25:22 +12:00
parent d021b7815b
commit d86c07352a
7 changed files with 44 additions and 29 deletions

View File

@ -60,6 +60,18 @@
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>

View File

@ -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

View File

@ -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());

View File

@ -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 {
* <p>
* 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;
}

View File

@ -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{

View File

@ -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);

View File

@ -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();