-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhero.js
124 lines (105 loc) · 3.25 KB
/
hero.js
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
118
119
120
121
122
123
124
'use strict';
var Hero = function(x, y, variant, facing, map) {
Phaser.Sprite.call(this, game, x, y, 'hero', 0);
this.map = map;
this.walking = false;
this.variant = variant;
this.direction = facing;
this.cursors = game.input.keyboard.createCursorKeys();
var t = this;
var i = -1;
[colorVariant.RED, colorVariant.BLUE, colorVariant.GREEN].forEach(function (color) {
[DIRECTION.DOWN, DIRECTION.LEFT, DIRECTION.RIGHT, DIRECTION.UP].forEach(function (dir) {
var arr = [];
for (var j=0; j < 3 ; ++j) {
arr.push(++i);
}
t.animations.add(t.getAnimName(dir, color), arr, 12, true);
})
});
this.changerSound = game.add.audio('changer');
this.walkingSound = game.add.audio('walking');
this.blockedSound = game.add.audio('blocked');
this.render();
game.add.existing(this);
};
Hero.prototype = Object.create(Phaser.Sprite.prototype);
Hero.prototype.constructor = Hero;
Hero.prototype.changeColor = function(newColor) {
this.variant = newColor;
};
Hero.prototype.update = function() {
if (!this.walking) {
this.checkMovement();
this.checkChanger();
}
};
Hero.prototype.move = function(xDir, yDir) {
var newX, newY;
if (yDir) {
this.direction = (yDir > 0) ? 'down' : 'up';
newX = this.x;
newY = this.y + Math.floor(tileSize * yDir)
} else if (xDir) {
this.direction = (xDir > 0) ? 'right' : 'left';
newX = this.x + Math.floor(tileSize * xDir);
newY = this.y;
}
// TODO: Change the frame but do not move
if (this.isWalkable(newX, newY)) {
var capsule = findCapsule(newX, newY);
if ((capsule === null) || (capsule.variant === this.variant && capsule.move(this.direction))) {
this.walking = true;
this.walkingSound.play();
this.animations.play(this.getAnimName());
var tween = game.add.tween(this);
tween.to({
x: newX,
y: newY
}, 200, Phaser.Easing.Linear.None, true);
tween.onComplete.add(function(){
this.walking = false;
game.global.moves += 1;
this.render();
}, this);
} else {
this.blockedSound.play();
}
} else {
this.blockedSound.play();
}
};
Hero.prototype.render = function() {
this.animations.stop();
this.animations.getAnimation(this.getAnimName(this.direction, this.variant)).frame = 0;
};
Hero.prototype.isWalkable = function(x, y) {
return !this.map.hasTile(x / 32, y / 32, 'Walls');
};
Hero.prototype.getAnimName = function(direction, color) {
if (!direction && !color) {
return this.direction + '-' + this.variant;
} else {
return direction + '-' + color;
}
}
Hero.prototype.checkMovement = function() {
var xDir = (this.cursors.left.isDown ? -1 : (this.cursors.right.isDown ? 1: 0));
var yDir = (this.cursors.up.isDown ? -1 : (this.cursors.down.isDown ? 1: 0));
if (yDir !== 0) {
this.move(null, yDir);
} else if (xDir !== 0) {
this.move(xDir, null);
}
};
Hero.prototype.checkChanger = function() {
var self = this;
var chg = findChanger(this.x, this.y);
if (chg !== null && chg.variant !== this.variant) {
var transf = new Transformation(this.x, this.y);
this.changerSound.play();
this.variant = chg.variant;
this.animations.stop();
this.animations.getAnimation(this.getAnimName()).frame = 0;
}
};