BlueGit

timfa questward

Files

questward / client / src / render / worldView.js

worldView.js

worldView.js 7.4 KiB Download Raw
1import { CHUNK_SIZE, SPRITE_SIZE_PX, GAIT } from "/shared/protocol/constants.js";
2import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS } from "/shared/definitions/tiles.js";
3
4export class WorldView
5{
6 constructor (game, scene)
7 {
8 this.game = game;
9 this.scene = scene;
10 this.zoom = 2;
11 this.chunks = new Map();
12 this.tiles = new Map();
13 this.entities = new Map();
14 this.localEntityId = null;
15 this.settings = { walkTilesPerSecond: 2 };
16 this.worldHour = 12;
17 this.nightPreview = null;
18 this.followPlayer = true;
19 }
20
21 setLocalEntity (entityId, tileX, tileY, gait, sex)
22 {
23 this.localEntityId = entityId;
24 this.#upsertEntity({
25 entityId,
26 tileX,
27 tileY,
28 gait,
29 sex,
30 firstName: "",
31 lastName: ""
32 });
33 }
34
35 applySnapshot (snapshot)
36 {
37 const key = `${snapshot.chunkX},${snapshot.chunkY}`;
38 this.chunks.set(key, snapshot);
39 this.#rebuildChunk(snapshot);
40 for (const entity of snapshot.entities)
41 { this.#upsertEntity(entity); }
42 }
43
44 removeChunk (chunkX, chunkY)
45 {
46 const key = `${chunkX},${chunkY}`;
47 this.chunks.delete(key);
48 for (const [tileKey, objects] of [...this.tiles.entries()])
49 {
50 if (objects.chunkX === chunkX && objects.chunkY === chunkY)
51 {
52 objects.ground?.destroy();
53 objects.block?.destroy();
54 objects.shade?.destroy();
55 this.tiles.delete(tileKey);
56 }
57 }
58 }
59
60 moveEntity (payload)
61 {
62 const existing = this.entities.get(payload.entityId);
63 if (!existing)
64 {
65 this.#upsertEntity({
66 entityId: payload.entityId,
67 tileX: payload.tileX,
68 tileY: payload.tileY,
69 gait: payload.gait,
70 sex: "male"
71 });
72 return;
73 }
74 existing.fromX = existing.visualX;
75 existing.fromY = existing.visualY;
76 existing.tileX = payload.tileX;
77 existing.tileY = payload.tileY;
78 existing.gait = payload.gait;
79 existing.lerpStarted = performance.now();
80 existing.lerpMs = this.#lerpMs(payload.gait);
81 }
82
83 despawnEntity (entityId)
84 {
85 const existing = this.entities.get(entityId);
86 if (!existing)
87 { return; }
88 existing.object?.destroy();
89 this.entities.delete(entityId);
90 }
91
92 setZoom (zoom, limits = { min: 1, max: 4 })
93 {
94 const min = limits.min ?? 1;
95 const max = limits.max ?? 4;
96 this.zoom = Math.min(max, Math.max(min, zoom));
97 for (const snapshot of this.chunks.values())
98 { this.#rebuildChunk(snapshot); }
99 const size = this.tileSize();
100 for (const entity of this.entities.values())
101 {
102 entity.visualX = entity.tileX * size + size / 2;
103 entity.visualY = entity.tileY * size + size / 2;
104 entity.fromX = entity.visualX;
105 entity.fromY = entity.visualY;
106 entity.lerpStarted = null;
107 if (entity.object)
108 { entity.object.transform.size = new vector(size, size); }
109 }
110 }
111
112 tileSize ()
113 {
114 return SPRITE_SIZE_PX * this.zoom;
115 }
116
117 screenToTile (mouseX, mouseY)
118 {
119 const size = this.tileSize();
120 const worldX = mouseX - this.scene.cameraPosition.x;
121 const worldY = mouseY - this.scene.cameraPosition.y;
122 return {
123 tileX: Math.floor(worldX / size),
124 tileY: Math.floor(worldY / size)
125 };
126 }
127
128 update (deltaTime)
129 {
130 const now = performance.now();
131 const size = this.tileSize();
132 for (const entity of this.entities.values())
133 {
134 const targetX = entity.tileX * size + size / 2;
135 const targetY = entity.tileY * size + size / 2;
136 if (entity.lerpStarted != null)
137 {
138 const t = Math.min(1, (now - entity.lerpStarted) / entity.lerpMs);
139 entity.visualX = entity.fromX + (targetX - entity.fromX) * t;
140 entity.visualY = entity.fromY + (targetY - entity.fromY) * t;
141 if (t >= 1)
142 { entity.lerpStarted = null; }
143 }
144 else
145 {
146 entity.visualX = targetX;
147 entity.visualY = targetY;
148 }
149 if (entity.object)
150 {
151 entity.object.transform.position.x = entity.visualX;
152 entity.object.transform.position.y = entity.visualY;
153 entity.object.transform.size = new vector(size, size);
154 }
155 }
156
157 this.#centerCamera();
158 this.#updateShade(deltaTime);
159 }
160
161 #lerpMs (gait)
162 {
163 const walk = this.settings.walkTilesPerSecond || 2;
164 const seconds = gait === GAIT.run ? 1 / (walk * 2) : 1 / walk;
165 return seconds * 1000;
166 }
167
168 #centerCamera ()
169 {
170 if (!this.followPlayer)
171 { return; }
172 const local = this.entities.get(this.localEntityId);
173 if (!local)
174 { return; }
175 this.scene.cameraPosition.x = this.scene.real_size.x / 2 - local.visualX;
176 this.scene.cameraPosition.y = this.scene.real_size.y / 2 - local.visualY;
177 }
178
179 #upsertEntity (payload)
180 {
181 const size = this.tileSize();
182 let entity = this.entities.get(payload.entityId);
183 if (!entity)
184 {
185 const spriteName = payload.sex === "female" ? "player-body-female" : "player-body-male";
186 const object = new GameObject(0, 0, this.game.images[spriteName], 2);
187 this.scene.addObject(object);
188 entity = { object, visualX: 0, visualY: 0 };
189 this.entities.set(payload.entityId, entity);
190 }
191 entity.tileX = payload.tileX;
192 entity.tileY = payload.tileY;
193 entity.gait = payload.gait;
194 entity.sex = payload.sex;
195 entity.visualX = payload.tileX * size + size / 2;
196 entity.visualY = payload.tileY * size + size / 2;
197 entity.fromX = entity.visualX;
198 entity.fromY = entity.visualY;
199 entity.object.transform.position.x = entity.visualX;
200 entity.object.transform.position.y = entity.visualY;
201 entity.object.transform.size = new vector(size, size);
202 }
203
204 #rebuildChunk (snapshot)
205 {
206 const size = this.tileSize();
207 for (let localY = 0; localY < CHUNK_SIZE; localY++)
208 {
209 for (let localX = 0; localX < CHUNK_SIZE; localX++)
210 {
211 const index = localY * CHUNK_SIZE + localX;
212 const tileX = snapshot.chunkX * CHUNK_SIZE + localX;
213 const tileY = snapshot.chunkY * CHUNK_SIZE + localY;
214 const key = `${tileX},${tileY}`;
215 let objects = this.tiles.get(key);
216 if (!objects)
217 {
218 objects = { chunkX: snapshot.chunkX, chunkY: snapshot.chunkY };
219 this.tiles.set(key, objects);
220 }
221 const px = tileX * size + size / 2;
222 const py = tileY * size + size / 2;
223 const groundName = GROUND_DEFINITIONS[snapshot.grounds[index]]?.sprite ?? "ground-grass";
224 objects.ground = this.#sprite(objects.ground, groundName, px, py, 0, size);
225 const block = snapshot.blocks[index];
226 const blockName = block ? BLOCK_DEFINITIONS[block.typeId]?.sprite : null;
227 if (blockName)
228 { objects.block = this.#sprite(objects.block, blockName, px, py, 1, size); }
229 else if (objects.block)
230 {
231 objects.block.destroy();
232 objects.block = null;
233 }
234 objects.light = snapshot.light[index] ?? 0;
235 objects.shade = this.#sprite(objects.shade, "night-shade", px, py, 3, size);
236 objects.shade.renderer.alpha = 0;
237 }
238 }
239 }
240
241 #sprite (existing, name, x, y, layer, size)
242 {
243 if (!existing || existing.renderer.sprite !== this.game.images[name])
244 {
245 existing?.destroy();
246 existing = new GameObject(x, y, this.game.images[name], layer);
247 this.scene.addObject(existing);
248 }
249 existing.transform.position.x = x;
250 existing.transform.position.y = y;
251 existing.transform.size = new vector(size, size);
252 existing.layer = layer;
253 return existing;
254 }
255
256 #updateShade ()
257 {
258 const hour = this.nightPreview ?? this.worldHour;
259 const ambientDark = nightFactor(hour);
260 for (const objects of this.tiles.values())
261 {
262 if (!objects.shade)
263 { continue; }
264 const lit = (objects.light ?? 0) > 0;
265 objects.shade.renderer.alpha = lit ? ambientDark * 0.12 : ambientDark;
266 }
267 }
268}
269
270export function nightFactor (hour)
271{
272 if (hour >= 8 && hour <= 16)
273 { return 0; }
274 if (hour >= 22 || hour <= 5)
275 { return 0.72; }
276 if (hour < 8)
277 { return lerp(0.72, 0, (hour - 5) / 3); }
278 return lerp(0, 0.72, (hour - 16) / 6);
279}
280
281function lerp (a, b, t)
282{
283 return a + (b - a) * Math.min(1, Math.max(0, t));
284}