-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
353 lines (341 loc) · 12.6 KB
/
script.js
File metadata and controls
353 lines (341 loc) · 12.6 KB
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
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
canvas.width = 900;
canvas.height = 600;
// global variables ====================================================================================================
const cellSize = 100;
const cellGap = 3;
let numberOfResources = 300;
// was 600
let enemiesInterval = 100;
let frame = 0;
let gameOver = false;
let score = 0;
const winningScore = 1000;
const gameGrid = [];
const defenders = [];
const enemies = [];
const enemyPositions = [];
const projectiles = [];
const resources = [];
// mouse
const mouse = {
x: 10,
y: 10,
width: 0.1,
height: 0.1,
}
let canvasPosition = canvas.getBoundingClientRect();
canvas.addEventListener('mousemove', function(e){
// with this, it will show the right mouse coordinate when hover over canvas
mouse.x = e.x - canvasPosition.left;
mouse.y = e.y - canvasPosition.top;
});
canvas.addEventListener('mouseleave', function(){
mouse.y = undefined;
mouse.y = undefined;
});
// game board
const controlsBar = {
width: canvas.width,
height: cellSize,
}
class Cell {
constructor(x, y){
this.x = x;
this.y = y;
this.width = cellSize;
this.height = cellSize;
}
draw(){
if (mouse.x && mouse.y && collision(this, mouse)){
ctx.strokeStyle = 'black';
ctx.strokeRect(this.x, this.y, this.width, this.height);
}
}
}
function createGrid(){
// with the following two for loops, it will create a 100x100 space form the top left (not including the toolbar)
// to the bottom right with this loop to cover the canvas with cell objects
for (let y = cellSize; y < canvas.height; y += cellSize){
for (let x = 0; x < canvas.width; x += cellSize){
gameGrid.push(new Cell(x, y));
}
}
}
createGrid();
// going through each cell and drawing each one
function handleGameGrid(){
// i is all the cell objects in the game grid array create by the two for loops above
for (let i = 0; i < gameGrid.length; i++){
gameGrid[i].draw();
}
}
// projectiles =========================================================================================================
class Projectile {
constructor(x, y){
this.x = x;
this.y = y;
this.width = 10;
this.height = 10;
this.power = 20;
this.speed = 5;
}
update(){
this.x += this.speed;
}
draw(){
ctx.fillStyle = 'black';
ctx.beginPath();
ctx.arc(this.x, this.y, this.width, 0, Math.PI * 2);
ctx.fill();
}
}
function handleProjectiles(){
for (let i = 0; i < projectiles.length; i++){
projectiles[i].update();
projectiles[i].draw();
for (let j = 0; j < enemies.length; j++){
// check collision of the projectile and the enemy
if (enemies[j] && projectiles[i] && collision(projectiles[i], enemies[j])){
// enemy health minus the projectile power
enemies[j].health -= projectiles[i].power;
// removes the projectile when hit an enemy
projectiles.splice(i, 1);
i--;
}
}
// projectiles stop at the end so that it doesn't hit new enemies coming in
if (projectiles[i] && projectiles[i].x > canvas.width - cellSize){
projectiles.splice(i, 1);
i--;
}
}
}
// defenders ===========================================================================================================
// this is a blueprint so when we make a new defender we call this
class Defender {
constructor(x, y){
this.x = x;
this.y = y;
// makes the defender smaller a bit because it will take damage on the corner touches it
this.width = cellSize - cellGap * 2;
this.height = cellSize - cellGap * 2;
this.shooting = false;
this.health = 100;
this.projectiles = [];
this.timer = 0;
}
draw(){
ctx.fillStyle = 'blue';
ctx.fillRect(this.x, this.y, this.width, this.height);
ctx.fillStyle = 'gold';
ctx.font = '30px Orbitron';
ctx.fillText(Math.floor(this.health), this.x + 15, this.y + 30);
}
update(){
if (this.shooting){
this.timer++;
// each defender has their own timer so that they don't shoot at the same time
if (this.timer % 1 === 0){
projectiles.push(new Projectile(this.x + 70, this.y + 50));
}
} else {
this.timer = 0;
}
}
}
canvas.addEventListener('click', function(){
// for example if mouse position is 250 and cell is 100 so 250 % 100 is 50
// with that 50 it's will be 250 - 50 = 200 so the cell would be the closest to 200
const gridPositionX = mouse.x - (mouse.x % cellSize) + cellGap;
const gridPositionY = mouse.y - (mouse.y % cellSize) + cellGap;
// this will make no defenders placed on the toolbar
if (gridPositionY < cellSize) return;
// the following will see if there is a defender in that spot, if there is no new defender is placed
for (let i = 0; i < defenders.length; i++){
// same grid position
if (defenders[i].x === gridPositionX && defenders[i].y === gridPositionY) return;
}
// let variable so that it can be changed depending on the type of defender we choose
let defenderCost = 100;
if (numberOfResources >= defenderCost){
defenders.push(new Defender(gridPositionX, gridPositionY));
numberOfResources -= defenderCost;
}
});
function handleDefenders(){
for (let i = 0; i < defenders.length; i++){
defenders[i].draw();
defenders[i].update();
// with indexOf and not -1, it means that it didn't find anything form this value in this array
if (enemyPositions.indexOf(defenders[i].y) !== -1){
defenders[i].shooting = true;
} else {
defenders[i].shooting = false;
}
for (let j = 0; j < enemies.length; j++){
// checks of the defenders and enemies touch, if so make defender take damage
// put defenders[i] in front so that the error goes away as we want to check of there is a defender there
// and collision
if (defenders[i] && collision(defenders[i], enemies[j])){
// removes element form array
// i = position of defender of health less than 0 in the defenders array
// 1 = removes 1 element in this index
enemies[j].movement = 0;
// Damage taken
defenders[i].health -= 1;
}
if (defenders[i] && defenders[i].health <= 0){
defenders.splice(i, 1);
// makes it so that next element doesn't get skipped
i--;
enemies[j].movement = enemies[j].speed;
}
}
}
}
// enemies =============================================================================================================
class Enemy {
constructor(verticalPosition){
this.x = canvas.width;
this.y = verticalPosition;
// the following two made it so that the defender will shoot as the cell matches the condition to shoot
this.width = cellSize - cellGap * 2;
this.height = cellSize - cellGap * 2;
// this.speed = Math.random() * 0.2 + 0.4;
this.speed = Math.random() * 0.2 + 2;
this.movement = this.speed;
this.health = 100;
// this here make it possible to change the reward depending on their max health
this.maxHealth = this.health;
}
update(){
this.x -= this.movement;
}
draw(){
ctx.fillStyle = 'red';
ctx.fillRect(this.x, this.y, this.width, this.height);
ctx.fillStyle = 'black';
ctx.font = '30px Orbitron';
ctx.fillText(Math.floor(this.health), this.x + 15, this.y + 30);
}
}
function handleEnemies(){
for (let i = 0; i < enemies.length; i++){
enemies[i].update();
enemies[i].draw();
if (enemies[i].x < 0){
gameOver = true;
}
// removes enemy if health is 0
if (enemies[i].health <= 0){
// gain resources depending on enemy max health / 10
let gainedResources = enemies[i].maxHealth/10;
numberOfResources += gainedResources;
// score by the amount gained form enemy defeat
score += gainedResources;
// when enemy is defeated, it is removed form enemy array
const findThisIndex = enemyPositions.indexOf(enemies[i].y);
enemyPositions.splice(findThisIndex, 1);
enemies.splice(i, 1);
i--;
}
}
// every enemiesInterval % frames which equals to zero will spawn new enemies
if (frame % enemiesInterval === 0 && score < winningScore){
let verticalPosition = Math.floor(Math.random() * 5 + 1) * cellSize + cellGap;
enemies.push(new Enemy(verticalPosition));
enemyPositions.push(verticalPosition);
// everytime new enemies spawn enemiesInterval lower by 50 frames so the wave comes faster and faster
// if (enemiesInterval > 120) enemiesInterval -= 50;
if (enemiesInterval > 1) enemiesInterval -= 99;
}
}
// resources ===========================================================================================================
const amounts = [20, 30, 40];
class Resource {
constructor(){
// stops the resources form spawning too much to the right
this.x = Math.random() * (canvas.width - cellSize);
// which row it spawns on, can set it to spawn on anywhere but, it will be a problem with sprites
this.y = (Math.floor(Math.random() * 5) + 1) * cellSize + 25;
this.width = cellSize * 0.6;
this.height = cellSize * 0.6;
this.amount = amounts[Math.floor(Math.random() * amounts.length)];
}
draw(){
ctx.fillStyle = 'yellow';
ctx.fillRect(this.x, this.y, this.width, this.height);
ctx.fillStyle = 'black';
ctx.font = '20px Orbitron';
ctx.fillText(this.amount, this.x + 15, this.y + 25);
}
}
function handleResources(){
// stops enemy spawning when you win
if (frame % 500 === 0 && score < winningScore){
resources.push(new Resource());
}
for (let i = 0; i < resources.length; i++){
resources[i].draw();
// if mouse hit resource, gain that amount
if (resources[i] && mouse.x && mouse.y && collision(resources[i], mouse)){
numberOfResources += resources[i].amount;
resources.splice(i, 1);
i--;
}
}
}
// utilities ===========================================================================================================
function handleGameStatus(){
ctx.fillStyle = 'gold';
ctx.font = '30px Orbitron';
ctx.fillText('Score: ' + score, 20, 40);
ctx.fillText('Resources: ' + numberOfResources, 20, 80);
if (gameOver){
ctx.fillStyle = 'black';
ctx.font = '90px Orbitron';
ctx.fillText('GAME OVER', 135, 330);
}
if (score >= winningScore && enemies.length === 0){
ctx.fillStyle = 'black';
ctx.font = '60px Orbitron';
ctx.fillText('LEVEL COMPLETE', 130, 300);
ctx.font = '30px Orbitron';
ctx.fillText('You win with ' + score + ' points!', 134, 340);
}
}
function animate(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'blue';
// fill the rectangle since it is where all canvas and drawing methods are stored
ctx.fillRect(0,0,controlsBar.width, controlsBar.height);
// this makes it so that animate will run whatever code is in it and requestAnimationFrame() will request the
// animate function again so, basically it's a loop (In programmer it is called recursion)
// draw each cell by placing handleGameGrid() inside the animate loop
handleGameGrid();
handleDefenders();
handleResources();
handleProjectiles();
handleEnemies();
handleGameStatus();
frame++;
if (!gameOver) requestAnimationFrame(animate);
}
// need to call animate to kick off animation loop
animate();
// reusable collision detection
function collision(first, second){
if ( !( first.x > second.x + second.width ||
first.x + first.width < second.x ||
first.y > second.y + second.height ||
first.y + first.height < second.y)
) {
return true;
}
}
// makes it so that the mouse position will be fixed when changing window size
window.addEventListener('resize', function(){
canvasPosition = canvas.getBoundingClientRect();
})