import { Component } from "./components.js"; import { EQUIPMENT_DRAW_ORDER, itemEquipSprite } from "../../../shared/definitions/items.js"; import { spriteImageName } from "../../../shared/definitions/tileSprites.js"; function bodySprite (sex) { const key = sex === "female" ? "player-body-female" : "player-body-male"; return spriteImageName("character", key, key); } export function normalizeMobileComponents (components) { const next = { ...(components ?? {}) }; if (next.movement && Array.isArray(next.movement.path) && !next.path) { const { path, ...movement } = next.movement; next.movement = movement; next.path = { tiles: path }; } if (next.movement && !next.path) { next.path = { tiles: [] }; } if (next.character && Object.prototype.hasOwnProperty.call(next.character, "sessionId")) { const { sessionId, ...character } = next.character; next.character = character; if (sessionId != null) { next.networkIdentity = { ...(next.networkIdentity ?? {}), sessionId }; } } return next; } export function serializeEntity (ecs, entityId) { const handle = ecs.handleOf(entityId); if (handle == null) { return null; } return { entityId, components: structuredClone(ecs.componentsOf(handle)) }; } export function deserializeEntity (ecs, payload) { const handle = ecs.spawn(payload.entityId); const components = normalizeMobileComponents(payload.components); for (const [name, data] of Object.entries(components)) { ecs.add(handle, name, structuredClone(data)); } return payload.entityId; } export function publicEntityView (ecs, entityId) { const handle = ecs.handleOf(entityId); if (handle == null) { return null; } const position = ecs.get(handle, Component.position); if (!position) { return null; } const movement = ecs.get(handle, Component.movement); const appearance = ecs.get(handle, Component.appearance); const character = ecs.get(handle, Component.character); const layers = appearanceLayers(ecs, handle, appearance); return { entityId, tileX: position.tileX, tileY: position.tileY, gait: movement?.gait ?? "walk", sex: appearance?.sex ?? null, firstName: appearance?.firstName ?? "", lastName: appearance?.lastName ?? "", characterId: character?.characterId ?? null, sprite: layers[0]?.sprite ?? null, layers }; } export function appearanceLayers (ecs, handle, appearance) { const sprite = appearance?.sprite ?? bodySprite(appearance?.sex); const layers = [{ sprite }]; const equipment = ecs.get(handle, Component.equipment); for (const slot of EQUIPMENT_DRAW_ORDER) { const worn = equipment?.slots?.[slot]; if (!worn) { continue; } const overlay = itemEquipSprite(worn.itemId); if (overlay) { layers.push({ sprite: overlay }); } } return layers; } export function replicationSpawn (view) { if (!view) { return null; } return { type: "entity-spawn", entityId: view.entityId, characterId: view.characterId, tileX: view.tileX, tileY: view.tileY, gait: view.gait, sex: view.sex, firstName: view.firstName, lastName: view.lastName, sprite: view.sprite ?? null, layers: view.layers ?? [] }; } export function replicationMove (view, fromTileX, fromTileY) { if (!view) { return null; } return { type: "entity-move", entityId: view.entityId, tileX: view.tileX, tileY: view.tileY, gait: view.gait, fromTileX, fromTileY }; } export function replicationAppearance (view) { if (!view) { return null; } return { type: "entity-appearance", entityId: view.entityId, tileX: view.tileX, tileY: view.tileY, sprite: view.sprite ?? null, layers: view.layers ?? [] }; } export function replicationDespawn (view) { if (!view) { return null; } return { type: "entity-despawn", entityId: view.entityId, tileX: view.tileX, tileY: view.tileY }; }