Permalink
Browse files

Splits Engine implementation into multiple classes #178

Engine is no longer a God-like class. The interface remains the same but
it now delegates its logic onto:
- EntityManager
- SystemManager
- FamilyManager
- ComponentOperationHandler
  • Loading branch information...
dsaltares committed Nov 29, 2015
1 parent 4927c6d commit 261342f74dc7c1b955e7605aadeb9bfcbc112128
@@ -0,0 +1,93 @@
package com.badlogic.ashley.core;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool;


class ComponentOperationHandler {
private BooleanInformer delayed;
private ComponentOperationPool operationPool = new ComponentOperationPool();;
private Array<ComponentOperation> operations = new Array<ComponentOperation>();;

public ComponentOperationHandler(BooleanInformer delayed) {
this.delayed = delayed;
}

public void add(Entity entity) {
if (delayed.value()) {
ComponentOperation operation = operationPool.obtain();
operation.makeAdd(entity);
operations.add(operation);
}
else {
entity.notifyComponentAdded();
}
}

public void remove(Entity entity) {
if (delayed.value()) {
ComponentOperation operation = operationPool.obtain();
operation.makeRemove(entity);
operations.add(operation);
}
else {
entity.notifyComponentRemoved();
}
}

public void processOperations() {
for (int i = 0; i < operations.size; ++i) {
ComponentOperation operation = operations.get(i);

switch(operation.type) {
case Add:
operation.entity.notifyComponentAdded();
break;
case Remove:
operation.entity.notifyComponentRemoved();
break;
default: break;
}

operationPool.free(operation);
}

operations.clear();
}

private static class ComponentOperation implements Pool.Poolable {
public enum Type {
Add,
Remove,
}

public Type type;
public Entity entity;

public void makeAdd(Entity entity) {
this.type = Type.Add;
this.entity = entity;
}

public void makeRemove(Entity entity) {
this.type = Type.Remove;
this.entity = entity;
}

@Override
public void reset() {
entity = null;
}
}

private static class ComponentOperationPool extends Pool<ComponentOperation> {
@Override
protected ComponentOperation newObject() {
return new ComponentOperation();
}
}

interface BooleanInformer {
public boolean value();
}
}
Oops, something went wrong.

0 comments on commit 261342f

Please sign in to comment.