|
@@ -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();
|
|
|
+
|
|
|
+
|
|
|
+ drawCircle(reference, RADIUS, 3, 'white');
|
|
|
+ };
|
|
|
+
|
|
|
+ var onKeyUp = function(e) {
|
|
|
+ switch(e.charCode) {
|
|
|
+ case 80:
|
|
|
+ case 112:
|
|
|
+ case 32:
|
|
|
+ Game.pause();
|
|
|
+ soundManager.play('pause');
|
|
|
+ break;
|
|
|
+ case 68:
|
|
|
+ debug = !debug;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ this.load = this.resume = function() {
|
|
|
+ addEventListener('keypress', onKeyUp);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.unload = this.pause = function() {
|
|
|
+ removeEventListener('keypress', onKeyUp);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+SoundDuck.prototype = new Scene();
|