| 1 | import { CHUNK_SIZE, SPRITE_SIZE_PX, GAIT } from "/shared/protocol/constants.js"; |
| 2 | import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS } from "/shared/definitions/tiles.js"; |
| 3 | import { spriteCenterFromPivot, spriteDrawSize, tilePivot, ySortLayer } from "/shared/render/pivot.js"; |
| 4 | |
| 5 | const LAYER_GROUND = 0; |
| 6 | const LAYER_SHADE = 500000; |
| 7 | const LAYER_EDGE = 900000; |
| 8 | const SORT_BLOCK = 1; |
| 9 | const SORT_ENTITY = 2; |
| 10 | |
| 11 | export class WorldView |
| 12 | { |
| 13 | constructor (game, scene) |
| 14 | { |
| 15 | this.game = game; |
| 16 | this.scene = scene; |
| 17 | this.zoom = 2; |
| 18 | this.chunks = new Map(); |
| 19 | this.tiles = new Map(); |
| 20 | this.entities = new Map(); |
| 21 | this.edges = new Map(); |
| 22 | this.localEntityId = null; |
| 23 | this.settings = { walkTilesPerSecond: 2 }; |
| 24 | this.worldHour = 12; |
| 25 | this.nightPreview = null; |
| 26 | this.followPlayer = true; |
| 27 | } |
| 28 | |
| 29 | setLocalEntity (entityId, tileX, tileY, gait, sex) |
| 30 | { |
| 31 | this.localEntityId = entityId; |
| 32 | this.#upsertEntity({ |
| 33 | entityId, |
| 34 | tileX, |
| 35 | tileY, |
| 36 | gait, |
| 37 | sex, |
| 38 | firstName: "", |
| 39 | lastName: "" |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | applySnapshot (snapshot) |
| 44 | { |
| 45 | const key = `${snapshot.chunkX},${snapshot.chunkY}`; |
| 46 | this.chunks.set(key, snapshot); |
| 47 | this.#rebuildChunk(snapshot); |
| 48 | for (const entity of snapshot.entities ?? []) |
| 49 | { this.#upsertEntity(entity); } |
| 50 | this.#rebuildAllEdges(); |
| 51 | } |
| 52 | |
| 53 | removeChunk (chunkX, chunkY) |
| 54 | { |
| 55 | const key = `${chunkX},${chunkY}`; |
| 56 | this.chunks.delete(key); |
| 57 | this.#destroyEdges(key); |
| 58 | for (const [tileKey, objects] of [...this.tiles.entries()]) |
| 59 | { |
| 60 | if (objects.chunkX === chunkX && objects.chunkY === chunkY) |
| 61 | { |
| 62 | objects.ground?.destroy(); |
| 63 | objects.block?.destroy(); |
| 64 | objects.shade?.destroy(); |
| 65 | this.tiles.delete(tileKey); |
| 66 | } |
| 67 | } |
| 68 | this.#rebuildAllEdges(); |
| 69 | } |
| 70 | |
| 71 | setChunkOpen (chunkX, chunkY, open) |
| 72 | { |
| 73 | const snapshot = this.chunks.get(`${chunkX},${chunkY}`); |
| 74 | if (!snapshot) |
| 75 | { return; } |
| 76 | snapshot.open = !!open; |
| 77 | this.#rebuildAllEdges(); |
| 78 | } |
| 79 | |
| 80 | moveEntity (payload) |
| 81 | { |
| 82 | const existing = this.entities.get(payload.entityId); |
| 83 | if (!existing) |
| 84 | { |
| 85 | this.#upsertEntity({ |
| 86 | entityId: payload.entityId, |
| 87 | tileX: payload.tileX, |
| 88 | tileY: payload.tileY, |
| 89 | gait: payload.gait, |
| 90 | sex: "male" |
| 91 | }); |
| 92 | return; |
| 93 | } |
| 94 | existing.fromX = existing.visualX; |
| 95 | existing.fromY = existing.visualY; |
| 96 | existing.tileX = payload.tileX; |
| 97 | existing.tileY = payload.tileY; |
| 98 | existing.gait = payload.gait; |
| 99 | existing.lerpStarted = performance.now(); |
| 100 | existing.lerpMs = this.#lerpMs(payload.gait); |
| 101 | } |
| 102 | |
| 103 | despawnEntity (entityId) |
| 104 | { |
| 105 | const existing = this.entities.get(entityId); |
| 106 | if (!existing) |
| 107 | { return; } |
| 108 | existing.object?.destroy(); |
| 109 | this.entities.delete(entityId); |
| 110 | } |
| 111 | |
| 112 | setZoom (zoom, limits = { min: 1, max: 4 }) |
| 113 | { |
| 114 | const min = limits.min ?? 1; |
| 115 | const max = limits.max ?? 4; |
| 116 | this.zoom = Math.min(max, Math.max(min, zoom)); |
| 117 | for (const snapshot of this.chunks.values()) |
| 118 | { this.#rebuildChunk(snapshot); } |
| 119 | const size = this.tileSize(); |
| 120 | for (const entity of this.entities.values()) |
| 121 | { |
| 122 | entity.visualX = tilePivot(entity.tileX, entity.tileY, size).x; |
| 123 | entity.visualY = tilePivot(entity.tileX, entity.tileY, size).y; |
| 124 | entity.fromX = entity.visualX; |
| 125 | entity.fromY = entity.visualY; |
| 126 | entity.lerpStarted = null; |
| 127 | this.#placeEntity(entity); |
| 128 | } |
| 129 | this.#rebuildAllEdges(); |
| 130 | } |
| 131 | |
| 132 | tileSize () |
| 133 | { |
| 134 | return SPRITE_SIZE_PX * this.zoom; |
| 135 | } |
| 136 | |
| 137 | screenToTile (mouseX, mouseY) |
| 138 | { |
| 139 | const size = this.tileSize(); |
| 140 | const worldX = mouseX - this.scene.cameraPosition.x; |
| 141 | const worldY = mouseY - this.scene.cameraPosition.y; |
| 142 | return { |
| 143 | tileX: Math.floor(worldX / size), |
| 144 | tileY: Math.floor(worldY / size) |
| 145 | }; |
| 146 | } |
| 147 | |
| 148 | update (deltaTime) |
| 149 | { |
| 150 | const now = performance.now(); |
| 151 | const size = this.tileSize(); |
| 152 | for (const entity of this.entities.values()) |
| 153 | { |
| 154 | const target = tilePivot(entity.tileX, entity.tileY, size); |
| 155 | if (entity.lerpStarted != null) |
| 156 | { |
| 157 | const t = Math.min(1, (now - entity.lerpStarted) / entity.lerpMs); |
| 158 | entity.visualX = entity.fromX + (target.x - entity.fromX) * t; |
| 159 | entity.visualY = entity.fromY + (target.y - entity.fromY) * t; |
| 160 | if (t >= 1) |
| 161 | { entity.lerpStarted = null; } |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | entity.visualX = target.x; |
| 166 | entity.visualY = target.y; |
| 167 | } |
| 168 | this.#placeEntity(entity); |
| 169 | } |
| 170 | |
| 171 | this.#centerCamera(); |
| 172 | this.#updateShade(deltaTime); |
| 173 | } |
| 174 | |
| 175 | #lerpMs (gait) |
| 176 | { |
| 177 | const walk = this.settings.walkTilesPerSecond || 2; |
| 178 | const seconds = gait === GAIT.run ? 1 / (walk * 2) : 1 / walk; |
| 179 | return seconds * 1000; |
| 180 | } |
| 181 | |
| 182 | #centerCamera () |
| 183 | { |
| 184 | if (!this.followPlayer) |
| 185 | { return; } |
| 186 | const local = this.entities.get(this.localEntityId); |
| 187 | if (!local) |
| 188 | { return; } |
| 189 | this.scene.cameraPosition.x = this.scene.real_size.x / 2 - local.visualX; |
| 190 | this.scene.cameraPosition.y = this.scene.real_size.y / 2 - local.visualY; |
| 191 | } |
| 192 | |
| 193 | #upsertEntity (payload) |
| 194 | { |
| 195 | const size = this.tileSize(); |
| 196 | const pivot = tilePivot(payload.tileX, payload.tileY, size); |
| 197 | let entity = this.entities.get(payload.entityId); |
| 198 | if (!entity) |
| 199 | { |
| 200 | const spriteName = payload.sex === "female" ? "player-body-female" : "player-body-male"; |
| 201 | const object = new GameObject(0, 0, this.game.images[spriteName], SORT_ENTITY); |
| 202 | this.scene.addObject(object); |
| 203 | entity = { object, visualX: pivot.x, visualY: pivot.y }; |
| 204 | this.entities.set(payload.entityId, entity); |
| 205 | } |
| 206 | entity.tileX = payload.tileX; |
| 207 | entity.tileY = payload.tileY; |
| 208 | entity.gait = payload.gait; |
| 209 | entity.sex = payload.sex; |
| 210 | entity.visualX = pivot.x; |
| 211 | entity.visualY = pivot.y; |
| 212 | entity.fromX = pivot.x; |
| 213 | entity.fromY = pivot.y; |
| 214 | this.#placeEntity(entity); |
| 215 | } |
| 216 | |
| 217 | #placeEntity (entity) |
| 218 | { |
| 219 | this.#placePivoted(entity.object, entity.visualX, entity.visualY, entity.visualY, SORT_ENTITY); |
| 220 | } |
| 221 | |
| 222 | #rebuildChunk (snapshot) |
| 223 | { |
| 224 | const size = this.tileSize(); |
| 225 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 226 | { |
| 227 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 228 | { |
| 229 | const index = localY * CHUNK_SIZE + localX; |
| 230 | const tileX = snapshot.chunkX * CHUNK_SIZE + localX; |
| 231 | const tileY = snapshot.chunkY * CHUNK_SIZE + localY; |
| 232 | const key = `${tileX},${tileY}`; |
| 233 | let objects = this.tiles.get(key); |
| 234 | if (!objects) |
| 235 | { |
| 236 | objects = { chunkX: snapshot.chunkX, chunkY: snapshot.chunkY }; |
| 237 | this.tiles.set(key, objects); |
| 238 | } |
| 239 | const pivot = tilePivot(tileX, tileY, size); |
| 240 | const groundName = GROUND_DEFINITIONS[snapshot.grounds[index]]?.sprite ?? "ground-grass"; |
| 241 | objects.ground = this.#ensureSprite(objects.ground, groundName, LAYER_GROUND); |
| 242 | this.#placePivoted(objects.ground, pivot.x, pivot.y, pivot.y, 0); |
| 243 | objects.ground.layer = LAYER_GROUND; |
| 244 | const block = snapshot.blocks[index]; |
| 245 | const blockName = block ? BLOCK_DEFINITIONS[block.typeId]?.sprite : null; |
| 246 | if (blockName) |
| 247 | { |
| 248 | objects.block = this.#ensureSprite(objects.block, blockName, SORT_BLOCK); |
| 249 | this.#placePivoted(objects.block, pivot.x, pivot.y, pivot.y, SORT_BLOCK); |
| 250 | } |
| 251 | else if (objects.block) |
| 252 | { |
| 253 | objects.block.destroy(); |
| 254 | objects.block = null; |
| 255 | } |
| 256 | objects.light = snapshot.light[index] ?? 0; |
| 257 | objects.shade = this.#tileFill(objects.shade, "night-shade", pivot.x, pivot.y, LAYER_SHADE + tileY); |
| 258 | objects.shade.renderer.alpha = 0; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | #tileFill (existing, name, x, y, layer) |
| 264 | { |
| 265 | existing = this.#ensureSprite(existing, name, layer); |
| 266 | const size = this.tileSize(); |
| 267 | existing.transform.position.x = x; |
| 268 | existing.transform.position.y = y; |
| 269 | existing.transform.size = new vector(size, size); |
| 270 | existing.layer = layer; |
| 271 | this.#markSortDirty(); |
| 272 | return existing; |
| 273 | } |
| 274 | |
| 275 | #placePivoted (object, pivotX, pivotY, sortY, subOrder) |
| 276 | { |
| 277 | const sprite = object.renderer.sprite; |
| 278 | const zoom = this.zoom; |
| 279 | const draw = spriteDrawSize(sprite.width, sprite.height, zoom); |
| 280 | const center = spriteCenterFromPivot(pivotX, pivotY, sprite.height, zoom); |
| 281 | object.transform.position.x = center.x; |
| 282 | object.transform.position.y = center.y; |
| 283 | object.transform.size = new vector(draw.w, draw.h); |
| 284 | object.layer = ySortLayer(sortY, subOrder); |
| 285 | this.#markSortDirty(); |
| 286 | } |
| 287 | |
| 288 | #ensureSprite (existing, name, layer) |
| 289 | { |
| 290 | const sprite = this.game.images[name]; |
| 291 | if (!existing || existing.renderer.sprite !== sprite) |
| 292 | { |
| 293 | existing?.destroy(); |
| 294 | existing = new GameObject(0, 0, sprite, layer); |
| 295 | this.scene.addObject(existing); |
| 296 | } |
| 297 | return existing; |
| 298 | } |
| 299 | |
| 300 | #neighborClosed (snapshot, dir, dx, dy) |
| 301 | { |
| 302 | const other = this.chunks.get(`${snapshot.chunkX + dx},${snapshot.chunkY + dy}`); |
| 303 | if (other) |
| 304 | { return !other.open; } |
| 305 | return !!snapshot.closedEdges?.[dir]; |
| 306 | } |
| 307 | |
| 308 | #rebuildAllEdges () |
| 309 | { |
| 310 | const seen = new Set(); |
| 311 | for (const snapshot of this.chunks.values()) |
| 312 | { |
| 313 | const key = `${snapshot.chunkX},${snapshot.chunkY}`; |
| 314 | seen.add(key); |
| 315 | this.#rebuildChunkEdges(snapshot); |
| 316 | } |
| 317 | for (const key of [...this.edges.keys()]) |
| 318 | { |
| 319 | if (!seen.has(key)) |
| 320 | { this.#destroyEdges(key); } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | #rebuildChunkEdges (snapshot) |
| 325 | { |
| 326 | const key = `${snapshot.chunkX},${snapshot.chunkY}`; |
| 327 | let bars = this.edges.get(key); |
| 328 | if (!bars) |
| 329 | { |
| 330 | bars = {}; |
| 331 | this.edges.set(key, bars); |
| 332 | } |
| 333 | |
| 334 | const size = this.tileSize(); |
| 335 | const chunkPx = CHUNK_SIZE * size; |
| 336 | const left = snapshot.chunkX * CHUNK_SIZE * size; |
| 337 | const top = snapshot.chunkY * CHUNK_SIZE * size; |
| 338 | const centerX = left + chunkPx / 2; |
| 339 | const centerY = top + chunkPx / 2; |
| 340 | const thickness = Math.max(2, Math.round(2 * this.zoom)); |
| 341 | const draw = snapshot.open === true; |
| 342 | |
| 343 | bars.north = this.#edgeBar(bars.north, draw && this.#neighborClosed(snapshot, "north", 0, -1), |
| 344 | centerX, top, chunkPx, thickness, true); |
| 345 | bars.south = this.#edgeBar(bars.south, draw && this.#neighborClosed(snapshot, "south", 0, 1), |
| 346 | centerX, top + chunkPx, chunkPx, thickness, true); |
| 347 | bars.west = this.#edgeBar(bars.west, draw && this.#neighborClosed(snapshot, "west", -1, 0), |
| 348 | left, centerY, chunkPx, thickness, false); |
| 349 | bars.east = this.#edgeBar(bars.east, draw && this.#neighborClosed(snapshot, "east", 1, 0), |
| 350 | left + chunkPx, centerY, chunkPx, thickness, false); |
| 351 | } |
| 352 | |
| 353 | #edgeBar (existing, visible, x, y, length, thickness, horizontal) |
| 354 | { |
| 355 | if (!visible) |
| 356 | { |
| 357 | if (existing) |
| 358 | { existing.renderer.visible = false; } |
| 359 | return existing; |
| 360 | } |
| 361 | |
| 362 | existing = this.#ensureSprite(existing, "chunk-edge", LAYER_EDGE); |
| 363 | existing.renderer.visible = true; |
| 364 | existing.transform.position.x = x; |
| 365 | existing.transform.position.y = y; |
| 366 | existing.transform.size = horizontal |
| 367 | ? new vector(length, thickness) |
| 368 | : new vector(thickness, length); |
| 369 | existing.layer = LAYER_EDGE; |
| 370 | this.#markSortDirty(); |
| 371 | return existing; |
| 372 | } |
| 373 | |
| 374 | #destroyEdges (key) |
| 375 | { |
| 376 | const bars = this.edges.get(key); |
| 377 | if (!bars) |
| 378 | { return; } |
| 379 | bars.north?.destroy(); |
| 380 | bars.east?.destroy(); |
| 381 | bars.south?.destroy(); |
| 382 | bars.west?.destroy(); |
| 383 | this.edges.delete(key); |
| 384 | } |
| 385 | |
| 386 | #markSortDirty () |
| 387 | { |
| 388 | this.scene._sortDirty = true; |
| 389 | } |
| 390 | |
| 391 | #updateShade () |
| 392 | { |
| 393 | const hour = this.nightPreview ?? this.worldHour; |
| 394 | const ambientDark = nightFactor(hour); |
| 395 | for (const objects of this.tiles.values()) |
| 396 | { |
| 397 | if (!objects.shade) |
| 398 | { continue; } |
| 399 | const intensity = Math.min(1, Math.max(0, (objects.light ?? 0) / 255)); |
| 400 | objects.shade.renderer.alpha = ambientDark * (1 - intensity); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | export function nightFactor (hour) |
| 406 | { |
| 407 | if (hour >= 8 && hour <= 16) |
| 408 | { return 0; } |
| 409 | if (hour >= 22 || hour <= 5) |
| 410 | { return 0.72; } |
| 411 | if (hour < 8) |
| 412 | { return lerp(0.72, 0, (hour - 5) / 3); } |
| 413 | return lerp(0, 0.72, (hour - 16) / 6); |
| 414 | } |
| 415 | |
| 416 | function lerp (a, b, t) |
| 417 | { |
| 418 | return a + (b - a) * Math.min(1, Math.max(0, t)); |
| 419 | } |