about summary refs log tree commit diff stats
path: root/html/isometric-bounce/js
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-12-29 17:31:41 -0500
committerelioat <elioat@tilde.institute>2024-12-29 17:31:41 -0500
commit3e7c8c4b6182f9ba8756cbf1ceceb9dd00a55423 (patch)
treecd2605ade947ec5b2341f58e6d88b7b5c13e3177 /html/isometric-bounce/js
parent4a0b56372d3ab3db7b25c1bf191c0effd06181a4 (diff)
downloadtour-3e7c8c4b6182f9ba8756cbf1ceceb9dd00a55423.tar.gz
*
Diffstat (limited to 'html/isometric-bounce/js')
-rw-r--r--html/isometric-bounce/js/game.js77
1 files changed, 50 insertions, 27 deletions
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);