Hand crank and shafts
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -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]);
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// rotation spreading through what's connected, like create's RotationPropagator: a block that's placed takes the speed
|
||||
// of what turns it, a generator's speed spreads to everything it turns, the faster of two takes the other's blocks
|
||||
// over, and a block that would turn two ways at once breaks. each walk is a queue, not recursion, and runs only when
|
||||
// something changes. no ctx, the world is anything that keeps these, so tests can give their own
|
||||
import { type Kinetic, neighbour_positions, speed_ratio } from "./connections.ts";
|
||||
|
||||
export type Pos = readonly [number, number, number];
|
||||
|
||||
// a kinetic block as the world keeps it
|
||||
export interface KineticNode extends Kinetic {
|
||||
// rpm around its axis, counterclockwise seen from the axis's positive end, like getTheoreticalSpeed
|
||||
readonly speed: number;
|
||||
// the block next to it it gets its speed from. undefined for a still block, and for a generator turning it
|
||||
readonly source?: Pos;
|
||||
// the generator its speed comes from, as "x y z", for telling a loop apart from another network meeting it
|
||||
readonly network?: string;
|
||||
// the speed it makes itself, 0 for anything that isn't a generator or one that's still, like getGeneratedSpeed
|
||||
readonly generated: number;
|
||||
}
|
||||
|
||||
export interface KineticWorld {
|
||||
// undefined when what's there isn't kinetic
|
||||
get(x: number, y: number, z: number): KineticNode | undefined;
|
||||
set(at: Pos, speed: number, source: Pos | undefined, network: string | undefined): void;
|
||||
// breaks it and drops it, like a shaft turned two ways at once. its on_break runs handle_removed
|
||||
break(at: Pos): void;
|
||||
// the fastest anything can turn, like the maxRotationSpeed config
|
||||
readonly max_speed: number;
|
||||
}
|
||||
|
||||
// a block was placed: it takes the speed of what turns it, and passes it on, like RotationPropagator.handleAdded
|
||||
export function handle_added(world: KineticWorld, at: Pos) {
|
||||
spread(world, at);
|
||||
}
|
||||
|
||||
// a block is going: what it turned stops, then what's still turning around that spreads back in, like handleRemoved.
|
||||
// on_break runs while it's still there, so the walks don't see it
|
||||
export function handle_removed(world: KineticWorld, at: Pos) {
|
||||
const node = world.get(...at);
|
||||
if (!node || node.speed === 0) return;
|
||||
const without: KineticWorld = {
|
||||
get: (x, y, z) => x === at[0] && y === at[1] && z === at[2] ? undefined : world.get(x, y, z),
|
||||
set: (...args) => world.set(...args),
|
||||
break: (where) => world.break(where),
|
||||
max_speed: world.max_speed,
|
||||
};
|
||||
restart(without, detach(without, node));
|
||||
}
|
||||
|
||||
// a generator's speed changed, like a crank being turned or let go. like GeneratingKineticBlockEntity's
|
||||
// updateGeneratedRotation
|
||||
export function update_generated(world: KineticWorld, at: Pos) {
|
||||
const node = world.get(...at);
|
||||
if (!node || node.generated === node.speed) return;
|
||||
const speed = node.generated;
|
||||
if (speed === 0) {
|
||||
// something else turns it, so it goes on turning with that
|
||||
if (node.source) return;
|
||||
world.set(at, 0, undefined, undefined);
|
||||
restart(world, detach(world, node));
|
||||
return;
|
||||
}
|
||||
if (node.source && node.speed !== 0 && Math.abs(node.speed) >= Math.abs(speed)) {
|
||||
// something as fast or faster turns it: it goes along, unless it's turning the other way
|
||||
if (Math.sign(node.speed) !== Math.sign(speed)) world.break(at);
|
||||
return;
|
||||
}
|
||||
// it turns everything it's connected to, at its own speed. what it turned before stops first, so its old speed
|
||||
// doesn't meet its new one
|
||||
world.set(at, speed, undefined, network_of(at));
|
||||
const stopped = node.speed !== 0 ? detach(world, node) : [];
|
||||
spread(world, at);
|
||||
restart(world, stopped);
|
||||
}
|
||||
|
||||
const same = (a: Pos | undefined, b: Pos) => a !== undefined && a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
|
||||
const pos_of = (node: Kinetic): Pos => [node.x, node.y, node.z];
|
||||
const network_of = (at: Pos) => at.join(" ");
|
||||
|
||||
// the kinetic blocks next to node that turn it or that it turns, like getConnectedNeighbours
|
||||
function connected(world: KineticWorld, node: Kinetic): KineticNode[] {
|
||||
return neighbour_positions(node).flatMap(([x, y, z]) => {
|
||||
const neighbour = world.get(x, y, z);
|
||||
const turns = neighbour && (speed_ratio(node, neighbour) !== 0 || speed_ratio(neighbour, node) !== 0);
|
||||
return turns ? [neighbour] : [];
|
||||
});
|
||||
}
|
||||
|
||||
// spreads the speed at start to what it's connected to, and on through whatever that changes, like
|
||||
// propagateNewSource
|
||||
function spread(world: KineticWorld, start: Pos) {
|
||||
const queue: Pos[] = [start];
|
||||
for (let i = 0; i < queue.length; i++) {
|
||||
const at = queue[i];
|
||||
const current = world.get(...at);
|
||||
if (!current) continue;
|
||||
for (const neighbour of connected(world, current)) {
|
||||
const new_speed = current.speed * speed_ratio(current, neighbour);
|
||||
const opposite_speed = neighbour.speed * speed_ratio(neighbour, current);
|
||||
if (new_speed === 0 && opposite_speed === 0) continue;
|
||||
|
||||
const against = new_speed !== 0 && neighbour.speed !== 0 &&
|
||||
Math.sign(new_speed) !== Math.sign(neighbour.speed);
|
||||
const too_fast = Math.abs(new_speed) > world.max_speed || Math.abs(opposite_speed) > world.max_speed;
|
||||
if (against || too_fast) {
|
||||
world.break(at);
|
||||
break;
|
||||
}
|
||||
// the neighbour is faster, so it turns this one, which passes that on from here
|
||||
if (Math.abs(opposite_speed) > Math.abs(current.speed)) {
|
||||
world.set(at, opposite_speed, pos_of(neighbour), neighbour.network);
|
||||
queue.push(at);
|
||||
break;
|
||||
}
|
||||
// this one is at least as fast, so it takes over everything the neighbour turns. unless the same generator
|
||||
// turns both, which is a loop: fine while the speeds agree, and this breaks when they don't
|
||||
const loop = current.network === undefined || current.network === neighbour.network;
|
||||
if (Math.abs(new_speed) >= Math.abs(neighbour.speed) && loop) {
|
||||
if (Math.abs(new_speed) > Math.abs(neighbour.speed) * (1 + 1 / 65536)) {
|
||||
world.break(at);
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (Math.abs(new_speed) < Math.abs(neighbour.speed) && Math.abs(neighbour.speed - new_speed) <= 1e-4) {
|
||||
continue;
|
||||
}
|
||||
world.set(pos_of(neighbour), new_speed, at, current.network);
|
||||
queue.push(pos_of(neighbour));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// the blocks node turned stop, like the removed or stopped block's part of RotationPropagator.handleRemoved. returns
|
||||
// the generators among them, see restart
|
||||
function detach(world: KineticWorld, node: Kinetic): Pos[] {
|
||||
const at = pos_of(node);
|
||||
return neighbour_positions(node).flatMap((where) =>
|
||||
same(world.get(...where)?.source, at) ? clear(world, where) : []
|
||||
);
|
||||
}
|
||||
|
||||
// generators that stopped with what turned them start again with their own speed, like reActivateSource. that's a
|
||||
// tick later in create, so here it's after the walk that stopped them, when what's turning around them has settled
|
||||
function restart(world: KineticWorld, generators: Pos[]) {
|
||||
for (const at of generators) update_generated(world, at);
|
||||
}
|
||||
|
||||
// stops start and everything that got its speed through it, then lets what's still turning at the edges spread back
|
||||
// in, like propagateMissingSource. returns the generators in it
|
||||
function clear(world: KineticWorld, start: Pos): Pos[] {
|
||||
const missing = world.get(...start)?.source;
|
||||
const edges: Pos[] = [];
|
||||
const generators: Pos[] = [];
|
||||
const frontier: Pos[] = [start];
|
||||
for (let i = 0; i < frontier.length; i++) {
|
||||
const at = frontier[i];
|
||||
const current = world.get(...at);
|
||||
if (!current) continue;
|
||||
world.set(at, 0, undefined, undefined);
|
||||
if (current.generated !== 0) generators.push(at);
|
||||
for (const neighbour of connected(world, current)) {
|
||||
const where = pos_of(neighbour);
|
||||
if (same(missing, where) || !neighbour.source) continue;
|
||||
if (same(neighbour.source, at)) frontier.push(where);
|
||||
else edges.push(where);
|
||||
}
|
||||
}
|
||||
// an edge can turn out to be part of what stopped, found from another side later
|
||||
const edge = edges.find((at) => world.get(...at)?.source !== undefined);
|
||||
if (edge) spread(world, edge);
|
||||
return generators;
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// create's blocks that do something: so far what turns (create:kinetic) and the hand crank that turns it
|
||||
import type { BlockRef, Id, ServerContext } from "bworld/server";
|
||||
import { along, kinetic_shape, type KineticParams } from "./kinetics/connections.ts";
|
||||
import { handle_added, handle_removed, type KineticWorld, type Pos, update_generated } from "./kinetics/spreading.ts";
|
||||
|
||||
interface Config {
|
||||
max_speed: number;
|
||||
crank_hunger: number;
|
||||
}
|
||||
|
||||
// a kinetic block's tile data, see KineticNode. its speed is its "speed" client data, which its visual turns it by
|
||||
interface KineticData {
|
||||
source?: Pos;
|
||||
network?: string;
|
||||
}
|
||||
|
||||
// the create:hand_crank component's params: the rpm it turns at
|
||||
interface CrankParams {
|
||||
speed: number;
|
||||
}
|
||||
|
||||
// how long a crank keeps turning after each click, in ticks. holding use clicks every 4
|
||||
const CRANK_USE_TICKS = 10;
|
||||
|
||||
interface CrankData extends KineticData {
|
||||
// ticks left turning
|
||||
in_use?: number;
|
||||
// turned the other way, by sneaking
|
||||
backwards?: boolean;
|
||||
}
|
||||
|
||||
export function setup(ctx: ServerContext) {
|
||||
const config: Config = ctx.config;
|
||||
const world = kinetic_world(ctx, config);
|
||||
|
||||
ctx.components.register_block<KineticParams>("create:kinetic", {
|
||||
on_create(block) {
|
||||
handle_added(world, [block.x, block.y, block.z]);
|
||||
},
|
||||
on_break(block) {
|
||||
handle_removed(world, [block.x, block.y, block.z]);
|
||||
},
|
||||
});
|
||||
|
||||
// turns while it's being clicked, a little longer each click, and the other way while sneaking, like create's
|
||||
// HandCrankBlock and HandCrankBlockEntity. turning it makes the player hungry
|
||||
ctx.components.register_block<CrankParams>("create:hand_crank", {
|
||||
on_interact(block, params, player) {
|
||||
const data: CrankData = block.data ?? {};
|
||||
const backwards = player.is_sneaking();
|
||||
const changed = !data.in_use || backwards !== (data.backwards ?? false);
|
||||
block.data = { ...data, in_use: CRANK_USE_TICKS, backwards };
|
||||
block.set_ticking(true);
|
||||
if (changed) update_generated(world, [block.x, block.y, block.z]);
|
||||
player.add_exhaustion(params.speed * config.crank_hunger);
|
||||
return true;
|
||||
},
|
||||
on_tick(block) {
|
||||
const data: CrankData = block.data ?? {};
|
||||
const in_use = Math.max(0, (data.in_use ?? 0) - 1);
|
||||
block.data = { ...data, in_use };
|
||||
if (in_use > 0) return;
|
||||
block.set_ticking(false);
|
||||
update_generated(world, [block.x, block.y, block.z]);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// the world as rotation spreading sees it: blocks with create:kinetic, their speed in client data and where it comes
|
||||
// from in tile data
|
||||
function kinetic_world(ctx: ServerContext, config: Config): KineticWorld {
|
||||
// components never change once loaded, so each block's are looked up once
|
||||
const params = new Map<Id, { kinetic?: KineticParams; crank?: CrankParams }>();
|
||||
const params_of = (id: Id) => {
|
||||
let found = params.get(id);
|
||||
if (!found) {
|
||||
const components = ctx.blocks.components(id);
|
||||
found = {
|
||||
kinetic: components["create:kinetic"] as KineticParams | undefined,
|
||||
crank: components["create:hand_crank"] as CrankParams | undefined,
|
||||
};
|
||||
params.set(id, found);
|
||||
}
|
||||
return found;
|
||||
};
|
||||
const block_at = (at: Pos): BlockRef => {
|
||||
const block = ctx.world.get_block_ref(...at);
|
||||
if (!block) throw new Error(`there's no kinetic block at ${at.join(" ")}`);
|
||||
return block;
|
||||
};
|
||||
|
||||
return {
|
||||
get(x, y, z) {
|
||||
const block = ctx.world.get_block_ref(x, y, z);
|
||||
if (!block) return undefined;
|
||||
const { kinetic, crank } = params_of(block.id);
|
||||
if (!kinetic) return undefined;
|
||||
const shape = kinetic_shape(kinetic, (name) => ctx.world.get_state(x, y, z, name));
|
||||
const data: CrankData = block.data ?? {};
|
||||
// a crank turns counterclockwise seen from its handle, or the other way while sneaking
|
||||
const generated = crank && data.in_use && shape.facing
|
||||
? along(shape.facing, data.backwards ? -crank.speed : crank.speed)
|
||||
: 0;
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
...shape,
|
||||
speed: block.get_client_data<number>("speed") ?? 0,
|
||||
source: data.source,
|
||||
network: data.network,
|
||||
generated,
|
||||
};
|
||||
},
|
||||
set(at, speed, source, network) {
|
||||
const block = block_at(at);
|
||||
block.set_client_data("speed", speed === 0 ? undefined : speed);
|
||||
const { source: _, network: __, ...rest }: KineticData = block.data ?? {};
|
||||
block.data = { ...rest, ...(source && { source }), ...(network !== undefined && { network }) };
|
||||
},
|
||||
break(at) {
|
||||
ctx.world.break_block(...at);
|
||||
},
|
||||
max_speed: config.max_speed,
|
||||
};
|
||||
}
|
||||
@@ -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