2020-02-11 21:20:11 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-02-12 21:29:06 +01:00
|
|
|
import Setting from "./Setting.js";
|
2020-02-14 00:12:24 +01:00
|
|
|
import GraphicSet from "./GraphicSet.js";
|
2020-02-16 00:53:03 +01:00
|
|
|
import ImageLoader from "./ImageLoader.js";
|
|
|
|
import Level from "./Level.js";
|
2020-02-16 19:39:44 +01:00
|
|
|
import UrlParam from "./UrlParam.js";
|
2023-09-17 14:18:21 +02:00
|
|
|
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
|
|
|
|
2023-09-17 14:18:21 +02: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));
|
2023-09-17 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-17 14:18:21 +02:00
|
|
|
const LEVEL = [
|
|
|
|
'level01.json',
|
|
|
|
'moon.json',
|
|
|
|
'moonbase.json',
|
|
|
|
'terrain8.json',
|
2023-09-19 00:33:04 +02:00
|
|
|
'darius.json',
|
2023-09-17 14:18:21 +02:00
|
|
|
];
|
|
|
|
|
2023-09-18 22:33:55 +02:00
|
|
|
let game;
|
2023-09-17 14:18:21 +02:00
|
|
|
|
2023-09-18 22:33:55 +02:00
|
|
|
const urlGetter = new UrlParam();
|
2023-09-17 14:18:21 +02:00
|
|
|
const levelIndex = urlGetter.getInt('level');
|
2023-09-17 14:29:42 +02:00
|
|
|
|
2023-09-19 11:53:43 +02:00
|
|
|
Level.createFromFile(
|
|
|
|
Setting.LEVELS_LOCATION + LEVEL[levelIndex < 0 || levelIndex >= LEVEL.length ? 0 : levelIndex],
|
|
|
|
(level) => {
|
|
|
|
loadLevel(level);
|
|
|
|
}
|
2023-09-17 14:29:42 +02:00
|
|
|
);
|