Refactored ECS to Engine
This commit is contained in:
parent
d021b7815b
commit
d86c07352a
@ -60,6 +60,18 @@
|
|||||||
</executions>
|
</executions>
|
||||||
|
|
||||||
</plugin>
|
</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>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-enforcer-plugin</artifactId>
|
<artifactId>maven-enforcer-plugin</artifactId>
|
||||||
|
@ -37,11 +37,11 @@ class ComponentArray{
|
|||||||
return componentArray.get(entityComponentDataMap.get(entity));
|
return componentArray.get(entityComponentDataMap.get(entity));
|
||||||
}
|
}
|
||||||
catch (NullPointerException ex){
|
catch (NullPointerException ex){
|
||||||
ECS.writeErr("Attempted to retrieve non-existent data");
|
Engine.writeErr("Attempted to retrieve non-existent data");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
catch (IndexOutOfBoundsException e){
|
catch (IndexOutOfBoundsException e){
|
||||||
ECS.writeErr("Index out-of-bounds");
|
Engine.writeErr("Index out-of-bounds");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ class ComponentArray{
|
|||||||
*/
|
*/
|
||||||
protected boolean insertData(Entity 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");
|
Engine.writeErr("Entity is already subscribed to the component");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Put data at the end of the componentArray
|
// Put data at the end of the componentArray
|
||||||
@ -94,7 +94,7 @@ class ComponentArray{
|
|||||||
*/
|
*/
|
||||||
protected boolean removeData(Entity entity){
|
protected boolean removeData(Entity entity){
|
||||||
if (!entityComponentDataMap.containsKey(entity)){
|
if (!entityComponentDataMap.containsKey(entity)){
|
||||||
ECS.writeErr("Attempted to remove non-existent entity");
|
Engine.writeErr("Attempted to remove non-existent entity");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Get the componentData index of the entity
|
// Get the componentData index of the entity
|
||||||
|
@ -112,7 +112,7 @@ class ComponentManager{
|
|||||||
*/
|
*/
|
||||||
protected boolean registerComponent(Type type){
|
protected boolean registerComponent(Type type){
|
||||||
if (componentArrays.containsKey(type)){
|
if (componentArrays.containsKey(type)){
|
||||||
ECS.writeErr("Component " + type.getTypeName() + " is already registered");
|
Engine.writeErr("Component " + type.getTypeName() + " is already registered");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
componentArrays.put(type, new ComponentArray());
|
componentArrays.put(type, new ComponentArray());
|
||||||
|
@ -21,7 +21,7 @@ import java.util.BitSet;
|
|||||||
* See https://git.software.kauripeak.co.nz/BrychanD/JavaECS
|
* See https://git.software.kauripeak.co.nz/BrychanD/JavaECS
|
||||||
* for documentation and more information.
|
* for documentation and more information.
|
||||||
*/
|
*/
|
||||||
public class ECS {
|
public class Engine {
|
||||||
// All internal functions write error messages to errorStream; which defaults to System.err
|
// 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
|
// Can be set to a different PrintStream, to allow errors to be logged elsewhere
|
||||||
protected static PrintStream errorStream = System.err;
|
protected static PrintStream errorStream = System.err;
|
||||||
@ -34,7 +34,7 @@ public class ECS {
|
|||||||
* <p>
|
* <p>
|
||||||
* Maximum 1024 enitites default
|
* Maximum 1024 enitites default
|
||||||
*/
|
*/
|
||||||
public ECS(){
|
public Engine(){
|
||||||
entityManager = new EntityManager();
|
entityManager = new EntityManager();
|
||||||
componentManager = new ComponentManager();
|
componentManager = new ComponentManager();
|
||||||
systemManager = new SystemManager();
|
systemManager = new SystemManager();
|
||||||
@ -44,7 +44,7 @@ public class ECS {
|
|||||||
* Initialises the ECS with the specified value
|
* Initialises the ECS with the specified value
|
||||||
* @param maxEntities the maximum number of entities to allow
|
* @param maxEntities the maximum number of entities to allow
|
||||||
*/
|
*/
|
||||||
public ECS(int maxEntities){
|
public Engine(int maxEntities){
|
||||||
entityManager = new EntityManager(maxEntities);
|
entityManager = new EntityManager(maxEntities);
|
||||||
componentManager = new ComponentManager();
|
componentManager = new ComponentManager();
|
||||||
systemManager = new SystemManager();
|
systemManager = new SystemManager();
|
||||||
@ -54,7 +54,7 @@ public class ECS {
|
|||||||
* /**
|
* /**
|
||||||
* Creates a new entity
|
* Creates a new entity
|
||||||
* @return the index of the 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{
|
public Entity createEntity() throws IndexOutOfBoundsException{
|
||||||
Entity newEntity = entityManager.addEntity();
|
Entity newEntity = entityManager.addEntity();
|
||||||
@ -83,7 +83,8 @@ public class ECS {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers the specified name in the component manager
|
* 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){
|
public boolean registerComponent(Type type){
|
||||||
return componentManager.registerComponent(type);
|
return componentManager.registerComponent(type);
|
||||||
@ -98,6 +99,7 @@ public class ECS {
|
|||||||
* @param entity the entity to add the component to
|
* @param entity the entity to add the component to
|
||||||
* @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
|
||||||
|
* @return true if the compnent was added to the entity
|
||||||
*/
|
*/
|
||||||
public boolean addComponent(Entity entity, Type componentName, Object component){
|
public boolean addComponent(Entity entity, Type componentName, Object component){
|
||||||
if (entityManager.registerComponent(componentManager.getComponentIndex(componentName), entity))
|
if (entityManager.registerComponent(componentManager.getComponentIndex(componentName), entity))
|
||||||
@ -112,7 +114,8 @@ public class ECS {
|
|||||||
/**
|
/**
|
||||||
* Removes the component from the specified entity
|
* Removes the component from the specified entity
|
||||||
* @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 componentType the class type of the component
|
||||||
|
* @return true if the component was removed
|
||||||
*/
|
*/
|
||||||
public boolean removeComponent(Entity entity, Type componentType){
|
public boolean removeComponent(Entity entity, Type componentType){
|
||||||
if (entityManager.unregisterComponent(componentManager.getComponentIndex(componentType), entity))
|
if (entityManager.unregisterComponent(componentManager.getComponentIndex(componentType), entity))
|
||||||
@ -136,8 +139,8 @@ public class ECS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Registers the system to
|
||||||
* @param systemName
|
* @param systemType
|
||||||
* @param action
|
* @param action
|
||||||
*/
|
*/
|
||||||
public boolean registerSystem(Type systemType, ECSSystem action){
|
public boolean registerSystem(Type systemType, ECSSystem action){
|
@ -59,7 +59,7 @@ class EntityManager{
|
|||||||
*/
|
*/
|
||||||
protected Entity addEntity(){
|
protected Entity addEntity(){
|
||||||
if (unusedEntities.size() == 0){
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
Entity result = unusedEntities.remove();
|
Entity result = unusedEntities.remove();
|
||||||
@ -85,7 +85,7 @@ class EntityManager{
|
|||||||
return entityRegistrations.get(entity.getValue());
|
return entityRegistrations.get(entity.getValue());
|
||||||
}
|
}
|
||||||
catch (IndexOutOfBoundsException e){
|
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)
|
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){
|
protected boolean registerComponent(int component, Entity entity){
|
||||||
if (entity.getValue() >= maxSize){
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
else if (entityRegistrations.get(entity.getValue()).get(component))
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@ -146,7 +146,7 @@ class EntityManager{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (NullPointerException e){
|
catch (NullPointerException e){
|
||||||
ECS.writeErr("Entity not initialised");
|
Engine.writeErr("Entity not initialised");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
catch (IndexOutOfBoundsException e)
|
catch (IndexOutOfBoundsException e)
|
||||||
@ -164,11 +164,11 @@ class EntityManager{
|
|||||||
*/
|
*/
|
||||||
protected boolean resize(int newSize, SystemManager systemManager, ComponentManager componentManager){
|
protected boolean resize(int newSize, SystemManager systemManager, ComponentManager componentManager){
|
||||||
if (newSize < maxSize - unusedEntities.size()){
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
else if (newSize == maxSize){
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
@ -59,7 +59,7 @@ class SystemManager{
|
|||||||
*/
|
*/
|
||||||
protected boolean registerSystem(Type systemType, ECSSystem system){
|
protected boolean registerSystem(Type systemType, ECSSystem system){
|
||||||
if (systems.containsKey(systemType)){
|
if (systems.containsKey(systemType)){
|
||||||
ECS.writeErr("System already registered");
|
Engine.writeErr("System already registered");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
systems.put(systemType, system);
|
systems.put(systemType, system);
|
||||||
|
@ -19,7 +19,7 @@ import org.junit.jupiter.api.BeforeEach;
|
|||||||
*/
|
*/
|
||||||
class AppTest {
|
class AppTest {
|
||||||
|
|
||||||
ECS gameEngine;
|
Engine gameEngine;
|
||||||
TestSystem system;
|
TestSystem system;
|
||||||
|
|
||||||
class TestObject{
|
class TestObject{
|
||||||
@ -169,7 +169,7 @@ class AppTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testEntityManagerConstructor(){
|
void testEntityManagerConstructor(){
|
||||||
gameEngine = new ECS(5);
|
gameEngine = new Engine(5);
|
||||||
assertEquals(5, gameEngine.getMaxEntities());
|
assertEquals(5, gameEngine.getMaxEntities());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,15 +189,15 @@ class AppTest {
|
|||||||
@Test
|
@Test
|
||||||
void testEqualResize(){
|
void testEqualResize(){
|
||||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||||
PrintStream orig = ECS.getErr();
|
PrintStream orig = Engine.getErr();
|
||||||
PrintStream newErr = new PrintStream(bytes);
|
PrintStream newErr = new PrintStream(bytes);
|
||||||
ECS.setErr(newErr);
|
Engine.setErr(newErr);
|
||||||
assertTrue(gameEngine.resizeMaximum(1024));
|
assertTrue(gameEngine.resizeMaximum(1024));
|
||||||
ECS.setErr(orig);
|
Engine.setErr(orig);
|
||||||
newErr.flush(); // ensure the bytes are recieved
|
newErr.flush(); // ensure the bytes are recieved
|
||||||
byte[] errBytes = bytes.toByteArray();
|
byte[] errBytes = bytes.toByteArray();
|
||||||
String result = new String(errBytes);
|
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"));
|
assertTrue(result.trim().equals("Attempted to set the newSize to the current size"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,7 +234,7 @@ class AppTest {
|
|||||||
*/
|
*/
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void testSetup(){
|
void testSetup(){
|
||||||
gameEngine = new ECS();
|
gameEngine = new Engine();
|
||||||
gameEngine.registerComponent(TestObject.class);
|
gameEngine.registerComponent(TestObject.class);
|
||||||
|
|
||||||
system = new TestSystem();
|
system = new TestSystem();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user