JavaECS-Docs/docs/implementation.md

62 lines
1.9 KiB
Markdown

*| [JavaECS](../README.md) | [docs](./overview.md) | implementation[]().md*
# Implementation
### Contents
[Steps](#steps)
[Example](#example)
## Steps
1. In the application's setup function, create an instance of `ECS`, with a specified maximum number of entity entries.
2. Define the component classes, (or associate the primitive types) and register the components.
3. Define the systems that interact with the components and register them
4. Run the game.
## Example
``` java
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;
}
}
```
### See also:
[entity](./entity/entity.md)
[component](./component/component.md)
[system](./system/system.md)