Fix for corner bug.

This commit is contained in:
Mal 2020-01-26 22:12:32 +01:00
parent 5e1c7fb4dd
commit 0d7b99a903
2 changed files with 21 additions and 34 deletions

View File

@ -4,7 +4,7 @@ import RetroAnimation from "./retro/RetroAnimation.js";
export default class MrCroc extends Movable
{
constructor() {
let SCALE = 3;
let SCALE = 2;
super(new RetroAnimation('graphics/mr-croc-walk-right.png', 2, SCALE), 7);
this.isJumping = false;
this.addAnimation('WALK_RIGHT', new RetroAnimation('graphics/mr-croc-walk-right.png', 2, SCALE, 10));

View File

@ -43,80 +43,69 @@ function MainLoop(timestamp)
let delta = (timestamp - lastTimestamp) / (10 / GAME_SPEED);
let lastCeilingHeight = Math.max(
let ceilingHeight = Math.max(
architecture.getCeilingHeight(mrCroc.getPositionHeadLeft()),
architecture.getCeilingHeight(mrCroc.getPositionHeadRight()),
);
lastGroundHeight = Math.min(
let groundHeight = Math.min(
architecture.getGroundHeight(mrCroc.getPositionFootLeft()),
architecture.getGroundHeight(mrCroc.getPositionFootRight())
);
/* Handle falling */
mrCroc.position.y += mrCroc.fallSpeed;
groundHeight = Math.min(
architecture.getGroundHeight(mrCroc.getPositionFootLeft()),
architecture.getGroundHeight(mrCroc.getPositionFootRight())
);
mrCroc.fallSpeed += GRAVITY * delta;
if (mrCroc.position.y >= lastGroundHeight) {
mrCroc.position.y = lastGroundHeight;
/* Handle ground collision */
if (mrCroc.position.y > groundHeight && mrCroc.fallSpeed > 0) {
mrCroc.position.y = groundHeight;
mrCroc.fallSpeed = 0;
}
if (mrCroc.position.y - mrCroc.getHeight() <= lastCeilingHeight) {
/* Handle ceiling collision */
if (mrCroc.position.y - mrCroc.getHeight() <= ceilingHeight) {
mrCroc.fallSpeed = 0;
mrCroc.position.y = lastCeilingHeight + mrCroc.getHeight() + 1;
mrCroc.position.y = ceilingHeight + mrCroc.getHeight() + 1;
}
/* Handle jumping */
if (!mrCroc.isJumping && mrCroc.fallSpeed === 0 && mrCroc.position.y === groundHeight && KeyJump.isPressed()) {
mrCroc.jump();
} else if (!KeyJump.isPressed()) {
mrCroc.isJumping = false;
}
let lastWallRight = Math.min(
architecture.getWallRight(mrCroc.getPositionHeadRight()),
architecture.getWallRight(mrCroc.getPositionFootRight())
);
/* Movement left and right */
if (KeyLeft.isPressed()) {
let lastWallLeft = Math.max(
architecture.getWallLeft(mrCroc.getPositionHeadLeft()),
architecture.getWallLeft(mrCroc.getPositionFootLeft())
);
if (KeyLeft.isPressed()) {
mrCroc.moveLeft(timestamp, delta);
let wallLeft = Math.max(
architecture.getWallLeft(mrCroc.getPositionHeadLeft()),
architecture.getWallLeft(mrCroc.getPositionFootLeft())
);
if (wallLeft < lastWallLeft) {
if (mrCroc.position.x <= lastWallLeft + mrCroc.getWidth() * 0.5) {
mrCroc.position.x = lastWallLeft + mrCroc.getWidth() * 0.5 + 1;
}
} else if (KeyRight.isPressed()) {
mrCroc.moveRight(timestamp, delta);
let wallRight = Math.min(
let lastWallRight = Math.min(
architecture.getWallRight(mrCroc.getPositionHeadRight()),
architecture.getWallRight(mrCroc.getPositionFootRight())
);
if (wallRight > lastWallRight) {
mrCroc.moveRight(timestamp, delta);
if (mrCroc.position.x >= lastWallRight - mrCroc.getWidth() * 0.5) {
mrCroc.position.x = lastWallRight - mrCroc.getWidth() * 0.5 - 1;
}
}
/* Drawing */
if (timestamp - lastRendered >= FRAME_DURATION) {
context.clearRect(0, 0, window.innerWidth, window.innerHeight);
architecture.draw(context);
mrCroc.draw(context);
/*
context.fillRect(0, lastCeilingHeight, window.innerWidth, 1);
context.fillRect(0, lastGroundHeight, window.innerWidth, 1);
@ -147,8 +136,6 @@ const LEVEL = levelJson.getContent();
let lastRendered = undefined;
let lastTimestamp = undefined;
let groundHeight = undefined;
let lastGroundHeight = undefined;
let context;
let mrCroc, architecture;