Fluid slots and kegs !

This commit is contained in:
2026-10-31 17:35:27 -03:00
parent 65934d34a1
commit 658a6453b7
19 changed files with 494 additions and 34 deletions
+2
View File
@@ -31,6 +31,8 @@ export const GUI_ATLAS = {
air_empty: { x: 100, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
air_full: { x: 110, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
air_half: { x: 120, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
// the slot with modern industrialization's fluid slot droplet, drawn over it by hand
fluid_slot: { x: 160, y: 0, width: 18, height: 18, border: [0, 0, 0, 0] },
} as const;
export type GuiSprite = keyof typeof GUI_ATLAS;
+40 -10
View File
@@ -11,11 +11,11 @@ import { ClientInventories } from "../inventory.ts";
import { canvas, draw_rect, draw_texture_region, Texture } from "../renderer/mod.ts";
import { AssetManager } from "../assets.ts";
import { get_sprite_region } from "../sprites.ts";
import { draw_item } from "../rendering/render_utils.ts";
import { draw_tooltip, item_tooltip } from "./tooltip.ts";
import { draw_fluid, draw_item } from "../rendering/render_utils.ts";
import { draw_tooltip, item_tooltip, tank_tooltip } from "./tooltip.ts";
import { ModGraphics } from "./mod_graphics.ts";
import { inventory_overlays } from "../mods.ts";
import type { ClientMenu, ClientSlot } from "../menu.ts";
import type { ClientFluidSlot, ClientMenu, ClientSlot } from "../menu.ts";
// how far past the slot's edge its highlight goes
const HIGHLIGHT_OVERHANG = 3 * GUI_SCALE;
@@ -42,8 +42,8 @@ export abstract class GuiScreen {
}
}
// a menu's screen, like minecraft's AbstractContainerScreen: in the middle of the window, it draws the menu's slots,
// the item on the cursor and tooltips, and sends clicks. the server owns what's in the slots: clicks are guessed
// a menu's screen, like minecraft's AbstractContainerScreen: in the middle of the window, it draws the menu's slots and
// fluid slots, the item on the cursor and tooltips, and sends clicks. the server owns what's in the slots: clicks are guessed
// locally so they feel instant, then whatever the server syncs back wins. subclasses draw the rest
export abstract class GuiMenuScreen extends GuiScreen {
inventories: ClientInventories;
@@ -54,6 +54,7 @@ export abstract class GuiMenuScreen extends GuiScreen {
height: number;
hovering: ClientSlot | undefined;
hovering_fluid: ClientFluidSlot | undefined;
// what the screen and mods draw with, in gui pixels
graphics = new ModGraphics();
@@ -109,6 +110,7 @@ export abstract class GuiMenuScreen extends GuiScreen {
if (this.inventory_overlays) this.#render_overlays();
if (g.mouse_blocked()) {
this.hovering = undefined;
this.hovering_fluid = undefined;
} else {
this.handle_interaction();
}
@@ -122,15 +124,17 @@ export abstract class GuiMenuScreen extends GuiScreen {
if (item) {
draw_tooltip(item_tooltip(item), mouse.x, mouse.y);
}
} else if (this.hovering_fluid) {
draw_tooltip(tank_tooltip(this.hovering_fluid.tank, this.hovering_fluid.output === true), mouse.x, mouse.y);
}
g.end();
}
#slot_x(slot: ClientSlot) {
#slot_x(slot: { x: number }) {
return this.x + slot.x * GUI_SCALE;
}
#slot_y(slot: ClientSlot) {
#slot_y(slot: { y: number }) {
return this.y + slot.y * GUI_SCALE;
}
@@ -138,15 +142,24 @@ export abstract class GuiMenuScreen extends GuiScreen {
for (const slot of this.menu.slots) {
draw_gui_sprite("slot", this.#slot_x(slot), this.#slot_y(slot));
}
// like modern industrialization's, an empty one shows a droplet
for (const slot of this.menu.fluid_slots) {
draw_gui_sprite(slot.tank.fluid ? "slot" : "fluid_slot", this.#slot_x(slot), this.#slot_y(slot));
}
// like minecraft's, the highlight is a bit bigger than the slot, part of it under the item and part over
const highlight = (sprite: "slot_highlight_back" | "slot_highlight_front") => {
if (this.hovering) {
const x = this.#slot_x(this.hovering) - HIGHLIGHT_OVERHANG;
draw_gui_sprite(sprite, x, this.#slot_y(this.hovering) - HIGHLIGHT_OVERHANG);
const hovered = this.hovering ?? this.hovering_fluid;
if (hovered) {
const x = this.#slot_x(hovered) - HIGHLIGHT_OVERHANG;
draw_gui_sprite(sprite, x, this.#slot_y(hovered) - HIGHLIGHT_OVERHANG);
}
};
highlight("slot_highlight_back");
// the whole slot in its fluid while there's any, whatever the amount, like modern industrialization's
for (const slot of this.menu.fluid_slots) {
if (slot.tank.fluid) draw_fluid(slot.tank.fluid, this.#slot_x(slot), this.#slot_y(slot));
}
for (const slot of this.menu.slots) {
const item = slot.container.get_item(slot.index);
if (item) {
@@ -218,8 +231,25 @@ export abstract class GuiMenuScreen extends GuiScreen {
handle_interaction() {
this.hovering = undefined;
this.hovering_fluid = undefined;
const mouse = InputManager.get_mouse_position();
for (const [index, slot] of this.menu.fluid_slots.entries()) {
const [x, y] = [this.#slot_x(slot), this.#slot_y(slot)];
if (!point_inside_rec(mouse.x, mouse.y, x, y, SLOT_SIZE, SLOT_SIZE)) {
continue;
}
this.hovering_fluid = slot;
for (const button of [0, 2]) {
if (InputManager.is_mouse_pressed(button)) {
InputManager.consume_mouse(button);
// what the cursor's container turns into depends on the tank, the server says
this.send({ type: "click_fluid", menu: this.menu.id, slot: index });
}
}
return;
}
for (const [index, slot] of this.menu.slots.entries()) {
const [x, y] = [this.#slot_x(slot), this.#slot_y(slot)];
if (!point_inside_rec(mouse.x, mouse.y, x, y, SLOT_SIZE, SLOT_SIZE)) {
+14
View File
@@ -9,6 +9,7 @@ import { display_name } from "$/common/utils.ts";
import { fluid_rules } from "$/client/fluid_rules.ts";
import { canvas, draw_text, draw_texture_region, measure_text, type Texture } from "$/client/renderer/mod.ts";
import { AssetManager } from "$/client/assets.ts";
import type { MenuTank } from "$/common/protocol.ts";
import { get_sprite_region } from "$/client/sprites.ts";
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts";
@@ -62,6 +63,19 @@ export function item_tooltip(item: ItemStack): TooltipLine[] {
return lines;
}
// a menu's fluid slot, like modern industrialization's: the fluid, how much of the tank it fills, and what clicking it
// with a fluid container does
export function tank_tooltip(tank: MenuTank, output: boolean): TooltipLine[] {
return [
tank.fluid ? { text: fluid_rules.name(tank.fluid), color: NAME_COLOR } : { text: "Empty", color: LORE_COLOR },
{ text: `${tank.amount} / ${tank.capacity} mB`, color: FLUID_COLOR },
{
text: output ? "Fluid Output, Click to Extract" : "Fluid Input, Click to Insert or Extract",
color: LORE_COLOR,
},
];
}
// below and right of the mouse, moved back inside the window when it would go past its edge
export function draw_tooltip(lines: TooltipLine[], mouse_x: number, mouse_y: number) {
const line_height = TEXT_HEIGHT * TEXT_SCALE;
+26 -3
View File
@@ -4,7 +4,14 @@
// the client's copy of a menu, like neoforge's client menu: built from what the server sent when it opened, kept up
// to date with what it syncs. clicks go to the server, which decides what they do
import { Container } from "$/common/inventory.ts";
import { CRAFTING_RESULT_SLOT, type MenuSlotInfo, PLAYER_MENU, PLAYER_MENU_EXTRA } from "$/common/protocol.ts";
import {
CRAFTING_RESULT_SLOT,
type MenuFluidSlotInfo,
type MenuSlotInfo,
type MenuTank,
PLAYER_MENU,
PLAYER_MENU_EXTRA,
} from "$/common/protocol.ts";
import type { ClientInventories } from "./inventory.ts";
import type { ItemStack as ApiItemStack } from "$/common/mod_api/client.ts";
@@ -23,6 +30,12 @@ export interface ClientSlot {
max_count?: number;
}
// a tank the server's menu shows, which clicks with a fluid container on the cursor fill it from or empty it into
export interface ClientFluidSlot extends MenuFluidSlotInfo {
// what the server last said is in it
tank: MenuTank;
}
export class ClientMenu {
readonly id: number;
readonly type: string;
@@ -30,6 +43,7 @@ export class ClientMenu {
readonly open_data: unknown;
data: Record<string, number>;
slots: ClientSlot[];
fluid_slots: ClientFluidSlot[] = [];
constructor(
id: number,
@@ -55,9 +69,16 @@ export class ClientMenu {
}
// one the server opened: its slots' items come in menu_content, into a container of its own
static opened(id: number, type: string, title: string, open_data: unknown, slots: MenuSlotInfo[]) {
static opened(
id: number,
type: string,
title: string,
open_data: unknown,
slots: MenuSlotInfo[],
fluid_slots: MenuFluidSlotInfo[] = [],
) {
const items = new Container(slots.length);
return new ClientMenu(
const menu = new ClientMenu(
id,
type,
title,
@@ -72,6 +93,8 @@ export class ClientMenu {
max_count: slot.max_count,
})),
);
menu.fluid_slots = fluid_slots.map((slot) => ({ ...slot, tank: { amount: 0, capacity: 0 } }));
return menu;
}
}
+15 -1
View File
@@ -246,6 +246,13 @@ export class ClientPacketListener {
menu?.slots[0]?.container.load(message.items);
break;
}
case "menu_tanks": {
const menu = this.#menu(message.id);
for (const [i, tank] of message.tanks.entries()) {
if (menu?.fluid_slots[i]) menu.fluid_slots[i].tank = tank;
}
break;
}
case "menu_data": {
const menu = this.#menu(message.id);
if (menu) menu.data = message.data;
@@ -291,7 +298,14 @@ export class ClientPacketListener {
close();
return;
}
const menu = ClientMenu.opened(message.id, message.menu_type, message.title, message.open_data, message.slots);
const menu = ClientMenu.opened(
message.id,
message.menu_type,
message.title,
message.open_data,
message.slots,
message.fluid_slots,
);
menu.data = message.data;
let screen: GuiModMenuScreen;
try {