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