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 export default class MrCroc extends Movable
{ {
constructor() { constructor() {
let SCALE = 3; let SCALE = 2;
super(new RetroAnimation('graphics/mr-croc-walk-right.png', 2, SCALE), 7); super(new RetroAnimation('graphics/mr-croc-walk-right.png', 2, SCALE), 7);
this.isJumping = false; this.isJumping = false;
this.addAnimation('WALK_RIGHT', new RetroAnimation('graphics/mr-croc-walk-right.png', 2, SCALE, 10)); 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 delta = (timestamp - lastTimestamp) / (10 / GAME_SPEED);
let lastCeilingHeight = Math.max( let ceilingHeight = Math.max(
architecture.getCeilingHeight(mrCroc.getPositionHeadLeft()), architecture.getCeilingHeight(mrCroc.getPositionHeadLeft()),
architecture.getCeilingHeight(mrCroc.getPositionHeadRight()), architecture.getCeilingHeight(mrCroc.getPositionHeadRight()),
); );
lastGroundHeight = Math.min( let groundHeight = Math.min(
architecture.getGroundHeight(mrCroc.getPositionFootLeft()), architecture.getGroundHeight(mrCroc.getPositionFootLeft()),
architecture.getGroundHeight(mrCroc.getPositionFootRight()) architecture.getGroundHeight(mrCroc.getPositionFootRight())
); );
/* Handle falling */
mrCroc.position.y += mrCroc.fallSpeed; mrCroc.position.y += mrCroc.fallSpeed;
groundHeight = Math.min(
architecture.getGroundHeight(mrCroc.getPositionFootLeft()),
architecture.getGroundHeight(mrCroc.getPositionFootRight())
);
mrCroc.fallSpeed += GRAVITY * delta; mrCroc.fallSpeed += GRAVITY * delta;
if (mrCroc.position.y >= lastGroundHeight) { /* Handle ground collision */
mrCroc.position.y = lastGroundHeight; if (mrCroc.position.y > groundHeight && mrCroc.fallSpeed > 0) {
mrCroc.position.y = groundHeight;
mrCroc.fallSpeed = 0; mrCroc.fallSpeed = 0;
} }
if (mrCroc.position.y - mrCroc.getHeight() <= lastCeilingHeight) { /* Handle ceiling collision */
if (mrCroc.position.y - mrCroc.getHeight() <= ceilingHeight) {
mrCroc.fallSpeed = 0; 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()) { if (!mrCroc.isJumping && mrCroc.fallSpeed === 0 && mrCroc.position.y === groundHeight && KeyJump.isPressed()) {
mrCroc.jump(); mrCroc.jump();
} else if (!KeyJump.isPressed()) { } else if (!KeyJump.isPressed()) {
mrCroc.isJumping = false; mrCroc.isJumping = false;
} }
let lastWallRight = Math.min( /* Movement left and right */
architecture.getWallRight(mrCroc.getPositionHeadRight()), if (KeyLeft.isPressed()) {
architecture.getWallRight(mrCroc.getPositionFootRight())
);
let lastWallLeft = Math.max( let lastWallLeft = Math.max(
architecture.getWallLeft(mrCroc.getPositionHeadLeft()), architecture.getWallLeft(mrCroc.getPositionHeadLeft()),
architecture.getWallLeft(mrCroc.getPositionFootLeft()) architecture.getWallLeft(mrCroc.getPositionFootLeft())
); );
if (KeyLeft.isPressed()) {
mrCroc.moveLeft(timestamp, delta); mrCroc.moveLeft(timestamp, delta);
let wallLeft = Math.max( if (mrCroc.position.x <= lastWallLeft + mrCroc.getWidth() * 0.5) {
architecture.getWallLeft(mrCroc.getPositionHeadLeft()),
architecture.getWallLeft(mrCroc.getPositionFootLeft())
);
if (wallLeft < lastWallLeft) {
mrCroc.position.x = lastWallLeft + mrCroc.getWidth() * 0.5 + 1; mrCroc.position.x = lastWallLeft + mrCroc.getWidth() * 0.5 + 1;
} }
} else if (KeyRight.isPressed()) { } else if (KeyRight.isPressed()) {
mrCroc.moveRight(timestamp, delta); let lastWallRight = Math.min(
let wallRight = Math.min(
architecture.getWallRight(mrCroc.getPositionHeadRight()), architecture.getWallRight(mrCroc.getPositionHeadRight()),
architecture.getWallRight(mrCroc.getPositionFootRight()) 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; mrCroc.position.x = lastWallRight - mrCroc.getWidth() * 0.5 - 1;
} }
} }
/* Drawing */
if (timestamp - lastRendered >= FRAME_DURATION) { if (timestamp - lastRendered >= FRAME_DURATION) {
context.clearRect(0, 0, window.innerWidth, window.innerHeight); context.clearRect(0, 0, window.innerWidth, window.innerHeight);
architecture.draw(context); architecture.draw(context);
mrCroc.draw(context); mrCroc.draw(context);
/* /*
context.fillRect(0, lastCeilingHeight, window.innerWidth, 1); context.fillRect(0, lastCeilingHeight, window.innerWidth, 1);
context.fillRect(0, lastGroundHeight, window.innerWidth, 1); context.fillRect(0, lastGroundHeight, window.innerWidth, 1);
@ -147,8 +136,6 @@ const LEVEL = levelJson.getContent();
let lastRendered = undefined; let lastRendered = undefined;
let lastTimestamp = undefined; let lastTimestamp = undefined;
let groundHeight = undefined;
let lastGroundHeight = undefined;
let context; let context;
let mrCroc, architecture; let mrCroc, architecture;