]> git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/serpentity.js
d1c1cb0cbfc3f70f0a89cbef249c3884020fe36a
[rbdr/serpentity] / lib / serpentity / serpentity.js
1 require("neon");
2
3 /*
4 Serpentity is a simple entity framework inspired by Ash.
5
6 Usage:
7
8 require('serpentity');
9
10 ## Instantiating an engine
11
12 var engine = Serpentity();
13
14 Add entities or systems:
15
16 engine.addEntity(entityFactory());
17 engine.addSystem(new GameSystem());
18
19 Update all systems:
20
21 engine.update(dt);
22
23 Remove entities or systems:
24
25 engine.removeEntity(entityReference);
26 engine.removeSystem(systemReference);
27
28 ## Creating Entities
29
30 Entities are the basic object of Serpentity, and they do nothing.
31
32 var entity = new Serpentity.Entity();
33
34 All the behavior is added through components
35
36 ## Creating Components
37
38 Components define data that we can add to an entity. This data will
39 eventually be consumed by "Systems"
40
41 Class("PositionComponent").inherits(Serpentity.Component)({
42 prototype : {
43 x : 0,
44 y : 0
45 }
46 });
47
48 You can add components to entities by using the add method:
49
50 entity.add(new PositionComponent());
51
52
53 Systems can refer to entities by requesting nodes.
54
55 ## Working with Nodes
56
57 Nodes are sets of components that you define, so your system can require
58 entities that always follow the API defined in the node.
59
60 Class("MovementNode").inherits(Serpentity.Node)({
61 types : {
62 position : PositionComponent,
63 motion : MotionComponent
64 }
65 });
66
67 You can then request an array of all the nodes representing entities
68 that comply with that API
69
70 engine.getNodes(MovementNode);
71
72 ## Creating Systems
73
74 Systems are called on every update, and they use components through nodes.
75
76 Class("TestSystem").inherits(Serpentity.System)({
77 prototype : {
78 added : function added(engine){
79 this.nodeList = engine.getNodes(MovementNode);
80 },
81 removed : function removed(engine){
82 this.nodeList = undefined;
83 }
84 update : function update(dt){
85 this.nodeList.forEach(function (node) {
86 console.log("Current position is: " + node.position.x + "," + node.position.y);
87 });
88 }
89 }
90 });
91
92 ## That's it
93
94 Just run `engine.update(dt)` in your game loop :D
95
96 */
97 Class("Serpentity")({
98 prototype : {
99 systems : null,
100 nodeCollections : null,
101 entities : null,
102
103 init : function init(config) {
104 var property;
105
106 config = config || {};
107
108 this.systems = [];
109 this.entities = [];
110 this.nodeCollections = {};
111
112 for (property in config) {
113 if (config.hasOwnProperty(property)) {
114 this[property] = config[property];
115 }
116 }
117 },
118
119 /*
120 * Adds a system to the engine, so its update method will be called
121 * with the others. Triggers added hook.
122 *
123 * returns true if added succesfully, false if already added
124 */
125 addSystem : function addSystem(system) {
126 if (this.systems.indexOf(system) >= 0) {
127 return false;
128 }
129 this.systems.push(system);
130 system.added(this);
131 return true;
132 },
133
134 /*
135 * Removes a system from the engine, so its update method will no
136 * longer will be called. Triggers the removed hook.
137 *
138 * returns true if removed succesfully, false if already added
139 */
140 removeSystem : function removeSystem(system) {
141 var position;
142
143 position = this.systems.indexOf(system);
144 if (position >= 0) {
145 this.systems[position].removed(this);
146 this.systems.splice(position, 1);
147 return true;
148 }
149
150 return false;
151 },
152
153 /*
154 * Adds an entity to the engine, adds to existing node collections
155 *
156 * returns true if added, false if already there
157 */
158 addEntity : function addEntity(entity) {
159 var property;
160
161 if (this.entities.indexOf(entity) >= 0) {
162 return false;
163 }
164 this.entities.push(entity);
165
166 for (property in this.nodeCollections) {
167 if (this.nodeCollections.hasOwnProperty(property)) {
168 this.nodeCollections[property].add(entity);
169 }
170 }
171 return true;
172 },
173
174 /*
175 * Removes entity from system, removing from all node collections
176 *
177 * returns true if removed, false if not present
178 */
179 removeEntity : function removeEntity(entity) {
180 var position;
181
182 position = this.entities.indexOf(entity);
183 if (position >= 0) {
184 for (property in this.nodeCollections) {
185 if (this.nodeCollections.hasOwnProperty(property)) {
186 this.nodeCollections[property].remove(entity);
187 }
188 }
189
190 this.entities.splice(position, 1);
191 return true;
192 }
193
194 return false;
195 },
196
197 /*
198 * Given a Node Class, retrieves a list of all the nodes for each
199 * applicable entity.
200 */
201 getNodes : function getNodes(nodeType) {
202 var nodeCollection;
203
204 if (this.nodeCollections.hasOwnProperty(nodeType)) {
205 return this.nodeCollections[nodeType].nodes;
206 }
207
208 nodeCollection = new Serpentity.NodeCollection({
209 type : nodeType,
210 });
211 this.nodeCollections[nodeType] = nodeCollection;
212
213 this.entities.forEach(function (entity) {
214 nodeCollection.add(entity);
215 });
216
217 return nodeCollection.nodes;
218 },
219
220 /*
221 * Calls update for every loaded system.
222 */
223 update : function update(dt) {
224 this.systems.forEach(function (system) {
225 system.update(dt);
226 });
227 }
228 }
229 });
230
231 require("./component.js");
232 require("./entity.js");
233 require("./node.js");
234 require("./node_collection.js");
235 require("./system.js");