Compare commits
No commits in common. "6e000492b003373a2fb8d6df5bd7208739784180" and "b1efb9802d0d19e52e7941947f7bab9f2923b992" have entirely different histories.
6e000492b0
...
b1efb9802d
@ -2,7 +2,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>nz.ac.massey.javaecs</groupId>
|
<groupId>nz.ac.massey.javaecs</groupId>
|
||||||
<artifactId>javaecs</artifactId>
|
<artifactId>javaecs</artifactId>
|
||||||
<version>1.2-PRERELEASE</version>
|
<version>1.1-SNAPSHOT</version>
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
@ -17,10 +17,10 @@
|
|||||||
<maven-javadoc-plugin.version>3.0.0</maven-javadoc-plugin.version>
|
<maven-javadoc-plugin.version>3.0.0</maven-javadoc-plugin.version>
|
||||||
<coveralls-maven-plugin.version>4.3.0</coveralls-maven-plugin.version>
|
<coveralls-maven-plugin.version>4.3.0</coveralls-maven-plugin.version>
|
||||||
<!-- JaCoCo thresholds. Increase gradually as you add tests. -->
|
<!-- JaCoCo thresholds. Increase gradually as you add tests. -->
|
||||||
<jacoco.unit-tests.limit.instruction-ratio>75%</jacoco.unit-tests.limit.instruction-ratio>
|
<jacoco.unit-tests.limit.instruction-ratio>0%</jacoco.unit-tests.limit.instruction-ratio>
|
||||||
<jacoco.unit-tests.limit.branch-ratio>50%</jacoco.unit-tests.limit.branch-ratio>
|
<jacoco.unit-tests.limit.branch-ratio>0%</jacoco.unit-tests.limit.branch-ratio>
|
||||||
<jacoco.unit-tests.limit.class-complexity>30</jacoco.unit-tests.limit.class-complexity>
|
<jacoco.unit-tests.limit.class-complexity>20</jacoco.unit-tests.limit.class-complexity>
|
||||||
<jacoco.unit-tests.limit.method-complexity>10</jacoco.unit-tests.limit.method-complexity>
|
<jacoco.unit-tests.limit.method-complexity>5</jacoco.unit-tests.limit.method-complexity>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -134,7 +134,7 @@
|
|||||||
<goal>report</goal>
|
<goal>report</goal>
|
||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>
|
||||||
<execution>
|
<!-- <execution>
|
||||||
<id>check-unit-test</id>
|
<id>check-unit-test</id>
|
||||||
<phase>test</phase>
|
<phase>test</phase>
|
||||||
<goals>
|
<goals>
|
||||||
@ -180,7 +180,7 @@
|
|||||||
</rule>
|
</rule>
|
||||||
</rules>
|
</rules>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution> -->
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
@ -34,7 +34,7 @@ class ComponentArray{
|
|||||||
|
|
||||||
public void removeData(int entity){
|
public void removeData(int entity){
|
||||||
if (!entityComponentDataMap.containsKey(entity)){
|
if (!entityComponentDataMap.containsKey(entity)){
|
||||||
ECS.writeErr("Attempted to remove non-existent entity");
|
System.err.println("Attempted to remove non-existent entity");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Get the componentData index of the entity
|
// Get the componentData index of the entity
|
||||||
@ -55,7 +55,7 @@ class ComponentArray{
|
|||||||
|
|
||||||
public void insertData(int entity, Object component){
|
public void insertData(int entity, Object component){
|
||||||
if (entityComponentDataMap.containsKey(entity)){
|
if (entityComponentDataMap.containsKey(entity)){
|
||||||
ECS.writeErr("Entity is already subscribed to the component");
|
System.err.println("Entity is already subscribed to the component");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int index = componentArray.size();
|
int index = componentArray.size();
|
||||||
@ -78,7 +78,7 @@ class ComponentArray{
|
|||||||
|
|
||||||
public Object getData(int entity){
|
public Object getData(int entity){
|
||||||
if (!entityComponentDataMap.containsKey(entity)){
|
if (!entityComponentDataMap.containsKey(entity)){
|
||||||
ECS.writeErr("Attempted to retrieve non-existent data");
|
System.err.println("Attempted to retrieve non-existent data");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,14 +17,13 @@ class ComponentManager{
|
|||||||
Map<Type, Integer> componentPosIndex = new HashMap<>();
|
Map<Type, Integer> componentPosIndex = new HashMap<>();
|
||||||
int componentPos = 0;
|
int componentPos = 0;
|
||||||
|
|
||||||
public boolean registerComponent(Type type){
|
public void registerComponent(Type type){
|
||||||
if (componentArrays.containsKey(type)){
|
if (componentArrays.containsKey(type)){
|
||||||
ECS.writeErr("Component " + type.getTypeName() + " is already registered");
|
System.err.println("Component " + type.getTypeName() + " is already registered");
|
||||||
return false;
|
return;
|
||||||
}
|
}
|
||||||
componentArrays.put(type, new ComponentArray());
|
componentArrays.put(type, new ComponentArray());
|
||||||
componentPosIndex.put(type, componentPos++);
|
componentPosIndex.put(type, componentPos++);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addComponentToEntity(Type componentName, Object componentData, int entity){
|
public void addComponentToEntity(Type componentName, Object componentData, int entity){
|
||||||
|
@ -12,7 +12,6 @@ package nz.ac.massey.javaecs;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.BitSet;
|
import java.util.BitSet;
|
||||||
/**
|
/**
|
||||||
@ -22,10 +21,6 @@ import java.util.BitSet;
|
|||||||
* for documentation and more information.
|
* for documentation and more information.
|
||||||
*/
|
*/
|
||||||
public class ECS {
|
public class ECS {
|
||||||
// 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;
|
|
||||||
// References to the manager classes
|
|
||||||
protected EntityManager entityManager;
|
protected EntityManager entityManager;
|
||||||
protected ComponentManager componentManager;
|
protected ComponentManager componentManager;
|
||||||
protected SystemManager systemManager;
|
protected SystemManager systemManager;
|
||||||
@ -41,7 +36,7 @@ public class ECS {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialises the ECS with the specified value
|
* Initialises the ECS with the specified value(s)
|
||||||
* @param maxEntities the maximum number of entities to allow
|
* @param maxEntities the maximum number of entities to allow
|
||||||
*/
|
*/
|
||||||
public ECS(int maxEntities){
|
public ECS(int maxEntities){
|
||||||
@ -85,8 +80,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 name the name to register. Should be the component class name or a suitable name for primitive types
|
||||||
*/
|
*/
|
||||||
boolean registerComponent(Type type){
|
void registerComponent(Type type){
|
||||||
return componentManager.registerComponent(type);
|
componentManager.registerComponent(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
Integer getComponentIndex(Type type){
|
Integer getComponentIndex(Type type){
|
||||||
@ -99,14 +94,10 @@ public class ECS {
|
|||||||
* @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
|
||||||
*/
|
*/
|
||||||
boolean addComponent(int entity, Type componentName, Object component){
|
void addComponent(int entity, Type componentName, Object component){
|
||||||
if (entityManager.registerComponent(componentManager.getComponentIndex(componentName), entity))
|
componentManager.addComponentToEntity(componentName, component, entity);
|
||||||
{
|
entityManager.registerComponent(componentManager.getComponentIndex(componentName), entity);
|
||||||
systemManager.entityRegistrationsChanged(entity, entityManager.getRegistrations(entity));
|
systemManager.entityRegistrationsChanged(entity, entityManager.getRegistrations(entity));
|
||||||
componentManager.addComponentToEntity(componentName, component, entity);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -114,14 +105,10 @@ public class ECS {
|
|||||||
* @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 componentName the class name of the component
|
||||||
*/
|
*/
|
||||||
boolean removeComponent(int entity, Type componentType){
|
void removeComponent(int entity, Type componentType){
|
||||||
if (entityManager.unregisterComponent(componentManager.getComponentIndex(componentType), entity))
|
componentManager.removeComponentFromEntity(componentType, entity);
|
||||||
{
|
entityManager.unregisterComponent(componentManager.getComponentIndex(componentType), entity);
|
||||||
componentManager.removeComponentFromEntity(componentType, entity);
|
systemManager.entityRegistrationsChanged(entity, entityManager.getRegistrations(entity));
|
||||||
systemManager.entityRegistrationsChanged(entity, entityManager.getRegistrations(entity));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -140,8 +127,8 @@ public class ECS {
|
|||||||
* @param systemName
|
* @param systemName
|
||||||
* @param action
|
* @param action
|
||||||
*/
|
*/
|
||||||
boolean registerSystem(Type systemType, ECSSystem action){
|
void registerSystem(Type systemType, ECSSystem action){
|
||||||
return systemManager.registerSystem(systemType, action);
|
systemManager.registerSystem(systemType, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -155,42 +142,4 @@ public class ECS {
|
|||||||
Integer getMaxEntities(){
|
Integer getMaxEntities(){
|
||||||
return entityManager.currentSize;
|
return entityManager.currentSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encapsulate syserr writes so they may be redirected out of the lib
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes an error message to the set PrintStream
|
|
||||||
* <p>
|
|
||||||
* <i>Default System.err
|
|
||||||
* @param message the message to write
|
|
||||||
*/
|
|
||||||
static void writeErr(String message){
|
|
||||||
errorStream.println(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Writes an object to the set PrintStream
|
|
||||||
* <p>
|
|
||||||
* <i>Default System.err
|
|
||||||
* @param obj the object to write
|
|
||||||
*/
|
|
||||||
static void writeErr(Object obj){
|
|
||||||
errorStream.println(obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the error PrintStream to the provided PrintStream
|
|
||||||
* @param newErr the PrintStream to set as the write destination
|
|
||||||
*/
|
|
||||||
static void setErr(PrintStream newErr){
|
|
||||||
errorStream = newErr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the current error PrintStream
|
|
||||||
* @return the current PrintStream that errors are written to
|
|
||||||
*/
|
|
||||||
static PrintStream getErr(){
|
|
||||||
return errorStream;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -53,7 +53,7 @@ class EntityManager{
|
|||||||
|
|
||||||
public Integer addEntity(){
|
public Integer addEntity(){
|
||||||
if (unusedEntities.size() == 0){
|
if (unusedEntities.size() == 0){
|
||||||
ECS.writeErr("No available space to create a new entity");
|
System.err.println("No available space to create a new entity");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return unusedEntities.remove();
|
return unusedEntities.remove();
|
||||||
@ -64,36 +64,16 @@ class EntityManager{
|
|||||||
entityRegistrations.get(entity).clear();
|
entityRegistrations.get(entity).clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean registerComponent(int component, int entity){
|
public void registerComponent(int component, int entity){
|
||||||
if (entity >= currentSize){
|
if (entity >= currentSize){
|
||||||
ECS.writeErr("Attempted to assign a component to non-existent entity: " + entity);
|
System.err.println("Attempted to assign a component to non-existent entity: " + entity);
|
||||||
return false;
|
return;
|
||||||
}
|
|
||||||
else if (entityRegistrations.get(entity).get(component))
|
|
||||||
{
|
|
||||||
ECS.writeErr("Entity is already assigned to the component");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
entityRegistrations.get(entity).set(component);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
entityRegistrations.get(entity).set(component);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean unregisterComponent(int component, int entity){
|
public void unregisterComponent(int component, int entity){
|
||||||
try{
|
entityRegistrations.get(entity).clear(component);
|
||||||
if (entityRegistrations.get(entity).get(component))
|
|
||||||
{
|
|
||||||
entityRegistrations.get(entity).clear(component);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else return false;
|
|
||||||
}
|
|
||||||
catch (IndexOutOfBoundsException e)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BitSet getRegistrations(int entity){
|
public BitSet getRegistrations(int entity){
|
||||||
@ -106,11 +86,11 @@ class EntityManager{
|
|||||||
|
|
||||||
public boolean resize(int newSize, SystemManager systemManager, ComponentManager componentManager){
|
public boolean resize(int newSize, SystemManager systemManager, ComponentManager componentManager){
|
||||||
if (newSize < currentSize - unusedEntities.size()){
|
if (newSize < currentSize - unusedEntities.size()){
|
||||||
ECS.writeErr("Attempted to resize the maximum entity count to a number smaller than the current assigned entity count.");
|
System.err.println("Attempted to resize the maximum entity count to a number smaller than the current assigned entity count.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else if (newSize == currentSize){
|
else if (newSize == currentSize){
|
||||||
ECS.writeErr("Attempted to set the newSize to the current size");
|
System.err.println("Attempted to set the newSize to the current size");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
@ -29,7 +29,7 @@ class SystemManager{
|
|||||||
// table.
|
// table.
|
||||||
public boolean registerSystem(Type systemType, ECSSystem system){
|
public boolean registerSystem(Type systemType, ECSSystem system){
|
||||||
if (systems.containsKey(systemType)){
|
if (systems.containsKey(systemType)){
|
||||||
ECS.writeErr("System already registered");
|
System.err.println("System already registered");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
systems.put(systemType, system);
|
systems.put(systemType, system);
|
||||||
|
@ -2,18 +2,12 @@ package nz.ac.massey.javaecs;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.PrintStream;
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.BitSet;
|
import java.util.BitSet;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
@ -27,10 +21,6 @@ class AppTest {
|
|||||||
TestSystem system;
|
TestSystem system;
|
||||||
|
|
||||||
class TestObject{
|
class TestObject{
|
||||||
TestObject() {}
|
|
||||||
TestObject(int i){
|
|
||||||
this.i = i;
|
|
||||||
}
|
|
||||||
int i = 1;
|
int i = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,67 +161,6 @@ class AppTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void testEntityManagerConstructor(){
|
|
||||||
gameEngine = new ECS(5);
|
|
||||||
assertEquals(5, gameEngine.getMaxEntities());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testAssignToNonExistentEntity(){
|
|
||||||
assertFalse(gameEngine.addComponent(1025, TestObject.class, new TestObject()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testRemoveComponent(){
|
|
||||||
int entity = gameEngine.createEntity();
|
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
|
||||||
gameEngine.removeComponent(entity, TestObject.class);
|
|
||||||
assertFalse(gameEngine.entityManager.getRegistrations(entity).get(gameEngine.getComponentIndex(TestObject.class)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testEqualResize(){
|
|
||||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
|
||||||
PrintStream orig = System.err;
|
|
||||||
PrintStream newErr = new PrintStream(bytes);
|
|
||||||
System.setErr(newErr);
|
|
||||||
assertTrue(gameEngine.resizeMaximum(1024));
|
|
||||||
System.setErr(orig);
|
|
||||||
newErr.flush(); // ensure the bytes are recieved
|
|
||||||
byte[] errBytes = bytes.toByteArray();
|
|
||||||
String result = new String(errBytes);
|
|
||||||
System.err.println("Captured in redirect: " + result);
|
|
||||||
assertTrue(result.trim().equals("Attempted to set the newSize to the current size"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testLargerResize(){
|
|
||||||
assertTrue(gameEngine.resizeMaximum(2048));
|
|
||||||
assertEquals(2048, gameEngine.getMaxEntities());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testComponentAlreadyRegistered(){
|
|
||||||
assertFalse(gameEngine.registerComponent(TestObject.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testRemoveNonExistentComponentData(){
|
|
||||||
assertFalse(gameEngine.removeComponent(1, TestObject.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testAssignComponentAlreadyAssigned(){
|
|
||||||
int entity = gameEngine.createEntity();
|
|
||||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
|
||||||
assertFalse(gameEngine.addComponent(entity, TestObject.class, new TestObject()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testRegisterExistingSystem(){
|
|
||||||
assertFalse(gameEngine.registerSystem(TestSystem.class, new TestSystem()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Establish a simple ECS, with a single system and component
|
* Establish a simple ECS, with a single system and component
|
||||||
|
Loading…
x
Reference in New Issue
Block a user