]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/node_collection.js
bb66a8db67c7f1e06720faeab77bec7328c8c490
3 const Events
= require('events');
6 * Node Collections contain nodes, in order to keep the lists of nodes
7 * that belong to each type.
9 * It has a type which is the class name of the node, and an array of
10 * instances of that class.
13 const NodeCollection
= class NodeCollection
extends Events
{
20 Object
.assign(this, config
);
24 * Creates a node for an entity if it matches, and adds it to the
27 * Returns true if added, false otherwise.
31 if (this.type
.matches(entity
) && !this._entityExists(entity
)) {
33 const node
= new this.type({});
34 const types
= this.type
.types
;
35 const typeNames
= Object
.keys(types
);
39 for (const typeName
of typeNames
) {
40 node
[typeName
] = entity
.getComponent(types
[typeName
]);
43 this.nodes
.push(node
);
44 this.emit('nodeAdded', { node
});
53 * Removes an entity by removing its related node from the list of nodes
55 * returns true if it was removed, false otherwise.
61 const found
= this.nodes
.some((node
, i
) => {
63 if (node
.entity
=== entity
) {
70 this.nodes
.splice(foundIndex
, 1);
71 this.emit('nodeRemoved', { node
});
78 * Checks whether we already have nodes for this entity.
80 _entityExists(entity
) {
84 for (const node
of this.nodes
) {
85 if (node
.entity
=== entity
) {
94 module
.exports
= NodeCollection
;