wobblyhead hg > minor_monsters
annotate game/js/main.js @ 31:d1ae50439686
Added tag 0.9.2 for changeset a51e44094df2
| author | ark |
|---|---|
| date | 2026-10-20 17:58 +0200 |
| parents | 75deda08db90 |
| children | 85d23328a215 |
| rev | line source |
|---|---|
| 17 | 1 // start-up and main loop |
| 2 | |
| 3 window.MM = window.MM || {}; | |
| 4 | |
| 5 (function () { | |
| 6 "use strict"; | |
| 7 | |
| 8 var STEP = 1 / 60; | |
| 9 // most simulation steps per frame and game speed step; the rest of a | |
| 10 // long frame is dropped, so the game slows down instead of freezing | |
| 11 var MAX_STEPS = 4; | |
| 12 | |
| 13 var canvas = document.getElementById("game"); | |
| 14 var ctx = canvas.getContext("2d"); | |
| 15 var game = null; | |
| 16 var L = null; | |
|
27
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
17
diff
changeset
|
17 var view = { dpr: 1, now: 0, refused: null, hover: null }; |
| 17 | 18 |
|
27
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
17
diff
changeset
|
19 // the first map, the wave it starts at and the seed, 0 for a new seed |
|
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
17
diff
changeset
|
20 // per map. js/debug.js may set them; only the first map starts at the |
| 17 | 21 // start wave |
|
27
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
17
diff
changeset
|
22 var start = MM.debug ? MM.debug.start : { map: 0, wave: 1, seed: 0 }; |
|
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
17
diff
changeset
|
23 var FIRST = MM.util.clamp(start.map, 0, MM.maps.length - 1); |
| 17 | 24 |
| 25 function startMap(m) { | |
|
27
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
17
diff
changeset
|
26 var seed = start.seed || (Math.random() * 4294967296) >>> 0; |
|
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
17
diff
changeset
|
27 game = new MM.Game(m, seed, m === FIRST ? start.wave : 1); |
| 17 | 28 resize(); |
| 29 } | |
| 30 | |
| 31 // the splash screen over the paused first map | |
| 32 function splash() { | |
| 33 startMap(FIRST); | |
| 34 game.splash = true; | |
| 35 game.paused = true; | |
| 36 } | |
| 37 | |
| 38 function play() { | |
| 39 game.splash = false; | |
| 40 game.paused = false; | |
| 41 } | |
| 42 | |
| 43 // canvas resolution follows the device pixel ratio | |
| 44 function resize() { | |
| 45 var w = canvas.clientWidth, h = canvas.clientHeight; | |
| 46 view.dpr = window.devicePixelRatio || 1; | |
| 47 canvas.width = Math.round(w * view.dpr); | |
| 48 canvas.height = Math.round(h * view.dpr); | |
| 49 if (game) L = MM.ui.layout(w, h, game.map); | |
| 50 } | |
| 51 | |
| 52 var last = 0, acc = 0; | |
| 53 | |
| 54 function frame(now) { | |
| 55 var dt = last ? (now - last) / 1000 : 0; | |
| 56 last = now; | |
| 57 view.now = now; | |
| 58 var t0 = performance.now(); | |
| 59 | |
| 60 if (!game.paused && !game.result) { | |
| 61 var speed = game.speed(); | |
| 62 acc += dt * speed; | |
| 63 var n = 0; | |
| 64 while (acc >= STEP && n < MAX_STEPS * speed) { | |
| 65 game.step(STEP); | |
| 66 acc -= STEP; | |
| 67 n++; | |
| 68 } | |
| 69 if (n >= MAX_STEPS * speed) acc = 0; | |
| 70 } else { | |
| 71 acc = 0; | |
| 72 } | |
| 73 | |
| 74 ctx.setTransform(1, 0, 0, 1, 0, 0); | |
| 75 ctx.fillStyle = "#e8e2d4"; | |
| 76 ctx.fillRect(0, 0, canvas.width, canvas.height); | |
| 77 MM.render.draw(ctx, game, L, view); | |
| 78 MM.ui.draw(ctx, game, L, view); | |
| 79 MM.render.drawFlights(ctx, game, L, view); | |
| 80 | |
|
27
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
17
diff
changeset
|
81 if (MM.debug) MM.debug.frameTime(performance.now() - t0); |
| 17 | 82 requestAnimationFrame(frame); |
| 83 } | |
| 84 | |
| 85 window.addEventListener("resize", resize); | |
| 86 MM.audio.init(); | |
| 87 MM.input.attach({ | |
| 88 canvas: canvas, | |
| 89 view: view, | |
| 90 game: function () { return game; }, | |
| 91 layout: function () { return L; }, | |
| 92 play: play, | |
| 93 next: function () { startMap(game.map.index + 1); }, | |
| 94 retry: function () { startMap(FIRST); }, | |
| 95 restart: splash | |
| 96 }); | |
| 97 MM.assets.load(function (failed) { | |
| 98 if (failed.length) console.error("images not loaded:", failed); | |
| 99 splash(); | |
| 100 requestAnimationFrame(frame); | |
| 101 }); | |
| 102 })(); |
