]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/node_collection.js
2 * Node Collections contain nodes, in order to keep the lists of nodes
3 * that belong to each type.
5 * It has a type which is the class name of the node, and an array of
6 * instances of that class.
9 export class NodeCollection
extends EventTarget
{
18 Object
.assign(this, config
);
22 * Creates a node for an entity if it matches, and adds it to the
25 * Returns true if added, false otherwise.
29 if (this.type
.matches(entity
) && !this._entityExists(entity
)) {
31 const node
= new this.type({});
32 const types
= this.type
.types
;
33 const typeNames
= Object
.keys(types
);
37 for (const typeName
of typeNames
) {
38 node
[typeName
] = entity
.getComponent(types
[typeName
]);
41 this.nodes
.push(node
);
42 const event
= new Event('nodeAdded');
44 this.dispatchEvent(event
);
53 * Removes an entity by removing its related node from the list of nodes
55 * returns true if it was removed, false otherwise.
62 const found
= this.nodes
.some((node
, i
) => {
64 if (node
.entity
=== entity
) {
72 this.nodes
.splice(foundIndex
, 1);
73 const event
= new Event('nodeRemoved');
74 event
.node
= foundNode
;
75 this.dispatchEvent(event
);
82 * Checks whether we already have nodes for this entity.
84 _entityExists(entity
) {
86 return this.nodes
.some((node
) => node
.entity
=== entity
);
92 * Make the node collection iterable without returning the array directly
94 NodeCollection
.prototype[Symbol
.iterator
] = function * () {