mr-crocs-adventures/js/module.js

228 lines
6.7 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-01-25 13:11:25 +01:00
import FileLoader from "./FileLoader.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";
2020-01-22 22:50:45 +01:00
class ImageLoader
{
images = [];
numberImagesLoaded = 0;
update()
{
this.numberImagesLoaded++;
if (this.numberImagesLoaded === this.images.length) {
window.dispatchEvent(new Event('imagesloaded'));
}
}
getCurrentProgress()
{
return this.numberImagesLoaded / this.images.length;
}
addImage(imagePath)
2020-01-22 22:50:45 +01:00
{
let image = new Image();
image.src = imagePath;
2020-01-22 22:50:45 +01:00
image.addEventListener(
'load', () => {
this.update();
2020-01-22 22:50:45 +01:00
}
);
this.images.push(image);
}
}
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-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);
2020-01-26 23:25:18 +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;
alert('Gisela: "Thanks for showing up, Mr. Croc, but I\'m not in danger."');
}
2020-01-22 22:50:45 +01:00
window.requestAnimationFrame(MainLoop);
}
const GAME_SPEED = 1;
2020-01-25 23:30:36 +01:00
const GRAVITY = 2;
let fps;
let frameDuration;
2020-01-25 13:11:25 +01:00
let levelJson = new FileLoader('levels/moon.json');
2020-02-04 21:42:14 +01:00
const LEVEL = JSON.parse(levelJson.getContent());
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-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.tileset].tileset);
loader.addImage(Setting.GRAPHICS_LOCATION + 'gisela-right.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.tileset].backgroundImage !== null) {
canvas.style.backgroundImage = 'url("' + Setting.GRAPHICS_LOCATION + GraphicSet[LEVEL.tileset].backgroundImage +'")';
}
canvas.style.backgroundColor = LEVEL.backgroundColor;
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-01-19 00:45:17 +01:00
2020-02-04 21:42:14 +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
fps = 120;
frameDuration = 1000 / fps;
2020-01-22 22:50:45 +01:00
window.requestAnimationFrame(MainLoop);
}
);