|
17
|
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;
|
|
|
18 var view = { dpr: 1, now: 0, refused: null, hover: null, stats: { ms: 0 } };
|
|
|
19
|
|
|
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) {
|
|
|
25 var seed = cfg.debug.seed || (Math.random() * 4294967296) >>> 0;
|
|
|
26 game = new MM.Game(m, seed, m === FIRST ? cfg.debug.start_wave : 1);
|
|
|
27 resize();
|
|
|
28 }
|
|
|
29
|
|
|
30 // the splash screen over the paused first map
|
|
|
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
|
|
|
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);
|
|
|
85 MM.audio.init();
|
|
|
86 MM.input.attach({
|
|
|
87 canvas: canvas,
|
|
|
88 view: view,
|
|
|
89 game: function () { return game; },
|
|
|
90 layout: function () { return L; },
|
|
|
91 play: play,
|
|
|
92 next: function () { startMap(game.map.index + 1); },
|
|
|
93 retry: function () { startMap(FIRST); },
|
|
|
94 restart: splash
|
|
|
95 });
|
|
|
96 MM.assets.load(function (failed) {
|
|
|
97 if (failed.length) console.error("images not loaded:", failed);
|
|
|
98 splash();
|
|
|
99 requestAnimationFrame(frame);
|
|
|
100 });
|
|
|
101 })();
|