Browse Source

Add fading in and out to scene manager

Thomas Dy 11 years ago
parent
commit
865d8357c0
3 changed files with 76 additions and 18 deletions
  1. 71 18
      scripts/game.js
  2. 1 0
      scripts/games/safari.js
  3. 4 0
      scripts/util.js

+ 71 - 18
scripts/game.js

@@ -49,7 +49,7 @@ var Game = {
     });
 
     Game.sceneManager.push(SplashScreen);
-    Q.allResolved([fontPromise, soundPromise]).done(function(s, f) {
+    Q.allResolved([fontPromise, soundPromise]).done(function() {
       Game.sceneManager.pop();
       Game.sceneManager.push(MainMenu);
     });
@@ -61,33 +61,82 @@ var Game = {
     });
   },
   pause: function() {
-    Game.sceneManager.push(PauseScreen);
+    Game.sceneManager.push(PauseScreen, 0);
   },
   sceneManager: {
     sceneStack: [],
     currentScene: null,
+    promise: Q(),
     update: function(delta) {
       if(this.currentScene) this.currentScene._update(delta);
     },
     draw: function() {
       if(this.currentScene) this.currentScene._draw();
+      this.drawFade();
     },
-    push: function(scene) {
-      if(this.currentScene) this.currentScene.pause();
-      this.sceneStack.push(scene);
-      if(!scene.hasTicker) scene._init();
-      scene.load();
-      this.currentScene = scene;
+    push: function(scene, outDuration, inDuration) {
+      outDuration = (outDuration == undefined) ? 0.5 : outDuration;
+      inDuration = (inDuration == undefined) ? outDuration : inDuration;
+      var self = this;
+      var promise = this.promise;
+      if(this.currentScene) {
+        console.log('hi');
+        this.currentScene.pause();
+        promise = this.fadeOut(outDuration);
+      }
+      promise.then(function() {
+        self.sceneStack.push(scene);
+        if(!scene.hasTicker) scene._init();
+        scene.load();
+        self.currentScene = scene;
+        return self.fadeIn(inDuration);
+      });
     },
-    pop: function() {
-      var scene = this.sceneStack.pop();
-      scene.unload();
-      if(this.sceneStack.length > 0) {
-        this.currentScene = this.sceneStack[this.sceneStack.length-1];
-        this.currentScene.resume();
+    pop: function(count, outDuration, inDuration) {
+      outDuration = (outDuration == undefined) ? 0.5 : outDuration;
+      inDuration = (inDuration == undefined) ? outDuration : inDuration;
+      count = count || 1;
+      var scenes = [];
+      for(var i = 0; i < count; ++i) {
+        scenes.push(this.sceneStack.pop());
       }
-      else {
-        this.currentScene = null;
+      var promise = scenes.reduce(function(acc, scene) {
+         return Q.when(acc, scene.unload());
+      }, Q());
+      var self = this;
+      Q.all([this.fadeOut(outDuration), promise]).then(function() {
+        if(self.sceneStack.length > 0) {
+          self.currentScene = self.sceneStack[self.sceneStack.length-1];
+          self.currentScene.resume();
+          self.fadeIn(inDuration);
+        }
+        else {
+          self.currentScene = null;
+        }
+      });
+    },
+    fadeIn: function(duration) {
+      var self = this
+      this.promise = this.promise.then(function() {
+        self.tween = tweenFrom(1).to(0, duration).animate();
+        return self.tween.promise;
+      });
+      return this.promise;
+    },
+    fadeOut: function(duration) {
+      var self = this;
+      this.promise = this.promise.then(function() {
+        self.tween = tweenFrom(0).to(1, duration).animate();
+        return self.tween.promise;
+      });
+      return this.promise;
+    },
+    drawFade: function() {
+      if(this.tween && this.promise.isPending()) {
+        Game.context.globalAlpha = this.tween.value;
+        Game.context.fillStyle = 'black';
+        Game.context.fillRect(0, 0, Game.canvas.width, Game.canvas.height);
+        Game.context.globalAlpha = 1;
       }
     }
   }
@@ -114,6 +163,8 @@ Scene.prototype.tween = function(from) {
   return tween;
 };
 
+Scene.prototype.update = function() {};
+Scene.prototype.draw = function() {};
 Scene.prototype.load = function() {};
 Scene.prototype.unload = function() {};
 Scene.prototype.resume = function() {};
@@ -257,11 +308,13 @@ var PauseScreen = new Scene();
       case 81:
       case 113:
       case 27:
-        Game.sceneManager.pop();
+        Game.sceneManager.pop(2);
+        soundManager.play('back');
+        break;
       case 80:
       case 112:
       case 32:
-        Game.sceneManager.pop();
+        Game.sceneManager.pop(1, 0);
         soundManager.play('back');
     }
   };

+ 1 - 0
scripts/games/safari.js

@@ -485,6 +485,7 @@ function SoundSafari(beatInfo) {
     soundPromises.push(createSound('get', 'sound/get.mp3'));
 
     this.textAlphaTween = this.tween(0)
+      .wait(0.5)
       .to(1, 1.5)
       .wait(1)
       .waitFor(Q.all(soundPromises))

+ 4 - 0
scripts/util.js

@@ -218,3 +218,7 @@ DeferredTween.prototype.update = function(dt) {
   this.tween.update(dt);
   this.value = this.tween.value;
 };
+
+ function tweenFrom(start) {
+  return new DeferredTween(start);
+};