mr-crocs-adventures/js/module.js

104 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-02-11 21:20:11 +01:00
'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";
2023-09-18 22:33:55 +02:00
import {EventBus} from "./events/EventBus.js";
import {Game} from "./Game.js";
2020-01-22 22:50:45 +01:00
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) => {
2023-09-18 22:33:55 +02:00
EventBus.clear();
loadLevel(Level.createFromJson(data));
}
game.isPaused = true;
}
window.requestAnimationFrame(mainLoop);
}
2023-09-18 22:33:55 +02:00
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');
2024-09-03 23:08:08 +02:00
loader.addImage(Setting.GRAPHICS_LOCATION + 'giesela-left-sleeping.png');
loader.addImage(Setting.GRAPHICS_LOCATION + 'giesela-right-sleeping.png');
2023-09-18 22:33:55 +02:00
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',
2023-09-19 00:33:04 +02:00
'darius.json',
2023-09-24 14:49:47 +02:00
'imperial-prison.json',
2024-12-08 23:11:52 +01:00
'southpole.json',
];
2023-09-18 22:33:55 +02:00
let game;
2023-09-18 22:33:55 +02:00
const urlGetter = new UrlParam();
const levelIndex = urlGetter.getInt('level');
2023-09-19 11:53:43 +02:00
Level.createFromFile(
2023-09-24 14:49:47 +02:00
Setting.LEVELS_LOCATION + LEVEL[levelIndex % LEVEL.length],
2023-09-19 11:53:43 +02:00
(level) => {
loadLevel(level);
}
);