You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.6 KiB
44 lines
1.6 KiB
export class PhysicsSystems { |
|
resolveCollisions(player, platforms, groundY) { |
|
const totalHeight = 60; |
|
const halfFootW = 12; |
|
const EPS = 0.5; |
|
|
|
if (player._prevY === undefined) { |
|
player._prevY = player.y; |
|
} |
|
|
|
let landedOnPlatform = false; |
|
|
|
// one-way platforms (land only when falling and crossing from above) |
|
if (player.vy >= 0) { |
|
for (const pf of platforms) { |
|
const feetLeft = player.x - halfFootW; |
|
const feetRight = player.x + halfFootW; |
|
const pfLeft = pf.x, pfRight = pf.x + pf.w; |
|
|
|
const horizOverlap = feetRight > pfLeft && feetLeft < pfRight; |
|
const wasAbove = (player._prevY + totalHeight) <= pf.y + EPS; |
|
const nowBelowTop = (player.y + totalHeight) >= pf.y - EPS; |
|
const dropping = player.crouching === true; // hold 's' to drop through |
|
|
|
if (horizOverlap && wasAbove && nowBelowTop && !dropping) { |
|
player.y = pf.y - totalHeight; |
|
player.vy = 0; |
|
landedOnPlatform = true; |
|
break; |
|
} |
|
} |
|
} |
|
// --- ground after platforms --- |
|
if (!landedOnPlatform && player.y + totalHeight >= groundY) { |
|
player.y = groundY - totalHeight; |
|
player.vy = 0; |
|
player.onGround = true; |
|
} else if (landedOnPlatform) { |
|
player.onGround = true; |
|
} else { |
|
player.onGround = false; |
|
} |
|
} |
|
}
|
|
|