| 1 | import { Component } from "./components.js"; |
| 2 | import { EQUIPMENT_DRAW_ORDER, itemEquipSprite } from "../../../shared/definitions/items.js"; |
| 3 | import { spriteImageName } from "../../../shared/definitions/tileSprites.js"; |
| 4 | |
| 5 | function bodySprite (sex) |
| 6 | { |
| 7 | const key = sex === "female" ? "player-body-female" : "player-body-male"; |
| 8 | return spriteImageName("character", key, key); |
| 9 | } |
| 10 | |
| 11 | export function normalizeMobileComponents (components) |
| 12 | { |
| 13 | const next = { ...(components ?? {}) }; |
| 14 | if (next.movement && Array.isArray(next.movement.path) && !next.path) |
| 15 | { |
| 16 | const { path, ...movement } = next.movement; |
| 17 | next.movement = movement; |
| 18 | next.path = { tiles: path }; |
| 19 | } |
| 20 | if (next.movement && !next.path) |
| 21 | { next.path = { tiles: [] }; } |
| 22 | if (next.character && Object.prototype.hasOwnProperty.call(next.character, "sessionId")) |
| 23 | { |
| 24 | const { sessionId, ...character } = next.character; |
| 25 | next.character = character; |
| 26 | if (sessionId != null) |
| 27 | { next.networkIdentity = { ...(next.networkIdentity ?? {}), sessionId }; } |
| 28 | } |
| 29 | return next; |
| 30 | } |
| 31 | |
| 32 | export function serializeEntity (ecs, entityId) |
| 33 | { |
| 34 | const handle = ecs.handleOf(entityId); |
| 35 | if (handle == null) |
| 36 | { return null; } |
| 37 | return { |
| 38 | entityId, |
| 39 | components: structuredClone(ecs.componentsOf(handle)) |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | export function deserializeEntity (ecs, payload) |
| 44 | { |
| 45 | const handle = ecs.spawn(payload.entityId); |
| 46 | const components = normalizeMobileComponents(payload.components); |
| 47 | for (const [name, data] of Object.entries(components)) |
| 48 | { ecs.add(handle, name, structuredClone(data)); } |
| 49 | return payload.entityId; |
| 50 | } |
| 51 | |
| 52 | export function publicEntityView (ecs, entityId) |
| 53 | { |
| 54 | const handle = ecs.handleOf(entityId); |
| 55 | if (handle == null) |
| 56 | { return null; } |
| 57 | const position = ecs.get(handle, Component.position); |
| 58 | if (!position) |
| 59 | { return null; } |
| 60 | const movement = ecs.get(handle, Component.movement); |
| 61 | const appearance = ecs.get(handle, Component.appearance); |
| 62 | const character = ecs.get(handle, Component.character); |
| 63 | const layers = appearanceLayers(ecs, handle, appearance); |
| 64 | return { |
| 65 | entityId, |
| 66 | tileX: position.tileX, |
| 67 | tileY: position.tileY, |
| 68 | gait: movement?.gait ?? "walk", |
| 69 | sex: appearance?.sex ?? null, |
| 70 | firstName: appearance?.firstName ?? "", |
| 71 | lastName: appearance?.lastName ?? "", |
| 72 | characterId: character?.characterId ?? null, |
| 73 | sprite: layers[0]?.sprite ?? null, |
| 74 | layers |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | export function appearanceLayers (ecs, handle, appearance) |
| 79 | { |
| 80 | const sprite = appearance?.sprite ?? bodySprite(appearance?.sex); |
| 81 | const layers = [{ sprite }]; |
| 82 | const equipment = ecs.get(handle, Component.equipment); |
| 83 | for (const slot of EQUIPMENT_DRAW_ORDER) |
| 84 | { |
| 85 | const worn = equipment?.slots?.[slot]; |
| 86 | if (!worn) |
| 87 | { continue; } |
| 88 | const overlay = itemEquipSprite(worn.itemId); |
| 89 | if (overlay) |
| 90 | { layers.push({ sprite: overlay }); } |
| 91 | } |
| 92 | return layers; |
| 93 | } |
| 94 | |
| 95 | export function replicationSpawn (view) |
| 96 | { |
| 97 | if (!view) |
| 98 | { return null; } |
| 99 | return { |
| 100 | type: "entity-spawn", |
| 101 | entityId: view.entityId, |
| 102 | characterId: view.characterId, |
| 103 | tileX: view.tileX, |
| 104 | tileY: view.tileY, |
| 105 | gait: view.gait, |
| 106 | sex: view.sex, |
| 107 | firstName: view.firstName, |
| 108 | lastName: view.lastName, |
| 109 | sprite: view.sprite ?? null, |
| 110 | layers: view.layers ?? [] |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | export function replicationMove (view, fromTileX, fromTileY) |
| 115 | { |
| 116 | if (!view) |
| 117 | { return null; } |
| 118 | return { |
| 119 | type: "entity-move", |
| 120 | entityId: view.entityId, |
| 121 | tileX: view.tileX, |
| 122 | tileY: view.tileY, |
| 123 | gait: view.gait, |
| 124 | fromTileX, |
| 125 | fromTileY |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | export function replicationAppearance (view) |
| 130 | { |
| 131 | if (!view) |
| 132 | { return null; } |
| 133 | return { |
| 134 | type: "entity-appearance", |
| 135 | entityId: view.entityId, |
| 136 | tileX: view.tileX, |
| 137 | tileY: view.tileY, |
| 138 | sprite: view.sprite ?? null, |
| 139 | layers: view.layers ?? [] |
| 140 | }; |
| 141 | } |
| 142 | |
| 143 | export function replicationDespawn (view) |
| 144 | { |
| 145 | if (!view) |
| 146 | { return null; } |
| 147 | return { |
| 148 | type: "entity-despawn", |
| 149 | entityId: view.entityId, |
| 150 | tileX: view.tileX, |
| 151 | tileY: view.tileY |
| 152 | }; |
| 153 | } |