summaryrefslogtreecommitdiff
path: root/game/entities/player.js
blob: b2214ff22861ef7bd4fd6e85b9701157ffbf3916 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';

var EntityBase = require('../entities/entityBase');

var Player = function(game, x, y, key, soundKey) {
  var player = this;

  EntityBase.call(this, game, x, y, key);
  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.keyboardPoisonLastPressed = 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(EntityBase.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;
};

module.exports = Player;