Fixed failed creating entities
Now throws when too many entities created
This commit is contained in:
parent
cfd397e10a
commit
8ffe1db4af
@ -46,11 +46,15 @@ public class ECS {
|
||||
}
|
||||
|
||||
/**
|
||||
* /**
|
||||
* Creates a new entity
|
||||
* @return the id of the new entity
|
||||
* @return the index of the new entity
|
||||
* @throws IndexOutOfBoundsException
|
||||
*/
|
||||
Integer createEntity(){
|
||||
return entityManager.addEntity();
|
||||
Integer createEntity() throws IndexOutOfBoundsException{
|
||||
int newEntity = entityManager.addEntity();
|
||||
if (newEntity == -1) throw new IndexOutOfBoundsException("Could not create a new entity");
|
||||
return newEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -65,6 +65,10 @@ class EntityManager{
|
||||
}
|
||||
|
||||
public void registerComponent(int component, int entity){
|
||||
if (entity >= currentSize){
|
||||
System.err.println("Attempted to assign a component to non-existent entity: " + entity);
|
||||
return;
|
||||
}
|
||||
entityRegistrations.get(entity).set(component);
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,9 @@ package nz.ac.massey.programming_project_159333_s1_2021;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.BitSet;
|
||||
|
||||
@ -58,10 +60,17 @@ class AppTest {
|
||||
@Test
|
||||
void testAssignMoreThanMax(){
|
||||
for (int i = 0; i < gameEngine.getMaxEntities() + 5; i++) {
|
||||
if (i >= gameEngine.getMaxEntities()){
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> {
|
||||
int entity = gameEngine.createEntity();
|
||||
});
|
||||
}
|
||||
else{
|
||||
int entity = gameEngine.createEntity();
|
||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test removing data
|
||||
@ -135,6 +144,23 @@ class AppTest {
|
||||
gameEngine.resizeMaximum(1024 - 255);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test large creations & deletions & smaller resize, to a value too small
|
||||
*/
|
||||
@Test
|
||||
void testLargeCreateDeleteResizeTooSmall(){
|
||||
for (int i = 0; i < gameEngine.getMaxEntities(); i++) {
|
||||
int entity = gameEngine.createEntity();
|
||||
gameEngine.addComponent(entity, TestObject.class, new TestObject());
|
||||
assertEquals(1, ((TestObject)gameEngine.getComponentData(entity, TestObject.class)).i);
|
||||
}
|
||||
for (int i = 256; i < 512; i++) {
|
||||
gameEngine.destroyEntity(i);
|
||||
}
|
||||
assertFalse(gameEngine.resizeMaximum(512));
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Establish a simple ECS, with a single system and component
|
||||
|
Loading…
x
Reference in New Issue
Block a user