Juicer visuals
This commit is contained in:
@@ -82,7 +82,9 @@ it's drawn on the ground (half a block across, a block's item a quarter), with i
|
||||
and it's placed by a matrix like a transformed model:
|
||||
|
||||
```ts
|
||||
const stack = block.get_client_data<ItemStack>("item");
|
||||
import { mat4 } from "gl-matrix";
|
||||
|
||||
const stack = block.get_client_data<ItemStack>("stack");
|
||||
if (stack) {
|
||||
const item = instances.item(stack);
|
||||
const lying = mat4.fromTranslation(mat4.create(), [0.5, 0.2, 0.5]);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+86
-2
@@ -5,7 +5,13 @@
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import type { FluidJson } from "$/common/mod_data.ts";
|
||||
import { display_name } from "$/common/utils.ts";
|
||||
import type { ClientContext, Instances, Visual, VisualBlock } from "$/common/mod_api/client.ts";
|
||||
import { CHUNK_AREA, CHUNK_SIZE } from "$/common/constants.ts";
|
||||
import { block_value, display_name } from "$/common/utils.ts";
|
||||
import { block_visuals, Visuals } from "$/client/rendering/visuals.ts";
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import { fluid_rules, set_fluid_rules } from "$/client/fluid_rules.ts";
|
||||
import { setup as setup_client } from "$/mods/bworld/scripts/client.ts";
|
||||
import { copy_dir, stone_floor, test_game } from "./helpers.ts";
|
||||
|
||||
const CLOUDBERRY = "testberries:cloudberry";
|
||||
@@ -87,25 +93,32 @@ Deno.test("the juicer takes juiceable items, juices one every 4 presses, and fil
|
||||
.sort((a, b) => Number(a!.data !== undefined) - Number(b!.data !== undefined));
|
||||
t.game.set_block(1, 100, 0, "bworld:juicer");
|
||||
|
||||
// what's in it and its juice, which clients see
|
||||
const shown = () => t.game.world.get_tile(1, 100, 0)?.client_data?.stack;
|
||||
const tank = () => t.game.world.get_tile(1, 100, 0)?.client_data?.tank;
|
||||
const juice = { id: "bworld:juice", data: { item: CLOUDBERRY } };
|
||||
hold(CLOUDBERRY);
|
||||
use();
|
||||
assertEquals(player.inventory.to_data().filter((item) => item?.id === CLOUDBERRY), []);
|
||||
assertEquals(shown(), { id: CLOUDBERRY, count: 4 });
|
||||
// right clicking with an empty hand without sneaking doesn't press it
|
||||
hold();
|
||||
use();
|
||||
|
||||
// 2 cloudberries and 3 presses toward the next are 200 mB, not a bottle's 250
|
||||
press(11);
|
||||
assertEquals(tank(), { fluid: juice, amount: 200 });
|
||||
hold("bworld:glass_bottle");
|
||||
use();
|
||||
assertEquals(bottles(), [{ id: "bworld:glass_bottle", count: 2 }]);
|
||||
|
||||
// the 3rd makes 300 mB, a bottle and 50 mB left, which isn't another
|
||||
press(1);
|
||||
assertEquals(shown(), { id: CLOUDBERRY, count: 1 });
|
||||
hold("bworld:glass_bottle");
|
||||
use();
|
||||
use();
|
||||
const juice = { id: "bworld:juice", data: { item: CLOUDBERRY } };
|
||||
assertEquals(tank(), { fluid: juice, amount: 50 });
|
||||
assertEquals(bottles(), [
|
||||
{ id: "bworld:glass_bottle", count: 1 },
|
||||
{ id: "bworld:glass_bottle", count: 1, data: { fluid: juice } },
|
||||
@@ -119,3 +132,74 @@ Deno.test("the juicer takes juiceable items, juices one every 4 presses, and fil
|
||||
{ id: CLOUDBERRY, count: 1 },
|
||||
]);
|
||||
});
|
||||
|
||||
// a juicer at 1 100 1 drawn by bworld's client script, and a way to set a field of its client data. its fluids have the
|
||||
// server's rules, the same as a client's
|
||||
async function drawn_juicer() {
|
||||
const { game } = await setup();
|
||||
AssetManager.instance.assets["bworld:textures_info"] = {};
|
||||
const client_rules = fluid_rules;
|
||||
set_fluid_rules(game.mods.fluid_rules);
|
||||
// bworld's client script, with only what it needs for its visuals
|
||||
const made = new Map<string, (block: VisualBlock, instances: Instances) => Visual>();
|
||||
setup_client({
|
||||
ui: { register_menu_screen() {} },
|
||||
render: { register_visual: (id: string, create: never) => made.set(id, create) },
|
||||
} as unknown as ClientContext);
|
||||
const juicer = EverythingRegistry.get_id("blocks", "bworld:juicer")!;
|
||||
block_visuals.clear();
|
||||
block_visuals.set(juicer, { mod: "bworld", replaces_model: false, create: made.get("bworld:juicer")! });
|
||||
const data: Record<string, unknown> = {};
|
||||
const visuals = new Visuals({ get_block_data: () => data, get_light: () => 0 });
|
||||
visuals.meshed(0, 0, Uint32Array.of(100 * CHUNK_AREA + 1 * CHUNK_SIZE + 1, block_value(juicer, 0)));
|
||||
const set = (field: string, value: unknown) => {
|
||||
data[field] = value;
|
||||
visuals.data_changed(1, 100, 1);
|
||||
};
|
||||
const done = () => {
|
||||
block_visuals.clear();
|
||||
set_fluid_rules(client_rules);
|
||||
};
|
||||
return { visuals, set, done };
|
||||
}
|
||||
|
||||
Deno.test("the juicer draws what's in it, more of it the more there is, and nothing when it's empty", async () => {
|
||||
const { visuals, set, done } = await drawn_juicer();
|
||||
// one item, however many of it are drawn
|
||||
const drawn = () =>
|
||||
[...visuals.groups()].filter((group) => group.instances.length > 0).map((group) => group.instances.length);
|
||||
|
||||
set("stack", { id: CLOUDBERRY, count: 1 });
|
||||
const one = drawn();
|
||||
assertEquals(one.length, 1);
|
||||
set("stack", { id: CLOUDBERRY, count: 64 });
|
||||
assertEquals(drawn().length, 1);
|
||||
assert(drawn()[0] > one[0], `${drawn()} for 64, ${one} for 1`);
|
||||
set("stack", undefined);
|
||||
assertEquals(drawn(), []);
|
||||
done();
|
||||
});
|
||||
|
||||
Deno.test("the juicer's juice fills its 12 by 12 floor, a pixel deep while there's any and 12 when it's full", async () => {
|
||||
const { visuals, set, done } = await drawn_juicer();
|
||||
const boxes = () => [...visuals.groups()].filter((group) => group.translucent);
|
||||
// where the box is on each axis, in pixels of the block
|
||||
const box = () =>
|
||||
[0, 1, 2].map((axis) => {
|
||||
const at = boxes()[0].quads.flatMap((quad) => quad.positions.filter((_, i) => i % 3 === axis));
|
||||
return [Math.min(...at) * 16, Math.max(...at) * 16];
|
||||
});
|
||||
const juice = { id: "bworld:juice", data: { item: CLOUDBERRY } };
|
||||
|
||||
set("tank", { fluid: juice, amount: 10 });
|
||||
assertEquals(box(), [[2, 14], [2, 3], [2, 14]]);
|
||||
// in the color of the juice of what it was made from
|
||||
assertEquals(boxes()[0].quads[0].tint, [0xe0 / 255, 0xa0 / 255, 0x30 / 255]);
|
||||
set("tank", { fluid: juice, amount: 500 });
|
||||
assertEquals(box(), [[2, 14], [2, 8], [2, 14]]);
|
||||
set("tank", { fluid: juice, amount: 1000 });
|
||||
assertEquals(box(), [[2, 14], [2, 14], [2, 14]]);
|
||||
set("tank", undefined);
|
||||
assertEquals(boxes(), []);
|
||||
done();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user