]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/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
;