wobblyhead hg > minor_monsters
annotate game/js/main.js @ 9:53f015966118
map, path, tower placement adjustments
| author | ark |
|---|---|
| date | 2026-10-19 15:55 +0200 |
| parents | 2c4fed8baed9 |
| children | 8df95050fc8b |
| rev | line source |
|---|---|
| 1 | 1 // start-up and main loop |
| 2 | |
| 3 window.MM = window.MM || {}; | |
| 4 | |
| 5 (function () { | |
| 6 "use strict"; | |
| 7 | |
| 8 var cfg = MM.config; | |
| 9 var STEP = 1 / 60; | |
| 10 // most simulation steps per frame and game speed step; the rest of a | |
| 11 // long frame is dropped, so the game slows down instead of freezing | |
| 12 var MAX_STEPS = 4; | |
| 13 | |
| 14 var canvas = document.getElementById("game"); | |
| 15 var ctx = canvas.getContext("2d"); | |
| 16 var game = null; | |
| 17 var L = null; | |
|
8
2c4fed8baed9
art update, persistent sound setting, tower selling as double action
ark
parents:
6
diff
changeset
|
18 var view = { dpr: 1, now: 0, refused: null, hover: null, stats: { ms: 0 } }; |
| 1 | 19 |
| 6 | 20 // the first map is the debug start map; only it starts at the debug |
| 21 // start wave | |
| 22 var FIRST = MM.util.clamp(cfg.debug.start_map, 0, MM.maps.length - 1); | |
| 23 | |
| 24 function startMap(m) { | |
| 1 | 25 var seed = cfg.debug.seed || (Math.random() * 4294967296) >>> 0; |
| 6 | 26 game = new MM.Game(m, seed, m === FIRST ? cfg.debug.start_wave : 1); |
| 1 | 27 resize(); |
| 28 } | |
| 29 | |
| 6 | 30 // the splash screen over the paused first map. the pause does not count |
| 31 // for the sell refund, as nothing can be bought on the splash screen. | |
| 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 | |
| 1 | 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 | |
| 81 view.stats.ms = view.stats.ms * 0.9 + (performance.now() - t0) * 0.1; | |
| 82 requestAnimationFrame(frame); | |
| 83 } | |
| 84 | |
| 85 window.addEventListener("resize", resize); | |
| 4 | 86 MM.audio.init(); |
| 1 | 87 MM.input.attach({ |
| 88 canvas: canvas, | |
| 89 view: view, | |
| 90 game: function () { return game; }, | |
| 91 layout: function () { return L; }, | |
| 6 | 92 play: play, |
| 93 next: function () { startMap(game.map.index + 1); }, | |
| 94 retry: function () { startMap(FIRST); }, | |
| 95 restart: splash | |
| 1 | 96 }); |
| 97 MM.assets.load(function (failed) { | |
| 98 if (failed.length) console.error("images not loaded:", failed); | |
| 6 | 99 splash(); |
| 1 | 100 requestAnimationFrame(frame); |
| 101 }); | |
| 102 })(); |
