-
Notifications
You must be signed in to change notification settings - Fork 1
/
moonlander.js
executable file
·765 lines (649 loc) · 24.6 KB
/
moonlander.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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
// -----------------------------------------------
function initLander () {
let landerSVG = new Blob(['<svg width="684.55" height="414.59" enable-background="new" version="1.1" viewBox="0 0 181.12 109.69" xmlns="http://www.w3.org/2000/svg" xmlns:osb="http://www.openswatchbook.org/uri/2009/osb" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><radialGradient id="radialGradient4614" cx="105.64" cy="74.372" r="13.172" gradientTransform="matrix(1 0 0 1.9326 0 -69.357)" gradientUnits="userSpaceOnUse"><stop stop-color="#501616" offset="0"/><stop stop-color="#501616" stop-opacity="0" offset="1"/></radialGradient></defs><g fill-rule="evenodd" stroke="#501616"><ellipse cx="127.1" cy="76.105" rx="46.784" ry="11.254" fill="#9888ad" stroke-width=".43475"/><ellipse cx="57.176" cy="74.216" rx="46.784" ry="11.254" fill="#9888ad" stroke-width=".43475"/><ellipse cx="90.248" cy="84.421" rx="20.253" ry="14.395" fill="#ac9393" stroke-width=".20114"/><ellipse cx="91.004" cy="62.687" rx="55.302" ry="17.821" fill="#f60" stroke-width=".25722"/><ellipse cx="91.193" cy="49.08" rx="36.438" ry="39.272" fill="#241c1c" stroke-width=".71672"/></g><g transform="translate(-14.64 -39.844)" fill-rule="evenodd"><g stroke-width=".26458"><ellipse cx="105.83" cy="87.979" rx="30.994" ry="48.003" fill="#f60" stroke="#501616"/><ellipse cx="105.83" cy="73.994" rx="13.985" ry="27.97" fill="#000080" opacity=".82778" stroke="#501616"/><ellipse cx="105.64" cy="74.372" rx="13.04" ry="25.324" fill="#e3dbdb" opacity=".588" stroke="url(#radialGradient4614)"/></g><ellipse cx="110.75" cy="74.183" rx="4.5332" ry="19.085" fill="#e3dbdb" opacity=".198" stroke="#501616" stroke-width=".26966"/><ellipse cx="105.76" cy="117.27" rx="34.048" ry="2.8885" fill="#241c1c" stroke="#501616" stroke-width=".1879"/><g transform="matrix(-.43385 -1.5731 -1.2119 .84872 195.52 96.15)" stroke="#501616"><ellipse cx="51.027" cy="109.52" rx="7.1623" ry="21.147" fill="#f60" stroke-width=".30317"/><ellipse cx="50.649" cy="117.84" rx="1.9732" ry="8.0208" fill="#241c1c" stroke-width=".097998"/><ellipse cx="51.128" cy="99.961" rx="4.2185" ry="7.9982" fill="#241c1c" stroke-width=".14309"/></g><g transform="matrix(-1.8172 0 0 1.2271 197.88 -10.998)" stroke="#501616"><ellipse cx="51.027" cy="109.52" rx="7.1623" ry="21.147" fill="#f60" stroke-width=".30317"/><ellipse cx="50.94" cy="117.84" rx="1.9732" ry="8.0208" fill="#241c1c" stroke-width=".097998"/><ellipse cx="50.94" cy="99.696" rx="4.2185" ry="7.9982" fill="#241c1c" stroke-width=".14309"/></g><g transform="matrix(-1.4072 .82622 1.1335 .95095 117.14 -38.012)" stroke="#501616"><ellipse cx="51.027" cy="109.52" rx="7.1623" ry="21.147" fill="#f60" stroke-width=".30317"/><ellipse cx="50.649" cy="117.84" rx="1.9732" ry="8.0208" fill="#241c1c" stroke-width=".097998"/><ellipse cx="51.134" cy="99.832" rx="4.2185" ry="7.9982" fill="#241c1c" stroke-width=".14309"/></g></g></svg>'], { type: 'image/svg+xml' })
let url = window.URL.createObjectURL(landerSVG)
lander.addEventListener('load', initLanderData)
lander.src = url
function initLanderData () {
game.landerWidth = lander.naturalWidth * game.landerScale
game.landerHeight = lander.naturalHeight * game.landerScale
let hitboxLeft = new Path2D()
let hitboxRight = new Path2D()
let hitboxCenter = new Path2D()
let hitboxTop = new Path2D()
hitboxTop.arc(0, game.landerHeight * 0.25, game.landerHeight * 0.85, Math.PI * 1, 0)
hitboxLeft.rect(game.landerWidth * -0.5, game.landerHeight * 0.25, game.landerWidth * 0.3, game.landerHeight * 0.275)
hitboxRight.rect(game.landerWidth * 0.2, game.landerHeight * 0.25, game.landerWidth * 0.3, game.landerHeight * 0.275)
hitboxCenter.rect(game.landerWidth * -0.35, game.landerHeight * 0.35, game.landerWidth * 0.7, game.landerHeight * 0.175)
game.landerHitBoxes.length = 0
game.landerHitBoxes = [hitboxTop, hitboxLeft, hitboxRight, hitboxCenter]
}
game.landerSpeedX = 0
game.landerSpeedY = 0
}
// -----------------------------------------------
function clearStage () {
dc.fillStyle = '#323246'
dc.fill(clearRect)
}
// -----------------------------------------------
function drawLanderSVG () {
let newAngle = (Math.PI / 180) * game.landerAngle
dc.translate(game.landerX, game.landerY)
dc.rotate(newAngle)
dc.drawImage(lander, -(game.landerWidth * 0.5), -(game.landerHeight * 0.5), game.landerWidth, game.landerHeight)
// for (let hitbox of game.landerHitBoxes) dc.fill(hitbox)
dc.setTransform(1, 0, 0, 1, 0, 0)
}
// -----------------------------------------------
function checkCollision () {
if (game.finished) return
if (game.landerX < 0 || game.landerX > stage.width) {
game.finished = true
game.won = false
sprayEnvParticle('explosion', game.landerX, game.landerY)
return
}
if (game.landerY + game.landerHeight + 30 < game.terrainMax) return
let currentX = -game.landerWidth * 0.5
let currentY = -game.landerHeight
let scanMaxX = game.landerWidth
let scanMaxY = game.landerHeight
let scanStepX = scanMaxX * 0.1
let scanStepY = scanMaxY * 0.25
let newAngle = (Math.PI / 180) * game.landerAngle
while (currentY < scanMaxY) {
while (currentX < scanMaxX) {
if (dc.isPointInPath(game.terrain, game.landerX + currentX, game.landerY + currentY)) {
dc.translate(game.landerX, game.landerY)
dc.rotate(newAngle)
let hitboxes = 0
let hitboxIndex = 0
for (let hitbox of game.landerHitBoxes) {
if (dc.isPointInPath(hitbox, game.landerX + currentX, game.landerY + currentY)) {
++hitboxes
if (hitboxIndex === 0) break
}
++hitboxIndex
}
if (hitboxes !== 0) {
if (hitboxIndex === 0 || (Math.abs(game.landerSpeedX) > game.maxCollisionSpeed || Math.abs(game.landerSpeedY > game.maxCollisionSpeed)) || (Math.abs(game.landerAngle) > game.maxAngle)) {
game.finished = true
game.won = false
dc.resetTransform()
sprayEnvParticle('explosion', game.landerX, game.landerY)
return
} else if (hitboxes >= 2) {
game.won = true
game.finished = true
dc.resetTransform()
animateWin()
return
}
}
dc.rotate(-newAngle)
dc.translate(-game.landerX, -game.landerY)
}
currentX += scanStepX
}
currentX = -game.landerWidth * 0.5
currentY += scanStepY
}
dc.setTransform(1, 0, 0, 1, 0, 0)
}
// -----------------------------------------------
function draw () {
if (!game.finished && game.landerFull > 0) {
if (game.keyMap['37'] === true || game.keyMap['65'] === true) {
game.landerAngle -= 1
game.landerSpeedX += Math.cos((game.landerAngle - 90 - 55) / 180 * Math.PI) * 0.025
game.landerSpeedY += Math.sin((game.landerAngle - 90 - 55) / 180 * Math.PI) * 0.025
game.landerFull -= game.fuelConsumption
if (Math.abs(game.landerAngle) >= 360) game.landerAngle = 360 - Math.abs(game.landerAngle)
sprayParticle(1)
}
if (game.keyMap['39'] === true || game.keyMap['68'] === true) {
game.landerAngle += 1
game.landerSpeedX -= Math.cos((game.landerAngle - 90 - 55) / 180 * Math.PI) * 0.025
game.landerSpeedY -= Math.sin((game.landerAngle - 90 - 55) / 180 * Math.PI) * 0.025
game.landerFull -= game.fuelConsumption
if (Math.abs(game.landerAngle) >= 360) game.landerAngle = 360 - Math.abs(game.landerAngle)
sprayParticle(2)
}
if (game.keyMap['38'] === true || game.keyMap['87'] === true) {
game.landerSpeedX += Math.cos((game.landerAngle - 90) / 180 * Math.PI) * 0.025
game.landerSpeedY += Math.sin((game.landerAngle - 90) / 180 * Math.PI) * 0.025
game.landerFull -= game.fuelConsumption
sprayParticle(0)
}
}
if (game.hasTimeout !== null) return
clearStage()
if (game.terrainImg !== null) dc.putImageData(game.terrainImg, 0, stage.height - game.terrainImg.height)
animateParticles()
drawFuel()
drawStats()
calculateWind()
if (game.terrainImg === null) drawTerrain()
checkCollision()
if (!game.finished) {
game.landerSpeedX += game.moonWindStrengthX * 0.005
game.landerSpeedY += game.moonWindStrengthY * 0.005
game.landerSpeedY += Math.sqrt(2 * game.moonGravity) * 0.005
game.landerX += game.landerSpeedX
game.landerY += game.landerSpeedY
drawLanderSVG()
} else if (game.won) {
drawLanderSVG()
dc.font = 'bold 114px sans-serif'
dc.fillStyle = 'rgba(255,255,255,0.7)'
dc.fillText('GAME WON', (stage.width * 0.5) - (dc.measureText('GAME WON').width * 0.5), stage.height * 0.42)
dc.font = '10px sans-serif'
dc.fillText('PRESS "SPACE" KEY TO RESTART', (stage.width * 0.5) - (dc.measureText('PRESS "SPACE" KEY TO RESTART').width * 0.5), stage.height * 0.45)
if (game.engineParticles.length === 0 && game.landerParticles.length === 0) return
} else {
dc.font = 'bold 114px sans-serif'
dc.fillStyle = 'rgba(255,255,255,0.7)'
dc.fillText('GAME LOST', (stage.width * 0.5) - (dc.measureText('GAME LOST').width * 0.5), stage.height * 0.42)
dc.font = '10px sans-serif'
dc.fillText('PRESS "SPACE" KEY TO RESTART', (stage.width * 0.5) - (dc.measureText('PRESS "SPACE" KEY TO RESTART').width * 0.5), stage.height * 0.45)
if (game.engineParticles.length === 0 && game.landerParticles.length === 0) return
}
window.requestAnimationFrame(draw)
}
// -----------------------------------------------
function drawTerrain () {
dc.fillStyle = 'rgba(170,140,140,0.7)'
dc.strokeStyle = 'rgba(250,250,250,0.85)'
dc.strokeWidth = 3
dc.fill(game.terrain)
dc.stroke(game.terrain)
dc.translate(0, 3)
dc.fill(game.terrain)
dc.translate(0, 6)
dc.fill(game.terrain)
dc.translate(0, -9)
}
// -----------------------------------------------
function drawFuel () {
dc.fillStyle = 'rgba(255,255,255,0.7)'
dc.fillRect(10, 10, 20, 100)
dc.fillStyle = 'rgba(0,0,0,0.4)'
dc.fillRect(10, 10, 20, 100 - (100 * game.landerFull))
dc.translate(14, 80)
dc.rotate((Math.PI / 180) * -90)
dc.fillStyle = 'rgba(255,255,255,0.75)'
dc.fillText('FUEL', 10, 10)
dc.rotate((Math.PI / 180) * 90)
dc.translate(-14, -80)
}
function drawStats () {
dc.fillStyle = 'rgba(255,255,255,0.7)'
dc.fillText('SPEED X: ' + game.landerSpeedX.toFixed(2) + ' m/s²', 10, 150)
dc.fillText('SPEED Y: ' + game.landerSpeedY.toFixed(2) + ' m/s²', 10, 164)
dc.fillText('ANGLE: ' + game.landerAngle + ' ° Grad', 10, 188)
dc.fillText('WIND X: ' + game.moonWindStrengthX.toFixed(2) + ' m/s²', 10, 218)
dc.fillText('WIND Y: ' + game.moonWindStrengthY.toFixed(2) + ' m/s²', 10, 232)
}
// -----------------------------------------------
function sprayEnvParticle (type, x, y, xOffset, yOffset) {
let srcX = x
let srcY = y
let maxWaves = []
let colorAmountMin = false
switch (type) {
case 'explosion':
maxWaves = [24, 16, 12, 5, 2, 1]
for (let waveSize of maxWaves) {
for (let angle = 0; angle < 360; ++angle) {
let angleX = -Math.cos(angle / 180 * Math.PI)
let angleY = -Math.sin(angle / 180 * Math.PI)
for (let i = 0; i < game.landerParticleSprayCount * 0.05; ++i) {
let particle = {
x: srcX,
y: srcY,
age: 0.8 + (waveSize * 0.025),
speedX: angleX * (Math.random() * waveSize + 0.95),
speedY: angleY * (Math.random() * waveSize + 0.95)
}
game.engineParticles.push(particle)
}
}
}
break
case 'explosionColor':
colorAmountMin = 185
maxWaves = [12, 7, 5, 9, 3, 1, 11, 6, 4, 6]
for (let i = 0, j = maxWaves.length; i < j; ++i) maxWaves[i] = maxWaves[i] * ((Math.random() * 0.35) + 0.65)
let colors = [Math.ceil(colorAmountMin + (Math.random() * 128)), Math.ceil(colorAmountMin + (Math.random() * 128)), Math.ceil(colorAmountMin + (Math.random() * 128))]
if (xOffset !== undefined && yOffset !== undefined) {
srcX += Math.random() * xOffset
srcY += Math.random() * yOffset
}
for (let waveSize of maxWaves) {
for (let angle = 0; angle < 360; ++angle) {
let angleX = -Math.cos(angle / 180 * Math.PI)
let angleY = -Math.sin(angle / 180 * Math.PI)
for (let i = 0; i < game.landerParticleSprayCount * 0.00065; ++i) {
let particle = {
x: srcX,
y: srcY,
age: 0.8 + (waveSize * 0.0125),
speedX: angleX * (Math.random() * waveSize + 1),
speedY: angleY * (Math.random() * waveSize + 1)
}
if (i === 0) particle.color = colors
game.engineParticles.push(particle)
}
}
}
break
default:
break
}
}
function sprayParticle (direction) {
let srcX = game.landerX
let srcY = game.landerY
let engineAngle = 0
let angle = 0
switch (direction) {
case 0:
// DOWN
angle = (game.landerAngle - 90) / 180 * Math.PI
srcX -= Math.cos(angle) * game.landerWidth * 0.375
srcY -= Math.sin(angle) * game.landerHeight * 0.6
break
case 2:
// LEFT
engineAngle = 55
angle = (game.landerAngle - 90 + 65) / 180 * Math.PI
srcX -= Math.cos(angle) * game.landerWidth * 0.55
srcY -= Math.sin(angle) * game.landerHeight * 0.9
break
case 1:
// RIGHT
engineAngle = -55
angle = (game.landerAngle - 90 - 65) / 180 * Math.PI
srcX -= Math.cos(angle) * game.landerWidth * 0.55
srcY -= Math.sin(angle) * game.landerHeight * 0.9
break
default:
return
}
let angleX = Math.cos((game.landerAngle + 90 + engineAngle) / 180 * Math.PI)
let angleY = Math.sin((game.landerAngle + 90 + engineAngle) / 180 * Math.PI)
for (let i = 0; i < game.landerParticleSprayCount; ++i) {
let particle = {
x: srcX,
y: srcY,
age: 0.9,
speedX: angleX * (Math.random() * 5 + 0.95),
speedY: angleY * (Math.random() * 5 + 0.95)
}
game.landerParticles.push(particle)
}
}
function animateParticles () {
for (let x = 0; x < 2; ++x) {
let particles = (x === 0 ? game.landerParticles : game.engineParticles)
if (particles[0] === undefined) continue
let joinedColors = false
if (particles[0].color !== undefined) joinedColors = particles[0].color.join(',')
let angle = 2 * Math.PI
dc.strokeWidth = 1
for (let i = 0, count = particles.length; i < count; ++i) {
let particle = particles[i]
particle.age -= 0.05
if (particle.age <= 0) {
particles.splice(i, 1)
--count
--i
continue
}
dc.beginPath()
dc.arc(particle.x, particle.y, 2, 0, angle)
dc.closePath()
if (joinedColors !== false) dc.strokeStyle = 'rgba(' + joinedColors + ',' + particle.age.toString() + ')'
else dc.strokeStyle = 'rgba(255,255,255,' + particle.age.toString() + ')'
dc.stroke()
particle.x += particle.speedX
particle.y += particle.speedY
}
}
}
function registerKey (evt) {
if (evt.target.nodeName === 'INPUT') return
switch (evt.keyCode) {
case 32:
evt.stopPropagation()
evt.preventDefault()
restartGame()
break
case 37:
case 65:
if (game.landerFull <= 0) return
game.keyMap[evt.keyCode.toString()] = true
evt.stopPropagation()
evt.preventDefault()
break
case 39:
case 68:
if (game.landerFull <= 0) return
game.keyMap[evt.keyCode.toString()] = true
evt.stopPropagation()
evt.preventDefault()
break
case 38:
case 87:
if (game.landerFull <= 0) return
game.keyMap[evt.keyCode.toString()] = true
evt.stopPropagation()
evt.preventDefault()
break
default:
break
}
}
function unregisterKey (evt) {
if (evt.target.nodeName === 'INPUT') return
switch (evt.keyCode) {
case 32:
evt.stopPropagation()
evt.preventDefault()
restartGame()
break
case 37:
case 65:
if (game.landerFull <= 0) return
game.keyMap[evt.keyCode.toString()] = false
evt.stopPropagation()
evt.preventDefault()
break
case 39:
case 68:
if (game.landerFull <= 0) return
game.keyMap[evt.keyCode.toString()] = false
evt.stopPropagation()
evt.preventDefault()
break
case 38:
case 87:
if (game.landerFull <= 0) return
game.keyMap[evt.keyCode.toString()] = false
evt.stopPropagation()
evt.preventDefault()
break
default:
break
}
}
function calculateWind () {
if (!game.moonWindEnabled) return
if (!game.finished) {
game.moonWindAngle += (Math.random() * -1) * ((Math.random() * 30) - 15)
game.moonWindStrengthX = Math.cos(game.moonWindAngle / 180 * Math.PI)
if (Math.abs(game.moonWindStrengthX) > game.moonWindMaxStrength) game.moonWindMaxStrengthX = game.moonWindStrengthX > 0 ? game.moonWindMaxStrength : -game.moonWindMaxStrength
game.moonWindStrengthY = Math.sin(game.moonWindAngle / 180 * Math.PI)
if (Math.abs(game.moonWindStrengthY) > game.moonWindMaxStrength) game.moonWindMaxStrengthX = game.moonWindStrengthY > 0 ? game.moonWindMaxStrength : -game.moonWindMaxStrength
}
dc.translate(90, 60)
dc.rotate((Math.PI / 180) * game.moonWindAngle)
dc.fillStyle = 'rgba(255,255,255,0.48)'
dc.beginPath()
dc.arc(0, 0, 50, 50, 0, 2 * Math.PI)
dc.closePath()
dc.fill()
dc.fillStyle = 'rgba(0,0,0,0.18)'
dc.beginPath()
dc.arc(0, 0, 40, 40, 0, 2 * Math.PI)
dc.closePath()
dc.fill()
dc.fillStyle = 'rgba(255,255,255,0.68)'
dc.strokeStyle = 'rgba(0,0,0,0.318)'
dc.beginPath()
dc.translate(-20, -5)
dc.moveTo(0, 0)
dc.lineTo(25, 0)
dc.lineTo(25, -10)
dc.lineTo(45, 5)
dc.lineTo(25, 20)
dc.lineTo(25, 10)
dc.lineTo(0, 10)
dc.closePath()
dc.fill()
dc.stroke()
dc.translate(20, 5)
dc.rotate((Math.PI / 180) * -game.moonWindAngle)
dc.translate(-90, -60)
}
// ----------------------------------------------------------------------------
function generateTerrain () {
let iterations = 50
let min = 30
let max = 30
let rareStep = 0.0
let randomValues = []
for (let x = 0; x < iterations; ++x) {
randomValues.push([max * (Math.random() * 1) + min, min, rareStep])
rareStep = (Math.random() * 0.125) + 0.0155
max += 3
min -= 1
}
let terrain = []
for (let randomLimits of randomValues) {
let lastTime = stage.width * (Math.random() * randomLimits[2])
for (let x = 0, width = stage.width + 150; x < width; ++x) {
if (--lastTime < 0) {
lastTime = stage.width * (Math.random() * randomLimits[2])
let randomVal = Math.random() * randomLimits[0] + randomLimits[1]
if (terrain[x] === undefined) terrain.push(randomVal)
else terrain[x] += randomVal
}
}
}
let offsetY = stage.height
let terrainPath = new Path2D()
terrainPath.moveTo(0, offsetY)
for (let x = 0, size = terrain.length - 1; x < size; x += Math.round(Math.random() * (stage.width * 0.05)) + 30) {
terrainPath.lineTo(x, offsetY - terrain[x])
if (game.terrainMax < terrain[x]) game.terrainMax = terrain[x] + 20
}
game.terrainMax = offsetY - game.terrainMax
terrainPath.lineTo(stage.width, offsetY)
terrainPath.lineTo(0, offsetY)
terrainPath.closePath()
game.terrain.addPath(terrainPath)
dc.fillStyle = 'rgba(170,140,140,0.4)'
dc.strokeStyle = 'rgba(250,250,250,0.55)'
dc.strokeWidth = 6
dc.fill(game.terrain)
dc.stroke(game.terrain)
dc.translate(0, 3)
dc.fill(game.terrain)
dc.translate(0, 6)
dc.fill(game.terrain)
dc.translate(0, -9)
}
// -----------------------------------------------
function generateTerrain2 () {
function interpolate (pa, pb, px) {
let ft = px * Math.PI
let f = (1 - Math.cos(ft)) * 0.5
return pa * (1 - f) + pb * f
}
let h = stage.height
let w = stage.width + 25
let x = 0
let y = h * 0.65
let amp = 240 // amplitude
let wl = 140 // wavelength
let a = Math.random()
let b = Math.random()
let terrainPath = new Path2D()
game.terrainMax = stage.height
terrainPath.moveTo(x, h)
while (x < w) {
if (x % wl === 0) {
a = b
b = Math.random()
y = h * 0.65 + a * amp
} else {
y = h * 0.65 + interpolate(a, b, (x % wl) / wl) * amp
}
terrainPath.lineTo(x, y)
if (Math.abs(y) < game.terrainMax) game.terrainMax = Math.abs(y)
x += 10
}
terrainPath.lineTo(stage.width + 15, game.terrainMax * 1.05)
terrainPath.lineTo(stage.width + 15, h)
terrainPath.lineTo(0, h)
terrainPath.closePath()
game.terrain.addPath(terrainPath)
dc.fillStyle = 'rgba(170,140,140,0.4)'
dc.strokeStyle = 'rgba(250,250,250,0.55)'
dc.strokeWidth = 6
dc.stroke(game.terrain)
dc.fill(game.terrain)
dc.translate(0, 3)
dc.fill(game.terrain)
dc.translate(0, 6)
dc.fill(game.terrain)
dc.translate(0, -9)
clearStage()
drawTerrain()
if (game.preFetchTerrain) game.terrainImg = dc.getImageData(0, stage.height - game.terrainMax, stage.width, game.terrainMax)
}
// -----------------------------------------------
const stage = document.querySelector('#stage')
const dc = stage.getContext('2d', { alpha: false })
dc.imageSmoothingEnabled = false
let clearRect = new Path2D()
clearRect.rect(0, 0, stage.width, stage.height)
const fuelConsumptionDivider = 0.00001
let game = {
keyMap: {},
terrainType: 1,
terrain: new Path2D(),
terrainImg: null,
preFetchTerrain: false,
terrainMax: 0,
hasTimeout: null,
animationInterval: null,
moonWindEnabled: true,
moonWindAngle: 0,
moonWindMaxStrength: 2,
moonWindStrengthX: 0,
moonWindStrengthY: 0,
moonGravity: 1.62,
maxAngle: 43,
maxCollisionSpeed: 1.275,
fuelConsumption: 125 * fuelConsumptionDivider,
landerX: stage.width * 0.3,
landerY: 10,
landerAngle: 0,
landerWidth: 0,
landerHeight: 0,
landerScale: 0.1,
landerSpeedX: 0,
landerSpeedY: 0,
landerParticleSprayCount: 25,
landerFull: 1.0,
landerHitBoxes: [],
finished: false,
won: false,
landerParticles: [],
engineParticles: []
}
// -----------------------------------------------
function resetGame () {
if (game.animationInterval !== null) clearInterval(game.animationInterval)
game.animationInterval = null
game.landerParticles.length = 0
game.engineParticles.length = 0
game.landerHitBoxes.length = 0
game.terrain = null
game.terrainImg = null
game = {
keyMap: {},
terrainType: 1,
terrain: new Path2D(),
terrainImg: null,
preFetchTerrain: false,
terrainMax: 0,
hasTimeout: null,
animationInterval: null,
moonWindEnabled: true,
moonWindAngle: 0,
moonWindMaxStrength: 2,
moonWindStrengthX: 0,
moonWindStrengthY: 0,
moonGravity: 1.62,
maxAngle: 43,
maxCollisionSpeed: 1.275,
fuelConsumption: 125 * fuelConsumptionDivider,
landerX: stage.width * 0.3,
landerY: 10,
landerAngle: 0,
landerWidth: 0,
landerHeight: 0,
landerScale: 0.1,
landerSpeedX: 0,
landerSpeedY: 0,
landerParticleSprayCount: 25,
landerFull: 1.0,
landerHitBoxes: [],
finished: true,
won: false,
landerParticles: [],
engineParticles: []
}
dc.setTransform(1, 0, 0, 1, 0, 0)
}
// -----------------------------------------------
function readUIOptionValues () {
let optionFields = document.querySelectorAll('.optionValue')
for (let option of optionFields) {
if (option.type === 'number') {
if (option.name === 'fuelConsumption') game[option.name] = parseFloat(option.value) * fuelConsumptionDivider
else game[option.name] = parseFloat(option.value)
} else if (option.type === 'radio') {
if (option.checked) {
let isBool = game[option.name] === true || game.optionName === false
game[option.name] = isBool ? parseInt(option.value) === 1 : parseInt(option.value)
}
}
}
}
// -----------------------------------------------
function resetTimeout () {
clearTimeout(game.hasTimeout)
game.hasTimeout = null
window.requestAnimationFrame(draw)
}
function restartGame () {
if (game.hasTimeout !== null) return
resetGame()
game.finished = false
game.won = false
readUIOptionValues()
clearStage()
if (game.terrainType <= 0) generateTerrain()
else generateTerrain2()
initLander()
game.hasTimeout = setTimeout(resetTimeout, 150)
}
// -----------------------------------------------
function animateWin () {
if (game.animationInterval === null) game.animationInterval = setInterval(animateWin, 1250)
if (game.engineParticles[0] === undefined) {
sprayEnvParticle('explosionColor', stage.width * 0.125, stage.height * 0.15, stage.width * 0.625, stage.height * 0.35)
window.requestAnimationFrame(draw)
}
}
for (let node of document.querySelectorAll('.optionValue')) node.addEventListener('change', readUIOptionValues)
document.addEventListener('keydown', registerKey)
document.addEventListener('keyup', unregisterKey)
let lander = new Image()
initLander()
restartGame()