Path: blob/main/projects/missiles/src/main.js
1835 views
MG.init = function () {1MG.fog.init();2MG.banner.init();3MG.game.init();4MG.hud.init();56document.addEventListener('mousemove', function(evt){7MG.game.onMouseMove(evt.clientX, evt.clientY);8}, false);9document.addEventListener('touchmove', function(evt){10MG.game.onMouseMove(evt.clientX || evt.touches[0].clientX, evt.clientY || evt.touches[0].clientY);11}, false);1213window.addEventListener('click', MG.game.onMouseClick, false);1415var update = function (dt) {16MG.fog.update(dt);17MG.game.update(dt);18MG.hud.update(dt);19MG.banner.update(dt);20MG.fog.updateDOM();21MG.game.updateDOM();22MG.hud.updateDOM();23MG.banner.updateDOM();24}2526var lastTick = 0;27var zeroCounter = 0;28var useFallback = false;2930if (!window.requestAnimationFrame) {31useFallback = true;32}3334var mainLoop = function(thisTick) {35var dt;3637// Some browsers don't pass in a time. If `thisTick` isn't set for38// more than a few frames fall back to `setTimeout`39if (!thisTick) {40zeroCounter += 1;41} else {42zeroCounter = 0;43}44if (zeroCounter > 10) {45useFallback = true;46}4748thisTick = thisTick || 0;49if (useFallback) {50dt = 1/30;51} else {52var dt = (thisTick - lastTick)/1000;53}54// pretend that the frame rate is actually higher if it drops below55// 10fps in order to avoid wierdness56if (dt > 1/10) {57dt = 1/10;58}5960lastTick = thisTick;6162update(dt);6364if (useFallback) {65window.setTimeout(mainLoop, 1000 / 30);66} else {67window.requestAnimationFrame(mainLoop);68}69}7071mainLoop();72}7374MG.init();757677