diff options
Diffstat (limited to 'game')
-rw-r--r-- | game/main.js | 2 | ||||
-rw-r--r-- | game/prefabs/hud.js | 48 | ||||
-rw-r--r-- | game/states/play.js | 56 | ||||
-rw-r--r-- | game/states/preload.js | 3 |
4 files changed, 80 insertions, 29 deletions
diff --git a/game/main.js b/game/main.js index a44c949..e67845c 100644 --- a/game/main.js +++ b/game/main.js @@ -2,7 +2,7 @@ //global variables window.onload = function () { - var game = new Phaser.Game(1100, 950, Phaser.AUTO, 'interactive-pacbot'); + var game = new Phaser.Game(1750, 1100, Phaser.AUTO, 'interactive-pacbot'); var Orientation = require('./plugins/orientation'); game.orientation = new Orientation(); diff --git a/game/prefabs/hud.js b/game/prefabs/hud.js new file mode 100644 index 0000000..13f8f51 --- /dev/null +++ b/game/prefabs/hud.js @@ -0,0 +1,48 @@ +'use strict'; + +var Hud = function(game, player, x, y, scorefontKey) { + Phaser.Group.call(this, game); + this.x = x; + this.y = y; + 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); + + this.controllerDiagram = new Phaser.Sprite(this.game, 0, 300, 'controller-diagram'); + this.controllerDiagram.scale = {x: 0.5, y: 0.5}; + this.add(this.controllerDiagram); + + this.sendToBack(this.background); + + this.currentScore = 0; +}; + +Hud.prototype = Object.create(Phaser.Group.prototype); +Hud.prototype.constructor = Hud; + +Hud.prototype.update = function() { + if (this.currentScore !== this.player.score) { + this.currentScore = this.player.score; + this.scoreText.setText(this.player.score+''); + + var numberOfDigits = Math.floor(Math.log(this.currentScore)/Math.log(10))+1; + this.scoreText.x = 200 - numberOfDigits*30; + } + + if (this.poisonIndicator && !this.player.hasPoisonPill) { + this.poisonIndicator.destroy(); + this.poisonIndicator = null; + } +}; + +module.exports = Hud; diff --git a/game/states/play.js b/game/states/play.js index 26242b3..cf4bf62 100644 --- a/game/states/play.js +++ b/game/states/play.js @@ -5,6 +5,7 @@ var Pill = require('../prefabs/pill'); var BonusPill = require('../prefabs/bonusPill'); var Wall = require('../prefabs/wall'); var PoisonPill = require('../prefabs/poisonPill'); +var Hud = require('../prefabs/hud'); function Play() {} @@ -15,15 +16,18 @@ Play.prototype = { this.readLevelFile(); this.world.scale = {x:50, y:50}; - this.world.bounds = {x: -25, y:-25, width: this.game.width, height: this.game.height}; + this.world.bounds = {x: -425, y:-25, width: this.game.width, height: this.game.height}; this.world.camera.setBoundsToWorld(); this.setupPlayerControls(); - this.playerAScoreText = this.game.add.bitmapText(-0.1, -0.4, 'spaced-scorefont-a','0',2); - this.playerBScoreText = this.game.add.bitmapText(this.world.width/this.world.scale.x - 4.5, -0.4, 'spaced-scorefont-b','0',2); - this.gameWon = false; + + this.hudA = new Hud(this.game, this.playerA, this.gameWidth-0.5, -0.5, 'spaced-scorefont-a'); + this.hudB = new Hud(this.game, this.playerB, -8.5, -0.5, 'spaced-scorefont-b'); + //this.game.add.existing(this.hudA); + //this.game.add.existing(this.hudB); + }, update: function() { this.checkForPlayerPillCollisions(); @@ -170,9 +174,9 @@ Play.prototype = { var levelText = this.game.cache.getText('level'); var splitRows = levelText.split('\n'); - for (var x=0; x<splitRows.length; x++) { - for (var y=0; y<splitRows[x].length; y++) { - switch(splitRows[x][y]) { + for (var y=0; y<splitRows.length; y++) { + for (var x=0; x<splitRows[y].length; x++) { + switch(splitRows[y][x]) { case '#': this.walls.add(new Wall(this.game, x, y)); break; @@ -220,7 +224,7 @@ Play.prototype = { this.respawnX = Math.ceil(this.gameWidth/2)-1; this.respawnY = Math.ceil(this.gameHeight/2)-1; - this.updatePlayerTurn(0); + this.playerB.isMyTurn = true; }, setupPlayerControls: function() { @@ -255,18 +259,10 @@ Play.prototype = { this.game.input.gamepad.start(); - this.game.orientation.onLeft.add(function() { - this.movePlayer(this.players.children[this.playerTurn], -1, 0); - }, this); - this.game.orientation.onRight.add(function() { - this.movePlayer(this.players.children[this.playerTurn], 1, 0); - }, this); - this.game.orientation.onUp.add(function() { - this.movePlayer(this.players.children[this.playerTurn], 0, -1); - }, this); - this.game.orientation.onDown.add(function() { - this.movePlayer(this.players.children[this.playerTurn], 0, 1); - }, this); + this.game.orientation.onLeft.add(this.moveActivePlayer.bind(this, -1, 0), this); + this.game.orientation.onRight.add(this.moveActivePlayer.bind(this, 1, 0), this); + this.game.orientation.onUp.add(this.moveActivePlayer.bind(this, 0, -1), this); + this.game.orientation.onDown.add(this.moveActivePlayer.bind(this, 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); @@ -280,6 +276,16 @@ Play.prototype = { player.poisonPillActive = !player.poisonPillActive; } }, + moveActivePlayer: function(deltaX, deltaY) { + var activePlayer = null; + for (var i=0; i<this.players.children.length; ++i) { + if (this.players.children[i].isMyTurn) { + activePlayer = this.players.children[i]; + } + } + + movePlayer(activePlayer, deltaX, deltaY); + }, movePlayer: function(player, deltaX, deltaY) { var newX = player.x + deltaX; var newY = player.y + deltaY; @@ -357,9 +363,6 @@ Play.prototype = { player.score += pill.score; pill.destroy(); player.scoreSound.play(); - - this.playerAScoreText.setText(this.playerA.score+''); - this.playerBScoreText.setText(this.playerB.score+''); }, playerPoisonPillCollision: function(player, poisonPill) { if (player.lastTween) { @@ -385,14 +388,11 @@ Play.prototype = { eatenPlayer.respawnSound.play(); }, togglePlayerTurn: function() { - this.updatePlayerTurn((this.playerTurn+1)%this.players.length); - }, - updatePlayerTurn: function(newPlayerTurn) { - this.playerTurn = newPlayerTurn; for (var i=0; i<this.players.children.length; ++i) { - this.players.children[i].isMyTurn = (i === this.playerTurn); + this.players.children[i].isMyTurn = !this.players.children[i].isMyTurn; this.players.children[i].canBeEaten = true; } + this.players.sort('isMyTurn'); }, setVictoryText: function(newText, winnerLetter) { this.victoryText = this.game.add.bitmapText(this.world.width/2/this.world.scale.x, 2, 'scorefont-'+winnerLetter, newText, 2); diff --git a/game/states/preload.js b/game/states/preload.js index 951c83c..f47b62d 100644 --- a/game/states/preload.js +++ b/game/states/preload.js @@ -30,6 +30,9 @@ Preload.prototype = { this.load.audio('owSound', 'assets/audio/ow.ogg', true); this.load.text('level', 'assets/levels/maze.lvl'); + + this.load.image('hud-bg', 'assets/images/hud-bg.svg'); + this.load.image('controller-diagram', 'assets/images/controller-diagram.svg'); }, create: function() { this.asset.cropEnabled = false; |