Cog wheels
This commit is contained in:
@@ -19,32 +19,60 @@ const HANDLE_TURNS: Record<Face, ModelRef> = {
|
||||
bottom: { model: HANDLE, x: 90 },
|
||||
};
|
||||
|
||||
// how a model made along y is turned to an axis, the way the kinetic blocks' variants turn theirs
|
||||
const AXIS_TURNS: Record<Axis, { x?: number; y?: number }> = { x: { x: 90, y: 90 }, y: {}, z: { x: 90, y: 180 } };
|
||||
|
||||
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 });
|
||||
const plain = (block: VisualBlock, instances: Instances): Visual => ({
|
||||
update: turning(block, instances, shape_of(block)),
|
||||
});
|
||||
ctx.render.register_visual("create:shaft", plain, { replaces_model: true });
|
||||
ctx.render.register_visual("create:cogwheel", plain, { replaces_model: true });
|
||||
|
||||
// a large cog's teeth are half as far apart as a small one's, so on the blocks where small cogs aren't offset it's
|
||||
// offset by half of one of its own teeth. its shaft is apart from it and offset like any other, so it lines up with
|
||||
// the shafts next to it. like BracketedKineticBlockEntityVisual.LargeCogVisual
|
||||
ctx.render.register_visual("create:large_cogwheel", (block, instances) => {
|
||||
const shape = shape_of(block);
|
||||
const turns = AXIS_TURNS[shape.axis];
|
||||
const cog = turning(
|
||||
block,
|
||||
instances,
|
||||
shape,
|
||||
{ model: "create:block_large_cogwheel_shaftless", ...turns },
|
||||
11.25,
|
||||
);
|
||||
const shaft = turning(block, instances, shape, { model: "create:block_cogwheel_shaft", ...turns });
|
||||
return {
|
||||
update() {
|
||||
cog();
|
||||
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 turn = 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,
|
||||
update: turn,
|
||||
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);
|
||||
velocity += (speed_of(block) * 0.3 - velocity) * Math.min(1, ticks / 4);
|
||||
angle = (angle + velocity * ticks) % 360;
|
||||
handle.set_rotation(shape.axis, 0, angle);
|
||||
},
|
||||
@@ -52,16 +80,17 @@ export function setup(ctx: ClientContext) {
|
||||
}, { 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);
|
||||
const speed_of = (block: VisualBlock) => block.get_client_data<number>("speed") ?? 0;
|
||||
|
||||
// a model turning around the block's axis at its speed, the block's own when not given. returns what turns it again
|
||||
// when the speed changes. on every other block it's offset by half a small cog's tooth, so cogs mesh with the ones
|
||||
// beside them, and on the rest by offset, like KineticBlockEntityVisual.rotationOffset
|
||||
function turning(block: VisualBlock, instances: Instances, shape: KineticShape, model?: ModelRef, offset = 0) {
|
||||
const instance = instances.rotating(model);
|
||||
const angle = should_offset(shape.axis, block) ? 22.5 : offset;
|
||||
const turn = () => instance.set_rotation(shape.axis, speed_of(block), angle);
|
||||
turn();
|
||||
const visual: Visual = { update: turn };
|
||||
return { visual, speed };
|
||||
return turn;
|
||||
}
|
||||
|
||||
// whether the coordinates across the axis add up to an even number, like KineticBlockEntityVisual.shouldOffset
|
||||
|
||||
Reference in New Issue
Block a user