-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
324 lines (303 loc) · 11.6 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
import attacks from './Data/attacks.js';
import monsters from './Data/monsters.js';
import Monster from './Data/classMonster.js';
import audio from './Data/audio.js';
import gameInitialization from './Data/gameInitialization.js';
const {context, boundaries, battleZones, background, foreground, player, keys, battle, movables, battleBackground} = gameInitialization();
let karen;
let flamby;
let renderedSprites;
let queue = [];
let moving;
let battleAnimationId;
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ///// to start audio only the 1st time the game is loaded /////
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
let firstStart = true;
if (firstStart) {
audio.Map.play();
firstStart = false;
}
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ///// COLLISION /////
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
const rectangularCollision = ({ rectangle1, rectangle2 }) =>
rectangle1.position.x + rectangle1.width >= rectangle2.position.x &&
rectangle1.position.x <= rectangle2.position.x + rectangle2.width &&
rectangle1.position.y + rectangle1.height >= rectangle2.position.y &&
rectangle1.position.y <= rectangle2.position.y + rectangle2.height;
const lookingForCollision = (moveX, moveY, playerImage) => {
for (let i = 0; i < boundaries.length; i++) {
const boundary = boundaries[i];
player.animate = true;
player.image = playerImage;
if (
rectangularCollision({
rectangle1: player,
rectangle2: {
...boundary,
position: {
x: boundary.position.x + moveX,
y: boundary.position.y + moveY,
},
},
})
) {
moving = false;
break;
}
}
};
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ///// TRANSITIONS /////
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: try to find a more clean way to do it
const eventListenerFunctionForDialogueBox = (e) => { //needed to be named in order to be removed
if (queue.length) {
queue[0]();
queue.shift();
} else {
e.currentTarget.style.display = 'none';
}
}
const transitionFromAnimateToBattle = () => {
audio.Map.stop();
audio.initBattle.play();
audio.battle.play();
battle.initiated = true;
gsap.to('#overlappingDiv', {
opacity: 1,
repeat: 3,
yoyo: true,
duration: 0.4,
onComplete() {
gsap.to('#overlappingDiv', {
opacity: 1,
duration: 0.4,
onComplete() {
initBattle(karen, flamby, renderedSprites, battleAnimationId);
animateBattle();
gsap.to('#overlappingDiv', {
opacity: 0,
duration: 0.4,
});
},
});
},
});
};
const transitionFromBattleToAnimate = () => {
document.querySelector('#dialogueBox').removeEventListener('click', eventListenerFunctionForDialogueBox);
battle.initiated = false;
gsap.to('#overlappingDiv', {
opacity: 1,
onComplete: () => {
cancelAnimationFrame(battleAnimationId);
animate();
document.querySelector('#userInterface').style.display = 'none';
gsap.to('#overlappingDiv', {
opacity: 0,
});
audio.Map.play();
},
});
};
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ///// animate /////
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
const animate = () => {
const animationId = window.requestAnimationFrame(animate);
background.draw(context);
boundaries.forEach((boundary) => {
boundary.draw(context);
if (rectangularCollision({ rectangle1: player, rectangle2: boundary })) {
}
});
battleZones.forEach((battleZone) => {
battleZone.draw(context);
});
player.draw(context);
foreground.draw(context);
moving = true;
player.animate = false;
if (battle.initiated) return;
if (keys.w.pressed || keys.a.pressed || keys.s.pressed || keys.d.pressed) {
for (let i = 0; i < battleZones.length; i++) {
const battleZone = battleZones[i];
const overlappingArea =
(Math.min(
player.position.x + player.width,
battleZone.position.x + battleZone.width
) -
Math.max(player.position.x, battleZone.position.x)) *
(Math.min(
player.position.y + player.height,
battleZone.position.y + battleZone.height
) -
Math.max(player.position.y, battleZone.position.y));
player.animate = true;
player.image = player.sprites.up;
if (
rectangularCollision({
rectangle1: player,
rectangle2: battleZone,
}) &&
overlappingArea > (player.width * player.height) / 2 &&
////////////////////////////////////////////////////////////////////////////////////////////////
Math.random() < 0.05 ////// value to modify to trigger battle //////
////////////////////////////////////////////////////////////////////////////////////////////////
) {
window.cancelAnimationFrame(animationId);
transitionFromAnimateToBattle();
break;
}
}
}
if (keys.w.pressed) {
lookingForCollision(0, 3, player.sprites.up);
if (moving) {
movables.forEach((movable) => (movable.position.y += 3));
}
}
if (keys.s.pressed) {
lookingForCollision(0, -3, player.sprites.down);
if (moving) {
movables.forEach((movable) => (movable.position.y -= 3));
}
}
if (keys.a.pressed) {
lookingForCollision(3, 0, player.sprites.left);
if (moving) {
movables.forEach((movable) => (movable.position.x += 3));
}
}
if (keys.d.pressed) {
lookingForCollision(0, 3, player.sprites.right);
if (moving) {
movables.forEach((movable) => (movable.position.x -= 3));
}
}
};
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ///// initBattle /////
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
function initBattle() {
document.querySelector('#userInterface').style.display = 'block';
document.querySelector('#dialogueBox').style.display = 'none';
document.querySelector('#enemyHealthBar').style.width = '100%';
document.querySelector('#playerHealthBar').style.width = '100%';
document.querySelector('#attacksBox').replaceChildren();
audio.Map.stop();
karen = new Monster(monsters.Karen);
flamby = new Monster(monsters.Flamby);
renderedSprites = [karen, flamby];
if(queue.length) { // in order to initialize the array to 0
queue.length = 0;
}
flamby.attacks.forEach((attack) => {
const button = document.createElement('button');
button.innerHTML = attack.name;
document.querySelector('#attacksBox').append(button);
});
// event listener for click on attack button while in battle.
const buttons = document.querySelectorAll('button');
buttons.forEach((button) => {
button.addEventListener('click', async (e) => {
const selectedAttack = attacks[e.currentTarget.innerHTML];
await flamby.attack({
attack: selectedAttack,
recipient: karen,
renderedSprites,
});
if (karen.health <= 0) {
queue.push(() => {
karen.faint();
});
queue.push(() => {
transitionFromBattleToAnimate();
});
}
// if want random attacks could use enemy.attacks[Math.floor(Math.random() * enemy.attacks.length)];
queue.push(async () => {
await karen.attack({
attack: karen.attacks[0],
recipient: flamby,
renderedSprites,
});
});
if (flamby.health <= 0) {
queue.push(() => {
flamby.faint();
});
queue.push(() => {
transitionFromBattleToAnimate();
});
}
});
button.addEventListener('mouseenter', (e) => {
const selectedAttack = attacks[e.currentTarget.innerHTML];
document.querySelector('#attackType').innerHTML = selectedAttack.type;
document.querySelector('#attackType').style.color = selectedAttack.color;
});
});
document.querySelector('#dialogueBox').addEventListener('click', eventListenerFunctionForDialogueBox)
}
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ///// animateBattle /////
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
function animateBattle() {
battleAnimationId = window.requestAnimationFrame(animateBattle);
battleBackground.draw(context);
renderedSprites.forEach((sprite) => {
sprite.draw(context);
});
}
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ///// EventListener /////
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
window.addEventListener('keydown', (e) => {
switch (e.key) {
case 'w':
keys.w.pressed = true;
break;
case 'a':
keys.a.pressed = true;
break;
case 's':
keys.s.pressed = true;
break;
case 'd':
keys.d.pressed = true;
break;
default:
console.log("press 'w' 'a' 's' 'd' to move the player");
}
});
window.addEventListener('keyup', (e) => {
switch (e.key) {
case 'w':
keys.w.pressed = false;
break;
case 'a':
keys.a.pressed = false;
break;
case 's':
keys.s.pressed = false;
break;
case 'd':
keys.d.pressed = false;
break;
default:
break;
}
});
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ///// Start /////
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
animate();
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ///// Start direclty with fight /////
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// initBattle();
// animateBattle();
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////