mr-crocs-adventures/js/module.js

188 lines
5.2 KiB
JavaScript
Raw Normal View History

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-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(image)
{
image.addEventListener(
'load', () => {
this.update();1
}
);
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-01-25 23:30:36 +01:00
if (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-01-25 23:30:36 +01:00
} else if (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-01-26 22:12:32 +01:00
/* Drawing */
2020-01-25 13:11:25 +01:00
if (timestamp - lastRendered >= FRAME_DURATION) {
context.clearRect(0, 0, window.innerWidth, window.innerHeight);
2020-01-23 23:09:03 +01:00
architecture.draw(context);
2020-01-25 13:11:25 +01:00
mrCroc.draw(context);
2020-01-26 23:25:18 +01:00
2020-01-25 23:30:36 +01:00
/*
2020-01-26 23:25:18 +01:00
context.fillRect(0, ceilingHeight, window.innerWidth, 1);
context.fillRect(0, groundHeight, window.innerWidth, 1);
2020-01-25 23:30:36 +01:00
context.fillStyle = 'black';
context.fillRect(lastWallRight, 0, 1, window.innerHeight);
context.fillStyle = 'red';
context.fillRect(lastWallLeft, 0, 1, window.innerHeight);
mrCroc.getPositionHeadRight().draw(context);
mrCroc.getPositionFootRight().draw(context);
mrCroc.getPositionHeadLeft().draw(context);
mrCroc.getPositionFootLeft().draw(context);
*/
2020-01-26 23:25:18 +01:00
2020-01-22 22:50:45 +01:00
lastRendered = timestamp;
}
lastTimestamp = timestamp;
window.requestAnimationFrame(MainLoop);
}
const FPS = 60;
const FRAME_DURATION = 1000 / FPS;
const GAME_SPEED = 1;
2020-01-25 23:30:36 +01:00
const GRAVITY = 2;
2020-01-25 13:11:25 +01:00
2020-01-26 23:25:18 +01:00
let levelJson = new FileLoader('levels/level.json');
2020-01-25 13:11:25 +01:00
const LEVEL = levelJson.getContent();
2020-01-22 22:50:45 +01:00
let lastRendered = undefined;
let lastTimestamp = undefined;
let context;
2020-01-25 13:11:25 +01:00
let mrCroc, architecture;
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-01-22 22:50:45 +01:00
let imgAnimation = new Image();
imgAnimation.src = 'graphics/mr-croc-walk-right.png';
loader.addImage(imgAnimation);
let imgAnimationB = new Image();
imgAnimationB.src = 'graphics/mr-croc-walk-left.png';
loader.addImage(imgAnimationB);
2020-01-23 23:09:03 +01:00
let imgArch = new Image();
2020-01-25 23:30:36 +01:00
imgArch.src = 'graphics/tileset-landscape01.jpg';
2020-01-23 23:09:03 +01:00
loader.addImage(imgArch);
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-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-01-23 23:09:03 +01:00
architecture = RetroArchitecture.createFromJson(LEVEL);
2020-01-22 22:50:45 +01:00
mrCroc = new MrCroc();
2020-01-26 23:25:18 +01:00
mrCroc.position.x = 250;
mrCroc.position.y = 0;
2020-01-25 13:11:25 +01:00
2020-01-22 22:50:45 +01:00
window.requestAnimationFrame(MainLoop);
}
);