1.9 KiB
1.9 KiB
| JavaECS | docs | implementation.md
Implementation
Contents
Steps
- In the application's setup function, create an instance of
ECS
, with a specified maximum number of entity entries. - Define the component classes, (or associate the primitive types) and register the components.
- Define the systems that interact with the components and register them
- Run the game.
Example
public static void main(){
ECS gameEngine = new ECS(100);
// Register the components, with a parameterised type
gameEngine.registerComponent<coord>(coord.class.getName());
gameEngine.registerComponent<ridgidBody>(ridgidbody.class.getName());
// Register the system to run on the component
gameEngine.registerSystem("ApplyGravitySystem", () -> {
List<Integer> relevantEntities = gameEngine.getEntitiesWithComponent(ridgidBody.class.getName());
for (Integer entity : relevantEntities) {
coord c = (coord)gameEngine.getComponent(entity, coord.class.getName());
ridgidBody r = (ridgidBody)gameEngine.getComponent(entity, ridgidBody.class.getName());
c.y -= r.gravity;
}
});
// Create an acutal game object
int someEntity = gameEngine.createEntity();
// Assign components to the new game object
gameEngine.addComponent(someEntity, coord.class.getName(), new coord());
gameEngine.addComponent(someEntity, ridgidBody.class.getName(), new ridgidBody());
// Finally, enter into the game loop
gameEngine.loop();
}
class coord{
double x = 0.0;
double y = 0.0;
}
class ridgidBody{
double gravity = -9.81; // has a default value, unless overridden in the constructor
ridgidBody(){}
ridgidBody(double gravity){
this.gravity = gravity;
}
}