]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity.js
4 Serpentity is a simple entity framework inspired by Ash.
8 const Serpentity = require('serpentity');
10 ## Instantiating an engine
12 const engine = new 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 const 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 const PositionComponent = class PositionComponent extends Serpentity.Component {
52 You can add components to entities by using the add method:
54 entity.addComponent(new PositionComponent());
57 Systems can refer to entities by requesting nodes.
61 Nodes are sets of components that you define, so your system can require
62 entities that always follow the API defined in the node.
64 const MovementNode = class MovementNode extends Serpentity.Node;
65 MovementNode.position = PositionComponent;
66 MovementNode.motion = MotionComponent;
68 You can then request an array of all the nodes representing entities
69 that comply with that API
71 engine.getNodes(MovementNode);
75 Systems are called on every update, and they use components through nodes.
77 const TestSystem = class TestSystem extends Serpentity.System {
80 this.nodeList = engine.getNodes(MovementNode);
85 this.nodeList = undefined;
90 for (const node of this.nodeList) {
91 console.log(`Current position is: ${node.position.x},${node.position.y}`);
98 Just run `engine.update(dt)` in your game loop :D
101 const Serpentity
= class Serpentity
{
103 constructor(config
) {
107 this._nodeCollections
= [];
108 this._nodeCollectionKeys
= [];
110 Object
.assign(this, config
);
114 * Adds a system to the engine, so its update method will be called
115 * with the others. Triggers added hook.
117 * returns true if added succesfully, false if already added
119 addSystem(system
, priority
) {
121 if (this.systems
.indexOf(system
) >= 0) {
125 system
.priority
= priority
;
129 const found
= this.systems
.some((existingSystem
, i
) => {
132 if (existingSystem
.priority
>= system
.priority
) {
141 this.systems
.splice(lastIndex
, 0, system
);
147 * Removes a system from the engine, so its update method will no
148 * longer will be called. Triggers the removed hook.
150 * returns true if removed succesfully, false if already added
152 removeSystem(system
) {
154 const position
= this.systems
.indexOf(system
);
156 this.systems
[position
].removed(this);
157 this.systems
.splice(position
, 1);
165 * Adds an entity to the engine, adds to existing node collections
167 * returns true if added, false if already there
171 if (this.entities
.indexOf(entity
) >= 0) {
174 this.entities
.push(entity
);
176 for (const collection
of this._nodeCollections
) {
177 collection
.add(entity
);
184 * Removes entity from system, removing from all node collections
186 * returns true if removed, false if not present
188 removeEntity(entity
) {
190 const position
= this.entities
.indexOf(entity
);
192 for (const collection
of this._nodeCollections
) {
193 collection
.remove(entity
);
196 this.entities
.splice(position
, 1);
204 * Given a Node Class, retrieves a list of all the nodes for each
209 const position
= this._nodeCollectionKeys
.indexOf(nodeType
);
212 return this._nodeCollections
[position
].nodes
;
215 const nodeCollection
= new Serpentity
.NodeCollection({
219 this._nodeCollectionKeys
.push(nodeType
);
220 this._nodeCollections
.push(nodeCollection
);
222 for (const entity
of this.entities
) {
223 nodeCollection
.add(entity
);
226 return nodeCollection
;
230 * Calls update for every loaded system.
234 for (const system
of this.systems
) {
240 // Add namespaced objects.
241 Serpentity
.Component
= require('./serpentity/component.js');
242 Serpentity
.Entity
= require('./serpentity/entity.js');
243 Serpentity
.Node
= require('./serpentity/node.js');
244 Serpentity
.NodeCollection
= require('./serpentity/node_collection.js');
245 Serpentity
.System
= require('./serpentity/system.js');
247 module
.exports
= Serpentity
;