javalibgdxentity-component-system

What is the proper way of creating an entity in libgdx ashley ecs framework pooled engine?


// GameWorld
PooledEngine engine = new PooledEngine();

// Gun class
class Gun {
   publc Gun(Entity entity) {
        createComponents();
        addComponents();
   }

   public Entity getEntity();
}

// Option 1
Gun gun = new Gun(engine.createEntity());
engine.addEntity(gun.getEntity());

// Option 2 or a simple method inside EntityFactory class
public Entity createGun() {
    Entity gun = engine.createEntity();
    // components creation here
    ..
    engine.addEntity(gun);
    return gun;
}

Question which one is the better way of creating an Entity made from the PooledEngine?


Solution

  • I'd prefer the second method via an EntityFactory.

    Explanation: