From 90650fefdda53759b7be501f5751bb0b648f1918 Mon Sep 17 00:00:00 2001 From: "Kacper Kostka (aider)" Date: Sat, 5 Apr 2025 17:26:25 +0200 Subject: [PATCH] fix: Improve rabbit collision, rotation, and jumping mechanics --- js/entities/entity.js | 23 +++++++++++++++++------ js/entities/rabbit.js | 15 +++++++++++---- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/js/entities/entity.js b/js/entities/entity.js index 053153f..ec5f9fc 100644 --- a/js/entities/entity.js +++ b/js/entities/entity.js @@ -76,12 +76,22 @@ class Entity { const halfWidth = this.width / 2; const halfHeight = this.height / 2; - // Check bottom points for ground collision - const bottomLeft = { x: newX - halfWidth * 0.8, y: newY + halfHeight }; - const bottomRight = { x: newX + halfWidth * 0.8, y: newY + halfHeight }; + // Check bottom points for ground collision - check multiple points along the bottom + const numBottomPoints = 5; + let groundCollision = false; - if (this.isPixelSolid(bottomLeft.x, bottomLeft.y) || - this.isPixelSolid(bottomRight.x, bottomRight.y)) { + for (let i = 0; i < numBottomPoints; i++) { + const ratio = i / (numBottomPoints - 1); + const bottomX = newX - halfWidth + (2 * halfWidth * ratio); + const bottomY = newY + halfHeight; + + if (this.isPixelSolid(bottomX, bottomY)) { + groundCollision = true; + break; + } + } + + if (groundCollision) { result.collision = true; result.vertical = true; result.ground = true; @@ -112,7 +122,8 @@ class Entity { } isPixelSolid(x, y) { - const pixel = getPixel(Math.floor(x), Math.floor(y)); + // Use ceiling for y coordinate to better detect ground below + const pixel = getPixel(Math.floor(x), Math.ceil(y)); return pixel !== EMPTY && pixel !== WATER && pixel !== FIRE && diff --git a/js/entities/rabbit.js b/js/entities/rabbit.js index 80a512b..98dcd77 100644 --- a/js/entities/rabbit.js +++ b/js/entities/rabbit.js @@ -58,7 +58,7 @@ class Rabbit extends Entity { while (this.isPixelSolid(this.x, newY)) { newY--; } - newY = Math.floor(newY) + 0.99; // Position just above ground + newY = Math.floor(newY) + 0.5; // Position just above ground (reduced from 0.99) } else { // Hit ceiling this.vy = 0; @@ -91,9 +91,16 @@ class Rabbit extends Entity { this.vx = 0; } - // Update sprite direction + // Update sprite direction but only flip the sprite, not rotate it this.flipped = this.direction < 0; + // Only apply rotation when jumping + if (this.isJumping) { + this.rotation = this.direction < 0 ? -0.2 : 0.2; + } else { + this.rotation = 0; + } + return true; } @@ -102,10 +109,10 @@ class Rabbit extends Entity { if (!this.isJumping && this.actionDuration <= 0) { const decision = Math.random(); - if (decision < 0.2) { + if (decision < 0.5) { // Increased from 0.2 to 0.5 for more frequent jumping // Jump this.jump(); - } else if (decision < 0.6) { + } else if (decision < 0.8) { // Adjusted range // Move this.move(); } else {