]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity.js
c5c3850c9946d468f65976f55005a8a7a181d626
1 export { Component
} from './serpentity/component.js';
2 export { Entity
} from './serpentity/entity.js';
3 export { Node
} from './serpentity/node.js';
4 export { System
} from './serpentity/system.js';
6 import { NodeCollection
} from './serpentity/node_collection.js';
9 * Serpentity is the main engine class
11 export default class Serpentity
{
17 this._nodeCollections
= [];
18 this._nodeCollectionKeys
= [];
20 Object
.assign(this, config
);
24 * Adds a system to the engine, so its update method will be called
25 * with the others. Triggers added hook.
27 * returns true if added succesfully, false if already added
29 addSystem(system
, priority
) {
31 if (this.systems
.indexOf(system
) >= 0) {
35 system
.priority
= priority
;
39 const found
= this.systems
.some((existingSystem
, i
) => {
42 if (existingSystem
.priority
>= system
.priority
) {
51 this.systems
.splice(lastIndex
, 0, system
);
57 * Removes a system from the engine, so its update method will no
58 * longer will be called. Triggers the removed hook.
60 * returns true if removed succesfully, false if already added
62 removeSystem(system
) {
64 const position
= this.systems
.indexOf(system
);
66 this.systems
[position
].removed(this);
67 this.systems
.splice(position
, 1);
75 * Adds an entity to the engine, adds to existing node collections
77 * returns true if added, false if already there
81 if (this.entities
.indexOf(entity
) >= 0) {
85 this.entities
.push(entity
);
87 for (const collection
of this._nodeCollections
) {
88 collection
.add(entity
);
95 * Removes entity from system, removing from all node collections
97 * returns true if removed, false if not present
99 removeEntity(entity
) {
101 const position
= this.entities
.indexOf(entity
);
103 for (const collection
of this._nodeCollections
) {
104 collection
.remove(entity
);
107 this.entities
.splice(position
, 1);
115 * Given a Node Class, retrieves a list of all the nodes for each
120 const position
= this._nodeCollectionKeys
.indexOf(nodeType
);
123 return this._nodeCollections
[position
];
126 const nodeCollection
= new NodeCollection({
130 this._nodeCollectionKeys
.push(nodeType
);
131 this._nodeCollections
.push(nodeCollection
);
133 for (const entity
of this.entities
) {
134 nodeCollection
.add(entity
);
137 return nodeCollection
;
141 * Calls update for every loaded system.
145 for (const system
of this.systems
) {