Some more kinetic blocks

This commit is contained in:
2026-10-28 22:10:48 -03:00
parent fa0748a9c5
commit b8cccb86ff
35 changed files with 670 additions and 73 deletions
+77 -12
View File
@@ -9,6 +9,8 @@ import type { Face } from "bworld/shared";
import {
actual_speed,
type Axis,
axis_modifier,
face_axis,
FACINGS,
kinetic_shape,
type KineticParams,
@@ -43,17 +45,73 @@ export function setup(ctx: ClientContext) {
};
const plain = (block: VisualBlock, instances: Instances): Visual => ({
update: turning(block, instances, shape_of(block)),
update: turning(block, instances, shape_of(block).axis),
});
ctx.render.register_visual("create:shaft", plain, { replaces_model: true });
ctx.render.register_visual("create:cogwheel", plain, { replaces_model: true });
// the casing is the block's model, and the shaft through it turns, like the encased shafts' and chain drives'
// ShaftRenderer
const encased_shaft = (block: VisualBlock, instances: Instances): Visual => {
const { axis } = shape_of(block);
return { update: turning(block, instances, axis, { model: "create:block_shaft", ...AXIS_TURNS[axis] }) };
};
for (
const id of [
"andesite_encased_shaft",
"brass_encased_shaft",
"encased_chain_drive",
"adjustable_chain_gearshift",
]
) {
ctx.render.register_visual(`create:${id}`, encased_shaft);
}
// a half shaft out of each side with an axle, turning as axis_modifier says from the side its speed comes from,
// which the server puts in its "source" client data, like GearboxVisual and SplitShaftVisual
const halves = (block: VisualBlock, instances: Instances): Visual => {
const shape = shape_of(block);
const turns = shape.shafts.map((face) =>
turning(
block,
instances,
face_axis(face),
half_shaft(face),
0,
() => axis_modifier(shape, block.get_client_data<Face>("source"), face),
)
);
return { update: () => turns.forEach((turn) => turn()) };
};
for (const id of ["gearbox", "clutch", "gearshift"]) ctx.render.register_visual(`create:${id}`, halves);
// the cog turns in its casing, offset like a large cog's when it's one, and a half shaft out of each end with an
// axle, like EncasedCogVisual
const encased_cog = (model: string, offset: number) => (block: VisualBlock, instances: Instances): Visual => {
const { axis, shafts } = shape_of(block);
const turns = [
turning(block, instances, axis, { model, ...AXIS_TURNS[axis] }, offset),
...shafts.map((face) => turning(block, instances, axis, half_shaft(face))),
];
return { update: () => turns.forEach((turn) => turn()) };
};
for (const casing of ["andesite", "brass"]) {
ctx.render.register_visual(
`create:${casing}_encased_cogwheel`,
encased_cog("create:block_cogwheel_shaftless", 0),
);
ctx.render.register_visual(
`create:${casing}_encased_large_cogwheel`,
encased_cog("create:block_large_cogwheel_shaftless", 11.25),
);
}
// the whole wheel turns, paddles and all, like WaterWheelVisual
ctx.render.register_visual("create:water_wheel", plain, { replaces_model: true });
// its casing is its block's model, and the half shaft out of its front turns, like CreativeMotorRenderer
ctx.render.register_visual("create:creative_motor", (block, instances) => {
const shape = shape_of(block);
return { update: turning(block, instances, shape, half_shaft(shape.facing!)) };
return { update: turning(block, instances, shape.axis, half_shaft(shape.facing!)) };
});
// a large cog's teeth are half as far apart as a small one's, so on the blocks where small cogs aren't offset it's
@@ -65,11 +123,11 @@ export function setup(ctx: ClientContext) {
const cog = turning(
block,
instances,
shape,
shape.axis,
{ model: "create:block_large_cogwheel_shaftless", ...turns },
11.25,
);
const shaft = turning(block, instances, shape, { model: "create:block_cogwheel_shaft", ...turns });
const shaft = turning(block, instances, shape.axis, { model: "create:block_cogwheel_shaft", ...turns });
return {
update() {
cog();
@@ -82,7 +140,7 @@ export function setup(ctx: ClientContext) {
// 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 turn = turning(block, instances, shape.axis, { model: "create:block_shaft", ...AXIS_TURNS[shape.axis] });
const head = instances.transformed({
model: "create:block_mechanical_press_head",
y: FACINGS.indexOf(shape.facing!) * 90,
@@ -113,7 +171,7 @@ export function setup(ctx: ClientContext) {
// with it, like HandCrankVisual's independent angle
ctx.render.register_visual("create:hand_crank", (block, instances) => {
const shape = shape_of(block);
const turn = turning(block, instances, shape);
const turn = turning(block, instances, shape.axis);
const handle = instances.rotating(HANDLE_TURNS[shape.facing!]);
// in degrees, and degrees a tick
let angle = 0;
@@ -133,13 +191,20 @@ export function setup(ctx: ClientContext) {
}, { replaces_model: true });
}
// a model turning around the block's axis at its speed, the block's own when not given. returns what turns it again
// when the speed changes. on every other block it's offset by half a small cog's tooth, so cogs mesh with the ones
// beside them, and on the rest by offset, like KineticBlockEntityVisual.rotationOffset
function turning(block: VisualBlock, instances: Instances, shape: KineticShape, model?: ModelRef, offset = 0) {
// a model turning around axis at the block's speed times modifier, the block's own model when not given. returns what
// turns it again when the speed changes. on every other block it's offset by half a small cog's tooth, so cogs mesh
// with the ones beside them, and on the rest by offset, like KineticBlockEntityVisual.rotationOffset
function turning(
block: VisualBlock,
instances: Instances,
axis: Axis,
model?: ModelRef,
offset = 0,
modifier = () => 1,
) {
const instance = instances.rotating(model);
const angle = should_offset(shape.axis, block) ? 22.5 : offset;
const turn = () => instance.set_rotation(shape.axis, actual_speed(block), angle);
const angle = should_offset(axis, block) ? 22.5 : offset;
const turn = () => instance.set_rotation(axis, actual_speed(block) * modifier(), angle);
turn();
return turn;
}