]>
git.r.bdr.sh - rbdr/serpentity/blob - dist/serpentity.js
4 Serpentity is a simple entity framework inspired by Ash.
8 let Serpentity = require('serpentity');
10 ## Instantiating an engine
12 let engine = Serpentity();
14 Add entities or systems, systems are added with a priority (the smaller
15 the number, the earlier it will be called):
17 engine.addEntity(entityFactory());
18 engine.addSystem(new GameSystem(), priority);
24 Remove entities or systems:
26 engine.removeEntity(entityReference);
27 engine.removeSystem(systemReference);
31 Entities are the basic object of Serpentity, and they do nothing.
33 let entity = new Serpentity.Entity();
35 All the behavior is added through components
37 ## Creating Components
39 Components define data that we can add to an entity. This data will
40 eventually be consumed by "Systems"
42 let PositionComponent = class PositionComponent extends Serpentity.Component {
43 constructor (config) {
51 You can add components to entities by using the add method:
53 entity.addComponent(new PositionComponent());
56 Systems can refer to entities by requesting nodes.
60 Nodes are sets of components that you define, so your system can require
61 entities that always follow the API defined in the node.
63 let MovementNode = class MovementNode extends Serpentity.Node;
64 MovementNode.position = PositionComponent;
65 MovementNode.motion = MotionComponent;
67 You can then request an array of all the nodes representing entities
68 that comply with that API
70 engine.getNodes(MovementNode);
74 Systems are called on every update, and they use components through nodes.
76 let TestSystem = class TestSystem extends Serpentity.System {
78 this.nodeList = engine.getNodes(MovementNode);
81 this.nodeList = undefined;
85 for (node of this.nodeList) {
86 console.log(`Current position is: ${node.position.x},${node.position.y}`);
93 Just run `engine.update(dt)` in your game loop :D
96 let Serpentity
= class Serpentity
{
98 constructor (config
) {
101 this._nodeCollections
= [];
102 this._nodeCollectionKeys
= [];
104 Object
.assign(this, config
|| {});
108 * Adds a system to the engine, so its update method will be called
109 * with the others. Triggers added hook.
111 * returns true if added succesfully, false if already added
113 addSystem (system
, priority
) {
114 let lastIndex
, found
;
116 if (this.systems
.indexOf(system
) >= 0) {
120 system
.priority
= priority
;
125 this.systems
.some(function findPriority(existingSystem
, i
) {
127 if (existingSystem
.priority
>= system
.priority
) {
137 this.systems
.splice(lastIndex
, 0, system
);
143 * Removes a system from the engine, so its update method will no
144 * longer will be called. Triggers the removed hook.
146 * returns true if removed succesfully, false if already added
148 removeSystem (system
) {
151 position
= this.systems
.indexOf(system
);
153 this.systems
[position
].removed(this);
154 this.systems
.splice(position
, 1);
162 * Adds an entity to the engine, adds to existing node collections
164 * returns true if added, false if already there
167 if (this.entities
.indexOf(entity
) >= 0) {
170 this.entities
.push(entity
);
172 this._nodeCollections
.forEach(function (collection
) {
173 collection
.add(entity
);
180 * Removes entity from system, removing from all node collections
182 * returns true if removed, false if not present
184 removeEntity (entity
) {
187 position
= this.entities
.indexOf(entity
);
189 this._nodeCollections
.forEach(function (collection
) {
190 collection
.remove(entity
);
193 this.entities
.splice(position
, 1);
201 * Given a Node Class, retrieves a list of all the nodes for each
204 getNodes (nodeType
) {
205 let position
, nodeCollection
;
207 position
= this._nodeCollectionKeys
.indexOf(nodeType
);
210 return this._nodeCollections
[position
].nodes
;
213 nodeCollection
= new Serpentity
.NodeCollection({
217 this._nodeCollectionKeys
.push(nodeType
);
218 this._nodeCollections
.push(nodeCollection
);
220 this.entities
.forEach(function (entity
) {
221 nodeCollection
.add(entity
);
224 return nodeCollection
.nodes
;
228 * Calls update for every loaded system.
231 this.systems
.forEach(function (system
) {
237 // Add namespaced objects.
238 if (typeof module
!== 'undefined' && this.module
!== module
) {
239 Serpentity
.Component
= require('./serpentity/component.js');
240 Serpentity
.Entity
= require('./serpentity/entity.js');
241 Serpentity
.Node
= require('./serpentity/node.js');
242 Serpentity
.NodeCollection
= require('./serpentity/node_collection.js');
243 Serpentity
.System
= require('./serpentity/system.js');
245 module
.exports
= Serpentity
;
247 window
.Serpentity
= Serpentity
;
252 /* global Serpentity */
255 * The entity gives the entity framework its name. It exists only
256 * to hold components.
259 let Entity
= class Entity
{
260 constructor (config
) {
261 this._componentKeys
= [];
262 this._components
= [];
264 Object
.assign(this, config
|| {});
268 * Adds a component to the entity.
270 * returns true if added, false if already present
272 addComponent (component
) {
273 if (this._componentKeys
.indexOf(component
.constructor) >= 0) {
276 this._componentKeys
.push(component
.constructor);
277 this._components
.push(component
);
282 * returns true if component is included, false otherwise
284 hasComponent (componentClass
) {
285 if (this._componentKeys
.indexOf(componentClass
) >= 0) {
292 * returns the component associated with that key
294 getComponent (componentClass
) {
296 position
= this._componentKeys
.indexOf(componentClass
);
298 return this._components
[position
];
303 if (typeof module
!== 'undefined' && this.module
!== module
) {
304 module
.exports
= Entity
;
306 Serpentity
.Entity
= Entity
;
311 /* global Serpentity */
314 * A node describes a set of components in order to describe entities
317 let Node
= class Node
{
320 * Returns true if the given entity matches the defined protocol,
323 static matches (entity
) {
328 for (property
in types
) {
329 if (types
.hasOwnProperty(property
)) {
333 type
= types
[property
];
334 if (entity
.hasComponent(type
)) {
346 constructor (config
) {
349 Object
.assign(this, config
|| {});
353 if (typeof module
!== 'undefined' && this.module
!== module
) {
354 module
.exports
= Node
;
356 Serpentity
.Node
= Node
;
361 /* global Serpentity */
364 * Node Collections contain nodes, in order to keep the lists of nodes
365 * that belong to each type.
367 * It has a type which is the class name of the node, and an array of
368 * instances of that class.
371 let NodeCollection
= class NodeCollection
{
373 constructor (config
) {
377 Object
.assign(this, config
|| {});
381 * Creates a node for an entity if it matches, and adds it to the
384 * Returns true if added, false otherwise.
388 if (this.type
.matches(entity
) && !this._entityExists(entity
)) {
389 let node
, types
, property
;
391 node
= new this.type({});
392 node
.entity
= entity
;
393 types
= this.type
.types
;
395 for (property
in types
) {
396 if (types
.hasOwnProperty(property
)) {
397 node
[property
] = entity
.getComponent(types
[property
]);
401 this.nodes
.push(node
);
410 * Removes an entity by removing its related node from the list of nodes
412 * returns true if it was removed, false otherwise.
418 this.nodes
.forEach(function (node
, i
) {
419 if (node
.entity
=== entity
) {
425 this.nodes
.splice(found
, 1);
433 * Checks whether we already have nodes for this entity.
435 _entityExists (entity
) {
439 for (node
of this.nodes
) {
440 if (node
.entity
=== entity
) {
449 if (typeof module
!== 'undefined' && this.module
!== module
) {
450 module
.exports
= NodeCollection
;
452 Serpentity
.NodeCollection
= NodeCollection
;
457 /* global Serpentity */
460 * Components store data. Nothing to say here really, just
461 * inherit and add a prototype, or don't even inherit, see?
462 * It's just an empty class, so what I'm trying to say is your
463 * components can be any class whatsoever.
466 let Component
= class Component
{
467 constructor (config
) {
468 Object
.assign(this, config
|| {});
472 if (typeof module
!== 'undefined' && this.module
!== module
) {
473 module
.exports
= Component
;
475 Serpentity
.Component
= Component
;
480 /* global Serpentity */
483 * Systems contain most of the logic, and work with nodes in order to
484 * act and change their values.
486 * You usually want to inherit from this class and override the
487 * three methods. They are shown here to document the interface.
490 let System
= class System
{
493 * This will be run when the system is added to the engine
496 // Override with added(engine)
497 // Receives an instance of the serpentity engine
501 * This will be run when the system is removed from the engine
504 // Override with removed(engine)
505 // Receives an instance of the serpentity engine
509 * This will run every time the engine's update method is called
512 // Override with update(dt)
513 // Receives a delta of the time
517 if (typeof module
!== 'undefined' && this.module
!== module
) {
518 module
.exports
= System
;
520 Serpentity
.System
= System
;