Hand crank and shafts
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// what turns, and what turns what: a kinetic block's axis and the ends of it with an axle, and how fast one block turns
|
||||
// the one next to it, like create's IRotate and RotationPropagator.getRotationSpeedModifier. no ctx, so placement
|
||||
// rules, the server and visuals all read blocks the same way
|
||||
import type { Face } from "bworld/shared";
|
||||
|
||||
export type Axis = "x" | "y" | "z";
|
||||
|
||||
// the create:kinetic component's params, from a block's json
|
||||
export interface KineticParams {
|
||||
// the 2 bit state that says its axis, x, y or z, like a shaft's
|
||||
axis?: string;
|
||||
// or the 3 bit state that says which way it faces, north, east, south, west, up or down, its axis going through
|
||||
// that face, like a crank's
|
||||
facing?: string;
|
||||
// which ends of its axis have an axle that turns the block there: both, or for one with a facing only the end it
|
||||
// faces ("front") or the other ("back"). both when not given
|
||||
shaft?: "both" | "front" | "back";
|
||||
}
|
||||
|
||||
// a 3 bit facing state's values, in the order the importer numbers java's facing
|
||||
export const FACINGS: readonly Face[] = ["north", "east", "south", "west", "top", "bottom"];
|
||||
const AXES: readonly Axis[] = ["x", "y", "z"];
|
||||
|
||||
export const FACE_OFFSETS: Readonly<Record<Face, readonly [number, number, number]>> = {
|
||||
west: [-1, 0, 0],
|
||||
east: [1, 0, 0],
|
||||
bottom: [0, -1, 0],
|
||||
top: [0, 1, 0],
|
||||
north: [0, 0, -1],
|
||||
south: [0, 0, 1],
|
||||
};
|
||||
|
||||
export const OPPOSITE: Readonly<Record<Face, Face>> = {
|
||||
west: "east",
|
||||
east: "west",
|
||||
bottom: "top",
|
||||
top: "bottom",
|
||||
north: "south",
|
||||
south: "north",
|
||||
};
|
||||
|
||||
const FACE_AXIS: Readonly<Record<Face, Axis>> = {
|
||||
west: "x",
|
||||
east: "x",
|
||||
bottom: "y",
|
||||
top: "y",
|
||||
north: "z",
|
||||
south: "z",
|
||||
};
|
||||
|
||||
// the faces at the positive and negative ends of an axis
|
||||
const AXIS_FACES: Readonly<Record<Axis, readonly [Face, Face]>> = {
|
||||
x: ["east", "west"],
|
||||
y: ["top", "bottom"],
|
||||
z: ["south", "north"],
|
||||
};
|
||||
|
||||
// how a block turns
|
||||
export interface KineticShape {
|
||||
axis: Axis;
|
||||
// the faces with an axle
|
||||
shafts: readonly Face[];
|
||||
// the way it faces, for a block with a facing
|
||||
facing?: Face;
|
||||
}
|
||||
|
||||
// a kinetic block where it is
|
||||
export interface Kinetic extends KineticShape {
|
||||
readonly x: number;
|
||||
readonly y: number;
|
||||
readonly z: number;
|
||||
}
|
||||
|
||||
// a block's shape from its params and its states, read by name
|
||||
export function kinetic_shape(params: KineticParams, state: (name: string) => number | undefined): KineticShape {
|
||||
if (params.facing !== undefined) {
|
||||
const facing = FACINGS[state(params.facing) ?? 0];
|
||||
const shaft = params.shaft ?? "both";
|
||||
const shafts = shaft === "front"
|
||||
? [facing]
|
||||
: shaft === "back"
|
||||
? [OPPOSITE[facing]]
|
||||
: [facing, OPPOSITE[facing]];
|
||||
return { axis: FACE_AXIS[facing], shafts, facing };
|
||||
}
|
||||
const axis = AXES[state(params.axis ?? "axis") ?? 1];
|
||||
return { axis, shafts: AXIS_FACES[axis] };
|
||||
}
|
||||
|
||||
export function face_axis(face: Face): Axis {
|
||||
return FACE_AXIS[face];
|
||||
}
|
||||
|
||||
// a speed turning counterclockwise seen from face, as rpm around face's axis, which is counterclockwise seen from its
|
||||
// positive end. like KineticBlockEntity.convertToDirection
|
||||
export function along(face: Face, speed: number): number {
|
||||
return AXIS_FACES[FACE_AXIS[face]][0] === face ? speed : -speed;
|
||||
}
|
||||
|
||||
// the face of from that to is beside, undefined when they aren't beside each other
|
||||
function face_toward(from: Kinetic, to: Kinetic): Face | undefined {
|
||||
const [dx, dy, dz] = [to.x - from.x, to.y - from.y, to.z - from.z];
|
||||
return (Object.keys(FACE_OFFSETS) as Face[]).find((face) => {
|
||||
const [ox, oy, oz] = FACE_OFFSETS[face];
|
||||
return ox === dx && oy === dy && oz === dz;
|
||||
});
|
||||
}
|
||||
|
||||
// how much faster to turns than from when from turns it, 0 when from doesn't turn it: 1 for axles that meet. like
|
||||
// RotationPropagator.getRotationSpeedModifier
|
||||
export function speed_ratio(from: Kinetic, to: Kinetic): number {
|
||||
const face = face_toward(from, to);
|
||||
if (face && from.shafts.includes(face) && to.shafts.includes(OPPOSITE[face])) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// where the blocks it could turn are, like getPotentialNeighbourLocations
|
||||
export function neighbour_positions(block: Kinetic): [number, number, number][] {
|
||||
return Object.values(FACE_OFFSETS).map(([dx, dy, dz]) => [block.x + dx, block.y + dy, block.z + dz]);
|
||||
}
|
||||
Reference in New Issue
Block a user