Mechanical press !

This commit is contained in:
2026-10-15 13:07:09 -03:00
parent b43e8fe8b8
commit c7a2392591
15 changed files with 403 additions and 86 deletions
+33 -1
View File
@@ -5,7 +5,8 @@
// as rotating instances, so a block turning steadily costs nothing per frame
import type { ClientContext, Instances, ModelRef, Visual, VisualBlock } from "bworld/client";
import type { Face } from "bworld/shared";
import { type Axis, kinetic_shape, type KineticParams, type KineticShape } from "./kinetics/connections.ts";
import { type Axis, FACINGS, kinetic_shape, type KineticParams, type KineticShape } from "./kinetics/connections.ts";
import { head_offset, type PressShown } from "./processing/press.ts";
// the crank's handle model has its hub on its south side and its grip reaching north, so it's turned to put north
// where the crank faces
@@ -56,6 +57,37 @@ export function setup(ctx: ClientContext) {
};
}, { replaces_model: true });
// its casing is its block's model. the shaft through it turns like any other, and the head comes down and goes back
// up through the cycle from where the server last said it was, like PressVisual
ctx.render.register_visual("create:mechanical_press", (block, instances) => {
const shape = shape_of(block);
const turn = turning(block, instances, shape, { model: "create:block_shaft", ...AXIS_TURNS[shape.axis] });
const head = instances.transformed({
model: "create:block_mechanical_press_head",
y: FACINGS.indexOf(shape.facing!) * 90,
});
let shown = block.get_client_data<PressShown>("press");
// when this client heard of it, in the time on_frame gets
let since: number | undefined;
let offset: number | undefined;
return {
update() {
turn();
const press = block.get_client_data<PressShown>("press");
if (JSON.stringify(press) === JSON.stringify(shown)) return;
shown = press;
since = undefined;
},
on_frame(time) {
since ??= time;
const now = shown ? head_offset(shown.steps + (time - since) * shown.rate) : 0;
if (now === offset) return;
offset = now;
head.set_transform([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, -offset, 0, 1]);
},
};
});
// 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) => {