-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·173 lines (157 loc) · 3.98 KB
/
script.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
//done
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const background = new Image();
background.src = "./assets/backgrounds/background1.png";
background.onload = () => {
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
};
const pauseButton = document.getElementById("pause");
const gameOverPopup = document.getElementById("game-over-pop");
const restartButton = document.getElementById("restart");
const playAgainButton = document.getElementById("play-again");
canvas.width = 1408;
canvas.height = 792;
let gamePaused = false;
let gameOver = false;
const gravity = 0.5;
const keys = {
w: { pressed: false },
a: { pressed: false },
d: { pressed: false },
};
const boxes = [
new Box({
position: { x: canvas.width / 2 + 100, y: canvas.height - 130 },
width: 100,
height: 100,
}),
new Box({
position: { x: canvas.width / 2 + 200, y: canvas.height - 130 },
width: 100,
height: 100,
}),
new Box({
position: { x: canvas.width / 2 + 150, y: canvas.height - 230 },
width: 100,
height: 100,
}),
new Box({
position: { x: canvas.width / 2 - 200, y: canvas.height - 130 },
width: 100,
height: 100,
}),
new Box({
position: { x: canvas.width / 2 - 300, y: canvas.height - 130 },
width: 100,
height: 100,
}),
new Box({
position: { x: canvas.width / 2 - 250, y: canvas.height - 230 },
width: 100,
height: 100,
}),
];
const bottomPlatform = new BottomPlatform({
position: { x: 0, y: canvas.height - 30 },
width: canvas.width,
height: 100,
});
const player = new Player({
position: { x: canvas.width / 2 - 50, y: 500 },
imageSrc: "./assets/hero1/Biker_idle.png",
frameRate: 4,
enlargementRatio: 2.7,
frameBuffer: 6,
gunImageSrc: "./assets/guns/6.png",
guns: [
new Gun({
name: "Normalgun",
bulletVelocity: 20,
fireRate: 20,
damage: 10,
recoil: 10,
bulletGravity: 0.65,
imageSrc: "./assets/guns/Normalgun.png",
}),
new Gun({
//low range but shoots 3 bullets moderate recoil
name: "Shotgun",
bulletVelocity: 20,
fireRate: 750,
damage: 25,
recoil: 30,
bulletGravity: 1.2,
imageSrc: "./assets/guns/Shotgun.png",
}),
new Gun({
//high range high damage high recoil
name: "Rifle",
bulletVelocity: 30,
fireRate: 750,
damage: 50,
recoil: 50,
bulletGravity: 0.5,
imageSrc: "./assets/guns/Rifle.png",
}),
],
animations: {
Idle: {
imageSrc: "./assets/hero1/Biker_idle.png",
frameRate: 4,
frameBuffer: 6,
},
Run: {
imageSrc: "./assets/hero1/Biker_run_right.png",
frameRate: 6,
frameBuffer: 6,
},
Jump: {
imageSrc: "./assets/hero1/Biker_jump.png",
frameRate: 4,
frameBuffer: 15,
},
Dead: {
imageSrc: "./assets/hero1/Biker_death.png",
frameRate: 6,
frameBuffer: 15,
},
Hurt: {
imageSrc: "./assets/hero1/Biker_hurt.png",
frameRate: 2,
frameBuffer: 5,
},
},
boxes,
});
const gameLogic = new GameLogic({
player,
boxes,
bottomPlatform: [bottomPlatform], //here im sending the bottom platform as an array because im iterating over it during powerUp collision
});
function mainGameLoop() {
if (gamePaused) {
return;
}
if (player.playerIsDead()) {
gameOverFunction();
return;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
// bat.update();
boxes.forEach((box) => {
box.draw();
player.collisionBetweenPlayerAndPlatform(box);
});
player.update();
gameLogic.update();
player.collisionBetweenPlayerAndPlatform(bottomPlatform);
bottomPlatform.draw("transparent");
// Update and draw the bat
requestAnimationFrame(mainGameLoop);
}
mainGameLoop();
pauseButton.addEventListener("click", pauseGame);
restartButton.addEventListener("click", resetGame);
playAgainButton.addEventListener("click", resetGame);