Files
NubzukiJump/src/game/platforms.js
2026-04-25 19:41:54 +09:00

53 lines
1.3 KiB
JavaScript

const SPACE_BETWEEN_PLATFORMS = 80;
function createPlatform(x, y, elevation, width = 60) {
const platform = new Sprite();
platform.x = x;
platform.y = y;
platform.elevation = elevation;
platform.w = width;
platform.h = 15;
platform.physics = 'static';
platform.bounciness = 0;
return platform;
}
export function updatePlatforms(platforms, elevation, elGain) {
if (elGain <= 0)
return platforms;
let minY = height;
for (let i = platforms.length - 1; i >= 0; i--) {
const plat = platforms[i];
plat.y += elGain;
if (plat.y > height) {
platforms.remove(plat);
continue;
}
if (plat.y < minY) {
minY = plat.y;
}
}
// Add new platforms at the top
let spawnY = minY - SPACE_BETWEEN_PLATFORMS;
while (spawnY >= SPACE_BETWEEN_PLATFORMS) {
const newPlat = createPlatform(random(40, width - 40), spawnY, elevation + minY - spawnY);
platforms.add(newPlat);
spawnY -= SPACE_BETWEEN_PLATFORMS;
}
return platforms;
}
export function initPlatforms() {
let platforms = new Group();
updatePlatforms(platforms, 10, 10);
const basePlatform = createPlatform(width / 2, height - 10, 0, width);
platforms.add(basePlatform);
return platforms;
}