Show encumbrance on HUD and document modular creator direction

This commit is contained in:
dingo
2026-09-12 08:53:32 -04:00
parent faf2ab3040
commit 75f71d5ded
9 changed files with 218 additions and 5 deletions
+4 -3
View File
@@ -2,7 +2,7 @@ import type { Character } from './generated';
import {itemControls,chooseItemTarget} from './item-actions';
import { itemIcon } from './item-icon';
import { experienceText, permanentTitles } from './progression';
import { equipmentSlots, itemDetails, modifiersView, node, numberText, primaryStats, statLabel, statsBreakdown } from './character-stats';
import { carryStatus, equipmentSlots, itemDetails, modifiersView, node, numberText, primaryStats, statLabel, statsBreakdown } from './character-stats';
type Send = (op:string,target?:string,x?:number,z?:number,slot?:string)=>void;
const root=node('section','','character-panel'),resources=node('p','','character-resources'),body=node('div');
root.append(resources,body);
@@ -12,7 +12,7 @@ function active(){return latest.entity.state==='alive'&&latest.remainingSeconds>
function button(label:string,key:string,fn:()=>void,disabled=false){const b=node('button',label);b.type='button';b.dataset.focusKey=key;b.disabled=disabled;b.onclick=fn;return b;}
function request(op:string,target:string,slot=''){sendIntent(op,target,0,0,slot);}
function render(){
const next=JSON.stringify([selected,selectedItem,latest.entity.name,latest.level,latest.experience,latest.experienceToNextLevel,latest.titles,latest.entity.state,latest.remainingSeconds>0,latest.statPoints,latest.pendingVitalChoices,latest.allocatedStats,latest.stats,latest.items,latest.equipment,latest.inventory,latest.stackNames,latest.craftedStacks,latest.carryWeight,latest.gathering,latest.gatheringEffects,latest.consumables,latest.crafting,latest.weapons,latest.powers]);
const next=JSON.stringify([selected,selectedItem,latest.entity.name,latest.level,latest.experience,latest.experienceToNextLevel,latest.titles,latest.entity.state,latest.remainingSeconds>0,latest.statPoints,latest.pendingVitalChoices,latest.allocatedStats,latest.stats,latest.items,latest.equipment,latest.inventory,latest.stackNames,latest.craftedStacks,latest.carryWeight,latest.movementFactor,latest.gathering,latest.gatheringEffects,latest.consumables,latest.crafting,latest.weapons,latest.powers]);
if(next===signature)return;signature=next;
const focus=root.contains(document.activeElement)?(document.activeElement as HTMLElement).dataset.focusKey:undefined;
const opened=new Set(Array.from(body.querySelectorAll<HTMLDetailsElement>('details[open]')).map(d=>d.dataset.key));body.replaceChildren();
@@ -30,7 +30,8 @@ function render(){
const effects=node('details','','stat-inspection');effects.dataset.key='equipment-effects';effects.append(node('summary','Equipment modifiers'));
for(const item of c.items??[])if(Object.values(c.equipment??{}).includes(item.id))effects.append(node('h3',item.name),modifiersView(item.modifiers));body.append(effects);
} else {
body.append(node('p',`Carry weight ${numberText(c.carryWeight)} / ${numberText(c.stats?.carryCapacity?.final)} · Movement ${Math.round((c.movementFactor??1)*100)}%${c.movementFactor===0?' · Drop or store items to move again':''}`,'progression-note'));
const carry = carryStatus(c);
body.append(node('p',`${carry.weight} · ${carry.movement}. ${carry.help}`,'progression-note'));
const held=new Set(Object.values(c.equipment??{})),grid=node('div','','bag-grid'),inspection=node('div','','bag-inspection');
grid.setAttribute('aria-label','Bag items');
function tile(id:string,name:string,count:number,category:string){const b=button('',`item-${id}`,()=>{if(chooseItemTarget(id))return;selectedItem=selectedItem===id?'':id;signature='';render()});b.className='bag-slot';b.title=name;b.setAttribute('aria-label',`${name} × ${count}`);b.setAttribute('aria-pressed',String(selectedItem===id));b.append(itemIcon(category),node('span',name,'bag-name'),node('b',String(count),'bag-count'));grid.append(b);return selectedItem===id;}
+14 -1
View File
@@ -9,6 +9,18 @@ const labels: Record<string,string> = { meleeDamage:'Close combat weapons damage
export const statLabel = (key:string) => labels[key] ?? key.replace(/([a-z])([A-Z0-9])/g,'$1 $2').replaceAll('_',' ').replace(/^./,c=>c.toUpperCase());
export const numberText = (value:number|undefined) => Number.isFinite(value) ? new Intl.NumberFormat(undefined,{maximumFractionDigits:2}).format(value!) : '—';
export const signed = (value:number) => `${value>0?'+':''}${numberText(value)}`;
export function carryStatus(c: Character) {
const factor = c.movementFactor;
const impaired = factor !== undefined && factor < 1;
return {
impaired,
weight: `Carry weight ${numberText(c.carryWeight)} / ${numberText(c.stats?.carryCapacity?.final)}`,
movement: factor === 0 ? 'Overburdened · Cannot move' : impaired
? `Encumbered · Movement ${numberText(factor * 100)}%`
: factor === undefined ? 'Movement unavailable' : 'Full movement',
help: factor === 0 ? 'Drop or store items to move again.' : impaired ? 'Reduce carried weight to restore movement speed.' : '',
};
}
export function node<K extends keyof HTMLElementTagNameMap>(tag:K,text='',className='') { const e=document.createElement(tag);e.textContent=text;e.className=className;return e; }
export function modifiersView(modifiers:StatModifier[] = []) {
const list=node('div','','title-effects');
@@ -43,7 +55,8 @@ export function itemDetails(item:ItemInstance,c:Character) {
}
export function equipmentDetails(c:Character) {
const root=node('div','','gear-list');
root.append(node('p',`Mana ${numberText(c.mana)} / ${numberText(c.stats?.manaPoints?.final)} · Stamina ${numberText(c.stamina)} / ${numberText(c.stats?.stamina?.final)} · Carry weight ${numberText(c.carryWeight)} / ${numberText(c.stats?.carryCapacity?.final)} (advisory)`));
const carry = carryStatus(c);
root.append(node('p',`Mana ${numberText(c.mana)} / ${numberText(c.stats?.manaPoints?.final)} · Stamina ${numberText(c.stamina)} / ${numberText(c.stats?.stamina?.final)} · ${carry.weight} · ${carry.movement}. ${carry.help}`));
for(const slot of equipmentSlots){const item=c.items?.find(i=>i.id===c.equipment?.[slot]);root.append(node('p',`${statLabel(slot)}: ${item?.name??'Empty'}`));}
for(const item of c.items??[]){const card=itemDetails(item,c);card.append(node('small',`Instance ${item.id}`));root.append(card);}
return root;
+14
View File
@@ -14,6 +14,7 @@ import type { BuildingContent, Character, Chunk, Entity, Intent, Snapshot } from
import { experienceText, statPointsText, updateExperienceMeter } from './progression';
import { InputActions, isEditing, type InputAction } from './input-actions';
import { hasOpenMenu, registerMenu } from './menu-focus';
import { carryStatus } from './character-stats';
type Config = { worldId: string; kind: string; development: boolean; worlds: { id: string; url: string }[]; playerSpeed: number; gatherRange: number; attackRange: number };
const $ = <T extends HTMLElement = HTMLElement>(id: string) => document.getElementById(id) as T;
@@ -204,6 +205,13 @@ function updateHUD(s: Snapshot) {
$('character-experience').textContent = experienceText(c);
updateExperienceMeter($<HTMLProgressElement>('experience-meter'), c);
$('stat-point-bank').textContent = statPointsText(c);
const carry = carryStatus(c);
$('carry-weight').textContent = carry.weight;
// Keep the explanation visible even when the bag is closed. The server owns
// the movement factor; do not reproduce encumbrance tuning in the browser.
if ($('carry-movement').textContent !== carry.movement) $('carry-movement').textContent = carry.movement;
$('carry-manage').classList.toggle('encumbered', carry.impaired);
$('carry-manage').title = `${carry.help} Open bag to manage carried items.`.trim();
const titleCount = c.titles?.length ?? 0;
$('title-count').textContent = `${titleCount} title${titleCount === 1 ? '' : 's'}`;
$('health-number').textContent = `${Math.max(0, Math.ceil(c.entity.hp))} / ${Math.ceil(c.entity.maxHp)}`;
@@ -232,6 +240,12 @@ function updateHUD(s: Snapshot) {
if (s.message && s.message !== lastNotice) { notice(s.message); lastNotice = s.message; }
}
async function boot() {
$('carry-manage').onclick = () => {
if ($('inventory').hidden) $('inventory-toggle').click();
const bag = $('inventory').querySelector<HTMLButtonElement>('[data-view="bag"]');
bag?.click();
bag?.focus();
};
$<HTMLAnchorElement>('login').href = `/auth/login?return=${encodeURIComponent(location.pathname + location.search)}`;
createIcons({ icons: { Crosshair, Settings2, X, Flame, LogOut, Hourglass, Swords, Pickaxe, HeartPulse, Gem, Backpack, ChevronUp, ChevronDown, ChevronLeft, ChevronRight, ArrowRight, RefreshCw } });
view = new WorldView($<HTMLCanvasElement>('world'));
+5
View File
@@ -10,6 +10,11 @@
@media(max-width:800px) and (min-height:521px){.movement:not([hidden]){bottom:255px}.inventory{bottom:258px;max-height:calc(100dvh - 350px)}.notice{bottom:375px}}
@media(max-height:520px){.movement:not([hidden]){bottom:178px}}
.carry-manage{display:grid;gap:3px;width:100%;margin-top:10px;padding:7px 8px;text-align:left;justify-content:start;font-size:11px;line-height:1.3}
.carry-manage small{color:#cbd7c2;font-size:10px}
.carry-manage.encumbered{color:#ffdc9b;background:#45321e;border-color:#c39152}
@media(max-width:800px) and (min-height:521px){.movement:not([hidden]){bottom:335px}}
@media(max-height:520px){.movement:not([hidden]){bottom:258px}.carry-manage{padding:4px 6px;margin-top:6px}}
#target-health{white-space:normal;line-height:1.5;max-width:min(550px,80vw)}
.crafting-workbench{display:grid;gap:12px;font-size:12px;line-height:1.5}.crafting-workbench h3{margin:8px 0 0}.crafting-workbench form{display:grid;gap:8px}.crafting-workbench .recipe{display:grid;grid-template-columns:minmax(0,1fr) 68px;gap:6px}.crafting-workbench .recipe button{grid-column:1/-1;justify-self:end}.crafting-workbench details{border-top:1px solid var(--progression-border);padding:10px 0;overflow-wrap:anywhere}.crafting-workbench summary{cursor:pointer}.crafting-workbench details p{margin:8px 0}.crafting-workbench [role=status]{color:#d6e4b6}