summaryrefslogtreecommitdiff
path: root/game/entities
diff options
context:
space:
mode:
Diffstat (limited to 'game/entities')
-rw-r--r--game/entities/bonusPill.js18
-rw-r--r--game/entities/hud.js50
-rw-r--r--game/entities/pill.js18
-rw-r--r--game/entities/player.js118
-rw-r--r--game/entities/poisonPill.js18
-rw-r--r--game/entities/wall.js12
6 files changed, 234 insertions, 0 deletions
diff --git a/game/entities/bonusPill.js b/game/entities/bonusPill.js
new file mode 100644
index 0000000..dd8dfce
--- /dev/null
+++ b/game/entities/bonusPill.js
@@ -0,0 +1,18 @@
+'use strict';
+
+var BonusPill = function(game, x, y, frame) {
+ Phaser.Sprite.call(this, game, x, y, 'bonus-pill', frame);
+ this.scale = {x: 0.01, y: 0.01};
+ this.anchor = {x: 0.5, y: 0.5};
+
+ this.score = 10;
+};
+
+BonusPill.prototype = Object.create(Phaser.Sprite.prototype);
+BonusPill.prototype.constructor = BonusPill;
+
+BonusPill.prototype.getBounds = function() {
+ return new Phaser.Rectangle(this.x, this.y, 0.2, 0.2);
+};
+
+module.exports = BonusPill;
diff --git a/game/entities/hud.js b/game/entities/hud.js
new file mode 100644
index 0000000..a7b39a0
--- /dev/null
+++ b/game/entities/hud.js
@@ -0,0 +1,50 @@
+'use strict';
+
+var Hud = function(game, player, x, y, scorefontKey, keyboardSpriteKey) {
+ 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.keyboardControls = new Phaser.Sprite(this.game, 0, 600, keyboardSpriteKey);
+ this.keyboardControls.scale = {x: 0.5, y: 0.5};
+ this.add(this.keyboardControls);
+
+ 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/entities/pill.js b/game/entities/pill.js
new file mode 100644
index 0000000..13054b0
--- /dev/null
+++ b/game/entities/pill.js
@@ -0,0 +1,18 @@
+'use strict';
+
+var Pill = function(game, x, y, frame) {
+ Phaser.Sprite.call(this, game, x, y, 'pill', frame);
+ this.scale = {x: 0.01, y: 0.01};
+ this.anchor = {x: 0.5, y: 0.5};
+
+ this.score = 1;
+};
+
+Pill.prototype = Object.create(Phaser.Sprite.prototype);
+Pill.prototype.constructor = Pill;
+
+Pill.prototype.getBounds = function() {
+ return new Phaser.Rectangle(this.x, this.y, 0.2, 0.2);
+};
+
+module.exports = Pill;
diff --git a/game/entities/player.js b/game/entities/player.js
new file mode 100644
index 0000000..c7f0b94
--- /dev/null
+++ b/game/entities/player.js
@@ -0,0 +1,118 @@
+'use strict';
+
+var Player = function(game, x, y, key, frame, soundKey) {
+ var player = this;
+
+ Phaser.Sprite.call(this, game, x, y, key, frame);
+ this.animations.add('active', [0]);
+ this.animations.add('waiting', [1]);
+ this.animations.add('activePoison', [2]);
+ this.animations.add('waitingPoison', [3]);
+
+ this.baseKey = key;
+ this.moving = false;
+ this.scale = {x: 0.01, y: 0.01};
+ this.anchor = {x: 0.5, y: 0.5};
+
+ this.score = 0;
+ this.maxScore = 1;
+ this.isMyTurn = false;
+ this.canBeEaten = true;
+
+ this.currentAnimation = {
+ isMyTurn: true,
+ poisonPillActive: false
+ }
+
+ this.hasPoisonPill = true;
+ this.poisonPillActive = false;
+ this.gamepadPoisonLastPressed = Number.NEGATIVE_INFINITY;
+ this.lastTween = null;
+
+ this.scoreSound = game.sound.add(soundKey);
+ this.respawnSound = game.sound.add('owSound');
+
+
+ //BEWARE! HORRIBLE HACK AHEAD!
+ //Intercepts the call to get a new buffer so that we can set the playbackRate.
+ var audioContext = this.scoreSound.context;
+ var childContext = Object.create(audioContext);
+ this.scoreSound.context = childContext;
+
+ childContext.createBufferSource = function() {
+ var source = audioContext.createBufferSource();
+ var scoreFraction = player.score / player.maxScore;
+ source.playbackRate.value = 0.75 + scoreFraction*6;
+ return source;
+ };
+};
+
+Player.prototype = Object.create(Phaser.Sprite.prototype);
+Player.prototype.constructor = Player;
+
+Player.prototype.update = function() {
+ if (this.isMyTurn !== this.currentAnimation.isMyTurn || this.poisonPillActive !== this.currentAnimation.poisonPillActive) {
+ this.currentAnimation.isMyTurn = this.isMyTurn;
+ this.currentAnimation.poisonPillActive = this.poisonPillActive;
+
+ var animation;
+ if (!this.currentAnimation.isMyTurn) {
+ if (!this.currentAnimation.poisonPillActive) {
+ animation = 'waiting';
+ }
+ else {
+ animation = 'waitingPoison'
+ }
+ }
+ else {
+ if (!this.currentAnimation.poisonPillActive) {
+ animation = 'active';
+ }
+ else {
+ animation = 'activePoison'
+ }
+ }
+ this.play(animation);
+ }
+};
+
+Player.prototype.move = function(newX, newY, callback, callbackContext) {
+ this.moving = true;
+
+ var tween = this.game.add.tween(this).to({x: newX, y: newY}, 500);
+ tween.onComplete.add(callback, callbackContext);
+ tween.onComplete.add(this.finishMovement, this);
+
+ this.lastTween = tween;
+
+ tween.start();
+};
+
+Player.prototype.multistepMove = function(moveX, moveY, teleportX, teleportY, finalX, finalY, callback, callbackContext) {
+ this.moving = true;
+
+ var firstTween = this.game.add.tween(this).to({x: moveX, y: moveY}, 500);
+
+ firstTween.onComplete.add(function() {
+ this.teleport(teleportX, teleportY);
+ this.move(finalX, finalY, callback, callbackContext);
+ }, this);
+
+ firstTween.start();
+}
+
+Player.prototype.teleport = function(newX, newY) {
+ this.x = newX;
+ this.y = newY;
+}
+
+Player.prototype.finishMovement = function() {
+ this.moving = false;
+ this.lastTween = null;
+};
+
+Player.prototype.getBounds = function() {
+ return new Phaser.Rectangle(this.x, this.y, 0.2, 0.2);
+};
+
+module.exports = Player;
diff --git a/game/entities/poisonPill.js b/game/entities/poisonPill.js
new file mode 100644
index 0000000..408f77f
--- /dev/null
+++ b/game/entities/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/entities/wall.js b/game/entities/wall.js
new file mode 100644
index 0000000..c4eee79
--- /dev/null
+++ b/game/entities/wall.js
@@ -0,0 +1,12 @@
+'use strict';
+
+var Wall = function(game, x, y, frame) {
+ Phaser.Sprite.call(this, game, x, y, 'wall', frame);
+ this.scale = {x: 0.01, y: 0.01};
+ this.anchor = {x: 0.5, y: 0.5};
+};
+
+Wall.prototype = Object.create(Phaser.Sprite.prototype);
+Wall.prototype.constructor = Wall;
+
+module.exports = Wall;