fix: Improve rabbit collision, rotation, and jumping mechanics

This commit is contained in:
Kacper Kostka (aider) 2025-04-05 17:26:25 +02:00
parent 61ee259f6b
commit 90650fefdd
2 changed files with 28 additions and 10 deletions

View File

@ -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 &&

View File

@ -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 {