generated from hi337/Canvas-Game-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
297 lines (271 loc) · 8.42 KB
/
main.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
//variables
let minuite,
score = 0;
let top_score = +window.localStorage.getItem("top_score") || 0;
let acceleration = 0;
let paused = false;
let allow_pause = true;
let shake_frame_count = 0;
let shot = false;
let shot_hold = false;
let shot_sound = new Audio("audio/shot.mp3"); //loading audio file for shot sound
//preloading images
let east = new Image();
let north = new Image();
let northeast = new Image();
let northwest = new Image();
let south = new Image();
let southeast = new Image();
let southwest = new Image();
let west = new Image();
east.src = "./img/ship-E.png";
north.src = "./img/ship-N.png";
northeast.src = "./img/ship-NE.png";
northwest.src = "./img/ship-NW.png";
south.src = "./img/ship-S.png";
southeast.src = "./img/ship-SE.png";
southwest.src = "./img/ship-SW.png";
west.src = "./img/ship-W.png";
function endPause() {
pause = true;
myGameArea.canvas.style.display = "block";
document.getElementById("play_screen").style.display = "none";
startGame();
}
//initialization of the game area and components
function startGame() {
myGameArea.start();
mainCharacter = new component(60, 60, mainCharx, mainChary);
borderTop = new border_comp(700, 10, 0, 0, "top");
borderBottom = new border_comp(700, 10, 0, 390, "bottom");
borderLeft = new border_comp(10, 393, 0, 10, "left");
borderRight = new border_comp(10, 393, 690, 10, "right");
myScore = new text_comp("12px", "Consolas", "white", 200, 40);
topScore = new text_comp("12px", "Consolas", "white", 310, 40);
Health = new text_comp("12px", "Consolas", "white", 445, 40);
pausedText = new text_comp("30px", "Consolas", "white", 310, 200);
gameOverText = new text_comp("30px", "Consolas", "white", 290, 200);
}
//Where the canvas element is initialized and controlled
var myGameArea = {
canvas: document.createElement("canvas"),
start: function () {
this.canvas.width = 700;
this.canvas.height = 400;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[10]);
this.interval = setInterval(updateGameArea, 20);
window.addEventListener("keydown", function (e) {
if (allow_pause) {
if (e.keyCode == 80) {
if (!paused) {
paused = true;
} else {
paused = false;
}
}
allow_pause = false;
}
myGameArea.keys = myGameArea.keys || {};
myGameArea.keys[e.keyCode] = true;
});
window.addEventListener("keyup", function (e) {
if (e.keyCode == 80) {
allow_pause = true;
} else {
myGameArea.keys[e.keyCode] = false;
}
});
},
clear: function () {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
stop: function () {
clearInterval(this.interval);
window.addEventListener("keydown", function (e) {
if (e.keyCode == 82) {
location.reload();
}
});
},
};
//what happens everytime the frame updates
function updateGameArea() {
//detecting r for reset
if (myGameArea.keys && myGameArea.keys[82]) {
location.reload();
}
if (!paused) {
if (mainCharacter.crashWith(borderTop)) {
mainCharacter.y = 330;
} else if (mainCharacter.crashWith(borderBottom)) {
mainCharacter.y = 13;
} else if (mainCharacter.crashWith(borderRight)) {
mainCharacter.x = 16;
} else if (mainCharacter.crashWith(borderLeft)) {
mainCharacter.x = 630;
}
myGameArea.clear();
borderTop.update();
borderBottom.update();
borderLeft.update();
borderRight.update();
//detecting r for reset and p for pause
if (myGameArea.keys && myGameArea.keys["r"]) {
location.reload();
}
time_before_next_shot++;
if (time_before_next_shot >= interval) {
choose_shooting_border();
if (chosen_border == "top") {
borderTop.shoot();
} else if (chosen_border == "bottom") {
borderBottom.shoot();
} else if (chosen_border == "left") {
borderLeft.shoot();
} else if (chosen_border == "right") {
borderRight.shoot();
}
time_before_next_shot = 0;
if (interval > 10) {
interval -= 0.1;
}
}
//detect if a border bullet hits a border or mainChar
border_bullet_arr.forEach((bullet, bulletIndex) => {
//detect collision with border
if (
bullet.crashWith(borderBottom) ||
bullet.crashWith(borderLeft) ||
bullet.crashWith(borderTop) ||
bullet.crashWith(borderRight)
) {
border_bullet_arr.splice(bulletIndex, 1);
}
//detect collision with mainChar
if (mainCharacter.crashWith(bullet)) {
border_bullet_arr.splice(bulletIndex, 1);
health -= 1;
shot = true;
myGameArea.canvas.classList.add("shake_screen");
}
//newPos() and update()
bullet.newPos();
bullet.update();
});
//detect mainChar bullet hitting border or enemy bullet
mainChar_bullet_arr.forEach((mainChar_bullet, mainChar_bulletIndex) => {
//detect collision with border
if (
mainChar_bullet.crashWith(borderBottom) ||
mainChar_bullet.crashWith(borderLeft) ||
mainChar_bullet.crashWith(borderTop) ||
mainChar_bullet.crashWith(borderRight)
) {
mainChar_bullet_arr.splice(mainChar_bulletIndex, 1);
}
//detect collision with enemy bullet
border_bullet_arr.forEach((bullet, bulletIndex) => {
if (bullet.crashWith(mainChar_bullet)) {
mainChar_bullet_arr.splice(mainChar_bulletIndex, 1);
border_bullet_arr.splice(bulletIndex, 1);
score += 1;
}
});
});
//detect mainChar bullet hitting border
for (var z = 0; z < mainChar_bullet_arr.length; z++) {
if (
mainChar_bullet_arr[z].crashWith(borderBottom) ||
mainChar_bullet_arr[z].crashWith(borderTop) ||
mainChar_bullet_arr[z].crashWith(borderLeft) ||
mainChar_bullet_arr[z].crashWith(borderRight)
) {
delete mainChar_bullet_arr[z];
}
mainChar_bullet_arr = mainChar_bullet_arr.filter(
(item) => item !== undefined
);
mainChar_bullet_arr[z].newPos();
mainChar_bullet_arr[z].update();
}
//processing how long to shake the screen
if (shot) {
shake_frame_count += 1;
if (shake_frame_count > 30) {
myGameArea.canvas.classList.remove("shake_screen");
shot = false;
shake_frame_count = 0;
}
}
// processing the one point per second rule
if (minuite < 50) {
minuite++;
} else {
minuite = 0;
score++;
}
//changing top_score for efficient instant adjustment
if (score > top_score) {
top_score++;
}
topScore.text = `TOP SCORE: ${top_score}`;
topScore.update();
//changing the text for myScore
myScore.text = `SCORE: ${score}`;
myScore.update();
//update health
if (health <= 0) {
window.localStorage.setItem("top_score", top_score);
if (shot) {
myGameArea.canvas.classList.add("shake_screen");
setTimeout(() => {
myGameArea.canvas.classList.remove("shake_screen");
}, 1000);
}
gameOverText.text = "GAME OVER";
gameOverText.update();
myGameArea.stop();
}
Health.text = `HEALTH: ${health}`;
Health.update();
//detecting shift to accelerate
if (myGameArea.keys && myGameArea.keys[16]) {
acceleration = 3;
} else {
acceleration = 0;
}
//Moving the character
mainCharacter.speedX = 0;
mainCharacter.speedY = 0;
if (myGameArea.keys && myGameArea.keys[65]) {
mainCharacter.speedX = -2 - acceleration;
}
if (myGameArea.keys && myGameArea.keys[68]) {
mainCharacter.speedX = 2 + acceleration;
}
if (myGameArea.keys && myGameArea.keys[87]) {
mainCharacter.speedY = -2 - acceleration;
}
if (myGameArea.keys && myGameArea.keys[83]) {
mainCharacter.speedY = 2 + acceleration;
}
//initiate a shot from mainChar
if (myGameArea.keys && myGameArea.keys[32]) {
if (!shot_hold) {
mainCharacter.shoot();
shot_hold = true;
console.log("shot");
}
} else {
shot_hold = false;
}
mainCharacter.newPos(); //adjust x and y based on inputs
mainCharacter.update_direction_facing(); //update the direction mainChar faces
mainCharacter.update(); //draw new mainChar spot
} else {
myGameArea.clear();
pausedText.text = "PAUSED";
pausedText.update();
}
}