Files
bworld/mods/create/scripts/client.ts
T
2026-10-15 11:45:12 -03:00

72 lines
3.1 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// 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
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";
// 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
const HANDLE = "create:block_hand_crank_handle";
const HANDLE_TURNS: Record<Face, ModelRef> = {
north: HANDLE,
east: { model: HANDLE, y: 90 },
south: { model: HANDLE, y: 180 },
west: { model: HANDLE, y: 270 },
top: { model: HANDLE, x: 270 },
bottom: { model: HANDLE, x: 90 },
};
export function setup(ctx: ClientContext) {
const shape_of = (block: VisualBlock): KineticShape => {
const params = ctx.blocks.components(block.id)["create:kinetic"] as KineticParams;
return kinetic_shape(params, (name) => block.states[name]);
};
const shaft = (block: VisualBlock, instances: Instances) => turning(block, instances, shape_of(block)).visual;
ctx.render.register_visual("create:shaft", shaft, { replaces_model: true });
// 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) => {
const shape = shape_of(block);
const { visual, speed } = turning(block, instances, shape);
const handle = instances.rotating(HANDLE_TURNS[shape.facing!]);
// in degrees, and degrees a tick
let angle = 0;
let velocity = 0;
let last: number | undefined;
return {
...visual,
on_frame(time) {
const ticks = last === undefined ? 0 : time - last;
last = time;
// a quarter of the way to the shaft's speed each tick
velocity += (speed() * 0.3 - velocity) * Math.min(1, ticks / 4);
angle = (angle + velocity * ticks) % 360;
handle.set_rotation(shape.axis, 0, angle);
},
};
}, { replaces_model: true });
}
// the block's own model turning around its axis at its speed. offset so cogs mesh with the ones beside them, like
// KineticBlockEntityVisual.rotationOffset: turned by half a tooth on every other block
function turning(block: VisualBlock, instances: Instances, shape: KineticShape) {
const speed = () => block.get_client_data<number>("speed") ?? 0;
const instance = instances.rotating();
const offset = should_offset(shape.axis, block) ? 22.5 : 0;
const turn = () => instance.set_rotation(shape.axis, speed(), offset);
turn();
const visual: Visual = { update: turn };
return { visual, speed };
}
// whether the coordinates across the axis add up to an even number, like KineticBlockEntityVisual.shouldOffset
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;
}