To get started, create a Dominion instance, define your components (as classes or records), create entities, and run systems using a Scheduler.
Key steps:
- Initialize: Use
Dominion.create() to create your world. - Create Entities: Use
hello.createEntity(name, components...) to add entities with specific data. - Define Systems: Systems are typically implemented as
Runnable tasks that use findEntitiesWith(Class... componentTypes) to retrieve entities and their associated components. - Schedule and Run: Create a
Scheduler via hello.createScheduler(), schedule your system, and start the loop with tickAtFixedRate(rate).
public class HelloDominion {
public static void main(String[] args) {
// creates your world
Dominion hello = Dominion.create();
// creates an entity with components
hello.createEntity(
"my-entity",
new Position(0, 0),
new Velocity(1, 1)
);
// creates a system
Runnable system = () -> {
//finds entities
hello.findEntitiesWith(Position.class, Velocity.class)
// stream the results
.stream().forEach(result -> {
Position position = result.comp1();
Velocity velocity = result.comp2();
position.x += velocity.x;
position.y += velocity.y;
System.out.printf("Entity %s moved with %s to %s\n",
result.entity().getName(), velocity, position);
});
};
// creates a scheduler
Scheduler scheduler = hello.createScheduler();
// schedules the system
scheduler.schedule(system);
// starts 3 ticks per second
scheduler.tickAtFixedRate(3);
}
// component types can be both classes and records
static class Position {
double x, y;
public Position(double x, double y) {/*..."}
@Override
public String toString() {/*..."}
}
record Velocity(double x, double y) {
}
}