This commit is contained in:
2026-10-28 22:26:42 -03:00
parent b8cccb86ff
commit 5d64b801a3
14 changed files with 480 additions and 12 deletions
+94 -2
View File
@@ -3,8 +3,18 @@
// how create's moving blocks are drawn: kinetic blocks turn by the speed the server puts in their "speed" client data,
// as rotating instances, so a block turning steadily costs nothing per frame. they're still while their network is
// overstressed
import type { ClientContext, Instances, ModelRef, Visual, VisualBlock } from "bworld/client";
// overstressed. and what's in a basin
import { mat4 } from "gl-matrix";
import type {
ClientContext,
Instance,
Instances,
ItemStack,
ModelRef,
TransformedInstance,
Visual,
VisualBlock,
} from "bworld/client";
import type { Face } from "bworld/shared";
import {
actual_speed,
@@ -18,6 +28,7 @@ import {
OPPOSITE,
} from "./kinetics/connections.ts";
import { head_offset, type PressShown } from "./processing/press.ts";
import { BASIN_TANK_CAPACITY, BASIN_TANKS, type BasinTank } from "./processing/basin.ts";
// a model reaching north turned to reach each way
const facing_turns = (model: string): Record<Face, ModelRef> => ({
@@ -167,6 +178,8 @@ export function setup(ctx: ClientContext) {
};
});
ctx.render.register_visual("create:basin", basin_visual);
// the half shaft turns with the speed like any kinetic block, and the handle catches up with it and slows down
// with it, like HandCrankVisual's independent angle
ctx.render.register_visual("create:hand_crank", (block, instances) => {
@@ -214,3 +227,82 @@ function should_offset(axis: Axis, { x, y, z }: VisualBlock) {
const across = (axis === "x" ? 0 : x) + (axis === "y" ? 0 : y) + (axis === "z" ? 0 : z);
return across % 2 === 0;
}
// a basin's items are a copy more for every this many
const BASIN_ITEMS_A_COPY = 8;
// what's in a basin, by its "tanks" and "items" client data (see register_basin in server.ts), like BasinRenderer. its
// fluids fill the 12 pixels inside its walls side by side, as wide as their share of it, and as high as all of them
// make it, filling faster at first. its items lie in a ring over the fluid, tipped over and turned toward the middle,
// one in the middle when it's alone, and bob up and down while there's fluid
function basin_visual(block: VisualBlock, instances: Instances): Visual {
let shown: Instance[] = [];
// each copy of an item, placed in the basin but for how high it bobs, and where in its bobbing it is
let floating: { instance: TransformedInstance; placed: mat4; phase: number }[] = [];
const update = () => {
for (const instance of shown) instance.delete();
shown = [];
floating = [];
const tanks = block.get_client_data<BasinTank[]>("tanks") ?? [];
const total = tanks.reduce((sum, tank) => sum + tank.amount, 0);
const full = 1 - (1 - Math.min(1, total / (BASIN_TANKS * BASIN_TANK_CAPACITY))) ** 2;
const top = total > 0 ? 2 + 12 * full : 0;
let x = 2;
for (const tank of tanks) {
const width = 12 * tank.amount / total;
const fluid = instances.fluid(tank.fluid);
fluid.set_box([x, 2, 2], [x + width, top, 14]);
shown.push(fluid);
x += width;
}
const items = block.get_client_data<ItemStack[]>("items") ?? [];
const level = Math.min(0.6, Math.max(0.125, top / 16 - 0.3));
const radius = items.length === 1 ? 0 : 0.125;
const random = seeded(block.x * 961 + block.y * 31 + block.z);
items.forEach((stack, i) => {
const angle = 360 / items.length * (items.length - i) * Math.PI / 180;
for (let copy = 0; copy <= Math.floor(stack.count / BASIN_ITEMS_A_COPY); copy++) {
const placed = mat4.fromTranslation(mat4.create(), [
radius * Math.cos(angle),
level,
-radius * Math.sin(angle),
]);
mat4.rotateY(placed, placed, angle + 35 * Math.PI / 180);
mat4.rotateX(placed, placed, 65 * Math.PI / 180);
const jitter = () => (random() - 0.5) / 8;
mat4.translate(placed, placed, [jitter() - 0.5, jitter() - 0.5, jitter() - 0.5]);
const instance = instances.item(stack);
shown.push(instance);
floating.push({ instance, placed, phase: angle * 180 / Math.PI });
place(instance, placed, 0);
}
});
if (top === 0) floating = [];
};
update();
return {
update,
on_frame(time) {
for (const { instance, placed, phase } of floating) {
place(instance, placed, (Math.sin(time / 12 + phase) + 1.5) / 32);
}
},
};
}
// an item copy in a basin, bob higher than where it was placed
function place(instance: TransformedInstance, placed: mat4, bob: number) {
const matrix = mat4.fromTranslation(mat4.create(), [0.5, 0.2 + bob, 0.5]);
instance.set_transform(mat4.multiply(matrix, matrix, placed));
}
// the same numbers from 0 to 1 every time for the same seed, so a basin's items lie the same way however often they're
// drawn again
function seeded(seed: number) {
let state = seed >>> 0;
return () => {
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
return state / 2 ** 32;
};
}