Files
p5_bounce/src/lib/platforms.js
2026-06-30 22:28:37 +03:00

49 lines
1.3 KiB
JavaScript

import { WORLD, VIEWPORT } from "./config.js";
export function createPlatforms() {
const result = [];
const count = 16;
const spacing = 145;
for (let index = 0; index < count; index += 1) {
const width = index === 0 ? 220 : 150 + (index % 4) * 34;
const x = index === 0 ? (WORLD.width - width) / 2 : 40 + ((index * 173) % (WORLD.width - width - 80));
const y = WORLD.height - 70 - index * spacing;
result.push({
id: index + 1,
x,
y,
width,
height: 22,
});
}
return result;
}
export function getVisiblePlatforms(platforms, camera) {
return platforms
.filter((platform) => isPlatformVisible(platform, camera))
.map((platform) => ({
...platform,
local: worldToViewport(platform, camera),
}));
}
export function isPlatformVisible(platform, camera) {
return (
platform.x + platform.width >= camera.x &&
platform.x <= camera.x + VIEWPORT.width &&
platform.y + platform.height >= camera.y &&
platform.y <= camera.y + VIEWPORT.height
);
}
export function worldToViewport(point, camera) {
return {
x: point.x - camera.x,
y: point.y - camera.y,
};
}