Skip to content

Commit

Permalink
Adds getEngine() to EntitySystem #167
Browse files Browse the repository at this point in the history
  • Loading branch information
dsaltares committed Jun 6, 2015
1 parent 4cbdb49 commit d6a1339
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
4 changes: 2 additions & 2 deletions ashley/src/com/badlogic/ashley/core/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void addSystem(EntitySystem system){
if (!systemsByClass.containsKey(systemType)) {
systems.add(system);
systemsByClass.put(systemType, system);
system.addedToEngine(this);
system.addedToEngineInternal(this);

systems.sort(comparator);
}
Expand All @@ -189,7 +189,7 @@ public void addSystem(EntitySystem system){
public void removeSystem(EntitySystem system){
if(systems.removeValue(system, true)) {
systemsByClass.remove(system.getClass());
system.removedFromEngine(this);
system.removedFromEngineInternal(this);
}
}

Expand Down
17 changes: 17 additions & 0 deletions ashley/src/com/badlogic/ashley/core/EntitySystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public abstract class EntitySystem {
public int priority;

private boolean processing;
private Engine engine;

/** Default constructor that will initialise an EntitySystem with priority 0. */
public EntitySystem () {
Expand Down Expand Up @@ -70,4 +71,20 @@ public boolean checkProcessing () {
public void setProcessing (boolean processing) {
this.processing = processing;
}

/** @return engine instance the system is registered to.
* It will be null if the system is not associated to any engine instance. */
public Engine getEngine () {
return engine;
}

final void addedToEngineInternal(Engine engine) {
this.engine = engine;
addedToEngine(engine);
}

final void removedFromEngineInternal(Engine engine) {
this.engine = null;
removedFromEngine(engine);
}
}
16 changes: 13 additions & 3 deletions ashley/tests/com/badlogic/ashley/core/EngineTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,9 @@ private static class CounterComponent extends Component {

private static class CounterSystem extends EntitySystem {
private ImmutableArray<Entity> entities;
private Engine engine;

@Override
public void addedToEngine (Engine engine) {
this.engine = engine;
entities = engine.getEntitiesFor(Family.all(CounterComponent.class).get());
}

Expand All @@ -139,7 +137,7 @@ public void update (float deltaTime) {
if (i % 2 == 0) {
entities.get(i).getComponent(CounterComponent.class).counter++;
} else {
engine.removeEntity(entities.get(i));
getEngine().removeEntity(entities.get(i));
}
}
}
Expand Down Expand Up @@ -274,6 +272,18 @@ public void systemUpdateOrder () {
previous = value;
}
}

@Test
public void entitySystemEngineReference () {
Engine engine = new Engine();
EntitySystem system = new EntitySystemMock();

assertNull(system.getEngine());
engine.addSystem(system);
assertEquals(engine, system.getEngine());
engine.removeSystem(system);
assertNull(system.getEngine());
}

@Test
public void ignoreSystem () {
Expand Down
12 changes: 3 additions & 9 deletions ashley/tests/com/badlogic/ashley/core/PooledEngineTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,9 @@ public void entityRemoved (Entity entity) {
}

public static class CombinedSystem extends EntitySystem {
private Engine engine;
private ImmutableArray<Entity> allEntities;
private int counter = 0;

public CombinedSystem (Engine engine) {
this.engine = engine;
}

@Override
public void addedToEngine (Engine engine) {
allEntities = engine.getEntitiesFor(Family.all(PositionComponent.class).get());
Expand All @@ -53,7 +48,7 @@ public void addedToEngine (Engine engine) {
@Override
public void update (float deltaTime) {
if (counter >= 6 && counter <= 8) {
engine.removeEntity(allEntities.get(2));
getEngine().removeEntity(allEntities.get(2));
}
counter++;
}
Expand All @@ -70,17 +65,16 @@ public void receive (Signal<Entity> signal, Entity object) {

private static class RemoveEntityTwiceSystem extends EntitySystem {
private ImmutableArray<Entity> entities;
private PooledEngine engine;

@Override
public void addedToEngine (Engine engine) {
entities = engine.getEntitiesFor(Family.all(PositionComponent.class).get());
this.engine = (PooledEngine)engine;
}

@Override
public void update (float deltaTime) {
Entity entity;
PooledEngine engine = (PooledEngine)getEngine();
for (int i = 0; i < 10; i++) {
entity = engine.createEntity();
assertEquals(0, entity.flags);
Expand Down Expand Up @@ -110,7 +104,7 @@ public void reset () {
public void entityRemovalListenerOrder () {
PooledEngine engine = new PooledEngine();

CombinedSystem combinedSystem = new CombinedSystem(engine);
CombinedSystem combinedSystem = new CombinedSystem();

engine.addSystem(combinedSystem);
engine.addEntityListener(Family.all(PositionComponent.class).get(), new MyPositionListener());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ public void processEntity (Entity entity, float deltaTime) {
}

private static class IteratingRemovalSystem extends IteratingSystem {

private Engine engine;
private ComponentMapper<SpyComponent> sm;
private ComponentMapper<IndexComponent> im;

Expand All @@ -102,14 +100,13 @@ public IteratingRemovalSystem () {
@Override
public void addedToEngine (Engine engine) {
super.addedToEngine(engine);
this.engine = engine;
}

@Override
public void processEntity (Entity entity, float deltaTime) {
int index = im.get(entity).index;
if (index % 2 == 0) {
engine.removeEntity(entity);
getEngine().removeEntity(entity);
} else {
sm.get(entity).updates++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@
import com.badlogic.ashley.utils.ImmutableArray;

public class RemovalSystem extends EntitySystem {
private Engine engine;
private ImmutableArray<Entity> entities;

@Override
public void addedToEngine (Engine engine) {
this.engine = engine;
entities = engine.getEntitiesFor(Family.all(RemovalComponent.class).get());
}

@Override
public void update (float deltaTime) {
while (entities.size() > 0) {
engine.removeEntity(entities.get(0));
getEngine().removeEntity(entities.get(0));
}
}
}

0 comments on commit d6a1339

Please sign in to comment.