Commits

Anonymous committed 68f0aac

implement objective success/failure detection for ball patterns

Comments (0)

Files changed (4)

         var level = JSON.parse(currentLevelData);
         space2d.loadWorld(level);
         space3d.loadWorld(level);
+        editor2d.loadWorld(level);
     }
 
     var levelLoader = {load:load, restart:restart};
         return json;
     }
 
+    var loadWorld = function(level) {
+        document.getElementById("objective").value = level.objective.join(",");
+    }
+
     var writeConsole= function(content) {
         top.consoleRef=window.open('','myconsole',
                 'width=350,height=250'
     return {
         draw: draw,
         isActive: function() { return isActive; },
-        setLevelLoader: function(delegate) { levelLoader = delegate; console.log(levelLoader); },
+        setLevelLoader: function(delegate) { levelLoader = delegate; },
+        loadWorld: loadWorld,
     }
 });
 
 define([],function() {
 
-    var level1 = '{"structure":[{"isBall":true,"ballType":"red","x":-7.961538791656494,"y":2.500000238418579},{"isBall":true,"ballType":"green","x":8.115384101867676,"y":-2.346153736114502},{"warpHoleId":2,"isWarpHole":true,"isOpen":false,"x":-7.730769157409668,"y":-5.730769157409668},{"warpHoleId":8,"isWarpHole":true,"isOpen":false,"x":-0.038461796939373016,"y":0.8846151828765869},{"isGround":true,"x":-0.05769229307770729,"y":0.4615384340286255,"x1":8.134615384615383,"y1":-1.5384615384615385,"x2":-8.134615384615383,"y2":1.5384615384615385},{"isGround":true,"x":0.26923078298568726,"y":-4.480769157409668,"x1":-8.192307692307693,"y1":-1.6730769230769234,"x2":8.192307692307693,"y2":1.6730769230769234}],"objective":["r","g"]}';
+    var level1 = '{"structure":[{"isBall":true,"ballType":"red","x":-7.961538791656494,"y":2.500000238418579},{"isBall":true,"ballType":"green","x":8.115384101867676,"y":-2.346153736114502},{"warpHoleId":2,"isWarpHole":true,"isOpen":false,"x":-7.730769157409668,"y":-5.730769157409668},{"warpHoleId":8,"isWarpHole":true,"isOpen":false,"x":-0.038461796939373016,"y":0.8846151828765869},{"isGround":true,"x":-0.05769229307770729,"y":0.4615384340286255,"x1":8.134615384615383,"y1":-1.5384615384615385,"x2":-8.134615384615383,"y2":1.5384615384615385},{"isGround":true,"x":0.26923078298568726,"y":-4.480769157409668,"x1":-8.192307692307693,"y1":-1.6730769230769234,"x2":8.192307692307693,"y2":1.6730769230769234}],"objective":["red","green"]}';
 
     return {
         level1:level1,
         tween.onUpdate( function(){
             ball.position.set( position.x, position.y, position.z );
         });
+        tween.onComplete( function() {
+            state.push(ballType);
+            updateObjective();
+        });
         tween.start();
     }
 
-    var objective = [];
-    var setObjective = function(string) {
-        console.log(objective);
+    var state = [];
+    var objectiveIndex = 0;
+    var objective = undefined;
+    var loadWorld = function(level) {
+        state.length = 0;
+        objective = level.objective;
+    }
+
+    var updateObjective = function() {
+        console.log( objective[objectiveIndex], state[objectiveIndex] );
+        if( objective[objectiveIndex] === state[objectiveIndex] ) {
+            console.log("!");
+            objectiveIndex++;
+            if( objectiveIndex === objective.length ) {
+                notifyObjectiveComplete();
+            }
+        }
+        else {
+            notifyObjectiveFailed();
+        }
+    }
+
+    var notifyObjectiveComplete = function() {
+        console.log("objective complete");
+    }
+
+    var notifyObjectiveFailed = function() {
+        console.log("objective failed.");
     }
 
     return {
         tick:update,
         onWarpHoleState: function(callback) { notifyWarpHoleState = callback; },
         addBall: addBall,
-        setObjective: setObjective,
-        loadWorld: function() {},
+        loadWorld: loadWorld,
     }
 });