-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.js
More file actions
310 lines (282 loc) ยท 11.7 KB
/
player.js
File metadata and controls
310 lines (282 loc) ยท 11.7 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
######################################
## JS Canvas Game - Quadrant Patrol ##
###################################### */
const playerImage = new Image();
playerImage.src = './images/ship_player.png';
const exhaustImage = new Image();
exhaustImage.src = './images/ship_player_exhaust.png';
const shieldImage = new Image();
shieldImage.src = './images/shield.png';
const powerBar = new Image();
powerBar.src = './images/ui_power_bar.png';
const powerUnit = new Image();
powerUnit.src = './images/ui_power_bar_unit.png';
const healthBar = new Image();
healthBar.src = './images/ui_health_bar.png';
const healthUnit = new Image();
healthUnit.src = './images/ui_health_bar_unit.png';
const dummyUnit = new Image();
dummyUnit.src = './images/ui_dummy_bar_unit.png';
class Player {
constructor(game, x, y) {
this.game = game;
this.width = 60;
this.height = 30;
// top player shape
this.widthXT = 22;
this.heightXT = 36;
this.xOffsetWings = 0;
this.animation = 6;
this.frame = 0;
this.exhaustFrame = 1;
this.animationStart = 0;
this.animationRunning = false;
// center player x-position
this.x = x - (this.width / 2);
this.y = y;
this.maxHealth = 3500;
this.healthUnits = 21;
this.health = this.maxHealth;
this.healthGain = 100;
this.powerGain = 1500;
this.powerUnits = 0;
this.shieldPower = 0;
this.wingsPower = 0;
this.shieldsUp = false;
this.wingsUp = false;
this.lives = 3;
}
checkIntersection(element) {
// ### Checking up to two shapes (enemy) against two shapes (player) ###
return (
// turns true if right side of element is beyond left side of player's base shape
element.x + element.width >= this.x &&
// turns true if left side of element is beyond right side of player's base shape
element.x <= this.x + this.width &&
// turns true if top edge of element is above bottom edge of player's base shape
element.y <= this.y + this.height &&
// turns true if bottom side of element is beyond top side of player's base shape
element.y + element.height >= this.y
) || (
// turns true if right side of element is beyond left side of player's cockpit shape
element.x + element.width >= this.x + 19 + this.xOffsetWings &&
// turns true if left side of element is beyond right side of player's cockpit shape
element.x <= this.x + 19 + this.xOffsetWings + this.widthXT &&
// turns true if top edge of element is above bottom edge of player's cockpit shape
element.y <= this.y - 36 + this.heightXT &&
// turns true if bottom side of element is beyond top side of player's cockpit shape
element.y + element.height >= this.y - 36
) || (
// turns true if right side of enemy cockpit is beyond left side of player's base shape
element.x + 22 + element.widthXT >= this.x &&
// turns true if left side of enemy cockpit is beyond right side of player's base shape
element.x + 22 <= this.x + this.width &&
// turns true if top edge of enemy cockpit is above bottom edge of player's base shape
element.y + 36 <= this.y + this.height &&
// turns true if bottom side of enemy cockpit is beyond top side of player's base shape
element.y + 36 + element.heightXT >= this.y
) || (
// turns true if right side of enemy cockpit is beyond left side of player's cockpit shape
element.x + 22 + element.widthXT >= this.x + 19 + this.xOffsetWings &&
// turns true if left side of enemy cockpit is beyond right side of player's cockpit shape
element.x + 22 <= this.x + 19 + this.xOffsetWings + this.widthXT &&
// turns true if top edge of enemy cockpit is above bottom edge of player's cockpit shape
element.y + 36 <= this.y - 36 + this.heightXT &&
// turns true if bottom side of enemy cockpit is beyond top side of player's cockpit shape
element.y + 36 + element.heightXT >= this.y - 36
);
}
checkIntersectionShield(element) {
// ### Circle collision check player shield aura ###
var distX = Math.abs(this.x + 55 - element.x - element.width / 2);
var distY = Math.abs(this.y + 24 - element.y - element.height / 2);
if (distX > (element.width / 2 + 56)) { return false; }
if (distY > (element.height / 2 + 56)) { return false; }
if (distX <= (element.width / 2)) { return true; }
if (distY <= (element.height / 2)) { return true; }
var dx = distX - element.width / 2;
var dy = distY - element.height / 2;
return (dx * dx + dy * dy <= (56 * 56));
}
moveLeft() {
this.x -= 3.5;
// ### player model rolls left ###
if (((Date.now() - this.animationStart) > 35) && this.animationStart !== 0) {
if (this.animation > 1) this.animation--;
this.animationStart = Date.now();
}
}
moveRight() {
this.x += 3.5;
// ### player model rolls right ###
if (((Date.now() - this.animationStart) > 35) && this.animationStart !== 0) {
if (this.animation < 11) this.animation++;
this.animationStart = Date.now();
}
}
fireProjectile() {
if (Date.now() - this.game.shotStamp > 183) {
// ### Fire double projectiles ###
const projectile1 = new Projectile(this, this.x + this.width / 2 - 15 - 3, this.y);
const projectile2 = new Projectile(this, this.x + (this.width / 2) + 15 - 3, this.y);
this.game.playerProjectiles.push(projectile1, projectile2);
// ### Fire powershots ###
if (this.wingsUp) {
const projectile3 = new Projectile(this, this.x + this.width / 2 - 44 - 3, this.y);
const projectile4 = new Projectile(this, this.x + (this.width / 2) + 44 - 3, this.y);
this.game.playerProjectiles.push(projectile3, projectile4);
}
this.game.shotStamp = Date.now();
}
}
activateShield() {
// ### Put shield up ###
if ((this.shieldPower > 0) && (this.shieldsUp === false)) {
this.x -= 25;
this.width += 50;
this.y -= 25;
this.height += 25;
this.shieldsUp = true;
}
}
lowerShield() {
// ### Lower shield ###
this.x += 25;
this.width -= 50;
this.y += 25;
this.height -= 25;
this.shieldsUp = false;
}
wingsOn() {
// ### Activate wings ###
if ((this.wingsPower > 0) && (this.wingsUp === false)) {
this.x -= 25;
this.xOffsetWings = 25;
this.width += 50;
this.wingsUp = true;
}
}
rollBack() {
// ### Roll back into horizontal position ###
if (this.animationStart === 0) {
this.animationStart = Date.now();
}
if (Date.now() - this.animationStart > 35) {
this.animation < 6 ? this.animation++ : this.animation--;
this.animationStart = Date.now();
}
}
wingsOff() {
// ### Disable wings ###
this.x += 25;
this.xOffsetWings = 0;
this.width -= 50;
this.wingsUp = false;
}
drawPlayer() {
// ### Draw player ship ###
this.game.context.drawImage(playerImage, (this.animation - 1) * 151, 0, 151, 151, Math.floor(this.x - 30), Math.floor(this.y - 45), 120, 80);
// ### Draw player ship exhaust ###
this.game.context.drawImage(exhaustImage, (128 * this.exhaustFrame - 128) - ((Math.ceil(this.exhaustFrame / 8)) * 1024 - 1024), (0 + Math.floor((this.exhaustFrame - 1) / 8) * 128), 128, 128, Math.floor(this.x + 5.5), Math.floor(this.y + 25), 50, 50);
}
animateExhaust() {
// ### Animate player ship exhaust ###
if (this.frame > 5) {
this.exhaustFrame++;
this.frame = 0;
if (this.exhaustFrame > 32) this.exhaustFrame = 1;
}
}
drawShields() {
// ### Draw shield state ### //
// draw player ship
this.game.context.drawImage(playerImage, (this.animation - 1) * 151, 0, 151, 151, Math.floor(this.x - 5), Math.floor(this.y - 20), 120, 80);
// draw player ship exhaust
this.game.context.drawImage(exhaustImage, (128 * this.exhaustFrame - 128) - ((Math.ceil(this.exhaustFrame / 8)) * 1024 - 1024), (0 + Math.floor((this.exhaustFrame - 1) / 8) * 128), 128, 128, Math.floor(this.x + 30.5), Math.floor(this.y + 50), 50, 50);
// draw player shield
this.game.context.drawImage(shieldImage, Math.floor(this.x - 5), Math.floor(this.y - 36), 120, 120);
this.game.context.beginPath();
this.game.context.stokeStyle = 'white';
// this.game.context.arc(this.x + 55, this.y + 24, 56, 0, 2 * Math.PI);
this.game.context.stroke();
}
drawWings() {
// ### Draw wingmen state ### //
// draw player ship
this.game.context.drawImage(playerImage, (this.animation - 1) * 151, 0, 151, 151, Math.floor(this.x - 5), Math.floor(this.y - 45), 120, 80);
// draw player ship exhaust
this.game.context.drawImage(exhaustImage, (128 * this.exhaustFrame - 128) - ((Math.ceil(this.exhaustFrame / 8)) * 1024 - 1024), (0 + Math.floor((this.exhaustFrame - 1) / 8) * 128), 128, 128, Math.floor(this.x + 30.5), Math.floor(this.y + 25), 50, 50);
// draw left wingman
this.game.context.drawImage(playerImage, (this.animation - 1) * 151, 0, 151, 151, Math.floor(this.x - 26), Math.floor(this.y - 18), 75, 50);
// draw left wingman exhaust
this.game.context.drawImage(exhaustImage, (128 * this.exhaustFrame - 128) - ((Math.ceil(this.exhaustFrame / 8)) * 1024 - 1024), (0 + Math.floor((this.exhaustFrame - 1) / 8) * 128), 128, 128, Math.floor(this.x - 3), Math.floor(this.y + 27), 30, 30);
// draw right wingman
this.game.context.drawImage(playerImage, (this.animation - 1) * 151, 0, 151, 151, Math.floor(this.x + 61), Math.floor(this.y - 18), 75, 50);
// draw left wingman exhaust
this.game.context.drawImage(exhaustImage, (128 * this.exhaustFrame - 128) - ((Math.ceil(this.exhaustFrame / 8)) * 1024 - 1024), (0 + Math.floor((this.exhaustFrame - 1) / 8) * 128), 128, 128, Math.floor(this.x + 84), Math.floor(this.y + 27), 30, 30);
}
checkBoundaries() {
// ### Prevent player from moving out of the canvas ###
if (!this.shieldsUp) {
if ((this.x + this.width) >= this.game.canvas.width) {
this.x = this.game.canvas.width - this.width;
} else if (this.x <= 0) {
this.x = 0;
}
} else {
// ### Let shields go out of canvas ###
if ((this.x + this.width - 25) >= this.game.canvas.width) {
this.x = this.game.canvas.width - this.width + 25;
} else if (this.x <= -25) {
this.x = -25;
}
}
}
runLogic() {
// ### Drain shieldPower ###
if (this.shieldsUp && this.shieldPower > 0) {
setTimeout(() => {
this.shieldPower -= 1;
}, 20);
}
// ### Remove shield at no power ###
if ((this.shieldPower <= 0) && (this.shieldsUp)) {
this.x += 25;
this.width -= 50;
this.y += 25;
this.height -= 25;
this.shieldsUp = false;
}
// ### Drain wingsPower ###
if (this.wingsUp && this.wingsPower > 0) {
setTimeout(() => {
this.wingsPower -= 1;
}, 20);
}
// ### Remove wings at no power ###
if ((this.wingsPower <= 0) && (this.wingsUp)) {
this.x += 25;
this.width -= 50;
this.wingsUp = false;
}
// ### Roll back to center position ###
if ((game.controls['ArrowLeft'].pressed === false) && (game.controls['ArrowRight'].pressed === false) && this.animation !== 6) {
this.rollBack();
}
// ### Calculate health units treshold ###
this.healthUnits = Math.ceil(this.health / (this.maxHealth / 21));
this.animateExhaust();
// ### Calculate power units treshold ###
if (this.shieldPower > 0) {
this.powerUnits = Math.floor(this.shieldPower / (this.powerGain / 21));
} else if (this.wingsPower > 0) {
this.powerUnits = Math.floor(this.wingsPower / (this.powerGain / 21))
}
}
drawLives() {
for (let i = 0; i < this.lives; i++) {
this.game.context.drawImage(playerImage, 755, 0, 151, 151, 435 - i * 34, 14, 48, 32);
}
}
}