]> git.r.bdr.sh - rbdr/serpentity/blobdiff - lib/serpentity/serpentity.js
Adds priorities
[rbdr/serpentity] / lib / serpentity / serpentity.js
index d1c1cb0cbfc3f70f0a89cbef249c3884020fe36a..d86c47e49fcbf112b1255682561f2d9a8278d19a 100644 (file)
@@ -11,10 +11,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:
 
@@ -122,11 +123,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;
         },