57 lines
1.2 KiB
Java
Executable File

package zero1hd.polyjet.entity;
import com.badlogic.gdx.utils.Pool;
public class EntityFrame<T extends Entity> {
private Pool<T> pool;
private EntityController ec;
Class<T> ct;
EntityFrame<T> ef;
public EntityFrame(EntityController entityController, Class<T> classType) {
ct = classType;
ef = this;
ec = entityController;
pool = new Pool<T>() {
@Override
protected T newObject() {
try {
T entity = ct.newInstance();
entity.setup(ec, ef);
return entity;
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
};
}
public T buildEntity() {
T entity = pool.obtain();
if (entity.enemy) {
ec.activeEnemies.add(entity);
} else {
ec.activeAllies.add(entity);
}
return entity;
}
/**
* Free the entity if no longer used.
* @param entity to be freed.
*/
public void recycleEntity(Entity entity) {
if (entity.enemy) {
ec.activeEnemies.removeValue(entity, true);
} else {
ec.activeAllies.removeValue(entity, true);
}
entity.remove();
pool.free(ct.cast(entity));
}
}