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
+51 -1
View File
@@ -4,7 +4,19 @@
// create's blocks that do something: so far what turns (create:kinetic), the generators that turn it (the hand crank,
// the creative motor and the water wheel), and the mechanical press
import type { BlockRef, EntityRef, Id, ServerContext } from "bworld/server";
import { actual_speed, along, kinetic_shape, type KineticParams, type Pos } from "./kinetics/connections.ts";
import {
actual_speed,
along,
face_axis,
FACE_OFFSETS,
FACINGS,
kinetic_shape,
type KineticParams,
OPPOSITE,
type Pos,
positive,
source_face,
} from "./kinetics/connections.ts";
import { flow_score, motor_step } from "./kinetics/generators.ts";
import { handle_added, handle_removed, type KineticWorld, update_generated } from "./kinetics/spreading.ts";
import {
@@ -81,6 +93,39 @@ export function setup(ctx: ServerContext) {
on_break(block) {
handle_removed(world, [block.x, block.y, block.z]);
},
// states that change how it turns things, like a clutch being powered or a chain drive joining a line, take it
// out and put it back, like Create's detachKinetics and attachKinetics on a change that isn't
// areStatesKineticallyEquivalent
on_state_change(block, params, before) {
const now = kinetic_shape(params, (name) => ctx.world.get_state(block.x, block.y, block.z, name));
if (JSON.stringify(now) === JSON.stringify(kinetic_shape(params, (name) => before[name]))) return;
handle_removed(world, [block.x, block.y, block.z]);
handle_added(world, [block.x, block.y, block.z]);
},
});
// a shaft or cog right clicked with a casing becomes the encased one, by the casing's id, turned the same way. an
// encased cog gets an axle out of its casing toward each axle beside it. the casing isn't used up. like
// EncasableBlock.tryEncase and handleEncasing
ctx.components.register_block<Record<Id, Id>>("create:encasable", {
on_interact(block, params, player) {
const into = params[player.held_item?.id ?? ""];
const node = world.get(block.x, block.y, block.z);
if (!into || !node) return false;
const own = ctx.blocks.components(block.id)["create:kinetic"] as KineticParams;
const encased = ctx.blocks.components(into)["create:kinetic"] as KineticParams;
const states = {
[encased.axis ?? "axis"]: ctx.world.get_state(block.x, block.y, block.z, own.axis ?? "axis")!,
};
for (const side of FACINGS) {
if (!encased.shaft_states || face_axis(side) !== node.axis) continue;
const [dx, dy, dz] = FACE_OFFSETS[side];
const axle = world.get(block.x + dx, block.y + dy, block.z + dz)?.shafts.includes(OPPOSITE[side]);
states[encased.shaft_states[positive(side) ? 0 : 1]] = axle ? 1 : 0;
}
ctx.world.set_block(block.x, block.y, block.z, into, states);
return true;
},
});
// turns while it's being clicked, a little longer each click, and the other way while sneaking, like create's
@@ -285,6 +330,11 @@ function kinetic_world(ctx: ServerContext, config: Config): KineticWorld {
// machines stop ticking while they're still, like a press, and start again once they turn
if (speed !== 0 && !block.get_client_data("speed")) block.set_ticking(true);
block.set_client_data("speed", speed === 0 ? undefined : speed);
// the side its speed comes from decides how a gearbox's or clutch's axles turn, which its visual shows
const { gearbox, split } = params_of(block.id).kinetic!;
if (gearbox || split) {
block.set_client_data("source", source_face({ x: at[0], y: at[1], z: at[2], source }));
}
const { source: _, network: __, ...rest }: KineticData = block.data ?? {};
block.data = { ...rest, ...(source && { source }), ...(network !== undefined && { network }) };
},