-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
341 lines (283 loc) · 9.23 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
audio.Map.play();
/* Setting up the canvas */
//get the canvas element from html
const canvas = document.querySelector('canvas');
//get the 2D context object
const context = canvas.getContext('2d');
//set the dimensions of the canvas
canvas.width = 1024;
canvas.height = 576;
/* Setting up the map */
//create an img element of the map
const mapImage = new Image();
mapImage.src = './Assets/Images/Pellet Town.png';
const tilesPerWidth = 70; //the map has 70 tiles as width
//create the foreground image
//create an img element of the map
const foregroundImage = new Image();
foregroundImage.src = './Assets/Images/foreground.png';
//create the images of the player
const playerDownImage = new Image();
playerDownImage.src = './Assets/Images/playerDown.png';
const playerUpImage = new Image();
playerUpImage.src = './Assets/Images/playerUp.png';
const playerLeftImage = new Image();
playerLeftImage.src = './Assets/Images/playerLeft.png';
const playerRightImage = new Image();
playerRightImage.src = './Assets/Images/playerRight.png';
//offset the map to get into the middle
const offset = {
x: -740,
y: -645,
}
//create a background object
const background = new Sprite({
position: offset,
image: mapImage
});
//create a foreground object
const foreground = new Sprite({
position: offset,
image: foregroundImage,
});
//create the player object
const playerImageWidth = 192;
const playerImageHeight = 68;
const player = new Sprite({
position: {
x: canvas.width / 2 - playerImageWidth /8,
y: canvas.height / 2 - playerImageHeight /2,
},
image: playerDownImage,
frames: {
max: 4,
hold: 10,
},
velocity: 5,
sprites: {
up: playerUpImage,
down: playerDownImage,
left: playerLeftImage,
right: playerRightImage,
}
})
//set up the collisions map
collisionsMap = [];
for(let i = 0; i < collisions.length; i+=tilesPerWidth) {
collisionsMap.push(collisions.slice(i, i+tilesPerWidth));
}
const boundaries = [];
collisionsMap.forEach((row, i) => {
row.forEach((symbol, j) => {
if(symbol === 1025) boundaries.push( new Boundary({
position: {
x: j * Boundary.width + offset.x,
y: i * Boundary.height + offset.y,
}
}))
});
});
//set up the battle zones map
const battleZonesMap = [];
for(let i = 0; i < battleZonesData.length; i+=tilesPerWidth) {
battleZonesMap.push(battleZonesData.slice(i, i+tilesPerWidth));
}
const battleZones = [];
battleZonesMap.forEach((row, i) => {
row.forEach((symbol, j) => {
if(symbol === 1025) battleZones.push( new Boundary({
position: {
x: j * Boundary.width + offset.x,
y: i * Boundary.height + offset.y,
}
}))
});
});
//setup the keys object
const keys = {
w: {
pressed: false,
},
a: {
pressed: false,
},
s: {
pressed: false,
},
d: {
pressed: false,
},
lastKey: ''
}
//group movable objects in one array
const movables = [background, ...boundaries, ...battleZones];
//setup the collision detection function
const rectangularCollision = (player, boundary) => {
return player.position.x + player.width >= boundary.position.x
&& player.position.x <= boundary.position.x + Boundary.width
&& player.position.y <= boundary.position.y + Boundary.width
&& player.position.y + player.height >= boundary.position.y
}
const battle = {
initiated: false,
}
//set up the animation function
function animate(){
const animationID = window.requestAnimationFrame(animate);
//draw the map on the canvas
background.draw();
//draw the boundaries
boundaries.forEach(boundary => {
boundary.draw();
})
//draw the battlezones
battleZones.forEach(battleZone => {
battleZone.draw();
})
//draw the player image cropped
player.draw();
foreground.draw();
player.animate = false;
if(battle.initiated) return;
//check if the player is moving in a battle zone
if(keys.w.pressed || keys.a.pressed || keys.s.pressed || keys.d.pressed){
let overlapingArea = 0;
for(let i = 0; i < battleZones.length; i++){
const battleZone = battleZones[i];
//calculate the area of the intersection between the player and the battle zone
overlapingArea = (Math.min(player.position.x + player.width, battleZone.position.x + Boundary.width) - Math.max(player.position.x, battleZone.position.x))
* (Math.min(player.position.y + player.height, battleZone.position.y + Boundary.height) - Math.max(player.position.y, battleZone.position.y));
if(rectangularCollision(player, battleZone)
&& overlapingArea > (player.width * player.height) / 2
&& Math.random() < 0.05){
//deactivate the current animatino loop
window.cancelAnimationFrame(animationID);
///Activate a battle
battle.initiated = true;
audio.Map.stop();
audio.initBattle.play();
audio.Battle.play();
gsap.to('#overlappingDiv', {
opacity: 1,
repeat: 3,
yoyo: true,
duration: 0.4,
onComplete(){
gsap.to('#overlappingDiv', {
opacity: 1,
duration: 0.4,
onComplete(){
//activate a new animation loop
initBattle();
animateBattle();
gsap.to('#overlappingDiv', {
opacity: 0,
duration: 0.4,
})
}
})
}
})
break;
}
}
}
//move the movables according to the keys
if(keys.w.pressed && keys.lastKey === 'w'){
player.animate = true;
player.image = player.sprites.up;
for(let i = 0; i < boundaries.length; i++){
const boundary = boundaries[i];
if(rectangularCollision(player, { ...boundary, position: {
x: boundary.position.x,
y: boundary.position.y + player.velocity,
}})
){
player.animate = false;
break;
}
}
if(player.animate){
movables.forEach( movable => movable.position.y += player.velocity);
}
}
else if(keys.a.pressed && keys.lastKey === 'a') {
player.animate = true;
player.image = player.sprites.left;
for(let i = 0; i < boundaries.length; i++){
const boundary = boundaries[i];
if(rectangularCollision(player, { ...boundary, position: {
x: boundary.position.x + player.velocity,
y: boundary.position.y,
}})
){
player.animate = false;
break;
}
}
if(player.animate){
movables.forEach( movable => movable.position.x += player.velocity);
}
}
else if(keys.s.pressed && keys.lastKey === 's'){
player.animate = true;
player.image = player.sprites.down;
for(let i = 0; i < boundaries.length; i++){
const boundary = boundaries[i];
if(rectangularCollision(player, { ...boundary, position: {
x: boundary.position.x,
y: boundary.position.y - player.velocity,
}})
){
player.animate = false;
break;
}
}
if(player.animate){
movables.forEach( movable => movable.position.y -= player.velocity);
}
}
else if(keys.d.pressed && keys.lastKey === 'd'){
player.animate = true;
player.image = player.sprites.right;
for(let i = 0; i < boundaries.length; i++){
const boundary = boundaries[i];
if(rectangularCollision(player, { ...boundary, position: {
x: boundary.position.x - player.velocity,
y: boundary.position.y,
}})
){
player.animate = false;
break;
}
}
if(player.animate){
movables.forEach( movable => movable.position.x -= player.velocity);
}
}
}
animate();
/* Setting up the move controls */
addEventListener('keydown', ( { key } )=>{
switch (key) {
case 'w':
keys.w.pressed = true;
keys.lastKey = key;
break;
case 'a':
keys.a.pressed = true;
keys.lastKey = key;
break;
case 's':
keys.s.pressed = true;
keys.lastKey = key;
break;
case 'd':
keys.d.pressed = true;
keys.lastKey = key;
break;
}
})
addEventListener('keyup', ( { key}) => {
keys[key] ? keys[key].pressed = false : '';
})