2020-02-11 21:20:11 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-01-22 22:50:45 +01:00
|
|
|
import Key from "./Key.js";
|
|
|
|
import MrCroc from "./MrCroc.js";
|
2020-01-23 23:09:03 +01:00
|
|
|
import RetroArchitecture from "./retro/RetroArchitecture.js";
|
2020-02-09 22:06:59 +01:00
|
|
|
import Camera from "./Camera.js";
|
2020-02-11 21:20:11 +01:00
|
|
|
import Gisela from "./Gisela.js";
|
2020-02-12 21:29:06 +01:00
|
|
|
import Setting from "./Setting.js";
|
2020-02-12 23:30:04 +01:00
|
|
|
import FrameRateMeasurer from "./FrameRateMeasurer.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 15:32:17 +01:00
|
|
|
import InterfaceEvent from "./events/InterfaceEvent.js";
|
2020-02-16 19:39:44 +01:00
|
|
|
import UrlParam from "./UrlParam.js";
|
2020-02-24 21:09:21 +01:00
|
|
|
import UserInterface from "./ui/UserInterface.js";
|
|
|
|
import TextMessageGisela from "./ui/TextMessageGisela.js";
|
|
|
|
import TextMessageMrCroc from "./ui/TextMessageMrCroc.js";
|
2020-01-22 22:50:45 +01:00
|
|
|
|
|
|
|
function MainLoop(timestamp)
|
|
|
|
{
|
|
|
|
if (lastRendered === undefined && lastTimestamp === undefined) {
|
|
|
|
lastRendered = timestamp;
|
|
|
|
lastTimestamp = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
let delta = (timestamp - lastTimestamp) / (10 / GAME_SPEED);
|
|
|
|
|
2020-01-26 22:12:32 +01:00
|
|
|
let ceilingHeight = Math.max(
|
2020-01-25 23:30:36 +01:00
|
|
|
architecture.getCeilingHeight(mrCroc.getPositionHeadLeft()),
|
|
|
|
architecture.getCeilingHeight(mrCroc.getPositionHeadRight()),
|
|
|
|
);
|
2020-01-22 22:50:45 +01:00
|
|
|
|
2020-01-26 22:12:32 +01:00
|
|
|
let groundHeight = Math.min(
|
2020-01-25 13:11:25 +01:00
|
|
|
architecture.getGroundHeight(mrCroc.getPositionFootLeft()),
|
|
|
|
architecture.getGroundHeight(mrCroc.getPositionFootRight())
|
|
|
|
);
|
2020-01-22 22:50:45 +01:00
|
|
|
|
2020-01-26 22:12:32 +01:00
|
|
|
/* Handle falling */
|
2020-01-25 13:11:25 +01:00
|
|
|
mrCroc.position.y += mrCroc.fallSpeed;
|
2020-01-25 23:30:36 +01:00
|
|
|
mrCroc.fallSpeed += GRAVITY * delta;
|
2020-01-25 13:11:25 +01:00
|
|
|
|
2020-01-26 22:12:32 +01:00
|
|
|
/* Handle ground collision */
|
|
|
|
if (mrCroc.position.y > groundHeight && mrCroc.fallSpeed > 0) {
|
|
|
|
mrCroc.position.y = groundHeight;
|
2020-01-25 23:30:36 +01:00
|
|
|
mrCroc.fallSpeed = 0;
|
2020-01-25 13:11:25 +01:00
|
|
|
}
|
|
|
|
|
2020-01-26 22:12:32 +01:00
|
|
|
/* Handle ceiling collision */
|
|
|
|
if (mrCroc.position.y - mrCroc.getHeight() <= ceilingHeight) {
|
2020-01-25 23:30:36 +01:00
|
|
|
mrCroc.fallSpeed = 0;
|
2020-01-26 22:12:32 +01:00
|
|
|
mrCroc.position.y = ceilingHeight + mrCroc.getHeight() + 1;
|
2020-01-25 23:30:36 +01:00
|
|
|
}
|
2020-01-25 13:11:25 +01:00
|
|
|
|
2020-01-26 22:12:32 +01:00
|
|
|
/* Handle jumping */
|
2020-01-25 23:30:36 +01:00
|
|
|
if (!mrCroc.isJumping && mrCroc.fallSpeed === 0 && mrCroc.position.y === groundHeight && KeyJump.isPressed()) {
|
|
|
|
mrCroc.jump();
|
|
|
|
} else if (!KeyJump.isPressed()) {
|
|
|
|
mrCroc.isJumping = false;
|
2020-01-25 13:11:25 +01:00
|
|
|
}
|
|
|
|
|
2020-01-26 22:12:32 +01:00
|
|
|
/* Movement left and right */
|
2020-02-11 21:20:11 +01:00
|
|
|
if (!hasPlayerLeftArchitecture && KeyLeft.isPressed()) {
|
2020-01-26 23:25:18 +01:00
|
|
|
let lastWallLeft = Math.min(
|
2020-01-25 23:30:36 +01:00
|
|
|
architecture.getWallLeft(mrCroc.getPositionHeadLeft()),
|
|
|
|
architecture.getWallLeft(mrCroc.getPositionFootLeft())
|
|
|
|
);
|
|
|
|
|
2020-01-26 22:12:32 +01:00
|
|
|
mrCroc.moveLeft(timestamp, delta);
|
|
|
|
|
|
|
|
if (mrCroc.position.x <= lastWallLeft + mrCroc.getWidth() * 0.5) {
|
2020-01-26 11:25:20 +01:00
|
|
|
mrCroc.position.x = lastWallLeft + mrCroc.getWidth() * 0.5 + 1;
|
2020-01-25 13:11:25 +01:00
|
|
|
}
|
2020-02-11 21:20:11 +01:00
|
|
|
} else if (!hasPlayerLeftArchitecture && KeyRight.isPressed()) {
|
2020-01-26 23:25:18 +01:00
|
|
|
let lastWallRight = Math.max(
|
2020-01-25 23:30:36 +01:00
|
|
|
architecture.getWallRight(mrCroc.getPositionHeadRight()),
|
|
|
|
architecture.getWallRight(mrCroc.getPositionFootRight())
|
|
|
|
);
|
|
|
|
|
2020-01-26 22:12:32 +01:00
|
|
|
mrCroc.moveRight(timestamp, delta);
|
|
|
|
|
|
|
|
if (mrCroc.position.x >= lastWallRight - mrCroc.getWidth() * 0.5) {
|
2020-01-26 11:25:20 +01:00
|
|
|
mrCroc.position.x = lastWallRight - mrCroc.getWidth() * 0.5 - 1;
|
2020-01-25 13:11:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 21:20:11 +01:00
|
|
|
if (!hasPlayerLeftArchitecture && !architecture.isInsideArchitecture(mrCroc.position)) {
|
|
|
|
hasPlayerLeftArchitecture = true;
|
|
|
|
|
|
|
|
setTimeout(
|
|
|
|
function () {
|
|
|
|
architecture.setMovableToStartPosition(mrCroc);
|
|
|
|
hasPlayerLeftArchitecture = false;
|
|
|
|
}, 2000
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:42:26 +01:00
|
|
|
camera.focusPosition(
|
|
|
|
mrCroc.position.x - mrCroc.getWidth() * 0.5,
|
|
|
|
mrCroc.position.y - mrCroc.getHeight() * 0.5,
|
|
|
|
20
|
|
|
|
);
|
|
|
|
camera.lockCameraIntoBorders();
|
|
|
|
|
2020-01-26 22:12:32 +01:00
|
|
|
/* Drawing */
|
2020-02-12 23:30:04 +01:00
|
|
|
if (timestamp - lastRendered >= frameDuration) {
|
2021-09-15 17:24:53 +02:00
|
|
|
lastRendered = timestamp;
|
|
|
|
|
2020-02-24 21:18:45 +01:00
|
|
|
if (gisela.currentAnimation !== 'LOOK_LEFT' && mrCroc.position.x < gisela.position.x) {
|
|
|
|
gisela.currentAnimation = 'LOOK_LEFT';
|
|
|
|
} else if (gisela.currentAnimation !== 'LOOK_RIGHT' && mrCroc.position.x >= gisela.position.x) {
|
|
|
|
gisela.currentAnimation = 'LOOK_RIGHT';
|
|
|
|
}
|
|
|
|
|
2020-01-25 13:11:25 +01:00
|
|
|
context.clearRect(0, 0, window.innerWidth, window.innerHeight);
|
2021-09-21 09:47:27 +02:00
|
|
|
|
|
|
|
switch (browser) {
|
|
|
|
case 'Chrome':
|
|
|
|
architecture.drawForChrome(context, camera);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
architecture.drawForFirefox(context, camera);
|
|
|
|
}
|
|
|
|
|
2020-02-09 22:06:59 +01:00
|
|
|
mrCroc.draw(context, camera);
|
2020-02-11 21:20:11 +01:00
|
|
|
gisela.draw(context, camera);
|
2020-02-24 21:09:21 +01:00
|
|
|
userInterface.draw(context);
|
2020-01-22 22:50:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lastTimestamp = timestamp;
|
|
|
|
|
2020-02-12 21:29:06 +01:00
|
|
|
if (!gameFinished && mrCroc.isJumping === false && architecture.isMovableInsideTargetPosition(mrCroc)) {
|
|
|
|
gameFinished = true;
|
|
|
|
KeyLeft.pressed = false;
|
|
|
|
KeyRight.pressed = false;
|
|
|
|
KeyJump.pressed = false;
|
|
|
|
lastTimestamp = undefined;
|
|
|
|
lastRendered = undefined;
|
2020-02-24 00:34:20 +01:00
|
|
|
textBoxGameFinished.updateLines(window.innerWidth - 40, context);
|
2020-02-24 21:09:21 +01:00
|
|
|
textBoxGameFinished.animate(75);
|
|
|
|
userInterface.addTextBox(textBoxGameFinished);
|
2020-02-12 21:29:06 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 22:50:45 +01:00
|
|
|
window.requestAnimationFrame(MainLoop);
|
|
|
|
}
|
|
|
|
|
2020-02-16 19:39:44 +01:00
|
|
|
const LEVEL = [
|
|
|
|
'level01.json',
|
|
|
|
'moon.json',
|
|
|
|
'moonbase.json',
|
|
|
|
'terrain8.json',
|
|
|
|
];
|
|
|
|
|
|
|
|
let urlGetter = new UrlParam();
|
|
|
|
|
|
|
|
let levelIndex = urlGetter.getInt('level');
|
|
|
|
|
|
|
|
if (levelIndex === undefined || levelIndex < 0 || levelIndex >= LEVEL.length) {
|
|
|
|
levelIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let level = Level.createFromFile(Setting.LEVELS_LOCATION + LEVEL[levelIndex]);
|
2020-02-16 00:53:03 +01:00
|
|
|
|
2020-01-22 22:50:45 +01:00
|
|
|
const GAME_SPEED = 1;
|
2020-02-16 00:53:03 +01:00
|
|
|
const GRAVITY = level.gravity;
|
2021-09-21 09:48:54 +02:00
|
|
|
let fps = 120;
|
2021-09-21 09:47:27 +02:00
|
|
|
const browser = navigator.userAgent.indexOf('Firefox') === -1 ? 'Chrome' : 'Firefox';
|
2020-02-12 23:30:04 +01:00
|
|
|
let frameDuration;
|
2020-01-22 22:50:45 +01:00
|
|
|
let lastRendered = undefined;
|
|
|
|
let lastTimestamp = undefined;
|
|
|
|
let context;
|
2020-02-11 21:20:11 +01:00
|
|
|
let mrCroc, gisela, architecture;
|
2020-02-09 22:06:59 +01:00
|
|
|
let camera = new Camera();
|
2020-02-11 21:20:11 +01:00
|
|
|
let gameFinished = false;
|
|
|
|
let hasPlayerLeftArchitecture = false;
|
2020-02-24 21:09:21 +01:00
|
|
|
let textBoxGameStart;
|
2020-02-24 00:19:21 +01:00
|
|
|
let textBoxGameFinished;
|
2020-02-24 21:09:21 +01:00
|
|
|
let userInterface = new UserInterface();
|
2020-01-22 22:50:45 +01:00
|
|
|
|
|
|
|
let KeyLeft = new Key('ArrowLeft');
|
|
|
|
let KeyRight = new Key('ArrowRight');
|
2020-01-25 13:11:25 +01:00
|
|
|
let KeyJump = new Key('Space');
|
2020-01-22 22:50:45 +01:00
|
|
|
|
|
|
|
let loader = new ImageLoader();
|
2020-01-19 00:45:17 +01:00
|
|
|
|
2020-02-12 23:30:04 +01:00
|
|
|
loader.addImage(Setting.GRAPHICS_LOCATION + 'mr-croc-walk-right.png');
|
|
|
|
loader.addImage(Setting.GRAPHICS_LOCATION + 'mr-croc-walk-left.png');
|
2020-02-16 00:53:03 +01:00
|
|
|
loader.addImage(Setting.TILESET_LOCATION + GraphicSet[level.getTilesetId()].tileset);
|
2020-02-12 23:30:04 +01:00
|
|
|
loader.addImage(Setting.GRAPHICS_LOCATION + 'gisela-right.png');
|
2020-02-24 21:18:45 +01:00
|
|
|
loader.addImage(Setting.GRAPHICS_LOCATION + 'gisela-left.png');
|
2020-01-22 22:50:45 +01:00
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
'imagesloaded',
|
|
|
|
() => {
|
|
|
|
let canvas = document.getElementById('canvas');
|
|
|
|
canvas.width = window.innerWidth;
|
|
|
|
canvas.height = window.innerHeight;
|
2020-02-14 00:12:24 +01:00
|
|
|
canvas.style.backgroundAttachment = 'fixed';
|
|
|
|
canvas.style.backgroundSize = 'cover';
|
|
|
|
canvas.style.backgroundPosition = 'center center';
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
if (GraphicSet[level.getTilesetId()].backgroundImage !== null) {
|
|
|
|
canvas.style.backgroundImage = 'url("' + Setting.GRAPHICS_LOCATION + GraphicSet[level.getTilesetId()].backgroundImage +'")';
|
2020-02-14 00:12:24 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
canvas.style.backgroundColor = level.getBackgroundColor();
|
2020-01-19 00:45:17 +01:00
|
|
|
|
2020-01-25 23:30:36 +01:00
|
|
|
window.addEventListener(
|
|
|
|
'resize',
|
|
|
|
function () {
|
|
|
|
canvas.width = window.innerWidth;
|
|
|
|
canvas.height = window.innerHeight;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-01-22 22:50:45 +01:00
|
|
|
context = canvas.getContext('2d');
|
2020-02-24 21:09:21 +01:00
|
|
|
textBoxGameFinished = new TextMessageGisela(
|
2020-02-24 00:19:21 +01:00
|
|
|
'Gisela: "Thanks for showing up, Mr. Croc, but I\'m not in danger."',
|
|
|
|
context
|
|
|
|
);
|
2020-02-24 21:09:21 +01:00
|
|
|
|
|
|
|
textBoxGameStart = new TextMessageMrCroc('Mr. Croc: "Where is Gisela? I have to find her!"', context);
|
|
|
|
textBoxGameStart.animate(75, 1000);
|
|
|
|
textBoxGameStart.show(1000);
|
|
|
|
textBoxGameStart.hide(10000);
|
|
|
|
userInterface.addTextBox(textBoxGameStart);
|
2020-01-19 00:45:17 +01:00
|
|
|
|
2020-02-16 00:53:03 +01:00
|
|
|
architecture = RetroArchitecture.createFromData(level);
|
2020-02-09 22:06:59 +01:00
|
|
|
camera.borderRight = architecture.columns * architecture.tileWidth;
|
|
|
|
camera.borderBottom = architecture.rows * architecture.tileHeight;
|
2020-01-23 23:09:03 +01:00
|
|
|
|
2020-01-22 22:50:45 +01:00
|
|
|
mrCroc = new MrCroc();
|
2020-02-11 21:20:11 +01:00
|
|
|
architecture.setMovableToStartPosition(mrCroc);
|
|
|
|
|
|
|
|
gisela = new Gisela();
|
|
|
|
architecture.setMovableToTargetPosition(gisela);
|
2020-01-25 13:11:25 +01:00
|
|
|
|
2021-09-15 17:24:53 +02:00
|
|
|
frameDuration = 1000 / fps;
|
|
|
|
window.requestAnimationFrame(MainLoop);
|
2020-01-22 22:50:45 +01:00
|
|
|
}
|
2021-09-15 17:24:53 +02:00
|
|
|
);
|