Initial Commit
This commit is contained in:
48
src/lib/platforms.js
Normal file
48
src/lib/platforms.js
Normal file
@@ -0,0 +1,48 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user