BlueGit

timfa questward

Files

questward / server / src / ecs / serialize.js

serialize.js

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