From 3e7c8c4b6182f9ba8756cbf1ceceb9dd00a55423 Mon Sep 17 00:00:00 2001 From: elioat Date: Sun, 29 Dec 2024 17:31:41 -0500 Subject: * --- html/isometric-bounce/js/game.js | 77 ++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 27 deletions(-) (limited to 'html/isometric-bounce/js') diff --git a/html/isometric-bounce/js/game.js b/html/isometric-bounce/js/game.js index e2b0021..853763e 100644 --- a/html/isometric-bounce/js/game.js +++ b/html/isometric-bounce/js/game.js @@ -22,7 +22,9 @@ function createGame() { isJumping: false, startX: 0, startY: 0 - } + }, + isHopping: false, + hopProgress: 0 }; state.ctx = state.canvas.getContext('2d'); @@ -123,30 +125,42 @@ function createGame() { const jumpDuration = 0.1; const maxJumpHeight = state.tileHeight; - if (!state.player.currentWaypoint && state.player.path.length > 0) { - state.player.currentWaypoint = state.player.path.shift(); - state.player.isJumping = true; - state.player.jumpProgress = 0; - state.player.startX = state.player.x; - state.player.startY = state.player.y; - } - - if (state.player.currentWaypoint && state.player.isJumping) { - state.player.jumpProgress += jumpDuration; - state.player.jumpProgress = Math.min(state.player.jumpProgress, 1); - - state.player.jumpHeight = Math.sin(state.player.jumpProgress * Math.PI) * maxJumpHeight; + if (state.isHopping) { + state.hopProgress += jumpDuration; + state.hopProgress = Math.min(state.hopProgress, 1); - state.player.x = state.player.startX + (state.player.currentWaypoint.x - state.player.startX) * state.player.jumpProgress; - state.player.y = state.player.startY + (state.player.currentWaypoint.y - state.player.startY) * state.player.jumpProgress; - - if (state.player.jumpProgress >= 1) { - state.player.isJumping = false; + state.player.jumpHeight = Math.sin(state.hopProgress * Math.PI) * maxJumpHeight; + + if (state.hopProgress >= 1) { + state.isHopping = false; state.player.jumpHeight = 0; - state.player.x = state.player.currentWaypoint.x; - state.player.y = state.player.currentWaypoint.y; - dustyParticles(state.player.x, state.player.y); - state.player.currentWaypoint = null; + } + } else { + if (!state.player.currentWaypoint && state.player.path.length > 0) { + state.player.currentWaypoint = state.player.path.shift(); + state.player.isJumping = true; + state.player.jumpProgress = 0; + state.player.startX = state.player.x; + state.player.startY = state.player.y; + } + + if (state.player.currentWaypoint && state.player.isJumping) { + state.player.jumpProgress += jumpDuration; + state.player.jumpProgress = Math.min(state.player.jumpProgress, 1); + + state.player.jumpHeight = Math.sin(state.player.jumpProgress * Math.PI) * maxJumpHeight; + + state.player.x = state.player.startX + (state.player.currentWaypoint.x - state.player.startX) * state.player.jumpProgress; + state.player.y = state.player.startY + (state.player.currentWaypoint.y - state.player.startY) * state.player.jumpProgress; + + if (state.player.jumpProgress >= 1) { + state.player.isJumping = false; + state.player.jumpHeight = 0; + state.player.x = state.player.currentWaypoint.x; + state.player.y = state.player.currentWaypoint.y; + dustyParticles(state.player.x, state.player.y); + state.player.currentWaypoint = null; + } } } } @@ -200,7 +214,7 @@ function createGame() { function drawPlayer() { const iso = toIsometric(state.player.x, state.player.y); - const jumpOffset = state.player.jumpHeight || 0; + const jumpOffset = state.player.jumpHeight || state.player.jumpHeight; let squashStretch = 1; if (state.player.isJumping) { @@ -279,9 +293,18 @@ function createGame() { const gridPos = fromIsometric(clickX, clickY); - if (gridPos.x >= 0 && gridPos.x < state.gridSize && - gridPos.y >= 0 && gridPos.y < state.gridSize) { - + const iso = toIsometric(state.player.x, state.player.y); + const playerRadius = state.player.size; + const distanceToPlayer = Math.sqrt( + Math.pow(clickX - (iso.x + state.offsetX), 2) + + Math.pow(clickY - (iso.y + state.offsetY), 2) + ); + + if (distanceToPlayer < playerRadius) { + state.isHopping = true; + state.hopProgress = 0; + } else if (gridPos.x >= 0 && gridPos.x < state.gridSize && + gridPos.y >= 0 && gridPos.y < state.gridSize) { state.player.targetX = Math.round(gridPos.x); state.player.targetY = Math.round(gridPos.y); -- cgit 1.4.1-2-gfad0