]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/node_collection.js
f5b1794a8a88f8fdf79255581a4871c9354f0d5d
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
)) {
31 let node
, types
, property
;
33 node
= new this.type({});
35 types
= this.type
.types
;
37 for (property
in types
) {
38 if (types
.hasOwnProperty(property
)) {
39 node
[property
] = entity
.getComponent(types
[property
]);
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.
60 this.nodes
.forEach(function (node
, i
) {
61 if (node
.entity
=== entity
) {
67 this.nodes
.splice(found
, 1);
75 * Checks whether we already have nodes for this entity.
77 _entityExists (entity
) {
81 for (node
of this.nodes
) {
82 if (node
.entity
=== entity
) {
91 if (typeof module
!== 'undefined' && this.module
!== module
) {
92 module
.exports
= NodeCollection
;
94 Serpentity
.NodeCollection
= NodeCollection
;