BlueGit

timfa questward

Files

questward / server / src / ecs / serialize.js

serialize.js

serialize.js 5.4 KiB Download Raw
1import { Component } from "./components.js";
2import { equippedLightRadiusTiles } from "../../../shared/definitions/items.js";
3import {
4 statusExtraLightRadius,
5 statusVisualScale,
6 publicStatusEffectsView
7} from "../../../shared/statusEffects/rules.js";
8import { mobDefinition } from "../../../shared/definitions/mobs.js";
9import { mobCanFight } from "../../../shared/definitions/mobCombat.js";
10import { shopHasInterface } from "../shop/service.js";
11import { composeAppearanceLayers } from "../../../shared/appearance/hair.js";
12
13export 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
34export 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
45export 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
54export 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 health: health ? health.current : undefined,
95 maxHealth: health ? health.max : undefined,
96 sprite: layers[0]?.sprite ?? null,
97 layers,
98 lightRadiusTiles: Math.max(gearLight, effectLight),
99 visualScale: statusVisualScale(statusEffects, now),
100 statusEffects: publicStatusEffectsView(statusEffects, now)
101 };
102}
103
104export function appearanceLayers (ecs, handle, appearance)
105{
106 const equipment = ecs.get(handle, Component.equipment);
107 return composeAppearanceLayers(appearance, equipment?.slots ?? {});
108}
109
110export function replicationSpawn (view)
111{
112 if (!view)
113 { return null; }
114 return {
115 type: "entity-spawn",
116 entityId: view.entityId,
117 characterId: view.characterId,
118 tileX: view.tileX,
119 tileY: view.tileY,
120 worldX: view.worldX,
121 worldY: view.worldY,
122 gait: view.gait,
123 sex: view.sex,
124 firstName: view.firstName,
125 lastName: view.lastName,
126 sprite: view.sprite ?? null,
127 layers: view.layers ?? [],
128 mobTypeId: view.mobTypeId ?? null,
129 canFight: !!view.canFight,
130 hostile: !!view.hostile,
131 talkable: !!view.talkable,
132 hasShop: !!view.hasShop,
133 untargetable: !!view.untargetable,
134 freeFlight: !!view.freeFlight,
135 health: view.health,
136 maxHealth: view.maxHealth,
137 lightRadiusTiles: view.lightRadiusTiles ?? 0,
138 visualScale: view.visualScale ?? 1
139 };
140}
141
142export function replicationMove (view, fromTileX, fromTileY)
143{
144 if (!view)
145 { return null; }
146 return {
147 type: "entity-move",
148 entityId: view.entityId,
149 tileX: view.tileX,
150 tileY: view.tileY,
151 worldX: view.worldX,
152 worldY: view.worldY,
153 gait: view.gait,
154 fromTileX,
155 fromTileY,
156 freeFlight: !!view.freeFlight
157 };
158}
159
160export function replicationAppearance (view)
161{
162 if (!view)
163 { return null; }
164 return {
165 type: "entity-appearance",
166 entityId: view.entityId,
167 tileX: view.tileX,
168 tileY: view.tileY,
169 sprite: view.sprite ?? null,
170 layers: view.layers ?? [],
171 lightRadiusTiles: view.lightRadiusTiles ?? 0,
172 visualScale: view.visualScale ?? 1
173 };
174}
175
176export function replicationDespawn (view)
177{
178 if (!view)
179 { return null; }
180 return {
181 type: "entity-despawn",
182 entityId: view.entityId,
183 tileX: view.tileX,
184 tileY: view.tileY
185 };
186}