Skip to content

Commit

Permalink
Fixes GWT compatibility issue #73
Browse files Browse the repository at this point in the history
  • Loading branch information
dsaltares committed Oct 5, 2014
1 parent f1ccdbe commit 9a01938
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 42 deletions.
84 changes: 45 additions & 39 deletions ashley/src/com/badlogic/ashley/core/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,47 +89,14 @@ public Engine(){
listeners = new SnapshotArray<EntityListener>(false, 16);
familyListeners = new ObjectMap<Family,SnapshotArray<EntityListener>>();

componentAdded = new Listener<Entity>(){
@Override
public void receive(Signal<Entity> signal, Entity object) {
updateFamilyMembership(object);
}
};

componentRemoved = new Listener<Entity>(){
@Override
public void receive(Signal<Entity> signal, Entity object) {
updateFamilyMembership(object);
}
};
componentAdded = new ComponentListener(this);
componentRemoved = new ComponentListener(this);

updating = false;

componentOperationsPool = new ComponentOperationPool();
componentOperations = new Array<ComponentOperation>();
componentOperationHandler = new ComponentOperationHandler() {
public void add(Entity entity, Component component) {
if (updating) {
ComponentOperation operation = componentOperationsPool.obtain();
operation.makeAdd(entity, component);
componentOperations.add(operation);
}
else {
entity.addInternal(component);
}
}

public void remove(Entity entity, Class<? extends Component> componentClass) {
if (updating) {
ComponentOperation operation = componentOperationsPool.obtain();
operation.makeRemove(entity, componentClass);
componentOperations.add(operation);
}
else {
entity.removeInternal(componentClass);
}
}
};
componentOperationHandler = new ComponentOperationHandler(this);
}

/**
Expand Down Expand Up @@ -403,9 +370,48 @@ else if (operation.type == ComponentOperation.Type.Remove) {
componentOperations.clear();
}

static interface ComponentOperationHandler {
public void add(Entity entity, Component component);
public void remove(Entity entity, Class<? extends Component> componentClass);
private static class ComponentListener implements Listener<Entity> {
private Engine engine;

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


@Override
public void receive(Signal<Entity> signal, Entity object) {
engine.updateFamilyMembership(object);
}
}

static class ComponentOperationHandler {
private Engine engine;

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

public void add(Entity entity, Component component) {
if (engine.updating) {
ComponentOperation operation = engine.componentOperationsPool.obtain();
operation.makeAdd(entity, component);
engine.componentOperations.add(operation);
}
else {
entity.addInternal(component);
}
}

public void remove(Entity entity, Class<? extends Component> componentClass) {
if (engine.updating) {
ComponentOperation operation = engine.componentOperationsPool.obtain();
operation.makeRemove(entity, componentClass);
engine.componentOperations.add(operation);
}
else {
entity.removeInternal(componentClass);
}
}
}

private static class ComponentOperation {
Expand Down
7 changes: 4 additions & 3 deletions ashley/src/com/badlogic/ashley/signals/Signal.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Signal<T> {
private SnapshotArray<Listener<T>> listeners;

public Signal () {
listeners = new SnapshotArray<Listener<T>>(Listener.class);
listeners = new SnapshotArray<Listener<T>>();
}

/**
Expand Down Expand Up @@ -60,9 +60,10 @@ public void removeAllListeners() {
* @param object The object to send off
*/
public void dispatch(T object){
Listener<T>[] items = listeners.begin();
Object[] items = listeners.begin();
for (int i = 0, n = listeners.size; i < n; i++) {
items[i].receive(this, object);
Listener<T> listener = (Listener<T>)items[i];
listener.receive(this, object);
}
listeners.end();
}
Expand Down

0 comments on commit 9a01938

Please sign in to comment.