-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
286 lines (238 loc) · 8.06 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
const canvas = document.querySelector('canvas');
const scoreEl = document.querySelector('#scoreEl');
const startGameBtn = document.querySelector('#startGameBtn');
const modalEl = document.querySelector('#modalEl');
const bigScoreEl = document.querySelector('#bigScoreEl');
// Context of the canvas
// Will allow drawing on the canvas
const c = canvas.getContext('2d');
canvas.width = innerWidth;
canvas.height = innerHeight;
class Player {
constructor(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
draw() {
c.beginPath();
c.arc(this.x, this.y, // Position
this.radius, // Radius
0, Math.PI * 2, // The arc (we want a full circle)
false) // draw counter clockwise? doesn't matter
c.fillStyle = this.color;
c.fill();
}
}
let projectiles = [];
class Projectile {
constructor(x, y, radius, color, velocity) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.velocity = velocity;
}
draw() {
c.beginPath();
c.arc(this.x, this.y, // Position
this.radius, // Radius
0, Math.PI * 2, // The arc (we want a full circle)
false) // draw counter clockwise? doesn't matter
c.fillStyle = this.color;
c.fill();
}
update() {
this.draw();
this.x = this.x + this.velocity.x;
this.y = this.y + this.velocity.y;
}
}
let enemies = [];
class Enemy {
constructor(x, y, radius, color, velocity) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.velocity = velocity;
}
draw() {
c.beginPath();
c.arc(this.x, this.y, // Position
this.radius, // Radius
0, Math.PI * 2, // The arc (we want a full circle)
false) // draw counter clockwise? doesn't matter
c.fillStyle = this.color;
c.fill();
}
update() {
this.draw();
this.x = this.x + this.velocity.x;
this.y = this.y + this.velocity.y;
}
}
let particles = [];
const friction = 0.99;
class Particle {
constructor(x, y, radius, color, velocity) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.velocity = velocity;
this.alpha = 1;
}
draw() {
c.save();
c.globalAlpha = this.alpha;
c.beginPath();
c.arc(this.x, this.y, // Position
this.radius, // Radius
0, Math.PI * 2, // The arc (we want a full circle)
false) // draw counter clockwise? doesn't matter
c.fillStyle = this.color;
c.fill();
c.restore();
}
update() {
this.draw();
// slow over time
this.velocity.x *= friction;
this.velocity.y *= friction;
this.x = this.x + this.velocity.x;
this.y = this.y + this.velocity.y;
this.alpha -= 0.01;
}
}
function spawnEnemies() {
setInterval(() => {
const radius = Math.random() * (30 - 4) + 4
let x;
let y;
if (Math.random < 0.5) {
x = Math.random() < 0.5 ? 0 - radius: canvas.width + radius; // spawn left or right
y = Math.random() * canvas.height;
} else {
y = Math.random() < 0.5 ? 0 - radius: canvas.height + radius; // spawn up or down
x = Math.random() * canvas.width;
}
const color = `hsl(${Math.random() * 360}, 50%, 50%)`; // Give enemy random color
const velocity = getUnitVector({x, y}, {x: canvas.width / 2, y: canvas.height /2}, (Math.random() * 0.35));
enemies.push(new Enemy(x, y, radius, color, velocity))
}, 1000)
}
let score = 0;
function animate() {
let animationId = requestAnimationFrame(animate);
c.fillStyle = 'rgba(0, 0, 0, 0.1)' // 0.1 to make the fade effect
c.fillRect(0, 0, canvas.width, canvas.height); // clear the screen
player.draw();
projectiles.forEach((projectile, index) => {
projectile.update();
if(projectile.x + projectile.radius < 0 ||
projectile.x - projectile.radius > canvas.width ||
projectile.y + projectile.radius < 0 ||
projectile.y - projectile.radius > canvas.height) {
setTimeout(() => {
projectiles.splice(index, 1); // Remove projectile out of screen
}, 0) // Wait until next frame to finish drawing
}
});
enemies.forEach((enemy, index) => {
enemy.update();
const dist = Math.hypot(player.x - enemy.x, player.y - enemy.y)
// Collision with player
if (dist - enemy.radius - player.radius < 1) {
// End game
cancelAnimationFrame(animationId);
modalEl.style.display = 'flex';
bigScoreEl.innerHTML = score;
}
// Collision detection
projectiles.forEach((projectile, projectileIndex) => {
const dist = Math.hypot(projectile.x - enemy.x, projectile.y - enemy.y)
// Collision with projectile
if (dist - enemy.radius - projectile.radius < 1) {
// Create particle explosion
for (let i = 0; i < 16; i++) {
particles.push(
new Particle(
projectile.x,
projectile.y,
Math.random() * 2,
enemy.color,
{
x: (Math.random() - 0.5) * (Math.random() * 8),
y: (Math.random() - 0.5) * (Math.random() * 8),
}
)
)
}
// Shrink large enemies
if(enemy.radius - 10 > 5) { // -10 to clear out really small enemies
gsap.to(enemy, {
radius: enemy.radius -= 10
})
setTimeout(() => {
projectiles.splice(projectileIndex, 1);
}, 0) // Wait until the next frame, so the enemies don't blink
} else { // Destroy small enemies
// Increase our score
score += 1;
scoreEl.innerHTML = score;
setTimeout(() => {
enemies.splice(index, 1);
projectiles.splice(projectileIndex, 1);
}, 0) // Wait until the next frame, so the enemies don't blink
}
}
})
})
particles.forEach((particle, index) => {
if(particle.alpha <= 0) {
particles.splice(index, 1);
} else {
particle.update();
}
})
}
let player = new Player(canvas.width/2, canvas.height/2, 10, 'white')
function init() {
score = 0;
scoreEl.innerHTML = score;
bigScoreEl.innerHTML = score;
player = new Player(canvas.width/2, canvas.height/2, 10, 'white');
projectiles = [];
enemies = [];
particles = [];
}
startGameBtn.addEventListener('click', () => {
init();
animate();
spawnEnemies();
modalEl.style.display = 'none';
});
addEventListener('click', (event) => {
const velocity = getUnitVector({x: canvas.width / 2, y: canvas.height /2}, {x: event.clientX, y: event.clientY}, 4)
const projectile = new Projectile(
canvas.width/2, canvas.height/2,
5,
'white',
velocity
)
projectiles.push(projectile);
});
function getUnitVector(start, end, multiplier = 1) {
let directionVector = {
x: end.x - start.x,
y: end.y - start.y
}
const length = Math.sqrt(directionVector.x * directionVector.x + directionVector.y * directionVector.y);
const unitVector = {
x: (directionVector.x / length) * multiplier,
y: (directionVector.y / length) * multiplier
}
return unitVector;
}