]> git.r.bdr.sh - rbdr/serpentity/blame_incremental - README.md
Make node components event emitters
[rbdr/serpentity] / README.md
... / ...
CommitLineData
1# Serpentity
2
3Serpentity is a simple entity framework inspired by [Ash][ash].
4
5Usage:
6
7 import Serpentity from '@serpentity/serpentity';
8
9## Instantiating an engine
10
11 const engine = new Serpentity();
12
13Add entities or systems, systems are added with a priority (the smaller
14the number, the earlier it will be called):
15
16 engine.addEntity(entityFactory());
17 engine.addSystem(new GameSystem(), priority);
18
19Update all systems:
20
21 engine.update(dt);
22
23Remove entities or systems:
24
25 engine.removeEntity(entityReference);
26 engine.removeSystem(systemReference);
27
28## Creating Entities
29
30Entities are the basic object of Serpentity, and they do nothing.
31
32 import { Entity } from '@serpentity/serpentity';
33 const entity = new Entity();
34
35All the behavior is added through components
36
37## Creating Components
38
39Components define data that we can add to an entity. This data will
40eventually be consumed by "Systems"
41
42 import { Component } from '@serpentity/serpentity';
43 const PositionComponent = class PositionComponent extends Component {
44 constructor(config) {
45
46 this.x = 0;
47 this.y = 0;
48
49 super(config);
50 }
51 };
52
53You can add components to entities by using the add method:
54
55 entity.addComponent(new PositionComponent());
56
57
58Systems can refer to entities by requesting nodes.
59
60## Working with Nodes
61
62Nodes are sets of components that you define, so your system can require
63entities that always follow the API defined in the node.
64
65 import { Node } from '@serpentity/serpentity';
66 const MovementNode = class MovementNode extends Node;
67 MovementNode.position = PositionComponent;
68 MovementNode.motion = MotionComponent;
69
70You can then request an array of all the nodes representing entities
71that comply with that API
72
73 engine.getNodes(MovementNode);
74
75## Creating Systems
76
77Systems are called on every update, and they use components through nodes.
78
79 import { System } from '@serpentity/serpentity';
80 const TestSystem = class TestSystem extends System {
81 added(engine){
82
83 this.nodeList = engine.getNodes(MovementNode);
84 }
85
86 removed(engine){
87
88 this.nodeList = undefined;
89 }
90
91 update(dt){
92
93 for (const node of this.nodeList) {
94 console.log(`Current position is: ${node.position.x},${node.position.y}`);
95 }
96 }
97 };
98
99## That's it
100
101Just run `engine.update(dt)` in your game loop :D
102
103[ash]: http://www.ashframework.org/