wobblyhead hg > minor_monsters
annotate game/js/main.js @ 14:7fda634d15ad
adjustments to wave logic, tower pivot points
| author | ark |
|---|---|
| date | 2026-10-19 22:50 +0200 |
| parents | 8df95050fc8b |
| children |
| 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 | |
|
13
8df95050fc8b
no more build during pause; tower popup closes on any click outside it
ark
parents:
8
diff
changeset
|
30 // the splash screen over the paused first map |
| 6 | 31 function splash() { |
| 32 startMap(FIRST); | |
| 33 game.splash = true; | |
| 34 game.paused = true; | |
| 35 } | |
| 36 | |
| 37 function play() { | |
| 38 game.splash = false; | |
| 39 game.paused = false; | |
| 40 } | |
| 41 | |
| 1 | 42 // canvas resolution follows the device pixel ratio |
| 43 function resize() { | |
| 44 var w = canvas.clientWidth, h = canvas.clientHeight; | |
| 45 view.dpr = window.devicePixelRatio || 1; | |
| 46 canvas.width = Math.round(w * view.dpr); | |
| 47 canvas.height = Math.round(h * view.dpr); | |
| 48 if (game) L = MM.ui.layout(w, h, game.map); | |
| 49 } | |
| 50 | |
| 51 var last = 0, acc = 0; | |
| 52 | |
| 53 function frame(now) { | |
| 54 var dt = last ? (now - last) / 1000 : 0; | |
| 55 last = now; | |
| 56 view.now = now; | |
| 57 var t0 = performance.now(); | |
| 58 | |
| 59 if (!game.paused && !game.result) { | |
| 60 var speed = game.speed(); | |
| 61 acc += dt * speed; | |
| 62 var n = 0; | |
| 63 while (acc >= STEP && n < MAX_STEPS * speed) { | |
| 64 game.step(STEP); | |
| 65 acc -= STEP; | |
| 66 n++; | |
| 67 } | |
| 68 if (n >= MAX_STEPS * speed) acc = 0; | |
| 69 } else { | |
| 70 acc = 0; | |
| 71 } | |
| 72 | |
| 73 ctx.setTransform(1, 0, 0, 1, 0, 0); | |
| 74 ctx.fillStyle = "#e8e2d4"; | |
| 75 ctx.fillRect(0, 0, canvas.width, canvas.height); | |
| 76 MM.render.draw(ctx, game, L, view); | |
| 77 MM.ui.draw(ctx, game, L, view); | |
| 78 MM.render.drawFlights(ctx, game, L, view); | |
| 79 | |
| 80 view.stats.ms = view.stats.ms * 0.9 + (performance.now() - t0) * 0.1; | |
| 81 requestAnimationFrame(frame); | |
| 82 } | |
| 83 | |
| 84 window.addEventListener("resize", resize); | |
| 4 | 85 MM.audio.init(); |
| 1 | 86 MM.input.attach({ |
| 87 canvas: canvas, | |
| 88 view: view, | |
| 89 game: function () { return game; }, | |
| 90 layout: function () { return L; }, | |
| 6 | 91 play: play, |
| 92 next: function () { startMap(game.map.index + 1); }, | |
| 93 retry: function () { startMap(FIRST); }, | |
| 94 restart: splash | |
| 1 | 95 }); |
| 96 MM.assets.load(function (failed) { | |
| 97 if (failed.length) console.error("images not loaded:", failed); | |
| 6 | 98 splash(); |
| 1 | 99 requestAnimationFrame(frame); |
| 100 }); | |
| 101 })(); |
