BlueGit

timfa questward

Files

questward / server / src / ecs / serialize.js

serialize.js

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