BlueGit

timfa questward

Files

questward / server / src / ecs / serialize.js

serialize.js

serialize.js 4.9 KiB Download Raw
1import { Component } from "./components.js";
2import { EQUIPMENT_DRAW_ORDER, 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";
6
7function bodySprite (sex)
8{
9 const key = sex === "female" ? "player-body-female" : "player-body-male";
10 return spriteImageName("character", key, key);
11}
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 layers = appearanceLayers(ecs, handle, appearance);
69 const mob = creature ? mobDefinition(creature.mobTypeId) : null;
70 const isMob = !!creature && !character;
71 return {
72 entityId,
73 tileX: position.tileX,
74 tileY: position.tileY,
75 worldX: flight?.x ?? position.tileX + 0.5,
76 worldY: flight?.y ?? position.tileY + 0.5,
77 gait: movement?.gait ?? "walk",
78 sex: appearance?.sex ?? null,
79 firstName: appearance?.firstName ?? "",
80 lastName: appearance?.lastName ?? "",
81 characterId: character?.characterId ?? null,
82 mobTypeId: creature?.mobTypeId ?? null,
83 canFight: mob ? mobCanFight(mob) : false,
84 hostile: !!mob?.hostile,
85 talkable: !!mob?.talkable,
86 untargetable: !!mob?.untargetable,
87 freeFlight: !!mob?.freeFlight,
88 health: isMob && health ? health.current : undefined,
89 maxHealth: isMob && health ? health.max : undefined,
90 sprite: layers[0]?.sprite ?? null,
91 layers
92 };
93}
94
95export function appearanceLayers (ecs, handle, appearance)
96{
97 const sprite = appearance?.sprite ?? bodySprite(appearance?.sex);
98 const layers = [{ sprite }];
99 const equipment = ecs.get(handle, Component.equipment);
100 for (const slot of EQUIPMENT_DRAW_ORDER)
101 {
102 const worn = equipment?.slots?.[slot];
103 if (!worn)
104 { continue; }
105 const overlay = itemEquipSprite(worn.itemId);
106 if (overlay)
107 { layers.push({ sprite: overlay }); }
108 }
109 return layers;
110}
111
112export function replicationSpawn (view)
113{
114 if (!view)
115 { return null; }
116 return {
117 type: "entity-spawn",
118 entityId: view.entityId,
119 characterId: view.characterId,
120 tileX: view.tileX,
121 tileY: view.tileY,
122 worldX: view.worldX,
123 worldY: view.worldY,
124 gait: view.gait,
125 sex: view.sex,
126 firstName: view.firstName,
127 lastName: view.lastName,
128 sprite: view.sprite ?? null,
129 layers: view.layers ?? [],
130 mobTypeId: view.mobTypeId ?? null,
131 canFight: !!view.canFight,
132 hostile: !!view.hostile,
133 talkable: !!view.talkable,
134 untargetable: !!view.untargetable,
135 freeFlight: !!view.freeFlight,
136 health: view.health,
137 maxHealth: view.maxHealth
138 };
139}
140
141export function replicationMove (view, fromTileX, fromTileY)
142{
143 if (!view)
144 { return null; }
145 return {
146 type: "entity-move",
147 entityId: view.entityId,
148 tileX: view.tileX,
149 tileY: view.tileY,
150 worldX: view.worldX,
151 worldY: view.worldY,
152 gait: view.gait,
153 fromTileX,
154 fromTileY,
155 freeFlight: !!view.freeFlight
156 };
157}
158
159export function replicationAppearance (view)
160{
161 if (!view)
162 { return null; }
163 return {
164 type: "entity-appearance",
165 entityId: view.entityId,
166 tileX: view.tileX,
167 tileY: view.tileY,
168 sprite: view.sprite ?? null,
169 layers: view.layers ?? []
170 };
171}
172
173export function replicationDespawn (view)
174{
175 if (!view)
176 { return null; }
177 return {
178 type: "entity-despawn",
179 entityId: view.entityId,
180 tileX: view.tileX,
181 tileY: view.tileY
182 };
183}