Started reordering methods in play state
authorJustin Worthe <justin.worthe@gmail.com>
Fri, 26 Sep 2014 06:12:39 +0000 (08:12 +0200)
committerJustin Worthe <justin.worthe@gmail.com>
Fri, 26 Sep 2014 06:12:39 +0000 (08:12 +0200)
game/entities/collisionMap.js [new file with mode: 0644]
game/entities/hud.js
game/states/play.js

diff --git a/game/entities/collisionMap.js b/game/entities/collisionMap.js
new file mode 100644 (file)
index 0000000..fa0a29e
--- /dev/null
@@ -0,0 +1,32 @@
+'use strict';
+
+var CollisionMap = function() {
+  this.map = [];
+};
+
+CollisionMap.prototype = {
+  addToMap: function(x, y) {
+    if (!this.map) {
+      this.map = [];
+    }
+    if (!this.map[x]) {
+      this.map[x] = [];
+    }
+
+    this.map[x][y] = true;
+  },
+  removeFromMap: function(x,y) {
+    if (!this.map || !this.map[x]) {
+      return;
+    }
+    this.map[x][y] = false;
+  },
+  checkMap: function(x,y) {
+    if (!this.map || !this.map[x]) {
+      return false;
+    }
+    return !!this.map[x][y];
+  }
+}
+
+module.exports = CollisionMap;
\ No newline at end of file
index a7b39a0..43e7a21 100644 (file)
@@ -7,14 +7,12 @@ var Hud = function(game, player, x, y, scorefontKey, keyboardSpriteKey) {
     this.player = player;
     this.scale = {x: 0.02, y: 0.02};
 
-
     this.background = new Phaser.Sprite(this.game, 0, 0, 'hud-bg');
     this.add(this.background);
     this.scoreText = new Phaser.BitmapText(this.game, 172, 10, scorefontKey, '0', 100);
     this.add(this.scoreText);
 
     this.poisonIndicator = new Phaser.Sprite(this.game, 200, 150, 'poison-pill');
-    //this.poisonIndicator.scale = {0.1, 0.1};
     this.poisonIndicator.anchor = {x:0.5, y:0.5};
     this.add(this.poisonIndicator);
 
index cabe9cf..4134173 100644 (file)
@@ -7,23 +7,22 @@ var Wall = require('../entities/wall');
 var PoisonPill = require('../entities/poisonPill');
 var Hud = require('../entities/hud');
 
+var CollisionMap = require('../entities/collisionMap')
+
 function Play() {}
 
 Play.prototype = {
   create: function() {
     this.readLevelFile();
+    this.addGameHud();
 
-    this.world.scale = {x:50, y:50};
+    this.world.scale = {x:50, y:50}; //manually tweaked based on size of level file
     this.world.bounds = {x: -425, y:-25, width: this.game.width, height: this.game.height};
     this.world.camera.setBoundsToWorld();
 
     this.setupPlayerControls();
 
     this.gameWon = false;
-
-    this.hudA = new Hud(this.game, this.playerA, this.gameWidth-0.5, -0.5, 'spaced-scorefont-a', 'keys-a');
-    this.hudB = new Hud(this.game, this.playerB, -8.5, -0.5, 'spaced-scorefont-b', 'keys-b');
-
   },
   update: function() {
     this.checkForPlayerPillCollisions();
@@ -36,136 +35,21 @@ Play.prototype = {
 
     if (!this.gameWon && this.pills.total === 0) {
       this.gameWon = true;
-      if (this.playerA.score > this.playerB.score) {
-        this.setVictoryText('RED WINS', 'a');
-      }
-      else if (this.playerA.score < this.playerB.score) {
-        this.setVictoryText('YELLOW WINS', 'b');
-      }
-      else {
-        var victoryColor = this.playerA.isMyTurn ? 'b' : 'a';
-        this.setVictoryText('DRAW', victoryColor);
-      }
-
-      var self = this;
-      setTimeout(function() {
-        self.game.state.start('play');
-      }, 5000);
-    }
+      this.displayVictoryText();
 
-    this.pollPlayerInput();
-  },
-  addToMap: function(x, y) {
-    if (!this.map) {
-      this.map = [];
-    }
-    if (!this.map[x]) {
-      this.map[x] = [];
-    }
-
-    this.map[x][y] = true;
-  },
-  removeFromMap: function(x,y) {
-    if (!this.map || !this.map[x]) {
-      return;
-    }
-    this.map[x][y] = false;
-  },
-  checkMap: function(x,y) {
-    if (!this.map || !this.map[x]) {
-      return false;
-    }
-    return this.map[x][y];
-  },
-
-  checkForPlayerPillCollisions: function() {
-    var pillCollisions = [];
-    this.players.forEach(function(player) {
-      var playerBounds = player.getBounds();
-      this.pills.forEach(function(pill) {
-        var pillBounds = pill.getBounds();
-        if (Phaser.Rectangle.intersects(playerBounds, pillBounds)) {
-          pillCollisions.push({player:player, pill:pill});
-        }
-      }, this);
-    }, this);
-    for (var i=0; i<pillCollisions.length; i++) {
-      this.playerPillCollision(pillCollisions[i].player, pillCollisions[i].pill);
-    }
-  },
-  checkForPlayerPoisonPillCollisions: function() {
-    var pillCollisions = [];
-    this.players.forEach(function(player) {
-      var playerBounds = player.getBounds();
-      this.poisonPills.forEach(function(pill) {
-        var pillBounds = pill.getBounds();
-        if (Phaser.Rectangle.intersects(playerBounds, pillBounds)) {
-          pillCollisions.push({player:player, pill:pill});
-        }
+      this.game.time.events.add(5000, function() {
+        this.game.state.start('play');
       }, this);
-    }, this);
-    for (var i=0; i<pillCollisions.length; i++) {
-      this.playerPoisonPillCollision(pillCollisions[i].player, pillCollisions[i].pill);
-    }
-  },
-  pollPlayerInput: function() {
-    if (this.game.input.gamepad.pad2.connected) {
-      this.pollAnalogStickForPlayer(this.game.input.gamepad.pad2, this.playerA);
-    }
-    if (this.game.input.gamepad.pad1.connected) {
-      this.pollAnalogStickForPlayer(this.game.input.gamepad.pad1, this.playerB);
-    }
-
-    if (this.game.input.keyboard.isDown(this.playerAControls.up)) {
-      this.movePlayer(this.playerA, 0, -1);
-    }
-    if (this.game.input.keyboard.isDown(this.playerAControls.down)) {
-      this.movePlayer(this.playerA, 0, 1);
-    }
-    if (this.game.input.keyboard.isDown(this.playerAControls.left)) {
-      this.movePlayer(this.playerA, -1, 0);
-    }
-    if (this.game.input.keyboard.isDown(this.playerAControls.right)) {
-      this.movePlayer(this.playerA, 1, 0);
-    }
-    if (this.game.input.keyboard.isDown(this.playerBControls.up)) {
-      this.movePlayer(this.playerB, 0, -1);
-    }
-    if (this.game.input.keyboard.isDown(this.playerBControls.down)) {
-      this.movePlayer(this.playerB, 0, 1);
-    }
-    if (this.game.input.keyboard.isDown(this.playerBControls.left)) {
-      this.movePlayer(this.playerB, -1, 0);
-    }
-    if (this.game.input.keyboard.isDown(this.playerBControls.right)) {
-      this.movePlayer(this.playerB, 1, 0);
-    }
-  },
-  pollAnalogStickForPlayer: function(pad, player) {
-    if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_LEFT) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) < -0.2) {
-      this.movePlayer(player, -1, 0);
-    }
-    if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_RIGHT) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) > 0.2) {
-      this.movePlayer(player, 1, 0);
-    }
-    if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_UP) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) < -0.2) {
-      this.movePlayer(player, 0, -1);
-    }
-    if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_DOWN) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) > 0.2) {
-      this.movePlayer(player, 0, 1);
     }
 
-    var poisonButton = pad.getButton(Phaser.Gamepad.XBOX360_A);
-    if (poisonButton.isDown && player.gamepadPoisonLastPressed < poisonButton.timeDown) {
-      player.gamepadPoisonLastPressed = poisonButton.timeDown;
-      this.togglePoisonPill(player);
-    }
+    this.pollPlayerInput();
   },
   readLevelFile: function() {
     this.pills = this.game.add.group();
     this.players = this.game.add.group();
     this.walls = this.game.add.group();
     this.poisonPills = this.game.add.group();
+    this.map = new CollisionMap();
 
     var levelText = this.game.cache.getText('level');
     var splitRows = levelText.split('\n');
@@ -175,6 +59,7 @@ Play.prototype = {
         switch(splitRows[y][x]) {
           case '#':
             this.walls.add(new Wall(this.game, x, y));
+            this.map.addToMap(x, y);
             break;
           case '.':
             this.pills.add(new Pill(this.game, x, y));
@@ -191,7 +76,7 @@ Play.prototype = {
             this.players.add(this.playerB);
             break;
           case '|':
-            this.addToMap(x, y);
+            this.map.addToMap(x, y);
             break;
           }
       }
@@ -209,7 +94,6 @@ Play.prototype = {
     this.gameWidth = 0;
     this.gameHeight = 0;
     this.walls.forEach(function(wall) {
-      this.addToMap(wall.x, wall.y);
       if (wall.x >= this.gameWidth) {
         this.gameWidth = wall.x+1;
       }
@@ -222,7 +106,10 @@ Play.prototype = {
 
     this.playerB.isMyTurn = true;
   },
-
+  addGameHud: function() {
+    this.hudA = new Hud(this.game, this.playerA, this.gameWidth-0.5, -0.5, 'spaced-scorefont-a', 'keys-a');
+    this.hudB = new Hud(this.game, this.playerB, -8.5, -0.5, 'spaced-scorefont-b', 'keys-b');
+  },
   setupPlayerControls: function() {
     this.playerBControls = {
       up: Phaser.Keyboard.W,
@@ -262,6 +149,91 @@ Play.prototype = {
       this.game.state.start('play');
     },this);
   },
+  checkForPlayerPillCollisions: function() {
+    var pillCollisions = [];
+    this.players.forEach(function(player) {
+      var playerBounds = player.getBounds();
+      this.pills.forEach(function(pill) {
+        var pillBounds = pill.getBounds();
+        if (Phaser.Rectangle.intersects(playerBounds, pillBounds)) {
+          pillCollisions.push({player:player, pill:pill});
+        }
+      }, this);
+    }, this);
+    for (var i=0; i<pillCollisions.length; i++) {
+      this.playerPillCollision(pillCollisions[i].player, pillCollisions[i].pill);
+    }
+  },
+  checkForPlayerPoisonPillCollisions: function() {
+    var pillCollisions = [];
+    this.players.forEach(function(player) {
+      var playerBounds = player.getBounds();
+      this.poisonPills.forEach(function(pill) {
+        var pillBounds = pill.getBounds();
+        if (Phaser.Rectangle.intersects(playerBounds, pillBounds)) {
+          pillCollisions.push({player:player, pill:pill});
+        }
+      }, this);
+    }, this);
+    for (var i=0; i<pillCollisions.length; i++) {
+      this.playerPoisonPillCollision(pillCollisions[i].player, pillCollisions[i].pill);
+    }
+  },
+  pollPlayerInput: function() {
+    if (this.game.input.gamepad.pad2.connected) {
+      this.pollAnalogStickForPlayer(this.game.input.gamepad.pad2, this.playerA);
+    }
+    if (this.game.input.gamepad.pad1.connected) {
+      this.pollAnalogStickForPlayer(this.game.input.gamepad.pad1, this.playerB);
+    }
+
+    if (this.game.input.keyboard.isDown(this.playerAControls.up)) {
+      this.movePlayer(this.playerA, 0, -1);
+    }
+    if (this.game.input.keyboard.isDown(this.playerAControls.down)) {
+      this.movePlayer(this.playerA, 0, 1);
+    }
+    if (this.game.input.keyboard.isDown(this.playerAControls.left)) {
+      this.movePlayer(this.playerA, -1, 0);
+    }
+    if (this.game.input.keyboard.isDown(this.playerAControls.right)) {
+      this.movePlayer(this.playerA, 1, 0);
+    }
+    if (this.game.input.keyboard.isDown(this.playerBControls.up)) {
+      this.movePlayer(this.playerB, 0, -1);
+    }
+    if (this.game.input.keyboard.isDown(this.playerBControls.down)) {
+      this.movePlayer(this.playerB, 0, 1);
+    }
+    if (this.game.input.keyboard.isDown(this.playerBControls.left)) {
+      this.movePlayer(this.playerB, -1, 0);
+    }
+    if (this.game.input.keyboard.isDown(this.playerBControls.right)) {
+      this.movePlayer(this.playerB, 1, 0);
+    }
+  },
+  pollAnalogStickForPlayer: function(pad, player) {
+    if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_LEFT) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) < -0.2) {
+      this.movePlayer(player, -1, 0);
+    }
+    if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_RIGHT) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_X) > 0.2) {
+      this.movePlayer(player, 1, 0);
+    }
+    if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_UP) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) < -0.2) {
+      this.movePlayer(player, 0, -1);
+    }
+    if (pad.isDown(Phaser.Gamepad.XBOX360_DPAD_DOWN) || pad.axis(Phaser.Gamepad.XBOX360_STICK_LEFT_Y) > 0.2) {
+      this.movePlayer(player, 0, 1);
+    }
+
+    var poisonButton = pad.getButton(Phaser.Gamepad.XBOX360_A);
+    if (poisonButton.isDown && player.gamepadPoisonLastPressed < poisonButton.timeDown) {
+      player.gamepadPoisonLastPressed = poisonButton.timeDown;
+      this.togglePoisonPill(player);
+    }
+  },
+  
+  
   togglePoisonPill: function(player) {
     if (player.hasPoisonPill) {
       player.poisonPillActive = !player.poisonPillActive;
@@ -283,7 +255,7 @@ Play.prototype = {
     var placePoisonPill = player.hasPoisonPill && player.poisonPillActive && (Math.abs(this.respawnX-player.x)>1.1 || Math.abs(this.respawnY-player.y)>1.1);
 
     //cannot move into walls, when it isn't your turn, or when you're already moving
-    if (this.checkMap(newX, newY) || !player.isMyTurn || player.moving) {
+    if (this.map.checkMap(newX, newY) || !player.isMyTurn || player.moving) {
       return;
     }
 
@@ -385,6 +357,18 @@ Play.prototype = {
     }
     this.players.sort('isMyTurn');
   },
+  displayVictoryText: function() {
+    if (this.playerA.score > this.playerB.score) {
+      this.setVictoryText('RED WINS', 'a');
+    }
+    else if (this.playerA.score < this.playerB.score) {
+      this.setVictoryText('YELLOW WINS', 'b');
+    }
+    else {
+      var victoryColor = this.playerA.isMyTurn ? 'b' : 'a';
+      this.setVictoryText('DRAW', victoryColor);
+    }
+  },
   setVictoryText: function(newText, winnerLetter) {
     this.victoryText = this.game.add.bitmapText(this.world.width/2/this.world.scale.x, 2, 'scorefont-'+winnerLetter, newText, 2);
     this.victoryText.position.x = (this.world.width/this.world.scale.x)/2 - 8 - this.victoryText.textWidth/2 - 0.5;
@@ -394,11 +378,13 @@ Play.prototype = {
     this.game.input.keyboard.removeKey(this.playerAControls.down);
     this.game.input.keyboard.removeKey(this.playerAControls.left);
     this.game.input.keyboard.removeKey(this.playerAControls.right);
+    this.game.input.keyboard.removeKey(this.playerAControls.poison);
 
     this.game.input.keyboard.removeKey(this.playerBControls.up);
     this.game.input.keyboard.removeKey(this.playerBControls.down);
     this.game.input.keyboard.removeKey(this.playerBControls.left);
     this.game.input.keyboard.removeKey(this.playerBControls.right);
+    this.game.input.keyboard.removeKey(this.playerBControls.poison);
 
     this.game.input.keyboard.removeKey(this.controls.reset);
   }