]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/node_collection.js
8da41da0360e43085596c10ac93633ffe831662f
4 * Node Collections contain nodes, in order to keep the lists of nodes
5 * that belong to each type.
7 * It has a type which is the class name of the node, and an array of
8 * instances of that class.
11 const NodeCollection
= class NodeCollection
{
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
);
50 * Removes an entity by removing its related node from the list of nodes
52 * returns true if it was removed, false otherwise.
58 const found
= this.nodes
.some((node
, i
) => {
60 if (node
.entity
=== entity
) {
67 this.nodes
.splice(foundIndex
, 1);
74 * Checks whether we already have nodes for this entity.
76 _entityExists(entity
) {
80 for (const node
of this.nodes
) {
81 if (node
.entity
=== entity
) {
90 module
.exports
= NodeCollection
;