]> git.r.bdr.sh - rbdr/serpentity/blobdiff - lib/serpentity/serpentity.js
Updates github url in bower
[rbdr/serpentity] / lib / serpentity / serpentity.js
index d1c1cb0cbfc3f70f0a89cbef249c3884020fe36a..ed1a9dffb020503e6cb5cc6636d1d4d8b0ae635e 100644 (file)
@@ -1,4 +1,6 @@
-require("neon");
+if (typeof require !== "undefined") {
+    require("neon");
+}
 
 /*
 Serpentity is a simple entity framework inspired by Ash.
@@ -11,10 +13,11 @@ Usage:
 
     var engine = Serpentity();
 
-Add entities or systems:
+Add entities or systems, systems are added with a priority (the smaller
+the number, the earlier it will be called):
 
     engine.addEntity(entityFactory());
-    engine.addSystem(new GameSystem());
+    engine.addSystem(new GameSystem(), priority);
 
 Update all systems:
 
@@ -47,7 +50,7 @@ eventually be consumed by "Systems"
 
 You can add components to entities by using the add method:
 
-    entity.add(new PositionComponent());
+    entity.addComponent(new PositionComponent());
 
 
 Systems can refer to entities by requesting nodes.
@@ -97,8 +100,9 @@ Just run `engine.update(dt)` in your game loop :D
 Class("Serpentity")({
     prototype : {
         systems : null,
-        nodeCollections : null,
         entities : null,
+        _nodeCollections : null,
+        _nodeCollectionKeys : null,
 
         init : function init(config) {
             var property;
@@ -107,7 +111,8 @@ Class("Serpentity")({
 
             this.systems = [];
             this.entities = [];
-            this.nodeCollections = {};
+            this._nodeCollections = [];
+            this._nodeCollectionKeys = [];
 
             for (property in config) {
                 if (config.hasOwnProperty(property)) {
@@ -122,11 +127,31 @@ Class("Serpentity")({
          *
          * returns true if added succesfully, false if already added
          */
-        addSystem : function addSystem(system) {
+        addSystem : function addSystem(system, priority) {
+            var lastIndex, found;
+
             if (this.systems.indexOf(system) >= 0) {
                 return false;
             }
-            this.systems.push(system);
+
+            system.priority = priority;
+
+            found = false;
+            lastIndex = 0;
+
+            this.systems.some(function findPriority(existingSystem, i) {
+                lastIndex = i;
+                if (existingSystem.priority >= system.priority) {
+                    found = true;
+                    return true;
+                }
+            });
+
+            if (!found) {
+              lastIndex += 1
+            }
+
+            this.systems.splice(lastIndex, 0, system);
             system.added(this);
             return true;
         },
@@ -156,18 +181,15 @@ Class("Serpentity")({
          * returns true if added, false if already there
          */
         addEntity : function addEntity(entity) {
-            var property;
-
             if (this.entities.indexOf(entity) >= 0) {
                 return false;
             }
             this.entities.push(entity);
 
-            for (property in this.nodeCollections) {
-                if (this.nodeCollections.hasOwnProperty(property)) {
-                    this.nodeCollections[property].add(entity);
-                }
-            }
+            this._nodeCollections.forEach(function (collection) {
+                collection.add(entity);
+            });
+
             return true;
         },
 
@@ -181,11 +203,9 @@ Class("Serpentity")({
 
             position = this.entities.indexOf(entity);
             if (position >= 0) {
-                for (property in this.nodeCollections) {
-                    if (this.nodeCollections.hasOwnProperty(property)) {
-                        this.nodeCollections[property].remove(entity);
-                    }
-                }
+                this._nodeCollections.forEach(function (collection) {
+                    collection.remove(entity);
+                });
 
                 this.entities.splice(position, 1);
                 return true;
@@ -199,16 +219,20 @@ Class("Serpentity")({
          * applicable entity.
          */
          getNodes : function getNodes(nodeType) {
-            var nodeCollection;
+            var position, nodeCollection;
+
+            position = this._nodeCollectionKeys.indexOf(nodeType);
 
-            if (this.nodeCollections.hasOwnProperty(nodeType)) {
-                return this.nodeCollections[nodeType].nodes;
+            if (position >= 0) {
+                return this._nodeCollections[position].nodes;
             }
 
             nodeCollection = new Serpentity.NodeCollection({
                 type : nodeType,
             });
-            this.nodeCollections[nodeType] = nodeCollection;
+
+            this._nodeCollectionKeys.push(nodeType);
+            this._nodeCollections.push(nodeCollection);
 
             this.entities.forEach(function (entity) {
                 nodeCollection.add(entity);
@@ -228,8 +252,10 @@ Class("Serpentity")({
     }
 });
 
-require("./component.js");
-require("./entity.js");
-require("./node.js");
-require("./node_collection.js");
-require("./system.js");
+if (typeof require !== "undefined") {
+    require("./component.js");
+    require("./entity.js");
+    require("./node.js");
+    require("./node_collection.js");
+    require("./system.js");
+}