From c508e9ef283a4069ea1af923ca1a241d2c954a1e Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sun, 21 Sep 2014 10:31:48 +0200 Subject: Renamed prefab folder --- game/entities/player.js | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 game/entities/player.js (limited to 'game/entities/player.js') 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; -- cgit v1.2.3