diff --git a/js/MrCroc.js b/js/MrCroc.js index 492d22a..6b1b8ea 100644 --- a/js/MrCroc.js +++ b/js/MrCroc.js @@ -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)); diff --git a/js/module.js b/js/module.js index 83cc694..5ed278c 100644 --- a/js/module.js +++ b/js/module.js @@ -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()) - ); - - let lastWallLeft = Math.max( - architecture.getWallLeft(mrCroc.getPositionHeadLeft()), - architecture.getWallLeft(mrCroc.getPositionFootLeft()) - ); - + /* Movement left and right */ if (KeyLeft.isPressed()) { - mrCroc.moveLeft(timestamp, delta); - - let wallLeft = Math.max( + let lastWallLeft = Math.max( architecture.getWallLeft(mrCroc.getPositionHeadLeft()), architecture.getWallLeft(mrCroc.getPositionFootLeft()) ); - if (wallLeft < lastWallLeft) { + mrCroc.moveLeft(timestamp, delta); + + 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;