Commits

Anonymous committed 3e31398

add objective state handler for space2d and space3d for detecting failure/success

Comments (0)

Files changed (5)

         editor2d.loadWorld(level);
     }
 
+    var objectiveHandler = function(success) {
+        console.log("objective success?:",success);
+    }
+
     var levelLoader = {load:load, restart:restart};
     editor2d.setLevelLoader( levelLoader );
+    space2d.setObjectiveDelegate( objectiveHandler );
+    space3d.setObjectiveDelegate( objectiveHandler );
 
     var go = function() {
         space3d.onWarpHoleState( space2d.setWarpHoleState );
         return ground;
     }
 
+    var createWall = function( world, centerx, centery, horizontal, vertical ) {
+        var bd_ground = new Box2D.b2BodyDef();
+        bd_ground.set_position( new Box2D.b2Vec2(centerx,centery) );
+        var ground = world.CreateBody(bd_ground);
+        ground.userData = {
+            attributes: {
+                isWall: true,
+                x: centerx,
+                y: centery,
+                horizontal: horizontal,
+                vertical: vertical,
+            }
+        }
+        var shape0 = new Box2D.b2EdgeShape();
+        shape0.Set(new Box2D.b2Vec2(horizontal/2, vertical/2), new Box2D.b2Vec2(-horizontal/2, -vertical/2));
+        ground.CreateFixture(shape0, 0.0);
+        return ground;
+    }
+
     var createBall = function(world, x, y, userData) {
         var shape = new Box2D.b2CircleShape();
         shape.set_m_radius(0.25); //a 0.5x0.5 rectangle
         createDoor: createDoor,
         createBall: createBall,
         createGround: createGround,
+        createWall: createWall,
     }
 });
         var e_pairBit = 0x0008;
         var e_centerOfMassBit = 0x0010;
 
-        debugDraw.SetFlags(e_shapeBit | e_centerOfMassBit | e_aabbBit | e_shapeBit);
+        debugDraw.SetFlags(e_shapeBit | e_centerOfMassBit | e_shapeBit);
         return debugDraw;
     }
 
         bodies.createGround(world, centerx, centery, x1, y1, x2, y2);
     }
 
+    var createWall = function(centerx, centery, horizontal, vertical) {
+        bodies.createWall(world, centerx, centery, horizontal, vertical);
+    }
+
     var world = undefined;
     var newWorld = function() {
         world = worlds.createWorld();
         return world;
     }
 
+
     var loadWorld = function(level) {
         world = newWorld();
+        createWall(0,-6.7,18,0);
+        createWall(0,6.7,18,0);
+        createWall(9,0,0,13.4);
+        createWall(-9,0,0,-13.4);
         level.structure.forEach( function(object) {
             if( object.isBall ) {
                 createBall(object.ballType, object.x, object.y);
             else if( object.isGround ) {
                 createGround(object.x, object.y, object.x1, object.y1, object.x2, object.y2);
             }
+            else if( object.isWall ) {
+                createWall(object.x, object.y, object.horizontal, object.vertical);
+            }
             else if( object.isWarpHole ) {
                 createWarpHole( object.warpHoleId, object.x, object.y );
             }
     }
 
     var onContact = function(a,b) {
-        if( fixtureToUserData(a).attributes.isWarpHole ) {
-            if( fixtureToUserData(b).attributes.isBall ) {
+        if( fixtureToUserData(b).attributes.isBall ) {
+            if( fixtureToUserData(a).attributes.isWarpHole ) {
                 if( isWarpHoleOpen( fixtureToUserData(a).attributes.warpHoleId ) ) {
                     warpBall( {fixture:b, body:b.GetBody(), userData:fixtureToUserData(b)} );
                 }
             }
+            else if( fixtureToUserData(a).attributes.isWall ) {
+                notifyObjective(false);
+            }
         }
-        else if( fixtureToUserData(b).attributes.isWarpHole ) {
-            if( fixtureToUserData(a).attributes.isBall ) {
+        else if( fixtureToUserData(a).attributes.isBall ) {
+            if( fixtureToUserData(b).attributes.isWarpHole ) {
                 if( isWarpHoleOpen( fixtureToUserData(b).attributes.warpHoleId ) ) {
                     warpBall( {fixture:a, body:a.GetBody(), userData:fixtureToUserData(a)} );
                 }
             }
+            else if( fixtureToUserData(b).attributes.isWall ) {
+                notifyObjective(false);
+            }
         }
     }
 
         isPaused = state;
     }
 
+    var notifyObjective = undefined;
+
     return {
         tick:tick,
         setWarpHoleState: setWarpHoleState,
         setPause: setPause,
         getWorld: function() { return world; },
         createBall: createBall,
-        
+        setObjectiveDelegate: function(delegate) { notifyObjective = delegate; },
     }
 });
         var position = { x : 0, y: 0, z:200 };
         var target = { x : -15+5*(ballPosition++), y: -14, z:100 };
         var tween = new TWEEN.Tween(position).to(target, 500);
-        ball.position.set(position.x,position.y,position.z);
         tween.onUpdate( function(){
             ball.position.set( position.x, position.y, position.z );
         });
             state.push(ballType);
             updateObjective();
         });
+        ball.position.set(position.x,position.y,position.z);
         tween.start();
     }
 
     var updateObjective = function() {
         console.log( objective[objectiveIndex], state[objectiveIndex] );
         if( objective[objectiveIndex] === state[objectiveIndex] ) {
-            console.log("!");
             objectiveIndex++;
             if( objectiveIndex === objective.length ) {
-                notifyObjectiveComplete();
+                notifyObjective(true);
             }
         }
         else {
-            notifyObjectiveFailed();
+            notifyObjective(false);
         }
     }
 
-    var notifyObjectiveComplete = function() {
-        console.log("objective complete");
-    }
-
-    var notifyObjectiveFailed = function() {
-        console.log("objective failed.");
-    }
+    var notifyObjective = undefined;
 
     return {
         tick:update,
         onWarpHoleState: function(callback) { notifyWarpHoleState = callback; },
         addBall: addBall,
         loadWorld: loadWorld,
+        setObjectiveDelegate: function(delegate) { notifyObjective = delegate; },
     }
 });