| 1 | import { Component } from "./components.js"; |
| 2 | import { EQUIPMENT_DRAW_ORDER, equippedLightRadiusTiles, itemEquipSprite } from "../../../shared/definitions/items.js"; |
| 3 | import { resolveEquipTint } from "../../../shared/render/tint.js"; |
| 4 | import { spriteImageName } from "../../../shared/definitions/tileSprites.js"; |
| 5 | import { mobDefinition } from "../../../shared/definitions/mobs.js"; |
| 6 | import { mobCanFight } from "../../../shared/definitions/mobCombat.js"; |
| 7 | import { shopHasInterface } from "../shop/service.js"; |
| 8 | |
| 9 | function bodySprite (sex) |
| 10 | { |
| 11 | const key = sex === "female" ? "player-body-female" : "player-body-male"; |
| 12 | return spriteImageName("character", key, key); |
| 13 | } |
| 14 | |
| 15 | export function normalizeMobileComponents (components) |
| 16 | { |
| 17 | const next = { ...(components ?? {}) }; |
| 18 | if (next.movement && Array.isArray(next.movement.path) && !next.path) |
| 19 | { |
| 20 | const { path, ...movement } = next.movement; |
| 21 | next.movement = movement; |
| 22 | next.path = { tiles: path }; |
| 23 | } |
| 24 | if (next.movement && !next.path) |
| 25 | { next.path = { tiles: [] }; } |
| 26 | if (next.character && Object.prototype.hasOwnProperty.call(next.character, "sessionId")) |
| 27 | { |
| 28 | const { sessionId, ...character } = next.character; |
| 29 | next.character = character; |
| 30 | if (sessionId != null) |
| 31 | { next.networkIdentity = { ...(next.networkIdentity ?? {}), sessionId }; } |
| 32 | } |
| 33 | return next; |
| 34 | } |
| 35 | |
| 36 | export function serializeEntity (ecs, entityId) |
| 37 | { |
| 38 | const handle = ecs.handleOf(entityId); |
| 39 | if (handle == null) |
| 40 | { return null; } |
| 41 | return { |
| 42 | entityId, |
| 43 | components: structuredClone(ecs.componentsOf(handle)) |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | export function deserializeEntity (ecs, payload) |
| 48 | { |
| 49 | const handle = ecs.spawn(payload.entityId); |
| 50 | const components = normalizeMobileComponents(payload.components); |
| 51 | for (const [name, data] of Object.entries(components)) |
| 52 | { ecs.add(handle, name, structuredClone(data)); } |
| 53 | return payload.entityId; |
| 54 | } |
| 55 | |
| 56 | export function publicEntityView (ecs, entityId) |
| 57 | { |
| 58 | const handle = ecs.handleOf(entityId); |
| 59 | if (handle == null) |
| 60 | { return null; } |
| 61 | const position = ecs.get(handle, Component.position); |
| 62 | if (!position) |
| 63 | { return null; } |
| 64 | const movement = ecs.get(handle, Component.movement); |
| 65 | const appearance = ecs.get(handle, Component.appearance); |
| 66 | const character = ecs.get(handle, Component.character); |
| 67 | const creature = ecs.get(handle, Component.creature); |
| 68 | const flight = ecs.get(handle, Component.flight); |
| 69 | const health = ecs.get(handle, Component.health); |
| 70 | const equipment = ecs.get(handle, Component.equipment); |
| 71 | const layers = appearanceLayers(ecs, handle, appearance); |
| 72 | const mob = creature ? mobDefinition(creature.mobTypeId) : null; |
| 73 | const isMob = !!creature && !character; |
| 74 | return { |
| 75 | entityId, |
| 76 | tileX: position.tileX, |
| 77 | tileY: position.tileY, |
| 78 | worldX: flight?.x ?? position.tileX + 0.5, |
| 79 | worldY: flight?.y ?? position.tileY + 0.5, |
| 80 | gait: movement?.gait ?? "walk", |
| 81 | sex: appearance?.sex ?? null, |
| 82 | firstName: appearance?.firstName ?? "", |
| 83 | lastName: appearance?.lastName ?? "", |
| 84 | characterId: character?.characterId ?? null, |
| 85 | mobTypeId: creature?.mobTypeId ?? null, |
| 86 | canFight: mob ? mobCanFight(mob) : !!character, |
| 87 | hostile: !!mob?.hostile, |
| 88 | talkable: !!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop)), |
| 89 | hasShop: shopHasInterface(ecs.get(handle, Component.shop)), |
| 90 | untargetable: !!mob?.untargetable, |
| 91 | freeFlight: !!mob?.freeFlight, |
| 92 | health: health ? health.current : undefined, |
| 93 | maxHealth: health ? health.max : undefined, |
| 94 | sprite: layers[0]?.sprite ?? null, |
| 95 | layers, |
| 96 | lightRadiusTiles: equippedLightRadiusTiles(equipment) |
| 97 | }; |
| 98 | } |
| 99 | |
| 100 | export function appearanceLayers (ecs, handle, appearance) |
| 101 | { |
| 102 | const sprite = appearance?.sprite ?? bodySprite(appearance?.sex); |
| 103 | const layers = [{ sprite }]; |
| 104 | const equipment = ecs.get(handle, Component.equipment); |
| 105 | for (const slot of EQUIPMENT_DRAW_ORDER) |
| 106 | { |
| 107 | const worn = equipment?.slots?.[slot]; |
| 108 | if (!worn) |
| 109 | { continue; } |
| 110 | const overlay = itemEquipSprite(worn.itemId); |
| 111 | if (overlay) |
| 112 | { |
| 113 | const tint = resolveEquipTint(worn.itemId, worn.instance); |
| 114 | layers.push({ |
| 115 | sprite: overlay, |
| 116 | itemId: worn.itemId, |
| 117 | ...(tint ? { tint } : {}) |
| 118 | }); |
| 119 | } |
| 120 | } |
| 121 | return layers; |
| 122 | } |
| 123 | |
| 124 | export function replicationSpawn (view) |
| 125 | { |
| 126 | if (!view) |
| 127 | { return null; } |
| 128 | return { |
| 129 | type: "entity-spawn", |
| 130 | entityId: view.entityId, |
| 131 | characterId: view.characterId, |
| 132 | tileX: view.tileX, |
| 133 | tileY: view.tileY, |
| 134 | worldX: view.worldX, |
| 135 | worldY: view.worldY, |
| 136 | gait: view.gait, |
| 137 | sex: view.sex, |
| 138 | firstName: view.firstName, |
| 139 | lastName: view.lastName, |
| 140 | sprite: view.sprite ?? null, |
| 141 | layers: view.layers ?? [], |
| 142 | mobTypeId: view.mobTypeId ?? null, |
| 143 | canFight: !!view.canFight, |
| 144 | hostile: !!view.hostile, |
| 145 | talkable: !!view.talkable, |
| 146 | hasShop: !!view.hasShop, |
| 147 | untargetable: !!view.untargetable, |
| 148 | freeFlight: !!view.freeFlight, |
| 149 | health: view.health, |
| 150 | maxHealth: view.maxHealth |
| 151 | }; |
| 152 | } |
| 153 | |
| 154 | export function replicationMove (view, fromTileX, fromTileY) |
| 155 | { |
| 156 | if (!view) |
| 157 | { return null; } |
| 158 | return { |
| 159 | type: "entity-move", |
| 160 | entityId: view.entityId, |
| 161 | tileX: view.tileX, |
| 162 | tileY: view.tileY, |
| 163 | worldX: view.worldX, |
| 164 | worldY: view.worldY, |
| 165 | gait: view.gait, |
| 166 | fromTileX, |
| 167 | fromTileY, |
| 168 | freeFlight: !!view.freeFlight |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | export function replicationAppearance (view) |
| 173 | { |
| 174 | if (!view) |
| 175 | { return null; } |
| 176 | return { |
| 177 | type: "entity-appearance", |
| 178 | entityId: view.entityId, |
| 179 | tileX: view.tileX, |
| 180 | tileY: view.tileY, |
| 181 | sprite: view.sprite ?? null, |
| 182 | layers: view.layers ?? [], |
| 183 | lightRadiusTiles: view.lightRadiusTiles ?? 0 |
| 184 | }; |
| 185 | } |
| 186 | |
| 187 | export function replicationDespawn (view) |
| 188 | { |
| 189 | if (!view) |
| 190 | { return null; } |
| 191 | return { |
| 192 | type: "entity-despawn", |
| 193 | entityId: view.entityId, |
| 194 | tileX: view.tileX, |
| 195 | tileY: view.tileY |
| 196 | }; |
| 197 | } |