Hand crank and shafts
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// 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 { Face, SharedContext } from "bworld/shared";
|
||||
import {
|
||||
face_axis,
|
||||
FACE_OFFSETS,
|
||||
FACINGS,
|
||||
kinetic_shape,
|
||||
type KineticParams,
|
||||
OPPOSITE,
|
||||
} from "./kinetics/connections.ts";
|
||||
|
||||
const AXES = ["x", "y", "z"];
|
||||
|
||||
export function setup(ctx: SharedContext) {
|
||||
// the side of x y z with an axle pointing at it, like getPreferredFacing. undefined when there's none, or when
|
||||
// axles point at it along two axes, since it can't line up with both
|
||||
const axle_beside = (x: number, y: number, z: number): Face | undefined => {
|
||||
let found: Face | undefined;
|
||||
for (const side of FACINGS) {
|
||||
const [dx, dy, dz] = FACE_OFFSETS[side];
|
||||
const [nx, ny, nz] = [x + dx, y + dy, z + dz];
|
||||
const id = ctx.world.get_block(nx, ny, nz);
|
||||
const params = id && ctx.blocks.exists(id)
|
||||
? ctx.blocks.components(id)["create:kinetic"] as KineticParams | undefined
|
||||
: undefined;
|
||||
if (!params) continue;
|
||||
const shape = kinetic_shape(params, (name) => ctx.world.get_state(nx, ny, nz, name));
|
||||
if (!shape.shafts.includes(OPPOSITE[side])) continue;
|
||||
if (found && face_axis(found) !== face_axis(side)) return undefined;
|
||||
found = side;
|
||||
}
|
||||
return found;
|
||||
};
|
||||
|
||||
// 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 away
|
||||
// from the axle, or toward the player, or away from them while sneaking, like DirectionalKineticBlock
|
||||
ctx.components.register_block<KineticParams>("create:kinetic", {
|
||||
state_for_placement(placement, params) {
|
||||
const side = axle_beside(placement.x, placement.y, placement.z);
|
||||
if (params.facing !== undefined) {
|
||||
const facing = side && !placement.sneaking
|
||||
? OPPOSITE[side]
|
||||
: placement.sneaking
|
||||
? placement.looking
|
||||
: OPPOSITE[placement.looking];
|
||||
return { [params.facing]: FACINGS.indexOf(facing) };
|
||||
}
|
||||
const axis = side && !placement.sneaking
|
||||
? face_axis(side)
|
||||
: side
|
||||
? face_axis(placement.face)
|
||||
: face_axis(placement.looking);
|
||||
return { [params.axis ?? "axis"]: AXES.indexOf(axis) };
|
||||
},
|
||||
});
|
||||
|
||||
// a crank faces away from what it's placed on, unless there's an axle beside it to turn, like HandCrankBlock
|
||||
ctx.components.register_block("create:hand_crank", {
|
||||
state_for_placement(placement) {
|
||||
const side = axle_beside(placement.x, placement.y, placement.z);
|
||||
return { facing: FACINGS.indexOf(side && !placement.sneaking ? OPPOSITE[side] : placement.face) };
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user