]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity.js
2 export { Component
} from './serpentity/component.js';
3 export { Entity
} from './serpentity/entity.js';
4 export { Node
} from './serpentity/node.js';
5 export { System
} from './serpentity/system.js';
7 import { NodeCollection
} from './serpentity/node_collection.js';
10 Serpentity is a simple entity framework inspired by Ash.
14 import Serpentity from '@serpentity/serpentity';
16 ## Instantiating an engine
18 const engine = new Serpentity();
20 Add entities or systems, systems are added with a priority (the smaller
21 the number, the earlier it will be called):
23 engine.addEntity(entityFactory());
24 engine.addSystem(new GameSystem(), priority);
30 Remove entities or systems:
32 engine.removeEntity(entityReference);
33 engine.removeSystem(systemReference);
37 Entities are the basic object of Serpentity, and they do nothing.
39 import { Entity } from '@serpentity/serpentity';
40 const entity = new Entity();
42 All the behavior is added through components
44 ## Creating Components
46 Components define data that we can add to an entity. This data will
47 eventually be consumed by "Systems"
49 import { Component } from '@serpentity/serpentity';
50 const PositionComponent = class PositionComponent extends Component {
60 You can add components to entities by using the add method:
62 entity.addComponent(new PositionComponent());
65 Systems can refer to entities by requesting nodes.
69 Nodes are sets of components that you define, so your system can require
70 entities that always follow the API defined in the node.
72 import { Node } from '@serpentity/serpentity';
73 const MovementNode = class MovementNode extends Node;
74 MovementNode.position = PositionComponent;
75 MovementNode.motion = MotionComponent;
77 You can then request an array of all the nodes representing entities
78 that comply with that API
80 engine.getNodes(MovementNode);
84 Systems are called on every update, and they use components through nodes.
86 import { System } from '@serpentity/serpentity';
87 const TestSystem = class TestSystem extends System {
90 this.nodeList = engine.getNodes(MovementNode);
95 this.nodeList = undefined;
100 for (const node of this.nodeList) {
101 console.log(`Current position is: ${node.position.x},${node.position.y}`);
108 Just run `engine.update(dt)` in your game loop :D
111 export default class Serpentity
{
113 constructor(config
) {
117 this._nodeCollections
= [];
118 this._nodeCollectionKeys
= [];
120 Object
.assign(this, config
);
124 * Adds a system to the engine, so its update method will be called
125 * with the others. Triggers added hook.
127 * returns true if added succesfully, false if already added
129 addSystem(system
, priority
) {
131 if (this.systems
.indexOf(system
) >= 0) {
135 system
.priority
= priority
;
139 const found
= this.systems
.some((existingSystem
, i
) => {
142 if (existingSystem
.priority
>= system
.priority
) {
151 this.systems
.splice(lastIndex
, 0, system
);
157 * Removes a system from the engine, so its update method will no
158 * longer will be called. Triggers the removed hook.
160 * returns true if removed succesfully, false if already added
162 removeSystem(system
) {
164 const position
= this.systems
.indexOf(system
);
166 this.systems
[position
].removed(this);
167 this.systems
.splice(position
, 1);
175 * Adds an entity to the engine, adds to existing node collections
177 * returns true if added, false if already there
181 if (this.entities
.indexOf(entity
) >= 0) {
185 this.entities
.push(entity
);
187 for (const collection
of this._nodeCollections
) {
188 collection
.add(entity
);
195 * Removes entity from system, removing from all node collections
197 * returns true if removed, false if not present
199 removeEntity(entity
) {
201 const position
= this.entities
.indexOf(entity
);
203 for (const collection
of this._nodeCollections
) {
204 collection
.remove(entity
);
207 this.entities
.splice(position
, 1);
215 * Given a Node Class, retrieves a list of all the nodes for each
220 const position
= this._nodeCollectionKeys
.indexOf(nodeType
);
223 return this._nodeCollections
[position
].nodes
;
226 const nodeCollection
= new NodeCollection({
230 this._nodeCollectionKeys
.push(nodeType
);
231 this._nodeCollections
.push(nodeCollection
);
233 for (const entity
of this.entities
) {
234 nodeCollection
.add(entity
);
237 return nodeCollection
;
241 * Calls update for every loaded system.
245 for (const system
of this.systems
) {