BlueGit

timfa questward

Files

questward / shared / appearance / layers.js

layers.js

layers.js 2.2 KiB Download Raw
1import { composeAppearanceLayers, bodySpriteName, appearanceHairFields, hairFromNameSeed } from "./hair.js";
2import { npcDefaultEquipment } from "./npcEquipment.js";
3import { mobDefinition, spawnerEquipmentSlot } from "../definitions/mobs.js";
4import { itemDefinition } from "../definitions/items.js";
5import { emptyEquipment } from "../items/inventory.js";
6
7export { bodySpriteName } from "./hair.js";
8
9/** Build public draw layers from sex + bag equipment + hair (no ECS). */
10export function layersFromBag (sex, bag, hair = null)
11{
12 const appearance = {
13 sex,
14 sprite: bodySpriteName(sex),
15 hairStyle: hair?.style ?? null,
16 hairHue: hair?.hue,
17 hairSaturation: hair?.saturation,
18 hairDarkness: hair?.darkness,
19 hairColor: hair?.color ?? null
20 };
21 return composeAppearanceLayers(appearance, bag?.equipment?.slots ?? {});
22}
23
24/**
25 * Editor / preview layers for a humanoid-style spawner (body + authored/default gear).
26 * Returns null when the mob type does not use a player body.
27 */
28export function layersFromSpawnerConfig (config)
29{
30 const mobTypeId = config?.mobTypeId;
31 const def = mobDefinition(mobTypeId);
32 if (!def?.usePlayerBody)
33 { return null; }
34 const sex = config?.sex === "female" ? "female" : "male";
35 const displayName = String(config?.displayName ?? def.displayName ?? "npc").trim() || "npc";
36 const hair = hairFromNameSeed(displayName, sex);
37 const appearance = {
38 sex,
39 sprite: bodySpriteName(sex),
40 ...appearanceHairFields(hair)
41 };
42 const equipmentSlots = emptyEquipment().slots;
43 const authored = config?.equipment ?? {};
44 const defaults = npcDefaultEquipment(displayName, sex);
45 for (const slot of Object.keys(equipmentSlots))
46 {
47 let { itemId, dyeColor } = spawnerEquipmentSlot(authored[slot]);
48 if (!itemId && defaults?.[slot])
49 {
50 itemId = defaults[slot].itemId;
51 dyeColor = defaults[slot].dyeColor ?? null;
52 }
53 if (!itemId)
54 { continue; }
55 const itemDef = itemDefinition(itemId);
56 if (!itemDef?.equipment || itemDef.equipment.slot !== slot)
57 { continue; }
58 const stack = { itemId, quantity: 1 };
59 if (dyeColor && itemDef.dyeable)
60 { stack.instance = { kind: "clothing", dyeColor }; }
61 equipmentSlots[slot] = stack;
62 }
63 return composeAppearanceLayers(appearance, equipmentSlots);
64}