/
index.html
487 lines (410 loc) · 15.8 KB
/
index.html
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<!doctype html>
<html>
<head>
<style>
body{
background: #202028;
padding: 0px;
}
#canvas_dom{
position: absolute;
top: 0;
left: 0;
}
</style>
<meta charset=utf-8>
<title>SPACE MINES</title>
<script>
const utils = {
rand: (min, max) => {
return (Math.random() * (max - min)) + min
},
degreesToCoords: (x, y, angle, distance) => {
return new Vector(
Math.round(Math.cos(angle * Math.PI / 180) * distance + x),
Math.round(Math.sin(angle * Math.PI / 180) * distance + y)
)
}
}
function Vector(x,y) {
this.x = x
this.y = y
this.add = (vector) => {
return new Vector(this.x + vector.x, this.y + vector.y)
}
this.subtract = (vector) => {
return new Vector(this.x - vector.x, this.y - vector.y)
}
this.multiply = (scalar) => {
return new Vector(this.x * scalar, this.y * scalar)
}
this.divide = (vector) => {
return new Vector(this.x / vector.x, this.y / vector.y)
}
this.dotProduct = (vector) => {
return this.x * vector.x + this.y * vector.y
}
this.setMagnitude = (newMagnitude) => {
const mag = this.magnitude()
return new Vector(
this.x * newMagnitude / mag,
this.y * newMagnitude / mag,
)
}
// distance new_vx = vx * New_Mag / Mag
this.magnitude = () => {
return Math.sqrt(this.x ** 2 + this.y ** 2);
}
this.direction = () => {
return Math.atan2(this.x, this.y);
}
}
function Tile() {
this.id = Math.round(utils.rand(0, 10000000000))
this.size = 20 // Math.round(utils.rand(10, 40))
this.sphereArea = 4 * Math.PI * this.size ** 2
this.velocity = new Vector(utils.rand(-1, 1), utils.rand(-1, 1))
this.collisions = []
this.neighbors = []
this.counter = 0
this.hovering = false
this.hasMine = false
this.cleared = false
this.flagged = false
this.position = utils.degreesToCoords(
game.position.x,
game.position.y,
Math.round(utils.rand(0, 360)), // random angle
Math.round(utils.rand(0, game.size - this.size)) // random distance
)
this.connections = () => {
this.neighbors = []
this.counter = 0
game.tiles.forEach((that) => {
if(that == this) return
const relPos = this.position.subtract(that.position)
if(relPos.magnitude() <= 120 ){
let highlightConnection = false
if(relPos.magnitude() < 80 ){
this.neighbors.push(that)
if(that.hasMine) this.counter++
if(this.hovering || that.hovering) highlightConnection = true
}
if(game.connectionsRendered.includes(that)) return
game.ctx.beginPath()
const opacity = 1 - ((relPos.magnitude() - 30) / 120) // 100% at 30px... 0% at 80px
game.ctx.strokeStyle = highlightConnection ? 'rgba(255,255,255,1)' : 'rgba(100,100,100,'+opacity+')'
game.ctx.moveTo(this.position.x, this.position.y)
game.ctx.lineTo(that.position.x, that.position.y)
game.ctx.stroke()
}
})
}
this.clearTile = () => {
if(this.cleared) return
this.cleared = true
// clear neighbors too. if this isn't a numbered tile
if(this.counter > 0) return
this.neighbors.forEach((that) => {
that.clearTile()
})
}
this.render = () => {
game.ctx.beginPath()
game.ctx.strokeStyle = 'rgba(0,0,0,0.8)'
if(this.cleared){
game.ctx.fillStyle = "#8b8b8b"
}else if(this.flagged){
game.ctx.fillStyle = "#844141"
}else if(this.hovering){
game.ctx.fillStyle = "#646176"
}else if(game.dead && this.hasMine){
game.ctx.fillStyle = "red"
}else{
game.ctx.fillStyle = "#4a4951"
}
game.ctx.arc(this.position.x, this.position.y, this.size - 1, 0, 2 * Math.PI )
game.ctx.fill()
game.ctx.stroke()
if(this.counter < 1 || !this.cleared) return
game.ctx.font = "15px Arial"
game.ctx.fillStyle = "#333333"
game.ctx.fillText(this.counter, this.position.x - 5, this.position.y + 5)
}
this.highlight = () => {
this.hovering = true
}
this.click = () => {
if(this.hasMine) return game.death()
this.clearTile()
}
this.flag = () => {
this.flagged = !this.flagged
}
this.tick = () => {
// for all other tiles
game.tiles.forEach((that) => {
// if self or if has already been processed
if(that == this) return
if(this.collisions.includes(that.id)) return
const relPos = this.position.subtract(that.position)
if(relPos.magnitude() <= this.size + that.size){
// update velocities for both tiles
let this_velocity = this.collisionVector(this, that)
let that_velocity = this.collisionVector(that, this)
this.velocity = this_velocity
that.velocity = that_velocity
// stop intersections - set magnitude to distance plus 1
// lets sadd the overlap as a velocity
const bufferVector = relPos.setMagnitude(this.size + that.size - relPos.magnitude())
this.position = this.position.add(bufferVector)
// add a slight repelant to overlaps
this.velocity = this.velocity.add(bufferVector.setMagnitude(0.1))
// add to other tile collisions so it won't be re-processed
that.collisions.push(this.id)
}
})
// check barge collisions
game.barges.forEach((barge) => {
const relPos = this.position.subtract(barge.position)
if(relPos.magnitude() <= this.size + barge.size){
let this_velocity = this.collisionVector(this, barge)
this.velocity = this_velocity
const bufferVector = relPos.setMagnitude(this.size + barge.size - relPos.magnitude())
this.position = this.position.add(bufferVector)
// add a slight repelant to overlaps
this.velocity = this.velocity.add(bufferVector.setMagnitude(0.1))
}
})
// check circle boundary
if(this.position.subtract(game.position).magnitude() > game.size - this.size){
this.velocity = this.collisionVector(this, game)
// repell from game edge
const relPost = this.position.subtract(game.position)
const bufferVector = relPost.setMagnitude(relPost.magnitude() - game.size)
this.postition = this.position.subtract(bufferVector)
this.velocity = this.velocity.add(bufferVector.setMagnitude(0.2))
}
// add resistance 1.01 for uniform speeds
this.velocity = this.velocity.setMagnitude(this.velocity.magnitude() / 1.04)
// move
this.position = this.position.add(this.velocity)
// clear collisions once it's processed
this.collisions = []
this.render()
}
this.collisionVector = (particle1, particle2) => {
return particle1.velocity
.subtract(particle1.position
.subtract(particle2.position)
.multiply(
particle1.velocity.subtract(particle2.velocity)
.dotProduct(particle1.position.subtract(particle2.position))
/ particle1.position.subtract(particle2.position).magnitude() ** 2
)
// add mass to the system
.multiply((2 * particle2.sphereArea) / (particle1.sphereArea + particle2.sphereArea))
)
}
}
function Barge() {
this.size = utils.rand(5, 20)
this.sphereArea = 4 * Math.PI * this.size ** 2
this.position = utils.degreesToCoords(
game.position.x,
game.position.y,
Math.round(utils.rand(0, 360)), // random angle
game.size + this.size
)
let randPointOnBoard = utils.degreesToCoords(
game.position.x,
game.position.y,
Math.round(utils.rand(0, 360)), // random angle
Math.round(utils.rand(0, game.size - this.size)) // random distance
)
this.velocity = this.position.subtract(randPointOnBoard).setMagnitude(-1)
this.tick = () => {
// move
this.position = this.position.add(this.velocity)
// delete if it's offscreen
if(
this.position.x > game.canvas.width + this.size ||
this.position.x < -this.size ||
this.position.y > game.canvas.height + this.size ||
this.position.y < - this.size
) this.delete()
this.render()
}
this.render = () => {
game.ctx.beginPath()
game.ctx.strokeStyle = 'rgba(0,0,0,0.8)'
game.ctx.fillStyle = 'rgba(0,0,0,0.8)'
game.ctx.arc(this.position.x, this.position.y, this.size - 1, 0, 2 * Math.PI )
game.ctx.fill()
game.ctx.stroke()
}
this.delete = () => {
game.barges.splice(game.barges.indexOf(this), 1)
}
}
function Game(){
this.bargesSpawnEveryNFrames = 1500
this.frame = 0
this.mousedown = false
this.mouse = false
this.started = true
this.paused = false
this.size = 450
this.velocity = new Vector(0,0)
this.sphereArea = 4 * Math.PI * this.size ** 2
this.tiles = []
this.tileCount = 150
this.mineCount = 20
this.numConnections = 6
this.barges = []
this.fps = {
lastFrame: performance.now(),
recentCount: new Array(30),
add: () => {
let fpsDelta = (performance.now() - this.fps.lastFrame) / 1000
this.fps.lastFrame = performance.now()
this.fps.recentCount.shift()
this.fps.recentCount.push(Math.round(1/fpsDelta))
},
print: () => {
let average = Math.round(this.fps.recentCount.reduce((a, b) => a + b, 0) / this.fps.recentCount.length)
this.ctx.font = "15px Arial"
if(this.paused){
this.ctx.fillText('PAUSED', this.canvas.width - 80, 15)
return
}
this.ctx.fillText('FPS: '+average, this.canvas.width - 80, 15)
}
}
this.init = () => {
let requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame
window.requestAnimationFrame = requestAnimationFrame
this.canvas = document.getElementById('canvas_dom')
this.ctx = game.canvas.getContext('2d')
this.canvas.width = window.innerWidth
this.canvas.height = window.innerHeight
this.position = new Vector(
window.innerWidth / 2,
window.innerHeight / 2
)
this.events.init()
// spawn tiles
for(i=0; i<this.tileCount; i++){
this.tiles.push(new Tile())
}
// spawn mines
for(i=0; i<this.mineCount; i++){
let randIndex = Math.round(utils.rand(0, this.tileCount - 1))
while(this.tiles[randIndex].hasMine){
randIndex = Math.round(utils.rand(0, this.mineCount - 1))
}
this.tiles[randIndex].hasMine = true
}
// start the game
game.tick()
}
this.tick = () => {
this.frame++
this.fps.add()
// render
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
this.renderGrid()
this.connectionsRendered = []
this.tiles.forEach((tile) => {
tile.connections()
this.connectionsRendered.push(tile)
})
this.tiles.forEach((tile) => {
tile.tick()
})
this.barges.forEach((barge) => {
barge.tick()
})
if(this.frame % this.bargesSpawnEveryNFrames == 0){
this.barges.push(new Barge())
}
// close loop
this.fps.print()
if(this.paused) return
requestAnimationFrame(function () {
game.tick()
});
}
this.death = () => {
this.paused = true
this.dead = true
}
this.renderGrid = () => {
// render circle
game.ctx.beginPath()
game.ctx.strokeStyle = "#000"
game.ctx.arc(
game.canvas.width / 2,
game.canvas.height / 2,
game.size,
0, 2 * Math.PI
)
game.ctx.stroke()
}
this.events = {
init: () => {
this.canvas.onmousedown = this.events.mousedown
this.canvas.onmousemove = this.events.mousemove
this.canvas.addEventListener('contextmenu', this.events.click)
this.canvas.onclick = this.events.click
},
click: (e) => {
event.preventDefault()
if(e.clientX < 50 && e.clientY < 50){
this.paused = !this.paused
this.tick()
return
}
const mouse = new Vector(e.clientX, e.clientY)
this.tiles.forEach((tile) => {
if(tile.position.subtract(mouse).magnitude() < tile.size){
if(event.button > 0) return tile.flag()
tile.click()
}
})
},
mousedown: (e) => {
},
mousemove: (e) => {
const mouse = new Vector(e.clientX, e.clientY)
// see if the mouse is in the game field
if(this.position.subtract(mouse).magnitude() > game.size) return
let found = false
// see if this is in a tile
this.tiles.forEach((tile) => {
tile.hovering = false
if(tile.position.subtract(mouse).magnitude() < tile.size){
found = true
tile.highlight()
}
})
game.canvas.style.cursor = found ? 'pointer' : 'default'
}
}
}
let init = () => {
game = new Game()
game.init()
}
</script>
<body onload="init()">
<div class='flex'>
<div class='panel' id="game">
<canvas id="canvas_dom"></canvas>
</div>
</div>
</body>
</html>