import { Component } from "./components.js"; import { EQUIPMENT_DRAW_ORDER, equippedLightRadiusTiles, itemEquipSprite } from "../../../shared/definitions/items.js"; import { resolveEquipTint } from "../../../shared/render/tint.js"; import { spriteImageName } from "../../../shared/definitions/tileSprites.js"; import { mobDefinition } from "../../../shared/definitions/mobs.js"; import { mobCanFight } from "../../../shared/definitions/mobCombat.js"; import { shopHasInterface } from "../shop/service.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 creature = ecs.get(handle, Component.creature); const flight = ecs.get(handle, Component.flight); const health = ecs.get(handle, Component.health); const equipment = ecs.get(handle, Component.equipment); const layers = appearanceLayers(ecs, handle, appearance); const mob = creature ? mobDefinition(creature.mobTypeId) : null; const isMob = !!creature && !character; return { entityId, tileX: position.tileX, tileY: position.tileY, worldX: flight?.x ?? position.tileX + 0.5, worldY: flight?.y ?? position.tileY + 0.5, gait: movement?.gait ?? "walk", sex: appearance?.sex ?? null, firstName: appearance?.firstName ?? "", lastName: appearance?.lastName ?? "", characterId: character?.characterId ?? null, mobTypeId: creature?.mobTypeId ?? null, canFight: mob ? mobCanFight(mob) : !!character, hostile: !!mob?.hostile, talkable: !!mob?.talkable || shopHasInterface(ecs.get(handle, Component.shop)), hasShop: shopHasInterface(ecs.get(handle, Component.shop)), untargetable: !!mob?.untargetable, freeFlight: !!mob?.freeFlight, health: health ? health.current : undefined, maxHealth: health ? health.max : undefined, sprite: layers[0]?.sprite ?? null, layers, lightRadiusTiles: equippedLightRadiusTiles(equipment) }; } 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) { const tint = resolveEquipTint(worn.itemId, worn.instance); layers.push({ sprite: overlay, itemId: worn.itemId, ...(tint ? { tint } : {}) }); } } 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, worldX: view.worldX, worldY: view.worldY, gait: view.gait, sex: view.sex, firstName: view.firstName, lastName: view.lastName, sprite: view.sprite ?? null, layers: view.layers ?? [], mobTypeId: view.mobTypeId ?? null, canFight: !!view.canFight, hostile: !!view.hostile, talkable: !!view.talkable, hasShop: !!view.hasShop, untargetable: !!view.untargetable, freeFlight: !!view.freeFlight, health: view.health, maxHealth: view.maxHealth }; } export function replicationMove (view, fromTileX, fromTileY) { if (!view) { return null; } return { type: "entity-move", entityId: view.entityId, tileX: view.tileX, tileY: view.tileY, worldX: view.worldX, worldY: view.worldY, gait: view.gait, fromTileX, fromTileY, freeFlight: !!view.freeFlight }; } 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 ?? [], lightRadiusTiles: view.lightRadiusTiles ?? 0 }; } export function replicationDespawn (view) { if (!view) { return null; } return { type: "entity-despawn", entityId: view.entityId, tileX: view.tileX, tileY: view.tileY }; }