]> git.r.bdr.sh - rbdr/serpentity/commitdiff
Adds priorities
authorBen Beltran <redacted>
Mon, 11 Aug 2014 11:56:50 +0000 (06:56 -0500)
committerBen Beltran <redacted>
Mon, 11 Aug 2014 11:56:50 +0000 (06:56 -0500)
README.md
bin/test
lib/serpentity/serpentity.js

index f90083bf9acb0d7b0256353f3c06bcdc46a1129b..3d2ebb0775533f283aef27c447f7a3c7f42e3049 100644 (file)
--- a/README.md
+++ b/README.md
@@ -10,10 +10,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:
 
@@ -94,7 +95,6 @@ Just run `engine.update(dt)` in your game loop :D
 
 ## TO-DO
 
-* Priorities
 * Removing components
 * Implement the ashteroids demo (Serpentoids)
 * Actually check performance
index 55e0aa2f2e9f7ca3e64fb15338714da1ed35d442..222a65316d32f3595010628182efb8c619d1ff4c 100755 (executable)
--- a/bin/test
+++ b/bin/test
@@ -39,7 +39,52 @@ Class("TestSystem").inherits(Serpentity.System)({
     }
 });
 var testSystem = new TestSystem();
-console.log("TestSystem: " + "CREATE OK".green)
+
+Class("LowProTestSystem").inherits(Serpentity.System)({
+    prototype : {
+        added : function added(engine) {
+            this.testNodes = engine.getNodes(TestNode);
+            console.log("System added callback: " + "EXEC OK".green);
+        },
+
+        removed : function removed(engine) {
+            this.testNodes = null;
+            console.log("System removed callback: " + "EXEC OK".green);
+        },
+
+        update : function update(dt) {
+            this.testNodes.forEach(function (node) {
+                console.log("Running Low Priority Node: " + (node.test.testMessage === "test" ? "SYSTEM OK".green : "FAIL".RED));
+            });
+            console.log("System update callback: " + "EXEC OK".green);
+        }
+    }
+});
+var lowProTestSystem = new LowProTestSystem();
+console.log("LowProTestSystem: " + "CREATE OK".green)
+
+Class("MidProTestSystem").inherits(Serpentity.System)({
+    prototype : {
+        added : function added(engine) {
+            this.testNodes = engine.getNodes(TestNode);
+            console.log("System added callback: " + "EXEC OK".green);
+        },
+
+        removed : function removed(engine) {
+            this.testNodes = null;
+            console.log("System removed callback: " + "EXEC OK".green);
+        },
+
+        update : function update(dt) {
+            this.testNodes.forEach(function (node) {
+                console.log("Running Mid Priority Node: " + (node.test.testMessage === "test" ? "SYSTEM OK".green : "FAIL".RED));
+            });
+            console.log("System update callback: " + "EXEC OK".green);
+        }
+    }
+});
+var midProTestSystem = new MidProTestSystem();
+console.log("MidProTestSystem: " + "CREATE OK".green)
 
 
 Class("TestComponent").inherits(Serpentity.Component)({
@@ -62,7 +107,7 @@ console.log("\n## Adding system to the engine".bold.black)
 var engine = new Serpentity();
 console.log("engine: " + "CREATE OK".green)
 
-engine.addSystem(testSystem);
+engine.addSystem(testSystem, 0);
 
 console.log("\n## Running update without any entities".bold.black)
 engine.update(10);
@@ -73,10 +118,18 @@ entity.add(new TestComponent());
 engine.addEntity(entity);
 engine.update(10);
 
+console.log("\n## Adding Low Priority System".bold.black)
+engine.addSystem(lowProTestSystem, 10);
+engine.update(10);
+
+console.log("\n## Adding Mid Priority System".bold.black)
+engine.addSystem(midProTestSystem, 5);
+engine.update(10);
+
 console.log("\n## Removing the system and readding".bold.black)
 engine.removeSystem(testSystem);
 engine.update(10);
-engine.addSystem(testSystem);
+engine.addSystem(testSystem, 0);
 engine.update(10);
 
 console.log("\n## Adding a second entity".bold.black)
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;
         },