wobblyhead hg > minor_monsters
view game/js/main.js @ 33:767970d80214
Added tag 0.10.0 for changeset 85d23328a215
| author | ark |
|---|---|
| date | 2026-10-20 18:39 +0200 |
| parents | 85d23328a215 |
| children |
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, with the intro music. // browsers start sound only in a user interaction, so on the first // splash screen, unless sound is switched off, a sound button covers // the play button until it is tapped. sound is unlocked when the // splash screen returns after a won game. function splash(first) { startMap(FIRST); game.splash = true; game.paused = true; if (first && MM.audio.enabled()) game.askSound = true; else MM.audio.playMusic("intro"); } function startSound() { MM.audio.start(); game.askSound = false; MM.audio.playMusic("intro"); } function play() { MM.audio.stopMusic(); 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; }, startSound: startSound, play: play, next: function () { startMap(game.map.index + 1); }, retry: function () { startMap(FIRST); }, restart: function () { splash(false); } }); MM.assets.load(function (failed) { if (failed.length) console.error("images not loaded:", failed); splash(true); requestAnimationFrame(frame); }); })();
