mr-crocs-adventures/js/module.js

239 lines
7.8 KiB
JavaScript
Raw Normal View History

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";
import Setting from "./Setting.js";
import FrameRateMeasurer from "./FrameRateMeasurer.js";
import GraphicSet from "./GraphicSet.js";
import ImageLoader from "./ImageLoader.js";
import Level from "./Level.js";
2020-02-16 15:32:17 +01:00
import InterfaceEvent from "./events/InterfaceEvent.js";
import UrlParam from "./UrlParam.js";
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 */
if (timestamp - lastRendered >= frameDuration) {
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);
2020-02-09 22:06:59 +01:00
architecture.draw(context, camera);
mrCroc.draw(context, camera);
2020-02-11 21:20:11 +01:00
gisela.draw(context, camera);
userInterface.draw(context);
2020-02-24 00:19:21 +01:00
2020-01-22 22:50:45 +01:00
lastRendered = timestamp;
}
lastTimestamp = timestamp;
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);
textBoxGameFinished.animate(75);
userInterface.addTextBox(textBoxGameFinished);
}
2020-01-22 22:50:45 +01:00
window.requestAnimationFrame(MainLoop);
}
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-01-22 22:50:45 +01:00
const GAME_SPEED = 1;
const GRAVITY = level.gravity;
let fps;
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;
let textBoxGameStart;
2020-02-24 00:19:21 +01:00
let textBoxGameFinished;
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
loader.addImage(Setting.GRAPHICS_LOCATION + 'mr-croc-walk-right.png');
loader.addImage(Setting.GRAPHICS_LOCATION + 'mr-croc-walk-left.png');
loader.addImage(Setting.TILESET_LOCATION + GraphicSet[level.getTilesetId()].tileset);
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
new FrameRateMeasurer();
2020-02-11 21:20:11 +01:00
2020-01-22 22:50:45 +01:00
window.addEventListener(
'imagesloaded',
() => {
let canvas = document.getElementById('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.backgroundAttachment = 'fixed';
canvas.style.backgroundSize = 'cover';
canvas.style.backgroundPosition = 'center center';
if (GraphicSet[level.getTilesetId()].backgroundImage !== null) {
canvas.style.backgroundImage = 'url("' + Setting.GRAPHICS_LOCATION + GraphicSet[level.getTilesetId()].backgroundImage +'")';
}
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');
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
);
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
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
2020-02-16 15:32:17 +01:00
window.addEventListener(
InterfaceEvent.FRAME_RATE_MEASURED,
(event) => {
fps = event.frameRate;
frameDuration = 1000 / fps;
window.requestAnimationFrame(MainLoop);
}
);
2020-01-22 22:50:45 +01:00
}
);