Juicer visuals
This commit is contained in:
+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