fix: Improve rabbit collision, rotation, and jumping mechanics
This commit is contained in:
parent
61ee259f6b
commit
90650fefdd
@ -76,12 +76,22 @@ class Entity {
|
|||||||
const halfWidth = this.width / 2;
|
const halfWidth = this.width / 2;
|
||||||
const halfHeight = this.height / 2;
|
const halfHeight = this.height / 2;
|
||||||
|
|
||||||
// Check bottom points for ground collision
|
// Check bottom points for ground collision - check multiple points along the bottom
|
||||||
const bottomLeft = { x: newX - halfWidth * 0.8, y: newY + halfHeight };
|
const numBottomPoints = 5;
|
||||||
const bottomRight = { x: newX + halfWidth * 0.8, y: newY + halfHeight };
|
let groundCollision = false;
|
||||||
|
|
||||||
if (this.isPixelSolid(bottomLeft.x, bottomLeft.y) ||
|
for (let i = 0; i < numBottomPoints; i++) {
|
||||||
this.isPixelSolid(bottomRight.x, bottomRight.y)) {
|
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.collision = true;
|
||||||
result.vertical = true;
|
result.vertical = true;
|
||||||
result.ground = true;
|
result.ground = true;
|
||||||
@ -112,7 +122,8 @@ class Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
isPixelSolid(x, y) {
|
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 &&
|
return pixel !== EMPTY &&
|
||||||
pixel !== WATER &&
|
pixel !== WATER &&
|
||||||
pixel !== FIRE &&
|
pixel !== FIRE &&
|
||||||
|
@ -58,7 +58,7 @@ class Rabbit extends Entity {
|
|||||||
while (this.isPixelSolid(this.x, newY)) {
|
while (this.isPixelSolid(this.x, newY)) {
|
||||||
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 {
|
} else {
|
||||||
// Hit ceiling
|
// Hit ceiling
|
||||||
this.vy = 0;
|
this.vy = 0;
|
||||||
@ -91,9 +91,16 @@ class Rabbit extends Entity {
|
|||||||
this.vx = 0;
|
this.vx = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update sprite direction
|
// Update sprite direction but only flip the sprite, not rotate it
|
||||||
this.flipped = this.direction < 0;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,10 +109,10 @@ class Rabbit extends Entity {
|
|||||||
if (!this.isJumping && this.actionDuration <= 0) {
|
if (!this.isJumping && this.actionDuration <= 0) {
|
||||||
const decision = Math.random();
|
const decision = Math.random();
|
||||||
|
|
||||||
if (decision < 0.2) {
|
if (decision < 0.5) { // Increased from 0.2 to 0.5 for more frequent jumping
|
||||||
// Jump
|
// Jump
|
||||||
this.jump();
|
this.jump();
|
||||||
} else if (decision < 0.6) {
|
} else if (decision < 0.8) { // Adjusted range
|
||||||
// Move
|
// Move
|
||||||
this.move();
|
this.move();
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user