]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/node_collection.js
7c519912364c3a7559b8f0131a27ce90db66f909
3 /* global Serpentity */
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 let NodeCollection
= class NodeCollection
{
15 constructor (config
) {
19 Object
.assign(this, config
|| {});
23 * Creates a node for an entity if it matches, and adds it to the
26 * Returns true if added, false otherwise.
30 if (this.type
.matches(entity
) && !this._entityExists(entity
)) {
32 let node
= new this.type({});
33 let types
= this.type
.types
;
37 for (let typeName
in types
) {
38 if (types
.hasOwnProperty(typeName
)) {
39 node
[typeName
] = entity
.getComponent(types
[typeName
]);
43 this.nodes
.push(node
);
52 * Removes an entity by removing its related node from the list of nodes
54 * returns true if it was removed, false otherwise.
59 this.nodes
.forEach(function (node
, i
) {
60 if (node
.entity
=== entity
) {
66 this.nodes
.splice(found
, 1);
74 * Checks whether we already have nodes for this entity.
76 _entityExists (entity
) {
79 for (let node
of this.nodes
) {
80 if (node
.entity
=== entity
) {
89 if (typeof module
!== 'undefined' && this.module
!== module
) {
90 module
.exports
= NodeCollection
;
92 Serpentity
.NodeCollection
= NodeCollection
;