Mini Tutorial

warmwaffles edited this page May 29, 2011 · 1 revision

Creating and running The first thing that needs to be done is to instantiate a new World.

world = new World();

Next, you need to add your own systems to the world, and then finally initialize the world. This can be done in the initialization method for your game.

SystemManager systemManager = world.getSystemManager();
             movementSystem = systemManager.setSystem(new MovementSystem());
               renderSystem = systemManager.setSystem(new RenderSystem());
//add more systems as needed
systemManager.initializeAll();

Then usually the game has both update and render methods. For update you'd invoke logic specific systems, and you also need to call framework specific methods to indicate that the loop as started.

update(int delta) {
    world.loopStart(); // signals loop start
    world.setDelta(delta); // sets delta for systems to use
    movementSystem.process();
}

render() {
    renderSystem.process();
}

Assembling entities To add entities to your world you can do something like:

Entity e = world.createEntity();
e.addComponent(new Position(200,400));
e.addComponent(new Velocity(2.4f,0.9f));
e.refresh(); // very important, see below

Entity.refresh()

It is very important to call Entity.refresh() after adding or removing components from a entity. Refresh will notify all systems of changes to this entity, and the systems will either add or remove the entity from itself if needed.

EntitySystem A typical entity system would be something like MovementSystem.

public class MovementSystem extends EntityProcessingSystem {
    private ComponentMapper velocityMapper;
    private ComponentMapper positionMapper;

    public MovementSystem() {
        super(Transform.class, Velocity.class); // Components represent the aspect
    }

    @Override
    public void initialize() {
        // Use these to be able to retrieve components from entities fast.
        velocityMapper = new ComponentMapper(Velocity.class, world.getEntityManager());
        positionMapper = new ComponentMapper(Position.class, world.getEntityManager());
    }

    @Override
    protected void process(Entity e) {
        // Get the components from entity.
        Position position = positionMapper.get(e);
        Velocity velocity = velocityMapper.get(e);

        // Calculate new position.
        float newX = position.getX() + (world.getDelta()*velocity.getVelocityX());
        float newY = position.getY() + (world.getDelta()*velocity.getVelocityY());

        // Set the new position.
        position.setPosition(newX, newY);
    }

}

While this does not provide a complete coverage of all the frameworks capabilities, it should, alongside the demo game, provide a sufficient understanding how to get things going. The JavaDoc does also contain more description, where description is needed, so be sure to look there as well.

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.