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
+65 -10
View File
@@ -2,11 +2,14 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// how create's blocks are placed, on the server and on every client: kinetic blocks line up with the axles they're
// placed against
import type { BlockPlacement, Face, SharedContext } from "bworld/shared";
// placed against, and chain drives join into lines
import type { BlockPlacement, Face, Id, SharedContext } from "bworld/shared";
import { chain_update, type ChainState } from "./kinetics/chains.ts";
import {
AXES,
type Axis,
chain_axis,
CHAIN_PARTS,
face_axis,
FACE_OFFSETS,
FACINGS,
@@ -72,8 +75,27 @@ export function setup(ctx: SharedContext) {
};
// a block with an axis lines up with the axle it's placed against, or sneaking, with the face it's clicked onto.
// otherwise it points the way the player looks, like RotatedPillarKineticBlock. a block with a facing faces as
// facing_for says. cogs are placed their own way, see cog_axis
// otherwise it points the way the player looks, like RotatedPillarKineticBlock
const axis_for = (placement: BlockPlacement): Axis => {
const side = axle_beside(placement.x, placement.y, placement.z);
if (side && !placement.sneaking) return face_axis(side);
return face_axis(side ? placement.face : placement.looking);
};
// a chain drive's line, undefined for a block that isn't one
const chain_of = (shape: KineticShape | undefined): ChainState | undefined =>
shape?.chain && {
axis: shape.axis,
part: shape.chain.part,
along_first: shape.chain.axis === chain_axis(shape.axis, true),
};
const chain_states = (params: KineticParams, chain: ChainState) => ({
[params.chain!.part]: CHAIN_PARTS.indexOf(chain.part),
[params.chain!.along_first]: chain.along_first ? 1 : 0,
});
// a block with an axis is placed as axis_for says, and a chain drive joins the lines beside it. a block with a
// facing faces as facing_for says. cogs are placed their own way, see cog_axis
ctx.components.register_block<KineticParams>("create:kinetic", {
state_for_placement(placement, params) {
if (params.cog) {
@@ -89,13 +111,46 @@ export function setup(ctx: SharedContext) {
if (params.facing !== undefined) {
return { [params.facing]: FACINGS.indexOf(facing_for(placement, params.shaft)) };
}
const side = axle_beside(placement.x, placement.y, placement.z);
const axis = side && !placement.sneaking
const axis = axis_for(placement);
const states = { [params.axis ?? "axis"]: AXES.indexOf(axis) };
if (!params.chain) return states;
const { x, y, z } = placement;
const alone: ChainState = { axis, part: "none", along_first: false };
const chain = FACINGS.reduce(
(chain, side) => chain_update(chain, side, chain_of(beside(x, y, z, side))),
alone,
);
return { ...states, ...chain_states(params, chain) };
},
// a chain drive joins one placed beside it, and ends its line where one goes, like ChainDriveBlock.updateShape
on_neighbor_change(block, params, neighbor) {
if (!params.chain) return;
const own = chain_of(kinetic_at(block.x, block.y, block.z));
const side = FACINGS.find((side) => {
const [dx, dy, dz] = FACE_OFFSETS[side];
return block.x + dx === neighbor.x && block.y + dy === neighbor.y && block.z + dz === neighbor.z;
});
if (!own || !side) return;
const chain = chain_update(own, side, chain_of(kinetic_at(neighbor.x, neighbor.y, neighbor.z)));
if (chain === own) return;
for (const [name, value] of Object.entries(chain_states(params, chain))) {
ctx.world.set_state(block.x, block.y, block.z, name, value);
}
},
});
// a gearbox lies flat with its axles level, and side_item, the vertical gearbox, stands it on its side: across the
// axle level with it, or else across the way the player looks, like GearboxBlock and VerticalGearboxItem
ctx.components.register_block<{ side_item: Id }>("create:gearbox", {
state_for_placement(placement, params) {
if (placement.item !== params.side_item) return { axis: AXES.indexOf("y") };
const side = axle_beside(placement.x, placement.y, placement.z, FACINGS.slice(0, 4));
const across = side
? face_axis(side)
: side
? face_axis(placement.face)
: face_axis(placement.looking);
return { [params.axis ?? "axis"]: AXES.indexOf(axis) };
: placement.facing === "north" || placement.facing === "south"
? "z"
: "x";
return { axis: AXES.indexOf(across === "x" ? "z" : "x") };
},
});