Cog wheels
This commit is contained in:
@@ -3,44 +3,76 @@
|
||||
|
||||
// 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 type { BlockPlacement, Face, SharedContext } from "bworld/shared";
|
||||
import {
|
||||
AXES,
|
||||
type Axis,
|
||||
face_axis,
|
||||
FACE_OFFSETS,
|
||||
FACINGS,
|
||||
kinetic_shape,
|
||||
type KineticParams,
|
||||
type KineticShape,
|
||||
OPPOSITE,
|
||||
} from "./kinetics/connections.ts";
|
||||
|
||||
const AXES = ["x", "y", "z"];
|
||||
|
||||
export function setup(ctx: SharedContext) {
|
||||
// what turns at x y z, undefined when nothing does
|
||||
const kinetic_at = (x: number, y: number, z: number): KineticShape | undefined => {
|
||||
const id = ctx.world.get_block(x, y, z);
|
||||
const params = id && ctx.blocks.exists(id)
|
||||
? ctx.blocks.components(id)["create:kinetic"] as KineticParams | undefined
|
||||
: undefined;
|
||||
return params && kinetic_shape(params, (name) => ctx.world.get_state(x, y, z, name));
|
||||
};
|
||||
const beside = (x: number, y: number, z: number, side: Face) => {
|
||||
const [dx, dy, dz] = FACE_OFFSETS[side];
|
||||
return kinetic_at(x + dx, y + dy, z + dz);
|
||||
};
|
||||
|
||||
// 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 (!beside(x, y, z, side)?.shafts.includes(OPPOSITE[side])) continue;
|
||||
if (found && face_axis(found) !== face_axis(side)) return undefined;
|
||||
found = side;
|
||||
}
|
||||
return found;
|
||||
};
|
||||
|
||||
// a cog lines up with the small cog it's placed against, or the axle beside it, or else the face it's clicked onto,
|
||||
// which is all sneaking looks at, like CogWheelBlock.getAxisForPlacement
|
||||
const cog_axis = (placement: BlockPlacement): Axis => {
|
||||
const { x, y, z, face } = placement;
|
||||
if (placement.sneaking) return face_axis(face);
|
||||
const against = beside(x, y, z, OPPOSITE[face]);
|
||||
if (against?.cog === "small") return against.axis;
|
||||
const side = axle_beside(x, y, z);
|
||||
return face_axis(side ?? face);
|
||||
};
|
||||
// whether a cog on axis at x y z keeps clear of the cogs beside it: a large one's teeth would cut into any cog
|
||||
// beside it across its axis, and a small one's into a large one, unless that one's axis points at it. like
|
||||
// isValidCogwheelPosition
|
||||
const cog_fits = (x: number, y: number, z: number, axis: Axis, cog: "small" | "large") =>
|
||||
FACINGS.every((side) => {
|
||||
const other = beside(x, y, z, side);
|
||||
if (face_axis(side) === axis || !other?.cog || other.axis === face_axis(side)) return true;
|
||||
return other.cog === "small" && cog === "small";
|
||||
});
|
||||
|
||||
// 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
|
||||
// from the axle, or toward the player, or away from them while sneaking, like DirectionalKineticBlock. cogs are
|
||||
// placed their own way, see cog_axis
|
||||
ctx.components.register_block<KineticParams>("create:kinetic", {
|
||||
state_for_placement(placement, params) {
|
||||
if (params.cog) {
|
||||
const axis = cog_axis(placement);
|
||||
if (!cog_fits(placement.x, placement.y, placement.z, axis, params.cog)) return false;
|
||||
return { [params.axis ?? "axis"]: AXES.indexOf(axis) };
|
||||
}
|
||||
const side = axle_beside(placement.x, placement.y, placement.z);
|
||||
if (params.facing !== undefined) {
|
||||
const facing = side && !placement.sneaking
|
||||
|
||||
Reference in New Issue
Block a user