Systems
Open JavadocSystems look for certain combinations of components in entities and modify the data of said components in each frame. Systems determine the behaviour of entities.
There are a total of 4 different types of systems:
Order | Type | Description |
---|---|---|
1 | Initialisation systems | Initialize entities when they are first added to the world and when they are re-activated. |
2 | Input systems | Read user input and modify data of components in each frame. |
3 | Update systems | Modify the data of components in each frame. |
4 | Render systems | Turn components into visuals that are rendered to the screen. |
Initialisation systems are also executed when an inactive entity is re-activated. Input, update and render systems are executed in each frame in the order specified above.
Creating systems
Below is a list of 3 different ways of creating systems, orderered from most to least simple.
All examples are update systems, but other types of systems can be created in the same way.
The examples are systems that require a Transform
component.
Option 1: Extending system base class
This option is best used for systems that need to perform certain logic once per frame, before or after processing all entities.
public class TransformInitializer extends InitSystemBase {
public TransformInitializer() { super(Transform.class); }
@Override public void init() { // ...
forEach(chunk -> { Transform transform = chunk.getComponent(Transform.class);
// ... }); }
}
public class TransformInputHandler extends InputSystemBase {
public TransformInputHandler() { super(Transform.class); }
@Override public void input(float deltaTime) { // ...
forEach(chunk -> { Transform transform = chunk.getComponent(Transform.class);
// ... }); }
}
public class TransformUpdater extends UpdateSystemBase {
public TransformUpdater() { super(Transform.class); }
@Override public void update(float deltaTime) { // ...
forEach(chunk -> { Transform transform = chunk.getComponent(Transform.class);
// ... }); }
}
public class TransformRenderSystem extends RenderSystemBase {
public TransformRenderSystem() { super(Transform.class); }
@Override public void render(Renderer renderer) { // ...
forEach(chunk -> { Transform transform = chunk.getComponent(Transform.class);
// ... }); }
}
Option 2: Extending system class
public class TransformInitializer extends UpdateSystem {
public TransformInitializer() { super(Transform.class); }
@Override public void process(EntityChunk chunk) { Transform transform = chunk.getComponent(Transform.class);
// ... }
}
public class TransformInputHandler extends UpdateSystem {
public TransformInputHandler() { super(Transform.class); }
@Override public void process(EntityChunk chunk, Input input, float deltaTime) { Transform transform = chunk.getComponent(Transform.class);
// ... }
}
public class TransformUpdater extends UpdateSystem {
public TransformUpdater() { super(Transform.class); }
@Override public void process(EntityChunk chunk, float deltaTime) { Transform transform = chunk.getComponent(Transform.class);
// ... }
}
public class TransformRenderSystem extends UpdateSystem {
public TransformRenderSystem() { super(Transform.class); }
@Override public void process(EntityChunk chunk, Renderer renderer) { Transform transform = chunk.getComponent(Transform.class);
// ... }
}
Option 3: Using system builder
InitSystemBuilder initSystemBuilder = new InitSystemBuilder(Transform.class);initSystemBuilder.setCallback(chunk -> { Transform transform = chunk.getComponent(Transform.class);
// ...});
InitSystem transformInitializer = initSystemBuilder.build();
InputSystemBuilder inputSystemBuilder = new InputSystemBuilder(Transform.class);inputSystemBuilder.setCallback(chunk, input, deltaTime -> { Transform transform = chunk.getComponent(Transform.class);
// ...});
InputSystem transformInputHandler = inputSystemBuilder.build();
UpdateSystemBuilder updateSystemBuilder = new UpdateSystemBuilder(Transform.class);updateSystemBuilder.setCallback(chunk, deltaTime -> { Transform transform = chunk.getComponent(Transform.class);
// ...});
UpdateSystem transformUpdater = updateSystemBuilder.build();
RenderSystemBuilder renderSystemBuilder = new RenderSystemBuilder(Transform.class);renderSystemBuilder.setCallback(chunk, renderer -> { Transform transform = chunk.getComponent(Transform.class);
// ...});
RenderSystem transformRenderSystem = renderSystemBuilder.build();
Adding systems to the world
Systems should be added to the world during scene loading, before any entities are added to the scene.
world.addSystem(transformUpdater);