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