view game/js/main.js @ 28:b0658e7fcee2

Added tag 0.9.1 for changeset 75deda08db90
author ark
date 2026-10-20 17:19 +0200
parents 75deda08db90
children 85d23328a215
line wrap: on
line source

// start-up and main loop

window.MM = window.MM || {};

(function () {
    "use strict";

    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 };

    // the first map, the wave it starts at and the seed, 0 for a new seed
    // per map. js/debug.js may set them; only the first map starts at the
    // start wave
    var start = MM.debug ? MM.debug.start : { map: 0, wave: 1, seed: 0 };
    var FIRST = MM.util.clamp(start.map, 0, MM.maps.length - 1);

    function startMap(m) {
        var seed = start.seed || (Math.random() * 4294967296) >>> 0;
        game = new MM.Game(m, seed, m === FIRST ? 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);

        if (MM.debug) MM.debug.frameTime(performance.now() - t0);
        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);
    });
})();