| 1 | /** |
| 2 | * Character-creation clothing: cotton shirt + chosen cotton legs, with HSL dyes. |
| 3 | * Applied on top of world-editor starter equipment (chest/legs overwrite). |
| 4 | */ |
| 5 | |
| 6 | import { ClothTier } from "../definitions/materialTiers.js"; |
| 7 | import { |
| 8 | shirtItemId, |
| 9 | shortsItemId, |
| 10 | skirtItemId, |
| 11 | trousersItemId |
| 12 | } from "../definitions/clothItems.js"; |
| 13 | import { |
| 14 | normalizeHairSliders, |
| 15 | hairColorFromSliders, |
| 16 | DEFAULT_HAIR_HUE, |
| 17 | DEFAULT_HAIR_SATURATION, |
| 18 | DEFAULT_HAIR_DARKNESS |
| 19 | } from "../appearance/hair.js"; |
| 20 | import { EQUIPMENT_SLOT } from "../definitions/items.js"; |
| 21 | import { emptyEquipment } from "./inventory.js"; |
| 22 | import { godsOrigin, attachOrigins } from "./origin.js"; |
| 23 | |
| 24 | export const CreationLegsKind = Object.freeze({ |
| 25 | shorts: "shorts", |
| 26 | skirt: "skirt", |
| 27 | trousers: "trousers" |
| 28 | }); |
| 29 | |
| 30 | export const CREATION_LEGS_KINDS = Object.freeze([ |
| 31 | CreationLegsKind.shorts, |
| 32 | CreationLegsKind.skirt, |
| 33 | CreationLegsKind.trousers |
| 34 | ]); |
| 35 | |
| 36 | const LEGS_ITEM_ID = Object.freeze({ |
| 37 | [CreationLegsKind.shorts]: () => shortsItemId(ClothTier.cotton), |
| 38 | [CreationLegsKind.skirt]: () => skirtItemId(ClothTier.cotton), |
| 39 | [CreationLegsKind.trousers]: () => trousersItemId(ClothTier.cotton) |
| 40 | }); |
| 41 | |
| 42 | export function defaultCreateClothing () |
| 43 | { |
| 44 | return Object.freeze({ |
| 45 | legsKind: CreationLegsKind.trousers, |
| 46 | shirt: Object.freeze({ |
| 47 | hue: DEFAULT_HAIR_HUE, |
| 48 | saturation: DEFAULT_HAIR_SATURATION, |
| 49 | darkness: DEFAULT_HAIR_DARKNESS |
| 50 | }), |
| 51 | legs: Object.freeze({ |
| 52 | hue: 210, |
| 53 | saturation: DEFAULT_HAIR_SATURATION, |
| 54 | darkness: DEFAULT_HAIR_DARKNESS |
| 55 | }) |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | function normalizePieceSliders (raw) |
| 60 | { |
| 61 | return normalizeHairSliders(raw ?? {}); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @returns {{ legsKind: string, shirt: object, legs: object }} |
| 66 | */ |
| 67 | export function normalizeCreateClothing (raw) |
| 68 | { |
| 69 | const defaults = defaultCreateClothing(); |
| 70 | const legsKind = CREATION_LEGS_KINDS.includes(raw?.legsKind) |
| 71 | ? raw.legsKind |
| 72 | : defaults.legsKind; |
| 73 | return { |
| 74 | legsKind, |
| 75 | shirt: normalizePieceSliders(raw?.shirt), |
| 76 | legs: normalizePieceSliders(raw?.legs) |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | export function creationLegsItemId (legsKind) |
| 81 | { |
| 82 | const resolve = LEGS_ITEM_ID[legsKind] ?? LEGS_ITEM_ID[CreationLegsKind.trousers]; |
| 83 | return resolve(); |
| 84 | } |
| 85 | |
| 86 | export function creationShirtItemId () |
| 87 | { |
| 88 | return shirtItemId(ClothTier.cotton); |
| 89 | } |
| 90 | |
| 91 | function dyedClothingStack (itemId, sliders, origin) |
| 92 | { |
| 93 | const dyeColor = hairColorFromSliders(sliders); |
| 94 | return attachOrigins({ |
| 95 | itemId, |
| 96 | quantity: 1, |
| 97 | instance: Object.freeze({ kind: "clothing", dyeColor }), |
| 98 | origins: [{ origin, quantity: 1 }] |
| 99 | }); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Build a preview bag (equipment only) for the creation clothing UI. |
| 104 | */ |
| 105 | export function creationClothingPreviewBag (clothingInput) |
| 106 | { |
| 107 | const clothing = normalizeCreateClothing(clothingInput); |
| 108 | const origin = godsOrigin(); |
| 109 | const equipment = emptyEquipment(); |
| 110 | equipment.slots[EQUIPMENT_SLOT.chest] = dyedClothingStack( |
| 111 | creationShirtItemId(), |
| 112 | clothing.shirt, |
| 113 | origin |
| 114 | ); |
| 115 | equipment.slots[EQUIPMENT_SLOT.legs] = dyedClothingStack( |
| 116 | creationLegsItemId(clothing.legsKind), |
| 117 | clothing.legs, |
| 118 | origin |
| 119 | ); |
| 120 | return { equipment }; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Overlay creation clothing onto an existing starter equipment object (mutates slots). |
| 125 | */ |
| 126 | export function applyCreationClothingToEquipment (equipment, clothingInput) |
| 127 | { |
| 128 | const clothing = normalizeCreateClothing(clothingInput); |
| 129 | const origin = godsOrigin(); |
| 130 | const slots = equipment?.slots ?? emptyEquipment().slots; |
| 131 | slots[EQUIPMENT_SLOT.chest] = dyedClothingStack( |
| 132 | creationShirtItemId(), |
| 133 | clothing.shirt, |
| 134 | origin |
| 135 | ); |
| 136 | slots[EQUIPMENT_SLOT.legs] = dyedClothingStack( |
| 137 | creationLegsItemId(clothing.legsKind), |
| 138 | clothing.legs, |
| 139 | origin |
| 140 | ); |
| 141 | return equipment; |
| 142 | } |