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