Hud api
This commit is contained in:
+85
-2
@@ -5,7 +5,16 @@
|
||||
// see docs/modding/networking/index.md
|
||||
import { AIR } from "@/common/constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts";
|
||||
import type { ClientContext, ClientMenu, InventoryOverlay, KeyMappings, MenuScreen } from "@/common/mod_api/client.ts";
|
||||
import type {
|
||||
ClientContext,
|
||||
ClientMenu,
|
||||
Graphics,
|
||||
HudLayer,
|
||||
HudSpace,
|
||||
InventoryOverlay,
|
||||
KeyMappings,
|
||||
MenuScreen,
|
||||
} from "@/common/mod_api/client.ts";
|
||||
import {
|
||||
check_recipe_types,
|
||||
ModListing,
|
||||
@@ -33,6 +42,7 @@ import { common_engine } from "@/common/engine_modules.ts";
|
||||
import { client_engine } from "./engine_modules.ts";
|
||||
import { block_visuals } from "./rendering/visuals.ts";
|
||||
import { biome_season } from "@/common/worldgen/biomes.ts";
|
||||
import { clock, farming_day } from "@/common/time.ts";
|
||||
import type { LootTableJson } from "@/common/loot.ts";
|
||||
|
||||
// each loaded mod's credits file, for the credits screen
|
||||
@@ -47,6 +57,27 @@ export const inventory_overlays: { mod: string; overlay: InventoryOverlay }[] =
|
||||
// each menu type's screen, see ctx.ui.register_menu_screen
|
||||
export const menu_screens = new Map<string, { mod: string; create: (menu: ClientMenu) => MenuScreen }>();
|
||||
|
||||
// what mods draw over the world, in order, see ctx.hud and client/gui/hud.ts
|
||||
export const hud_layers: { mod: string; id: string; layer: HudLayer }[] = [];
|
||||
|
||||
// draws every hud layer in order, all raising the same space. one that throws is removed so it doesn't throw every frame
|
||||
export function render_hud_layers(g: Graphics, space: HudSpace) {
|
||||
for (const entry of [...hud_layers]) {
|
||||
try {
|
||||
entry.layer.on_render(g, space);
|
||||
} catch (e) {
|
||||
console.error(`[${entry.mod}] hud layer ${entry.id} threw, so it's removed:`, e);
|
||||
hud_layers.splice(hud_layers.indexOf(entry), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
const overlay_listeners: { mod: string; fn: (text: string) => void }[] = [];
|
||||
|
||||
// the server sent an overlay message, see MessageOptions.overlay
|
||||
export function receive_overlay_message(text: string) {
|
||||
for (const { mod, fn } of overlay_listeners) run_mod(mod, "an overlay message listener", () => fn(text));
|
||||
}
|
||||
|
||||
// mod channels, see ctx.net: who listens on each channel
|
||||
const net_handlers = new Map<string, { mod: string; handler: (data: unknown) => void }[]>();
|
||||
|
||||
@@ -166,6 +197,8 @@ export async function load_client_mods(listings: ModListing[], mods: Mod[]) {
|
||||
const recipes = register_mod_data(mods.map((mod) => ({ id: mod.manifest.id, data: mod.data })));
|
||||
recipe_book = recipes;
|
||||
inventory_overlays.length = 0;
|
||||
hud_layers.length = 0;
|
||||
overlay_listeners.length = 0;
|
||||
item_tooltips.length = 0;
|
||||
menu_screens.clear();
|
||||
net_handlers.clear();
|
||||
@@ -316,7 +349,21 @@ function client_context(listing: ModListing): ClientContext {
|
||||
item_tooltips.push((item, lines) => run_mod(mod, "an item tooltip", () => fn(item, lines)));
|
||||
},
|
||||
},
|
||||
hud: not_yet("hud", "HUD elements") as ClientContext["hud"],
|
||||
hud: {
|
||||
register(id, layer) {
|
||||
if (!client_setup) throw new Error(`[${mod}] hud layers can only be registered during setup`);
|
||||
own(id, "hud layer");
|
||||
if (typeof layer?.on_render !== "function") throw new Error(`[${mod}] a hud layer needs an on_render`);
|
||||
if (hud_layers.some((other) => other.id === id)) {
|
||||
throw new Error(`[${mod}] hud layer ${id} already exists`);
|
||||
}
|
||||
hud_layers.push({ mod, id, layer });
|
||||
},
|
||||
on_overlay_message(fn) {
|
||||
if (typeof fn !== "function") throw new Error(`[${mod}] on_overlay_message takes a function`);
|
||||
overlay_listeners.push({ mod, fn });
|
||||
},
|
||||
},
|
||||
input: not_yet("input", "keybinds") as ClientContext["input"],
|
||||
options: key_mappings(need_client),
|
||||
net: {
|
||||
@@ -349,6 +396,35 @@ function client_context(listing: ModListing): ClientContext {
|
||||
},
|
||||
is_swimming: () => need_client().player.swimming,
|
||||
is_sneaking: () => need_client().player.holding_sneak,
|
||||
get health() {
|
||||
return need_client().player.health;
|
||||
},
|
||||
get max_health() {
|
||||
return need_client().player.max_health;
|
||||
},
|
||||
get food() {
|
||||
return need_client().player.food;
|
||||
},
|
||||
get saturation() {
|
||||
return need_client().player.saturation;
|
||||
},
|
||||
get air() {
|
||||
return need_client().player.air;
|
||||
},
|
||||
is_underwater: () => need_client().player.eyes_in_water,
|
||||
attack_strength: () => need_client().player.attack_strength(),
|
||||
inventory: {
|
||||
get size() {
|
||||
return need_client().player.inventories.inventory.size;
|
||||
},
|
||||
get(slot) {
|
||||
const item = need_client().player.inventories.inventory.get_item(slot);
|
||||
return item && structuredClone(item.to_data());
|
||||
},
|
||||
},
|
||||
get selected_slot() {
|
||||
return need_client().player.inventories.hotbar_selected;
|
||||
},
|
||||
},
|
||||
world: {
|
||||
get_block(x, y, z) {
|
||||
@@ -369,6 +445,13 @@ function client_context(listing: ModListing): ClientContext {
|
||||
get time() {
|
||||
return need_client().level.time;
|
||||
},
|
||||
get day() {
|
||||
return farming_day(need_client().level.time);
|
||||
},
|
||||
get hour() {
|
||||
const { hour, minute } = clock(need_client().level.time);
|
||||
return hour + minute / 60;
|
||||
},
|
||||
},
|
||||
items: {
|
||||
...shared_items_api(mod, fluid_rules),
|
||||
|
||||
Reference in New Issue
Block a user