Browse Source

Use state machine

Thomas Dy 11 years ago
parent
commit
6c1a9b26fb
1 changed files with 34 additions and 24 deletions
  1. 34 24
      scripts/game.js

+ 34 - 24
scripts/game.js

@@ -45,6 +45,21 @@ var SoundSafari = function(canvas, beatInfo, pSoundManager) {
   var DOT_SIZE = 3;
   var DOT_DISTANCE = 4 * 5;
 
+  var state = StateMachine.create({
+    initial: 'waiting',
+    events: [
+      {name: 'play', from: 'waiting', to: 'playing'},
+      {name: 'point', from: 'playing', to: 'waiting'}
+    ],
+    callbacks: {
+      onpoint: function() {
+        ++points;
+        soundManager.play('get');
+        switchBeat(points % beats.length);
+      }
+    }
+  });
+
   var starColors = [
     'maroon',
     'red',
@@ -64,7 +79,6 @@ var SoundSafari = function(canvas, beatInfo, pSoundManager) {
   var beats = [];
   var gameTime = 0;
   var currentBeats = [];
-  var gotPoint = false;
 
   var switchBeat = function(index) {
     var currentBeat = beats[index];
@@ -262,18 +276,22 @@ var SoundSafari = function(canvas, beatInfo, pSoundManager) {
   };
 
   Star.prototype.onUpdate = function() {
-    if(gotPoint) {
-      this.randomize();
-    }
     if(this.y > canvas.height + DOT_DISTANCE * 2) {
       this.y -= canvas.height + DOT_DISTANCE * 4;
+      this.randomize();
+    }
+
+    if(state.current == 'playing') {
+      var newAlpha = 1.5 * activeTarget.spectrum[this.type];
+      var a = 0.9;
+      if(newAlpha > this.alpha) {
+        a = 0.9;
+      }
+      this.alpha = Math.min(1, activeTarget.shimmerFactor * (this.alpha * (1-a) + newAlpha * a));
     }
-    var newAlpha = 1.5 * activeTarget.spectrum[this.type];
-    var a = 0.001;
-    if(newAlpha > this.alpha) {
-      a = 0.9;
+    else if(state.current == 'waiting') {
+      this.alpha = this.alpha * 0.95;
     }
-    this.alpha = Math.min(1, activeTarget.shimmerFactor * (this.alpha * (1-a) + newAlpha * a));
   };
 
   function Dot(x, y) {
@@ -330,12 +348,8 @@ var SoundSafari = function(canvas, beatInfo, pSoundManager) {
       this.rippleCounter = 0;
     }
 
-    if(gotPoint) {
-      gotPoint = false;
-    }
-
     if(colliding(player, this)) {
-      gotPoint = true;
+      state.point();
       this.beat.sound.setPan(0);
       this.beat.sound.setVolume(100);
       this.shouldDelete = true;
@@ -438,9 +452,13 @@ var SoundSafari = function(canvas, beatInfo, pSoundManager) {
       }
     });
 
-
     currentBeats.map(function(beat) {
-      if(beat.repeat) beat.sound.play();
+      if(beat.repeat) {
+        if(beat == activeTarget.beat && state.current == 'waiting') {
+          state.play();
+        }
+        beat.sound.play();
+      }
     });
 
     if(37 in keysDown) {
@@ -461,14 +479,6 @@ var SoundSafari = function(canvas, beatInfo, pSoundManager) {
     }
     meter.onUpdate(dt);
 
-    if(gotPoint) {
-      ++points;
-      soundManager.play('get');
-      if(beats.length > 0) {
-        switchBeat(points % beats.length);
-      }
-    }
-
     gameTime += delta;
   };