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();
|
2024-12-21 16:51:31 +01:00
|
|
|
|
|
|
|
dialog.onClose = () => {
|
2023-09-17 14:18:21 +02:00
|
|
|
dialog.close();
|
|
|
|
game.isPaused = false;
|
|
|
|
window.requestAnimationFrame(mainLoop);
|
|
|
|
}
|
|
|
|
dialog.onLoad = (data) => {
|
2023-09-18 22:33:55 +02:00
|
|
|
EventBus.clear();
|
2024-12-21 16:51:31 +01:00
|
|
|
|
|
|
|
for (const effect of game.level.fullscreenEffects) {
|
|
|
|
effect.destroy();
|
|
|
|
}
|
|
|
|
|
2023-09-18 22:33:55 +02:00
|
|
|
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');
|
2024-12-21 16:51:31 +01:00
|
|
|
loader.addImage(Setting.GRAPHICS_LOCATION + 'mr-croc-walk-right-christmas.png');
|
|
|
|
loader.addImage(Setting.GRAPHICS_LOCATION + 'mr-croc-walk-left-christmas.png');
|
2023-09-18 22:33:55 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-24 14:49:47 +02:00
|
|
|
'imperial-prison.json',
|
2024-12-08 23:11:52 +01:00
|
|
|
'southpole.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(
|
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);
|
|
|
|
}
|
2023-09-17 14:29:42 +02:00
|
|
|
);
|