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;
+1
View File
@@ -241,6 +241,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
if ((effect_owners.get(json.id)?.length ?? 0) > 1) {
errors.push(`${file}: effect ${json.id} is also defined by ${effect_owners.get(json.id)!.join(", ")}`);
}
if (json.icon !== undefined) uses(file, json.icon, "texture");
}
for (const { file, json } of mod.spawns) {
check_namespace(file, json.entity);
+3
View File
@@ -140,6 +140,8 @@ export interface EffectJson {
category?: EffectCategory;
// happens once when added instead of lasting, like instant health. its components' on_tick runs once
instant?: boolean;
// a texture, shown at the top of the inventory screen while the player has it
icon?: string;
// attribute modifiers while it's on, for level 1. each level adds that much again, like minecraft's speed
modifiers?: { attribute: string; amount: number; operation: EffectModifierOperation }[];
components?: Record<string, unknown>;
@@ -809,6 +811,7 @@ export function validate_effect(json: unknown): Problems {
problems.push('category must be "beneficial", "harmful" or "neutral"');
}
if (json.instant !== undefined && typeof json.instant !== "boolean") problems.push("instant must be true or false");
if (json.icon !== undefined) problems.push(...validate_id(json.icon, "icon"));
if (json.modifiers !== undefined) {
if (!Array.isArray(json.modifiers)) {
problems.push("modifiers must be a list");
+3
View File
@@ -241,6 +241,9 @@ export type ServerMessage =
| { type: "vitals"; health: number; max_health: number; food: number; saturation: number; air: number }
// the player's own attack speed attribute, when it changes, for the attack indicator under the crosshair
| { type: "attack_speed"; value: number }
// the player's own effects that show an icon, when they change. duration is in ticks, -1 for forever, and the
// client counts it down itself, so it isn't sent every tick
| { type: "effects"; effects: { id: string; amplifier: number; duration: number }[] }
// a mod's server script talking to its client script, see ctx.net
| { type: "mod"; channel: string; data: unknown };
+9 -7
View File
@@ -17,18 +17,20 @@ game's effects are defined this way, in `mods/bworld/effects`.
"effect": {
"id": "copper_tools:swiftness",
"category": "beneficial",
"icon": "copper_tools:swiftness",
"modifiers": [{ "attribute": "bworld:movement_speed", "amount": 0.1, "operation": "add_multiplied_total" }]
}
}
```
| Field | Required | What it does |
| ------------ | -------- | ---------------------------------------------------------------------------------------------- |
| `id` | yes | The effect's id, in the mod's [namespace](../concepts/registries.md#identifiers). |
| `category` | no | `"beneficial"`, `"harmful"` or `"neutral"` (the default), like NeoForge's `MobEffectCategory`. |
| `instant` | no | `true` makes it happen once when added instead of lasting, like `bworld:instant_health`. |
| `modifiers` | no | [Attribute modifiers](players.md#attributes) while it's on. Each level adds `amount` again. |
| `components` | no | What it does over time, see below. |
| Field | Required | What it does |
| ------------ | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | yes | The effect's id, in the mod's [namespace](../concepts/registries.md#identifiers). |
| `category` | no | `"beneficial"`, `"harmful"` or `"neutral"` (the default), like NeoForge's `MobEffectCategory`. |
| `instant` | no | `true` makes it happen once when added instead of lasting, like `bworld:instant_health`. |
| `icon` | no | A texture, shown at the top of the inventory screen while the player has it, with its name, level and time left on hover. 16×16 like an item's. |
| `modifiers` | no | [Attribute modifiers](players.md#attributes) while it's on. Each level adds `amount` again. |
| `components` | no | What it does over time, see below. |
The modifiers' `operation` is `"add_value"`, `"add_multiplied_base"` or `"add_multiplied_total"`. They come off when the
effect ends.
+6 -1
View File
@@ -3,6 +3,11 @@
"effect": {
"id": "bworld:poison",
"category": "harmful",
"components": { "bworld:poison": { "every": 25 } }
"icon": "bworld:poison",
"components": {
"bworld:poison": {
"every": 25
}
}
}
}
+6 -1
View File
@@ -3,6 +3,11 @@
"effect": {
"id": "bworld:regeneration",
"category": "beneficial",
"components": { "bworld:heal_over_time": { "every": 50 } }
"icon": "bworld:regeneration",
"components": {
"bworld:heal_over_time": {
"every": 50
}
}
}
}
+2 -1
View File
@@ -2,6 +2,7 @@
"format_version": 1,
"effect": {
"id": "bworld:resistance",
"category": "beneficial"
"category": "beneficial",
"icon": "bworld:resistance"
}
}
+8 -1
View File
@@ -3,6 +3,13 @@
"effect": {
"id": "bworld:slowness",
"category": "harmful",
"modifiers": [{ "attribute": "bworld:movement_speed", "amount": -0.15, "operation": "add_multiplied_total" }]
"icon": "bworld:slow",
"modifiers": [
{
"attribute": "bworld:movement_speed",
"amount": -0.15,
"operation": "add_multiplied_total"
}
]
}
}
+8 -1
View File
@@ -3,6 +3,13 @@
"effect": {
"id": "bworld:speed",
"category": "beneficial",
"modifiers": [{ "attribute": "bworld:movement_speed", "amount": 0.2, "operation": "add_multiplied_total" }]
"icon": "bworld:speed",
"modifiers": [
{
"attribute": "bworld:movement_speed",
"amount": 0.2,
"operation": "add_multiplied_total"
}
]
}
}
+2 -1
View File
@@ -2,6 +2,7 @@
"format_version": 1,
"effect": {
"id": "bworld:water_breathing",
"category": "beneficial"
"category": "beneficial",
"icon": "bworld:water_breathing"
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 118 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 232 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 210 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 B

+8
View File
@@ -1552,6 +1552,14 @@ export class GameServer {
);
const attack_speed = player.attribute(ATTRIBUTES.attack_speed);
sync("attack_speed", attack_speed, () => ({ type: "attack_speed", value: attack_speed }));
const effects = [...player.effects.values()]
.filter((effect) => effect.show_icon)
.map(({ type, amplifier, duration }) => ({ id: type.id, amplifier, duration }));
// by when they end, which stays the same while they count down
const ends = effects.map((
{ id, amplifier, duration },
) => [id, amplifier, duration < 0 ? -1 : this.#tick + duration]);
sync("effects", ends, () => ({ type: "effects", effects }));
const menu = player.menu;
if (menu) {
+13
View File
@@ -196,3 +196,16 @@ Deno.test("under water they run out of air, shown in bubbles, then drown", async
for (let i = 0; i < 300; i++) t.game.tick();
assert(t.player.health < MAX_HEALTH);
});
Deno.test("the effects that show an icon are sent to the player when they change", async () => {
const t = await setup();
const api = t.game.mods.player(t.player);
api.add_effect("bworld:speed", 100, { amplifier: 1 });
api.add_effect("bworld:poison", 100, { show_icon: false });
t.take(1);
t.game.tick();
const sent = t.take(1).filter((m: Msg) => m.type === "effects");
assertEquals(sent.at(-1).effects, [{ id: "bworld:speed", amplifier: 1, duration: 99 }]);
t.game.tick();
assert(!t.take(1).some((m: Msg) => m.type === "effects"), "not again while they only count down");
});