-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
194 lines (186 loc) · 5.82 KB
/
player.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Player.js
function Player(game, image)
{
this.x = 1;
this.y = 1;
this.width = 16;
this.height = 24;
this.currentFrame = 0;
this.animationOffsets = {
"up": [12, 2],
"right": [68, 2],
"down":[123, 2],
"left":[179, 2],
"death":[152, 177],
};
this.currentAnimation = "down";
this.idle = true; // used when idleing to play only the first animation
this.image = new Image();
this.image.src = image;
this.lastUpdated = 0;
this.speed = 32; // Move one tile at a time
this.alive = true;
this.bombCooldown = 3000;
this.movementCooldown = 350; // How long before you're allowed to move again
this.lastMoved = 0;
// references to game objects
this.game = game;
this.map = game.map;
this.objects = game.objects;
this.keyboard = game.keyboard;
this.cxt = game.cxt;
}
Player.prototype.getPosition = function()
{
return {x: ~~(this.x + this.width/2), y: ~~(this.y + this.height/2)};
}
Player.prototype.update = function(dt)
{
// Player is dead, play death animation
if(!this.alive)
{
this.cxt.drawImage(this.image,
this.animationOffsets["death"][0] + (this.width * this.currentFrame + (this.currentFrame*3)), this.animationOffsets["death"][1],
this.width, this.height,
this.x, this.y, this.width, this.height);
if (this.currentFrame <= 4)
this.lastUpdated+=dt;
if(this.lastUpdated >= 200)
{
this.currentFrame++;
this.lastUpdated = 0;
}
return;
}
// Movement + Related Animations
if(this.lastMoved > this.movementCooldown &&
(this.keyboard[37] || this.keyboard[65]))
{
if(this.map.getTile(this.x-this.speed,this.y)==0)
{
this.x -= this.speed;
this.lastMoved = 0;
}
if(this.currentAnimation!="left")
{
this.idle = false;
this.currentAnimation = "left";
this.currentFrame, this.lastUpdated = 0;
}
}
else if(this.lastMoved > this.movementCooldown &&
(this.keyboard[39] || this.keyboard[68]))
{
if(this.map.getTile(this.x+this.speed, this.y)==0)
{
this.x += this.speed;
this.lastMoved = 0;
}
if(this.currentAnimation!="right")
{
this.idle = false;
this.currentAnimation = "right";
this.currentFrame, this.lastUpdated = 0;
}
}
else if(this.lastMoved > this.movementCooldown &&
(this.keyboard[38] || this.keyboard[87]))
{
if(this.map.getTile(this.x, this.y-this.speed)==0)
{
this.y -= this.speed;
this.lastMoved = 0;
}
if(this.currentAnimation!="up")
{
this.idle = false;
this.currentAnimation = "up";
this.currentFrame, this.lastUpdated = 0;
}
}
else if(this.lastMoved > this.movementCooldown &&
(this.keyboard[40] || this.keyboard[83]))
{
if(this.map.getTile(this.x, this.y + this.speed)==0)
{
this.y += this.speed;
this.lastMoved = 0;
}
if(this.currentAnimation!="down")
{
this.idle = false;
this.currentAnimation = "down";
this.currentFrame, this.lastUpdated = 0;
}
}
else
{
this.lastMoved += dt;
this.idle = true;
this.currentFrame = 0;
}
// Drop the Bomb
if(this.keyboard[32] && this.bombCooldown >= 2000) //DROP THE BOMB
{
this.bombCooldown = 0;
// check if player is dropping a bomb on a blocked tile, if they are the bomb spawns ontop of him
switch(this.currentAnimation)
{
case "left":
if(this.map.getTile(this.x-33, this.y)==0)
{
this.objects.push(new Bomb(this.x-33, this.y, this.game));
}
else
{
this.objects.push(new Bomb(this.x, this.y, this.game));
}
break;
case "right":
if(this.map.getTile(this.x+33, this.y)==0)
{
this.objects.push(new Bomb(this.x+33, this.y, this.game));
}
else
{
this.objects.push(new Bomb(this.x, this.y, this.game));
}
break;
case "up":
if(this.map.getTile(this.x, this.y-33)==0)
{
this.objects.push(new Bomb(this.x, this.y-33, this.game));
}
else
{
this.objects.push(new Bomb(this.x, this.y, this.game));
}
break;
case "down":
if(this.map.getTile(this.x, this.y+33)==0)
{
this.objects.push(new Bomb(this.x, this.y+32, this.game));
}
else
{
this.objects.push(new Bomb(this.x, this.y, this.game));
}
break;
}
}
this.cxt.drawImage(this.image,
this.animationOffsets[this.currentAnimation][0] + (this.width * this.currentFrame + (this.currentFrame*3)), this.animationOffsets[this.currentAnimation][1],
this.width, this.height,
this.x, this.y, this.width, this.height);
this.lastUpdated+= dt;
this.bombCooldown+= dt;
var pos = this.getPosition();
var tilePos = this.map.getTileIndex(pos.x, pos.y);
this.cxt.strokeStyle = "yellow";
this.cxt.strokeRect(tilePos[1] * 32, tilePos[0]* 32, 32, 32);
if(this.lastUpdated>=100 && !this.idle)
{
this.lastUpdated = 0;
this.currentFrame = (this.currentFrame + 1)%3;
}
}