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";
|
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', () => {
|
2020-02-02 23:00:14 +01:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gameFinished && architecture.isMovableInsideTargetPosition(mrCroc)) {
|
|
|
|
gameFinished = true;
|
|
|
|
|
|
|
|
setTimeout(
|
|
|
|
function () {
|
|
|
|
alert('Gisela: "Thanks for showing up, Mr. Croc, but I\'m not in danger."');
|
|
|
|
lastTimestamp = timestamp;
|
|
|
|
lastRendered = timestamp;
|
|
|
|
}, 1000
|
|
|
|
)
|
2020-02-10 19:53:30 +01:00
|
|
|
}
|
|
|
|
|
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-01-25 13:11:25 +01:00
|
|
|
if (timestamp - lastRendered >= FRAME_DURATION) {
|
|
|
|
context.clearRect(0, 0, window.innerWidth, window.innerHeight);
|
2020-02-09 22:06:59 +01:00
|
|
|
|
2020-02-11 23:42:26 +01:00
|
|
|
// camera.centerCamera(mrCroc.position.x - mrCroc.getWidth() * 0.5, mrCroc.position.y - mrCroc.getHeight() * 0.5);
|
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;
|
|
|
|
|
|
|
|
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-02-10 19:36:46 +01:00
|
|
|
let levelJson = new FileLoader('levels/level01.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
|
|
|
|
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-02-11 21:20:11 +01:00
|
|
|
let imgGisela = new Image();
|
|
|
|
imgGisela.src = 'graphics/gisela-right.png';
|
|
|
|
loader.addImage(imgGisela);
|
|
|
|
|
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-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
|
|
|
|
2020-01-22 22:50:45 +01:00
|
|
|
window.requestAnimationFrame(MainLoop);
|
|
|
|
}
|
|
|
|
);
|