Effect icons and tooltip !

This commit is contained in:
2026-10-02 13:16:50 -03:00
parent e560cb3928
commit f749f6be79
22 changed files with 131 additions and 19 deletions
+1 -1
View File
@@ -215,7 +215,7 @@ export class Client {
if (InputManager.is_key_pressed(options.key_inventory)) {
if (!this.screen) {
this.push_screen(new GuiPlayerInventory(player.inventories, (m) => this.connection.send(m)));
this.push_screen(new GuiPlayerInventory(player, (m) => this.connection.send(m)));
} else if (this.screen instanceof GuiMenuScreen) {
this.pop_screen();
}
+4
View File
@@ -85,6 +85,9 @@ export class LocalPlayer extends Player {
food = 20;
saturation = 5;
air = 20;
// the effects it has that show an icon, see the inventory screen. counted down here, the server only says when
// they change
effects: { id: string; amplifier: number; duration: number }[] = [];
// the attack speed attribute the server sent, and when they last attacked or switched slots, for the indicator
attack_speed = 4;
last_attack = -Infinity;
@@ -119,6 +122,7 @@ export class LocalPlayer extends Player {
}
tick() {
for (const effect of this.effects) if (effect.duration > 0) effect.duration -= 1;
this.#in_water = this.in_water;
const was_swimming = this.swimming;
const input = this.client.screen ? NO_INPUT : this.#read_input();
+54 -5
View File
@@ -1,5 +1,9 @@
import { GuiMenuScreen } from "./gui_screen.ts";
import { ClientInventories } from "../inventory.ts";
import type { LocalPlayer } from "../entity/local_player.ts";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import type { EffectJson } from "$/common/mod_data.ts";
import { TICKS_PER_SECOND } from "$/common/constants.ts";
import { display_name } from "$/common/utils.ts";
import { ClientMessage, PLAYER_MENU_EXTRA } from "$/common/protocol.ts";
import { player_menu } from "../menu.ts";
import type { ModGraphics } from "./mod_graphics.ts";
@@ -9,16 +13,25 @@ const HEIGHT = 166;
const SLOT = 18;
// around the slots, like minecraft's
const MARGIN = 7;
// effect icons in a row on a panel of their own, just above the inventory's
const ICON = 16;
const ICON_GAP = 2;
const ICON_MARGIN = 4;
const TIME_COLOR: [number, number, number, number] = [0.66, 0.66, 0.66, 1];
// menu 0's screen: the crafting grid over the inventory, and any slots mods added. the recipes live on the server,
// which fills in the result
// menu 0's screen: the crafting grid over the inventory, and any slots mods added, with the player's effects above.
// the recipes live on the server, which fills in the result
export class GuiPlayerInventory extends GuiMenuScreen {
constructor(inventories: ClientInventories, send: (message: ClientMessage) => void) {
super(inventories, send, player_menu(inventories), WIDTH, HEIGHT);
#player: LocalPlayer;
constructor(player: LocalPlayer, send: (message: ClientMessage) => void) {
super(player.inventories, send, player_menu(player.inventories), WIDTH, HEIGHT);
this.#player = player;
}
render_background(g: ModGraphics): void {
g.panel(0, 0, WIDTH, HEIGHT);
this.#render_effects(g);
// the slots mods added outside the panel get one of their own, around them with the same margin
const outside = this.menu.slots.slice(PLAYER_MENU_EXTRA)
.filter((s) => s.x + SLOT < 0 || s.x > WIDTH || s.y + SLOT < 0 || s.y > HEIGHT);
@@ -31,8 +44,44 @@ export class GuiPlayerInventory extends GuiMenuScreen {
}
}
// the ones with an icon, left to right in the order they were added. hovering one says what it is
#render_effects(g: ModGraphics) {
const shown = this.#player.effects.flatMap((effect) => {
const icon = EverythingRegistry.get<EffectJson>("effects", effect.id)?.icon;
return icon ? [{ ...effect, icon }] : [];
});
if (shown.length === 0) return;
const width = shown.length * (ICON + ICON_GAP) - ICON_GAP + ICON_MARGIN * 2;
const height = ICON + ICON_MARGIN * 2;
const top = -height - ICON_GAP;
g.panel(0, top, width, height);
const mouse = g.mouse;
shown.forEach((effect, i) => {
const x = ICON_MARGIN + i * (ICON + ICON_GAP);
const y = top + ICON_MARGIN;
g.texture(effect.icon, x, y, ICON, ICON);
if (mouse.x >= x && mouse.x < x + ICON && mouse.y >= y && mouse.y < y + ICON) {
const name = display_name(effect.id) + level(effect.amplifier);
g.tooltip([name, { text: time_left(effect.duration), color: TIME_COLOR }], mouse.x, mouse.y);
}
});
}
render_labels(g: ModGraphics): void {
g.text(this.menu.title, 26, 6);
g.text("Inventory", 8, 73);
}
}
// like minecraft: nothing for level 1, then II, III...
function level(amplifier: number) {
const numerals = ["", " II", " III", " IV", " V", " VI", " VII", " VIII", " IX", " X"];
return numerals[amplifier] ?? ` ${amplifier + 1}`;
}
// minutes and seconds, like 1:05
function time_left(ticks: number) {
if (ticks < 0) return "Forever";
const seconds = Math.ceil(ticks / TICKS_PER_SECOND);
return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
}
+3
View File
@@ -84,6 +84,9 @@ export class ClientPacketListener {
case "attack_speed":
client.player.attack_speed = message.value;
break;
case "effects":
client.player.effects = message.effects;
break;
case "player_leave":
level.remove_entity(message.id);
break;