diff game/js/main.js @ 17:fe680946164b

reorganised repo
author ark
date 2026-10-20 00:12 +0200
parents
children 75deda08db90
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/game/js/main.js	Tue Oct 20 00:12:36 2026 +0200
@@ -0,0 +1,101 @@
+// start-up and main loop
+
+window.MM = window.MM || {};
+
+(function () {
+    "use strict";
+
+    var cfg = MM.config;
+    var STEP = 1 / 60;
+    // most simulation steps per frame and game speed step; the rest of a
+    // long frame is dropped, so the game slows down instead of freezing
+    var MAX_STEPS = 4;
+
+    var canvas = document.getElementById("game");
+    var ctx = canvas.getContext("2d");
+    var game = null;
+    var L = null;
+    var view = { dpr: 1, now: 0, refused: null, hover: null, stats: { ms: 0 } };
+
+    // the first map is the debug start map; only it starts at the debug
+    // start wave
+    var FIRST = MM.util.clamp(cfg.debug.start_map, 0, MM.maps.length - 1);
+
+    function startMap(m) {
+        var seed = cfg.debug.seed || (Math.random() * 4294967296) >>> 0;
+        game = new MM.Game(m, seed, m === FIRST ? cfg.debug.start_wave : 1);
+        resize();
+    }
+
+    // the splash screen over the paused first map
+    function splash() {
+        startMap(FIRST);
+        game.splash = true;
+        game.paused = true;
+    }
+
+    function play() {
+        game.splash = false;
+        game.paused = false;
+    }
+
+    // canvas resolution follows the device pixel ratio
+    function resize() {
+        var w = canvas.clientWidth, h = canvas.clientHeight;
+        view.dpr = window.devicePixelRatio || 1;
+        canvas.width = Math.round(w * view.dpr);
+        canvas.height = Math.round(h * view.dpr);
+        if (game) L = MM.ui.layout(w, h, game.map);
+    }
+
+    var last = 0, acc = 0;
+
+    function frame(now) {
+        var dt = last ? (now - last) / 1000 : 0;
+        last = now;
+        view.now = now;
+        var t0 = performance.now();
+
+        if (!game.paused && !game.result) {
+            var speed = game.speed();
+            acc += dt * speed;
+            var n = 0;
+            while (acc >= STEP && n < MAX_STEPS * speed) {
+                game.step(STEP);
+                acc -= STEP;
+                n++;
+            }
+            if (n >= MAX_STEPS * speed) acc = 0;
+        } else {
+            acc = 0;
+        }
+
+        ctx.setTransform(1, 0, 0, 1, 0, 0);
+        ctx.fillStyle = "#e8e2d4";
+        ctx.fillRect(0, 0, canvas.width, canvas.height);
+        MM.render.draw(ctx, game, L, view);
+        MM.ui.draw(ctx, game, L, view);
+        MM.render.drawFlights(ctx, game, L, view);
+
+        view.stats.ms = view.stats.ms * 0.9 + (performance.now() - t0) * 0.1;
+        requestAnimationFrame(frame);
+    }
+
+    window.addEventListener("resize", resize);
+    MM.audio.init();
+    MM.input.attach({
+        canvas: canvas,
+        view: view,
+        game: function () { return game; },
+        layout: function () { return L; },
+        play: play,
+        next: function () { startMap(game.map.index + 1); },
+        retry: function () { startMap(FIRST); },
+        restart: splash
+    });
+    MM.assets.load(function (failed) {
+        if (failed.length) console.error("images not loaded:", failed);
+        splash();
+        requestAnimationFrame(frame);
+    });
+})();