mr-crocs-adventures/js/module.js

101 lines
2.5 KiB
JavaScript

'use strict';
import Setting from "./Setting.js";
import GraphicSet from "./GraphicSet.js";
import ImageLoader from "./ImageLoader.js";
import Level from "./Level.js";
import UrlParam from "./UrlParam.js";
import {LoadLevelDialog} from "./ui/LoadLevelDialog.js";
import {EventBus} from "./events/EventBus.js";
import {Game} from "./Game.js";
function mainLoop(timestamp)
{
if (game.isPaused) {
return;
}
if (game.lastRendered === undefined && game.lastTimestamp === undefined) {
game.lastRendered = timestamp;
game.lastTimestamp = timestamp;
}
let delta = (timestamp - game.lastTimestamp) / (10 / Game.GAME_SPEED);
game.handlePhysics(delta, timestamp);
game.updateCamera();
game.render(timestamp);
game.lastTimestamp = timestamp;
if (game.canBeFinished()) {
game.finish();
}
if (game.KeyLoad.isPressed()) {
const dialog = new LoadLevelDialog();
dialog.onClose = () => {
dialog.close();
game.isPaused = false;
window.requestAnimationFrame(mainLoop);
}
dialog.onLoad = (data) => {
EventBus.clear();
loadLevel(Level.createFromJson(data));
}
game.isPaused = true;
}
window.requestAnimationFrame(mainLoop);
}
function loadLevel(level)
{
const loader = new ImageLoader();
loader.addImage(Setting.GRAPHICS_LOCATION + 'mr-croc-walk-right.png');
loader.addImage(Setting.GRAPHICS_LOCATION + 'mr-croc-walk-left.png');
loader.addImage(Setting.GRAPHICS_LOCATION + 'gisela-right.png');
loader.addImage(Setting.GRAPHICS_LOCATION + 'gisela-left.png');
loader.addImage(Setting.GRAPHICS_LOCATION + 'gisela-left.png');
for (const graphicSet of GraphicSet) {
loader.addImage(Setting.TILESET_LOCATION + graphicSet.tileset);
if (graphicSet.backgroundImage !== null) {
loader.addImage(Setting.GRAPHICS_LOCATION + graphicSet.backgroundImage);
}
}
loader.load();
EventBus.addEventListener(
'imagesloaded',
() => {
game = new Game(level);
game.init(mainLoop);
}
);
}
const LEVEL = [
'level01.json',
'moon.json',
'moonbase.json',
'terrain8.json',
'darius.json',
'imperial-prison.json',
];
let game;
const urlGetter = new UrlParam();
const levelIndex = urlGetter.getInt('level');
Level.createFromFile(
Setting.LEVELS_LOCATION + LEVEL[levelIndex % LEVEL.length],
(level) => {
loadLevel(level);
}
);