BlueGit

timfa questward

Files

questward / server / src / ecs / serialize.js

serialize.js

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