Browse Source

Initial code for duck

Thomas Dy 11 years ago
parent
commit
b10015955b
3 changed files with 123 additions and 1 deletions
  1. 1 0
      index.html
  2. 7 1
      scripts/game.js
  3. 115 0
      scripts/games/duck.js

+ 1 - 0
index.html

@@ -36,5 +36,6 @@
 <script type="text/javascript" src="scripts/util.js"></script>
 <script type="text/javascript" src="scripts/game.js"></script>
 <script type="text/javascript" src="scripts/games/safari.js"></script>
+<script type="text/javascript" src="scripts/games/duck.js"></script>
 </body>
 </html>

+ 7 - 1
scripts/game.js

@@ -227,7 +227,13 @@ var MainMenu = new Scene();
         Game.sceneManager.push(new SoundSafari(beats, true));
       }
     },
-    notYetDone,
+    {
+      name: 'sound duck',
+      alt: 'sound cock',
+      start: function() {
+        Game.sceneManager.push(new SoundDuck());
+      }
+    },
     notYetDone,
     notYetDone,
     notYetDone,

+ 115 - 0
scripts/games/duck.js

@@ -0,0 +1,115 @@
+function SoundDuck() {
+  var ctx = Game.context;
+  var canvasHM = Game.canvas.height/2;
+  var canvasWM = Game.canvas.width/2;
+  var debug = false;
+
+  var RADIUS = 8;
+  var FIELD_SIZE = 600;
+  var DOT_SIZE = 3;
+  var DOT_DISTANCE = 4 * 5;
+  var DOT_NUM = FIELD_SIZE / DOT_DISTANCE - 1;
+
+  var box = [
+    {x: 0, y: 0},
+    {x: 0, y: FIELD_SIZE},
+    {x: FIELD_SIZE, y: FIELD_SIZE},
+    {x: FIELD_SIZE, y: 0}
+  ];
+
+  var dots = [];
+  for(var i = 1; i <= DOT_NUM; ++i) {
+    for(var j = 1; j <= DOT_NUM; ++j) {
+      if((i % 2 == 1 && j % 2 == 0) || (i % 4 == 0 && j % 4 != 2) || (i % 4 == 2 && j % 4 != 0)) {
+        dots.push({x: j * DOT_DISTANCE, y: i * DOT_DISTANCE});
+      }
+    }
+  }
+
+  var player = {
+    x: FIELD_SIZE / 2,
+    y: FIELD_SIZE / 2,
+    angle: 0,
+    dx: 100,
+    da: Math.PI
+  };
+
+  var reference = {
+    x: canvasWM,
+    y: canvasHM
+  };
+
+  function translatePoints(point) {
+    var x = point.x - player.x;
+    var y = point.y - player.y;
+    var nx = x * Math.cos(-player.angle) + y * Math.sin(-player.angle);
+    var ny = -x * Math.sin(-player.angle) + y * Math.cos(-player.angle);
+    return {x: nx + reference.x, y: ny + reference.y};
+  }
+
+  this.update = function(dt) {
+    if(38 in Game.keysDown) {
+      player.x += Math.cos(Math.PI/2+player.angle) * player.dx * dt;
+      player.y -= Math.sin(Math.PI/2+player.angle) * player.dx * dt;
+    }
+    if(37 in Game.keysDown) {
+      player.angle -= player.da * dt;
+    }
+    if(39 in Game.keysDown) {
+      player.angle += player.da * dt;
+    }
+    player.x = clamp(player.x, 0, FIELD_SIZE);
+    player.y = clamp(player.y, 0, FIELD_SIZE);
+  };
+
+  this.draw = function() {
+    drawBG();
+    ctx.beginPath();
+    box.forEach(function(p, i) {
+      var pos = translatePoints(p);
+      if(i == 0) {
+        ctx.moveTo(pos.x, pos.y);
+      }
+      else {
+        ctx.lineTo(pos.x, pos.y);
+      }
+    });
+
+    ctx.fillStyle = 'gray';
+    dots.forEach(function(p) {
+      var pos = translatePoints(p);
+      ctx.fillRect(pos.x-1, pos.y-1, 3, 3);
+    });
+    ctx.closePath();
+    ctx.lineWidth = 2;
+    ctx.strokeStyle = 'white';
+    ctx.stroke();
+
+    // player
+    drawCircle(reference, RADIUS, 3, 'white');
+  };
+
+  var onKeyUp = function(e) {
+    switch(e.charCode) {
+      case 80:  // P
+      case 112: // p
+      case 32:  // space
+        Game.pause();
+        soundManager.play('pause');
+        break;
+      case 68:  // D
+        debug = !debug;
+        break;
+    }
+  };
+
+  this.load = this.resume = function() {
+    addEventListener('keypress', onKeyUp);
+  }
+
+  this.unload = this.pause = function() {
+    removeEventListener('keypress', onKeyUp);
+  }
+}
+
+SoundDuck.prototype = new Scene();