Juicer visuals

This commit is contained in:
2026-10-25 19:07:27 -03:00
parent 233c518def
commit a28e0b9abb
5 changed files with 174 additions and 16 deletions
+59 -2
View File
@@ -1,9 +1,23 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the screens of the base game's menus, see scripts/server.ts for their slots. in gui pixels, like minecraft's
import type { ClientContext, ClientMenu, Color, Graphics, ItemStack, MenuScreen } from "bworld/client";
// the screens of the base game's menus, see scripts/server.ts for their slots. in gui pixels, like minecraft's. and the
// juicer's visual
import { mat4 } from "gl-matrix";
import type {
ClientContext,
ClientMenu,
Color,
Graphics,
Instance,
Instances,
ItemStack,
MenuScreen,
Visual,
VisualBlock,
} from "bworld/client";
import { MINIGAMES } from "./minigames/mod.ts";
import { JUICER_CAPACITY, type JuicerTank } from "./shared.ts";
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
const WIDTH = 176;
@@ -18,6 +32,49 @@ export function setup(ctx: ClientContext) {
ctx.ui.register_menu_screen("bworld:smithing_table", smithing_screen);
ctx.ui.register_menu_screen("bworld:stonecutter", (menu) => stonecutter_screen(ctx, menu));
ctx.ui.register_menu_screen("bworld:fishing_minigame", fishing_minigame_screen);
ctx.render.register_visual("bworld:juicer", juicer_visual);
}
// the middle of the bottom copy of what's in the juicer, just over its floor, and how much higher each next one is
const JUICER_ITEM_Y = 4 / 16;
const JUICER_COPY_RISE = 1 / 48;
// how many more copies a stack this much bigger shows
const JUICER_ITEMS_A_COPY = 16;
// the juice fills the 12 by 12 pixels inside its walls from its floor up, a pixel deep while there's any and 12 when
// it's full
const JUICER_INSIDE = [2, 14];
const JUICER_DEPTH = 12;
// what's in the juicer and its juice, its "stack" and "tank" client data (see register_juicer in scripts/server.ts).
// the items are drawn like create's basin draws its ingredients: tipped over and turned, with a copy more for every
// JUICER_ITEMS_A_COPY. each copy is turned further and a bit higher than the one under it, so they pile up
function juicer_visual(block: VisualBlock, instances: Instances): Visual {
let shown: Instance[] = [];
const update = () => {
for (const instance of shown) instance.delete();
shown = [];
const tank = block.get_client_data<JuicerTank>("tank");
if (tank) {
const juice = instances.fluid(tank.fluid);
const [low, high] = JUICER_INSIDE;
const depth = Math.min(JUICER_DEPTH, Math.max(1, JUICER_DEPTH * tank.amount / JUICER_CAPACITY));
juice.set_box([low, low, low], [high, low + depth, high]);
shown.push(juice);
}
const stack = block.get_client_data<ItemStack>("stack");
const copies = stack ? 1 + Math.floor(stack.count / JUICER_ITEMS_A_COPY) : 0;
shown.push(...Array.from({ length: copies }, (_, i) => {
const copy = instances.item(stack!);
const matrix = mat4.fromTranslation(mat4.create(), [0.5, JUICER_ITEM_Y + i * JUICER_COPY_RISE, 0.5]);
mat4.rotateY(matrix, matrix, (35 + i * 72) * Math.PI / 180);
mat4.rotateX(matrix, matrix, 65 * Math.PI / 180);
mat4.translate(matrix, matrix, [-0.5, -0.5, -0.5]);
copy.set_transform(matrix);
return copy;
}));
};
update();
return { update };
}
// a panel with the menu's title and the inventory's label over its rows, drawing anything else over it
+17 -10
View File
@@ -8,7 +8,7 @@ import { register_effects } from "./effects.ts";
import { register_fishing } from "./fishing.ts";
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
import { register_verdigris } from "./verdigris/server.ts";
import type { JuiceableParams } from "./shared.ts";
import { type JuiceableParams, JUICER_CAPACITY, type JuicerTank } from "./shared.ts";
export function setup(ctx: ServerContext) {
register_hoeable(ctx);
@@ -851,22 +851,27 @@ function register_buckets(ctx: ServerContext) {
// the juicer has no screen. right clicking it with something juiceable puts that in, one kind at a time, and
// pressing it (sneaking with an empty hand) JUICER_PRESSES times turns one of it into its juice, while that fits in the
// tank. right clicking it with an empty fluid container fills it from the tank. breaking it drops what's in it, and the
// juice is lost
// juice is lost. clients see what's in it and its juice as its "stack" and "tank" client data, which its visual draws
// (see scripts/client.ts)
const JUICE = "bworld:juice";
const JUICER_PRESSES = 4;
const JUICER_CAPACITY = 1000;
interface JuicerData {
container?: string;
// how many times it's been pressed toward the next juice
presses?: number;
tank?: { fluid: FluidVariant; amount: number };
tank?: JuicerTank;
}
function register_juicer(ctx: ServerContext) {
// what juice is made from, read by whatever makes it, like the juicer. the juice's name and color are in shared.ts
ctx.components.register_item<JuiceableParams>("bworld:juiceable", {});
const juiceable = (id: string) => ctx.items.components(id)["bworld:juiceable"] as JuiceableParams | undefined;
const show = (block: BlockRef, container: Container) => {
const item = container.get(0);
const data = item?.data !== undefined && { data: item.data };
block.set_client_data("stack", item && { id: item.id, count: item.count, ...data });
};
ctx.components.register_block("bworld:juicer", {
on_interact(block, _params, player) {
@@ -875,6 +880,7 @@ function register_juicer(ctx: ServerContext) {
const data = block.data as JuicerData;
if (held && juiceable(held.id)) {
held.count = container.add(held)?.count ?? 0;
show(block, container);
// true even when none fit, so the fruit isn't eaten instead
return true;
}
@@ -885,7 +891,9 @@ function register_juicer(ctx: ServerContext) {
const tank = data.tank;
if (!tank || tank.amount < capacity) return false;
const amount = tank.amount - capacity;
block.data = { ...data, tank: amount > 0 ? { ...tank, amount } : undefined } satisfies JuicerData;
const left = amount > 0 ? { ...tank, amount } : undefined;
block.data = { ...data, tank: left } satisfies JuicerData;
block.set_client_data("tank", left);
player.exchange_held({ id: held.id, count: 1, data: { fluid: tank.fluid } });
return true;
}
@@ -905,11 +913,10 @@ function register_juicer(ctx: ServerContext) {
return true;
}
item.count -= 1;
block.data = {
...data,
presses: 0,
tank: { fluid: juice, amount: (tank?.amount ?? 0) + params.juice },
} satisfies JuicerData;
show(block, container);
const filled = { fluid: juice, amount: (tank?.amount ?? 0) + params.juice };
block.data = { ...data, presses: 0, tank: filled } satisfies JuicerData;
block.set_client_data("tank", filled);
return true;
},
on_break(block) {
+9 -1
View File
@@ -4,7 +4,7 @@
// the base game's block rules: how blocks are placed and how they change with the blocks beside them. they run on the
// server and on every client, so players see a fence join or a trapdoor turn the moment they place it, and a trapdoor
// or a door open the moment they click it. and the juice's rules, which name and color it by the item it's from
import type { SharedBlock, SharedContext } from "bworld/shared";
import type { FluidVariant, SharedBlock, SharedContext } from "bworld/shared";
import { register_verdigris_rules } from "./verdigris/shared.ts";
export function setup(ctx: SharedContext) {
@@ -144,6 +144,14 @@ export interface JuiceableParams {
color: string;
}
// how much juice the juicer holds, in mB, and the juice in it: in its tile data, and in its "tank" client data for its
// visual to draw (see register_juicer in server.ts and juicer_visual in client.ts)
export const JUICER_CAPACITY = 1000;
export interface JuicerTank {
fluid: FluidVariant;
amount: number;
}
// juice is one fluid for every fruit, like create's potion fluid: which fruit sits in its data, { item }. it's named
// after that item, "Strawberry Juice", in the color its bworld:juiceable says
function register_juice(ctx: SharedContext) {