-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
399 lines (338 loc) · 9.43 KB
/
index.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
const canvas = document.querySelector('canvas') //querySelector = select ELEMENTS from HTML
//const canvas = document.getElementById('canvas')
const c = canvas.getContext('2d')
canvas.width = 1024
canvas.height = 576
c.fillRect(0, 0, canvas.width, canvas.height)
const gravity = 0.7 // downward acceleration as we go down
const background = new Sprite({
position:{
x: 0,
y: 0
},
imageSrc: './img/background.png' // /. start from index.js then img folder then background.png
})
const shop = new Sprite({
position:{
x: 600,
y: 128
},
imageSrc: './img/shop.png', // /. start from index.js then img folder then background.png
scale: 2.75,
framesMax: 6 // SHOP ANIMATION
})
const player = new Fighter({ //create a new object from this Claass (object oriented) following this this.position and this.velocity
position: {
x: 0,
y: 0
},
velocity:{
x: 0,
y: 0
},
offset:{
x: 0,
y: 0
},
imageSrc: './img/samuraiMack/Idle.png',
framesMax: 8,
scale: 2.5,
offset: {
x: 215,
y: 167,
},
sprites: {
idle: {
imageSrc: './img/samuraiMack/Idle.png',
framesMax: 8
},
run: {
imageSrc: './img/samuraiMack/Run.png',
framesMax: 8 //cai số frame á
},
jump: {
imageSrc: './img/samuraiMack/Jump.png',
framesMax: 2 //cai số frame á
},
fall: {
imageSrc: './img/samuraiMack/Fall.png',
framesMax: 2,
},
attack1: {
imageSrc: './img/samuraiMack/Attack1.png',
framesMax: 6,
},
takeHit: {
imageSrc: './img/samuraiMack/Take Hit - white silhouette.png',
framesMax: 4,
},
death: {
imageSrc: './img/samuraiMack/Death.png',
framesMax: 6,
},
},
attackBox:{
offset:{
x: 100,
y: 50,
},
width: 160,
height: 50
},
})
//player.draw() // make it appear!! SHAMBLA!
const enemy = new Fighter({
position: {
x: 400,
y: 100
},
velocity:{
x: 0,
y: 0
},
color: 'blue',
offset:{
x: -50,
y: 0
},
imageSrc: './img/kenji/Idle.png',
framesMax: 4,
scale: 2.5,
offset: {
x: 215,
y: 157,
},
sprites: {
idle: {
imageSrc: './img/kenji/Idle.png',
framesMax: 4,
},
run: {
imageSrc: './img/kenji/Run.png',
framesMax: 8 //cai số frame á
},
jump: {
imageSrc: './img/kenji/Jump.png',
framesMax: 2 //cai số frame á
},
fall: {
imageSrc: './img/kenji/Fall.png',
framesMax: 2,
},
attack1: {
imageSrc: './img/kenji/Attack1.png',
framesMax: 4,
},
takeHit: {
imageSrc: './img/kenji/Take hit.png',
framesMax: 3,
},
death: {
imageSrc: './img/kenji/Death.png',
framesMax: 7,
},
},
attackBox:{
offset:{
x: -170, // objects are drawn from top left corner
y: 50,
},
width: 170,
height: 50
},
})
enemy.draw() // make enemy appear!!
console.log(player)
// WASD keys control
const keys = {
a:{
pressed: false
},
d:{
pressed: false
},
w: {
pressed: false
},
ArrowRight: {
pressed: false
},
ArrowLeft: {
pressed: false
}
}
decreaseTimer()
function animate(){
window.requestAnimationFrame(animate) // which function do I want to REPEAT FRAMES OVER AND OVER AGAIN?? an infinite loop
//console.log('meo') // arbitrary argument 'go'
c.fillStyle = 'black' //black rectangle
c.fillRect(0, 0, canvas.width, canvas.height) // the same as painting the background to make te animation frame by frame
background.update() // cái OOP const background = new Sprite á
shop.update() // LAYER THE SHOP BEFORE THE PLAYER AND AFTERT HE BACKGROUND
// SEPERATE BACKGROUND FROM CHARACTER
c.fillStyle = 'rgba(255,255,255, 0)' // rgba ( red, green, blue, opacity)
c.fillRect(0,0, canvas.width,canvas.height)
player.update() //from line 24, instead of draw
enemy.update() // update = draw + y.postion +=10 lun ó
player.velocity.x = 0 // DEFAULT VELOCITY to 0 after keyup
enemy.velocity.x = 0
// player movement
// player + IDLE movement - default setting:
// sprite này về framesMax = 9
//player.image = player.sprites.idle.image
// player + MOVING movement
if (keys.a.pressed && player.lastKey === 'a'){
player.velocity.x = -5
player.switchSprite('run')
// line 131 below determines what animation goes with what action: determine when RUN animation get plays with which button
//player.image = player.sprites.run.image // select run property
} else if (keys.d.pressed && player.lastKey === 'd'){
player.velocity.x = 5
player.switchSprite('run')
//player.image = player.sprites.run.image
} else {
player.switchSprite('idle')
}
// player + JUMP movement
//if (keys.' '.pressed && player.lastKey === ' '){
if (player.velocity.y < 0){
player.switchSprite('jump')
//player.image = player.sprites.jump.image
//player.framesMax = player.sprites.jump.framesMax // because the framesMax is different with Jump
} else if (player.velocity.y > 0){
player.switchSprite('fall')
}
// enemy movement
if (keys.ArrowLeft.pressed && enemy.lastKey === 'ArrowLeft'){
enemy.velocity.x = -5
enemy.switchSprite('run')
} else if (keys.ArrowRight.pressed && enemy.lastKey === 'ArrowRight'){
enemy.velocity.x = 5
enemy.switchSprite('run')
} else { // when not moving on x-axis
enemy.switchSprite('idle')
}
// enemy + JUMP movement
//if (keys.' '.pressed && player.lastKey === ' '){
if (enemy.velocity.y < 0){
enemy.switchSprite('jump')
//player.image = player.sprites.jump.image
//player.framesMax = player.sprites.jump.framesMax // because the framesMax is different with Jump
} else if (enemy.velocity.y > 0){
enemy.switchSprite('fall')
}
// detect collisions && enemy get hit : if right side attack box >= enemey left side
if (
rectangularCollision({
rectangle1: player,
rectangle2: enemy
}) &&
player.isAttacking &&
player.framesCurrent === 4 // meticulous that the health decreases when hit
) {
enemy.takeHit()
player.isAttacking = false // to stop the animation after action set
//enemy.health -= 20
//document.querySelector('#enemyHealth').style.width = enemy.health + '%'
gsap.to('#enemyHealth', {
width: enemy.health + '%'
})
console.log('player attack succesful')
}
// if player misses the enemy
if (player.isAttacking && player.framesCurrent === 4 ){
player.isAttacking = false
}
// detect collisions && player get hit : if right side attack box >= enemey left side
if (
rectangularCollision({
rectangle1: enemy,
rectangle2: player
}) &&
enemy.isAttacking &&
enemy.framesCurrent === 2
) {
player.takeHit()
enemy.isAttacking = false
//player.health -=20
//document.querySelector('#playerHealth').style.width = player.health + '%'
console.log('enemy attack succesful')
gsap.to('#playerHealth', { //animate smoother healthline
width: player.health + '%'
})
}
// if enemy misses the player
if (enemy.isAttacking && enemy.framesCurrent === 2 ){
enemy.isAttacking = false
}
// END GAME based on Health
if (enemy.health <= 0 || player.health <=0 ){
determineWinner({player, enemy, timerId}) // pass the timerId through to get the time to stop once health === 0
}
}
animate()
//event listener = react to a specific event
// I. when PRESSED KEY DOWN
window.addEventListener('keydown', (event) => {
if (!player.dead){
console.log(event.key)
switch (event.key) {
case 'd':
keys.d.pressed = true //move 1 pixel every d pressed
player.lastKey = 'd'
break //grab key = "d" and do just "if-then"
case 'a':
keys.a.pressed = true
player.lastKey = 'a'
break
case 'w':
player.velocity.y = -20
break
case ' ': //SPACEBAR
player.attack()
break
}
}
if(!enemy.dead){
switch(event.key){ // indepedent switch case
// SECOND PLAYER PROPERTY INDEPEDENT FROM ONE - diffferntlastkey
case 'ArrowRight':
keys.ArrowRight.pressed = true //move 1 pixel every d pressed
enemy.lastKey = 'ArrowRight'
break //grab key = "d" and do just "if-then"
case 'ArrowLeft':
keys.ArrowLeft.pressed = true
enemy.lastKey = 'ArrowLeft'
break
case 'ArrowUp':
enemy.velocity.y = -20
break
case 'ArrowDown':
enemy.attack() // yaes i got this right
break
}
}
console.log(event.key)
})
// II. when RELEASE KEY UP
window.addEventListener('keyup', (event) => {
switch (event.key) {
case 'd':
keys.d.pressed = false //move 1 pixel every d pressed
break //grab key = "d" and do just "if-then"
case 'a':
keys.a.pressed = false
break
// KHÔNG CẦN case 'w':keys.w.pressed = false do đã có gravity làm nhiệm vụ kéo về!
}
switch (event.key) {
case 'ArrowRight':
keys.ArrowRight.pressed = false //move 1 pixel every d pressed
break //grab key = "d" and do just "if-then"
case 'ArrowLeft':
keys.ArrowLeft.pressed = false
break
}
console.log(event.key)
})
// 'keydown' = press any key!!