All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m4s
157 lines
4.9 KiB
JavaScript
157 lines
4.9 KiB
JavaScript
import Matter from "matter-js";
|
|
|
|
const { Bodies, Body, Composite, Engine, Sleeping } = Matter;
|
|
export const BUBBLE_RADIUS = 9.5;
|
|
export const DRAW_BUBBLE_RADIUS = 8.4;
|
|
export const VESSEL_RADIUS = 142;
|
|
const packedPositions = [];
|
|
for (let row = -9; row <= 9; row++)
|
|
for (let col = -9; col <= 9; col++) {
|
|
const x = 160 + (col + (row % 2) * 0.5) * 19.1;
|
|
const y = 160 + (row * 19.1 * Math.sqrt(3)) / 2;
|
|
if (Math.hypot(x - 160, y - 160) <= VESSEL_RADIUS - BUBBLE_RADIUS)
|
|
packedPositions.push({ x, y });
|
|
}
|
|
packedPositions.sort(
|
|
(a, b) => b.y - a.y || Math.abs(a.x - 160) - Math.abs(b.x - 160),
|
|
);
|
|
export const MAX_BUBBLES = packedPositions.length;
|
|
export function bubbleCount(value, goal, enabled) {
|
|
const ratio = Math.max(0, Math.min(value / (enabled ? goal : 2500), 1));
|
|
return ratio > 0 ? Math.max(1, Math.round(ratio * MAX_BUBBLES)) : 0;
|
|
}
|
|
|
|
// Project Earth's gravity onto the screen, then account for portrait/landscape.
|
|
// Alpha is a compass heading and does not affect gravity.
|
|
export function screenGravity(beta, gamma, screenAngle = 0) {
|
|
const b = (beta * Math.PI) / 180;
|
|
const g = (gamma * Math.PI) / 180;
|
|
const angle = (screenAngle * Math.PI) / 180;
|
|
const x = Math.sin(g) * Math.cos(b);
|
|
const y = Math.sin(b);
|
|
if (Math.hypot(x, y) < 0.12) return { x: 0, y: 0.18 };
|
|
return {
|
|
x: x * Math.cos(angle) + y * Math.sin(angle),
|
|
y: -x * Math.sin(angle) + y * Math.cos(angle),
|
|
};
|
|
}
|
|
|
|
export function createBubbleWorld(initialCount = 0, random = Math.random) {
|
|
const engine = Engine.create({
|
|
enableSleeping: true,
|
|
positionIterations: 24,
|
|
velocityIterations: 8,
|
|
});
|
|
engine.gravity.scale = 0.00035;
|
|
const balls = [];
|
|
let target = initialCount;
|
|
let spawnTime = 0;
|
|
const walls = Array.from({ length: 72 }, (_, i) => {
|
|
const angle = (i / 72) * Math.PI * 2;
|
|
const radius = VESSEL_RADIUS + 9;
|
|
return Bodies.rectangle(
|
|
160 + Math.cos(angle) * radius,
|
|
160 + Math.sin(angle) * radius,
|
|
(2 * Math.PI * radius) / 72 + 2,
|
|
18,
|
|
{
|
|
isStatic: true,
|
|
angle: angle + Math.PI / 2,
|
|
friction: 0.05,
|
|
restitution: 0.2,
|
|
},
|
|
);
|
|
});
|
|
Composite.add(engine.world, walls);
|
|
function add(x, y) {
|
|
const ball = Bodies.polygon(x, y, 24, BUBBLE_RADIUS, {
|
|
restitution: 0.34,
|
|
friction: 0.035,
|
|
frictionStatic: 0.15,
|
|
frictionAir: 0.018,
|
|
sleepThreshold: 45,
|
|
});
|
|
ball.circleRadius = BUBBLE_RADIUS;
|
|
balls.push(ball);
|
|
Composite.add(engine.world, ball);
|
|
return ball;
|
|
}
|
|
function wake() {
|
|
for (const ball of balls) Sleeping.set(ball, false);
|
|
}
|
|
function seed(count) {
|
|
for (const ball of balls) Composite.remove(engine.world, ball);
|
|
balls.length = 0;
|
|
target = Math.min(MAX_BUBBLES, Math.max(0, count));
|
|
for (let i = 0; i < target; i++)
|
|
add(packedPositions[i].x, packedPositions[i].y);
|
|
}
|
|
seed(initialCount);
|
|
function setTarget(count) {
|
|
target = Math.min(MAX_BUBBLES, Math.max(0, count));
|
|
while (balls.length > target) Composite.remove(engine.world, balls.pop());
|
|
wake();
|
|
}
|
|
function gravity(x, y) {
|
|
if (Math.hypot(engine.gravity.x - x, engine.gravity.y - y) < 0.025)
|
|
return false;
|
|
engine.gravity.x = x;
|
|
engine.gravity.y = y;
|
|
wake();
|
|
return true;
|
|
}
|
|
function step(delta = 1000 / 60) {
|
|
spawnTime += delta;
|
|
if (balls.length < target && spawnTime >= 45) {
|
|
spawnTime = 0;
|
|
// Each new bubble enters at the upper inside edge, then falls naturally.
|
|
const x = 160 + (random() - 0.5) * 55;
|
|
const y =
|
|
160 -
|
|
Math.sqrt((VESSEL_RADIUS - BUBBLE_RADIUS - 2) ** 2 - (x - 160) ** 2);
|
|
const ball = add(x, y);
|
|
Body.setVelocity(ball, { x: (random() - 0.5) * 1.5, y: 0.6 });
|
|
wake();
|
|
}
|
|
Engine.update(engine, Math.min(delta, 1000 / 60));
|
|
// Prevent rare tunnelling during fast rotation or a suspended-tab resume.
|
|
for (const ball of balls) {
|
|
const dx = ball.position.x - 160;
|
|
const dy = ball.position.y - 160;
|
|
const distance = Math.hypot(dx, dy);
|
|
const limit = VESSEL_RADIUS - DRAW_BUBBLE_RADIUS;
|
|
if (distance > VESSEL_RADIUS + BUBBLE_RADIUS) {
|
|
const nx = dx / distance;
|
|
const ny = dy / distance;
|
|
Body.setPosition(ball, { x: 160 + nx * limit, y: 160 + ny * limit });
|
|
const outward = ball.velocity.x * nx + ball.velocity.y * ny;
|
|
if (outward > 0)
|
|
Body.setVelocity(ball, {
|
|
x: ball.velocity.x - outward * 1.3 * nx,
|
|
y: ball.velocity.y - outward * 1.3 * ny,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
function nudge() {
|
|
for (const ball of balls) {
|
|
Sleeping.set(ball, false);
|
|
Body.setVelocity(ball, { x: (random() - 0.5) * 7, y: -2 - random() * 4 });
|
|
}
|
|
}
|
|
return {
|
|
engine,
|
|
balls,
|
|
step,
|
|
seed,
|
|
setTarget,
|
|
gravity,
|
|
nudge,
|
|
awake: () =>
|
|
balls.length < target || balls.some((ball) => !ball.isSleeping),
|
|
dispose: () => {
|
|
Composite.clear(engine.world, false);
|
|
Engine.clear(engine);
|
|
},
|
|
};
|
|
}
|