diff options
Diffstat (limited to 'game')
-rw-r--r-- | game/prefabs/player.js | 3 | ||||
-rw-r--r-- | game/prefabs/poisonPill.js | 18 | ||||
-rw-r--r-- | game/states/play.js | 40 | ||||
-rw-r--r-- | game/states/preload.js | 1 |
4 files changed, 60 insertions, 2 deletions
diff --git a/game/prefabs/player.js b/game/prefabs/player.js index a4d1b57..fb63707 100644 --- a/game/prefabs/player.js +++ b/game/prefabs/player.js @@ -17,6 +17,9 @@ var Player = function(game, x, y, key, frame, soundKey) { this.isMyTurn = false; this.animIsMyTurn = true; + this.hasPoisonPill = true; + this.poisonPillActive = false; + this.scoreSound = game.sound.add(soundKey); diff --git a/game/prefabs/poisonPill.js b/game/prefabs/poisonPill.js new file mode 100644 index 0000000..408f77f --- /dev/null +++ b/game/prefabs/poisonPill.js @@ -0,0 +1,18 @@ +'use strict'; + +var PoisonPill = function(game, x, y, frame) { + Phaser.Sprite.call(this, game, x, y, 'poison-pill', frame); + this.scale = {x: 0.01, y: 0.01}; + this.anchor = {x: 0.5, y: 0.5}; + + this.score = 1; +}; + +PoisonPill.prototype = Object.create(Phaser.Sprite.prototype); +PoisonPill.prototype.constructor = PoisonPill; + +PoisonPill.prototype.getBounds = function() { + return new Phaser.Rectangle(this.x, this.y, 0.2, 0.2); +}; + +module.exports = PoisonPill; diff --git a/game/states/play.js b/game/states/play.js index b79ea32..5461c1b 100644 --- a/game/states/play.js +++ b/game/states/play.js @@ -4,6 +4,7 @@ var Player = require('../prefabs/player'); var Pill = require('../prefabs/pill'); var BonusPill = require('../prefabs/bonusPill'); var Wall = require('../prefabs/wall'); +var PoisonPill = require('../prefabs/poisonPill'); function Play() {} @@ -48,6 +49,7 @@ Play.prototype = { }, update: function() { this.checkForPlayerPillCollisions(); + this.checkForPlayerPoisonPillCollisions(); if (Phaser.Rectangle.intersects(this.playerA.getBounds(), this.playerB.getBounds())) { this.playerPlayerCollision(this.playerA, this.playerB); @@ -89,6 +91,21 @@ Play.prototype = { 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.pad1.connected) { this.pollAnalogStickForPlayer(this.game.input.gamepad.pad1, this.playerA); @@ -140,6 +157,7 @@ Play.prototype = { this.pills = this.game.add.group(); this.players = this.game.add.group(); this.walls = this.game.add.group(); + this.poisonPills = this.game.add.group(); var levelText = this.game.cache.getText('level'); var splitRows = levelText.split('\n'); @@ -196,13 +214,15 @@ Play.prototype = { up: Phaser.Keyboard.W, left: Phaser.Keyboard.A, down: Phaser.Keyboard.S, - right: Phaser.Keyboard.D + right: Phaser.Keyboard.D, + poison: Phaser.Keyboard.Q }; this.playerBControls = { up: Phaser.Keyboard.UP, left: Phaser.Keyboard.LEFT, down: Phaser.Keyboard.DOWN, - right: Phaser.Keyboard.RIGHT + right: Phaser.Keyboard.RIGHT, + poison: Phaser.Keyboard.ENTER }; function addKeyCaptures(controls, keyboard) { @@ -229,6 +249,14 @@ Play.prototype = { this.game.orientation.onDown.add(function() { this.movePlayer(this.players.children[this.playerTurn], 0, 1); }, this); + + this.game.input.keyboard.addKey(this.playerBControls.poison).onDown.add(this.togglePoisonPill.bind(this, this.playerB), this); + this.game.input.keyboard.addKey(this.playerAControls.poison).onDown.add(this.togglePoisonPill.bind(this, this.playerA), this); + }, + togglePoisonPill: function(player) { + if (player.hasPoisonPill) { + player.poisonPillActive = !player.poisonPillActive; + } }, movePlayer: function(player, deltaX, deltaY) { var newX = player.x + deltaX; @@ -275,6 +303,14 @@ Play.prototype = { this.playerAScoreText.setText(this.playerA.score+''); this.playerBScoreText.setText(this.playerB.score+''); }, + playerPoisonPillCollision: function(player, poisonPill) { + poisonPill.destroy(); + + var respawnX = Math.ceil(this.gameWidth/2)-1; + var respawnY = Math.ceil(this.gameHeight/2)-1; + + player.teleport(respawnX, respawnY); + }, playerPlayerCollision: function(playerA, playerB) { var eatenPlayer = playerA.isMyTurn ? playerB : playerA; diff --git a/game/states/preload.js b/game/states/preload.js index 4e13679..6ec5b31 100644 --- a/game/states/preload.js +++ b/game/states/preload.js @@ -17,6 +17,7 @@ Preload.prototype = { this.load.spritesheet('player-b', 'assets/images/player-b-spritesheet.svg', 100, 100); this.load.image('pill', 'assets/images/pill.svg'); this.load.image('bonus-pill', 'assets/images/bonus-pill.svg'); + this.load.image('poison-pill', 'assets/images/poison-pill.svg'); this.load.bitmapFont('spaced-scorefont-a', 'assets/fonts/scorefont-a.png', 'assets/fonts/scorefont.fnt', undefined, 10); this.load.bitmapFont('scorefont-a', 'assets/fonts/scorefont-a.png', 'assets/fonts/scorefont.fnt'); |