Browse Source

Add endgame for safari

Thomas Dy 11 years ago
parent
commit
956773b9ed
2 changed files with 27 additions and 9 deletions
  1. 2 2
      scripts/game.js
  2. 25 7
      scripts/games/safari.js

+ 2 - 2
scripts/game.js

@@ -80,7 +80,6 @@ var Game = {
       var self = this;
       var promise = this.promise;
       if(this.currentScene) {
-        console.log('hi');
         this.currentScene.pause();
         promise = this.fadeOut(outDuration);
       }
@@ -100,6 +99,7 @@ var Game = {
       for(var i = 0; i < count; ++i) {
         scenes.push(this.sceneStack.pop());
       }
+
       var promise = scenes.reduce(function(acc, scene) {
          return Q.when(acc, scene.unload());
       }, Q());
@@ -216,7 +216,7 @@ var MainMenu = new Scene();
           {url: 'sound/b5.mp3', duration: 4000}
         ];
 
-        Game.sceneManager.push(new SoundSafari(beats));
+        Game.sceneManager.push(new SoundSafari(beats, true));
       }
     },
     notYetDone,

+ 25 - 7
scripts/games/safari.js

@@ -1,4 +1,4 @@
-function SoundSafari(beatInfo) {
+function SoundSafari(beatInfo, endless) {
   var canvas = Game.canvas;
   var ctx = Game.context;
 
@@ -36,16 +36,26 @@ function SoundSafari(beatInfo) {
     events: [
       {name: 'ready', from: 'loading', to: 'waiting'},
       {name: 'play', from: 'waiting', to: 'playing'},
-      {name: 'point', from: 'playing', to: 'waiting'}
+      {name: 'point', from: 'playing', to: 'waiting'},
+      {name: 'win', from: 'waiting', to: 'ending'},
+      {name: 'end', from: 'ending', to: 'done'}
     ],
     callbacks: {
       onready: function() {
         reset();
       },
+      onend: function() {
+        Game.sceneManager.pop();
+      },
       onpoint: function() {
         ++points;
         soundManager.play('get');
-        switchBeat(points % beats.length);
+        if(!endless && points >= beats.length) {
+          state.win();
+        }
+        else {
+          switchBeat(points % beats.length);
+        }
       }
     }
   });
@@ -223,7 +233,7 @@ function SoundSafari(beatInfo) {
         var a = 0.9;
         this.alpha = Math.min(1, activeTarget.shimmerFactor * (this.alpha * (1-a) + newAlpha * a));
       }
-      else if(state.current == 'waiting') {
+      else if(state.current == 'waiting' || state.current == 'ending') {
         this.alpha = this.alpha * 0.95;
       }
     };
@@ -329,7 +339,7 @@ function SoundSafari(beatInfo) {
   function switchBeat(index) {
     var currentBeat = beats[index];
     currentBeats.push(currentBeat);
-    if(currentBeats.length > 4) {
+    if(currentBeats.length > Math.min(4, beats.length)) {
       currentBeats.splice(0, 1);
     }
 
@@ -378,14 +388,22 @@ function SoundSafari(beatInfo) {
     if(state.current == 'loading') return;
     var delta = dt * 1000;
 
-    currentBeats.map(function(beat) {
+    for(var i = currentBeats.length-1; i >= 0; --i) {
+      var beat = currentBeats[i];
       if(gameTime + delta > nextPlayTime(beat.info.duration)) {
+        if(state.current == 'ending' && i == 0) {
+          currentBeats.splice(0, 1);
+          continue;
+        }
         if(beat == activeTarget.beat && state.current == 'waiting') {
           state.play();
         }
         beat.sound.play();
       }
-    });
+    }
+    if(currentBeats.length == 0 && state.can('end')) {
+      state.end();
+    }
 
     if(37 in Game.keysDown) {
       player.x -= player.dx * dt;