]>
git.r.bdr.sh - rbdr/serpentity/blob - lib/serpentity/node_collection.js
7bac90f5b82a463f59cb93bff73ead3cf71c1472
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.
8 Class(Serpentity
, "NodeCollection")({
13 init : function init(config
) {
16 config
= config
|| {};
20 for (property
in config
) {
21 if (config
.hasOwnProperty(property
)) {
22 this[property
] = config
[property
];
28 * Creates a node for an entity if it matches, and adds it to the
31 * Returns true if added, false otherwise.
33 add : function add(entity
) {
34 var node
, types
, property
;
36 if (this.type
.matches(entity
) && !this._entityExists(entity
)) {
37 node
= new this.type({});
41 types
= this.type
.types
;
43 for (property
in types
) {
44 if (types
.hasOwnProperty(property
)) {
45 node
[property
] = entity
.components
[types
[property
]]
49 this.nodes
.push(node
);
58 * Removes an entity by removing its related node from the list of nodes
60 * returns true if it was removed, false otherwise.
62 remove : function (entity
) {
65 this.nodes
.forEach(function (node
, i
) {
66 if (node
.entity
=== entity
) {
72 this.nodes
.splice(found
, 1);
80 * Checks whether we already have nodes for this entity.
82 _entityExists : function entityExists(entity
) {
85 this.nodes
.forEach(function (node
) {
86 if (node
.entity
=== entity
) {