| 1 | import { CHUNK_SIZE, SPRITE_SIZE_PX, GAIT, DEFAULT_WORLD_SETTINGS } from "/shared/protocol/constants.js"; |
| 2 | import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock } from "/shared/definitions/tiles.js"; |
| 3 | import { bloomCompanionName, bloomImageName, resolveState, spriteImageName } from "/shared/definitions/tileSprites.js"; |
| 4 | import { itemSpriteName } from "/shared/definitions/items.js"; |
| 5 | import { spriteCenterFromPivot, spriteDrawSize, tilePivot, ySortLayer } from "/shared/render/pivot.js"; |
| 6 | |
| 7 | const LAYER_GROUND = 0; |
| 8 | const LAYER_SHADE = 500000; |
| 9 | const LAYER_EDGE = 900000; |
| 10 | const SORT_BLOCK = 1; |
| 11 | const SORT_ENTITY = 2; |
| 12 | const RENDER_LAYER_BLOOM = "bloom"; |
| 13 | |
| 14 | export class WorldView |
| 15 | { |
| 16 | constructor (game, scene, catalog = { grounds: {}, blocks: {} }) |
| 17 | { |
| 18 | this.game = game; |
| 19 | this.scene = scene; |
| 20 | this.catalog = catalog; |
| 21 | this.preview = null; |
| 22 | this.zoom = 2; |
| 23 | this.chunks = new Map(); |
| 24 | this.tiles = new Map(); |
| 25 | this.entities = new Map(); |
| 26 | this.groundItems = new Map(); |
| 27 | this.edges = new Map(); |
| 28 | this.localEntityId = null; |
| 29 | this.settings = { ...DEFAULT_WORLD_SETTINGS }; |
| 30 | this.worldHour = 12; |
| 31 | this.nightPreview = null; |
| 32 | this.followPlayer = true; |
| 33 | this.#syncBloomLayer(); |
| 34 | } |
| 35 | |
| 36 | setSettings (settings) |
| 37 | { |
| 38 | this.settings = { ...DEFAULT_WORLD_SETTINGS, ...settings }; |
| 39 | this.#syncBloomLayer(); |
| 40 | } |
| 41 | |
| 42 | setLocalEntity (entityId, tileX, tileY, gait, sex, firstName = "", lastName = "", characterId = null, layers = null) |
| 43 | { |
| 44 | this.localEntityId = entityId; |
| 45 | this.#upsertEntity({ |
| 46 | entityId, |
| 47 | characterId, |
| 48 | tileX, |
| 49 | tileY, |
| 50 | gait, |
| 51 | sex, |
| 52 | firstName, |
| 53 | lastName, |
| 54 | layers: layers ?? undefined |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | applySnapshot (snapshot) |
| 59 | { |
| 60 | const key = `${snapshot.chunkX},${snapshot.chunkY}`; |
| 61 | this.chunks.set(key, snapshot); |
| 62 | this.#rebuildChunk(snapshot); |
| 63 | this.#redrawPreview(); |
| 64 | for (const entity of snapshot.entities ?? []) |
| 65 | { this.#upsertEntity(entity); } |
| 66 | const chunkPrefix = `${snapshot.chunkX},${snapshot.chunkY}`; |
| 67 | for (const [id, item] of [...this.groundItems.entries()]) |
| 68 | { |
| 69 | if (Math.floor(item.tileX / CHUNK_SIZE) === snapshot.chunkX && Math.floor(item.tileY / CHUNK_SIZE) === snapshot.chunkY) |
| 70 | { this.despawnGroundItem(id); } |
| 71 | } |
| 72 | for (const item of snapshot.groundItems ?? []) |
| 73 | { this.upsertGroundItem(item); } |
| 74 | this.#rebuildAllEdges(); |
| 75 | } |
| 76 | |
| 77 | removeChunk (chunkX, chunkY) |
| 78 | { |
| 79 | const key = `${chunkX},${chunkY}`; |
| 80 | this.chunks.delete(key); |
| 81 | this.#destroyEdges(key); |
| 82 | for (const [tileKey, objects] of [...this.tiles.entries()]) |
| 83 | { |
| 84 | if (objects.chunkX === chunkX && objects.chunkY === chunkY) |
| 85 | { |
| 86 | objects.ground?.destroy(); |
| 87 | objects.groundBloom?.destroy(); |
| 88 | objects.block?.destroy(); |
| 89 | objects.blockBloom?.destroy(); |
| 90 | objects.shade?.destroy(); |
| 91 | this.tiles.delete(tileKey); |
| 92 | } |
| 93 | } |
| 94 | for (const [id, item] of [...this.groundItems.entries()]) |
| 95 | { |
| 96 | if (Math.floor(item.tileX / CHUNK_SIZE) === chunkX && Math.floor(item.tileY / CHUNK_SIZE) === chunkY) |
| 97 | { this.despawnGroundItem(id); } |
| 98 | } |
| 99 | this.#rebuildAllEdges(); |
| 100 | } |
| 101 | |
| 102 | setChunkOpen (chunkX, chunkY, open) |
| 103 | { |
| 104 | const snapshot = this.chunks.get(`${chunkX},${chunkY}`); |
| 105 | if (!snapshot) |
| 106 | { return; } |
| 107 | snapshot.open = !!open; |
| 108 | this.#rebuildAllEdges(); |
| 109 | } |
| 110 | |
| 111 | spawnEntity (payload) |
| 112 | { |
| 113 | this.#upsertEntity(payload); |
| 114 | } |
| 115 | |
| 116 | moveEntity (payload) |
| 117 | { |
| 118 | const existing = this.entities.get(payload.entityId); |
| 119 | if (!existing) |
| 120 | { |
| 121 | this.#upsertEntity(payload); |
| 122 | return; |
| 123 | } |
| 124 | existing.fromX = existing.visualX; |
| 125 | existing.fromY = existing.visualY; |
| 126 | existing.tileX = payload.tileX; |
| 127 | existing.tileY = payload.tileY; |
| 128 | existing.gait = payload.gait; |
| 129 | existing.lerpStarted = performance.now(); |
| 130 | existing.lerpMs = this.#lerpMs(payload.gait); |
| 131 | } |
| 132 | |
| 133 | despawnEntity (entityId) |
| 134 | { |
| 135 | const existing = this.entities.get(entityId); |
| 136 | if (!existing) |
| 137 | { return; } |
| 138 | existing.object?.destroy(); |
| 139 | for (const overlay of existing.overlays ?? []) |
| 140 | { overlay.destroy(); } |
| 141 | for (const overlay of existing.bloomOverlays ?? []) |
| 142 | { overlay.destroy(); } |
| 143 | this.entities.delete(entityId); |
| 144 | } |
| 145 | |
| 146 | setAppearance (payload) |
| 147 | { |
| 148 | const existing = this.entities.get(payload.entityId); |
| 149 | if (!existing) |
| 150 | { return; } |
| 151 | this.#applyLayers(existing, payload.layers ?? [{ sprite: payload.sprite }]); |
| 152 | this.#placeEntity(existing); |
| 153 | } |
| 154 | |
| 155 | upsertGroundItem (payload) |
| 156 | { |
| 157 | const size = this.tileSize(); |
| 158 | const pivot = tilePivot(payload.tileX, payload.tileY, size); |
| 159 | let item = this.groundItems.get(payload.groundItemId); |
| 160 | if (!item) |
| 161 | { |
| 162 | const object = new GameObject(0, 0, this.game.images[payload.sprite] ?? this.game.images[itemSpriteName(null)], SORT_BLOCK); |
| 163 | this.scene.addObject(object); |
| 164 | item = { object }; |
| 165 | this.groundItems.set(payload.groundItemId, item); |
| 166 | } |
| 167 | item.groundItemId = payload.groundItemId; |
| 168 | item.tileX = payload.tileX; |
| 169 | item.tileY = payload.tileY; |
| 170 | item.itemId = payload.itemId; |
| 171 | item.quantity = payload.quantity; |
| 172 | item.displayName = payload.displayName; |
| 173 | item.sprite = payload.sprite; |
| 174 | this.#placePivoted(item.object, pivot.x, pivot.y, pivot.y - 0.01, SORT_BLOCK); |
| 175 | } |
| 176 | |
| 177 | despawnGroundItem (groundItemId) |
| 178 | { |
| 179 | const item = this.groundItems.get(groundItemId); |
| 180 | if (!item) |
| 181 | { return; } |
| 182 | item.object?.destroy(); |
| 183 | this.groundItems.delete(groundItemId); |
| 184 | } |
| 185 | |
| 186 | groundItemAtTile (tileX, tileY) |
| 187 | { |
| 188 | for (const item of this.groundItems.values()) |
| 189 | { |
| 190 | if (item.tileX === tileX && item.tileY === tileY) |
| 191 | { return item; } |
| 192 | } |
| 193 | return null; |
| 194 | } |
| 195 | |
| 196 | setZoom (zoom, limits = { min: 1, max: 4 }) |
| 197 | { |
| 198 | const min = limits.min ?? 1; |
| 199 | const max = limits.max ?? 4; |
| 200 | this.zoom = Math.min(max, Math.max(min, zoom)); |
| 201 | for (const snapshot of this.chunks.values()) |
| 202 | { this.#rebuildChunk(snapshot); } |
| 203 | const size = this.tileSize(); |
| 204 | for (const entity of this.entities.values()) |
| 205 | { |
| 206 | entity.visualX = tilePivot(entity.tileX, entity.tileY, size).x; |
| 207 | entity.visualY = tilePivot(entity.tileX, entity.tileY, size).y; |
| 208 | entity.fromX = entity.visualX; |
| 209 | entity.fromY = entity.visualY; |
| 210 | entity.lerpStarted = null; |
| 211 | this.#placeEntity(entity); |
| 212 | } |
| 213 | for (const item of this.groundItems.values()) |
| 214 | { |
| 215 | const pivot = tilePivot(item.tileX, item.tileY, size); |
| 216 | this.#placePivoted(item.object, pivot.x, pivot.y, pivot.y - 0.01, SORT_BLOCK); |
| 217 | } |
| 218 | this.#rebuildAllEdges(); |
| 219 | this.#redrawPreview(); |
| 220 | } |
| 221 | |
| 222 | tileSize () |
| 223 | { |
| 224 | return SPRITE_SIZE_PX * this.zoom; |
| 225 | } |
| 226 | |
| 227 | screenToTile (mouseX, mouseY) |
| 228 | { |
| 229 | const size = this.tileSize(); |
| 230 | const worldX = mouseX - this.scene.cameraPosition.x; |
| 231 | const worldY = mouseY - this.scene.cameraPosition.y; |
| 232 | return { |
| 233 | tileX: Math.floor(worldX / size), |
| 234 | tileY: Math.floor(worldY / size) |
| 235 | }; |
| 236 | } |
| 237 | |
| 238 | entityAtScreen (mouseX, mouseY) |
| 239 | { |
| 240 | let best = null; |
| 241 | let bestLayer = -Infinity; |
| 242 | for (const entity of this.entities.values()) |
| 243 | { |
| 244 | const box = this.#entityScreenBox(entity); |
| 245 | if (!box) |
| 246 | { continue; } |
| 247 | if (mouseX >= box.left && mouseX < box.right && mouseY >= box.top && mouseY < box.bottom) |
| 248 | { |
| 249 | const layer = entity.object?.layer ?? 0; |
| 250 | if (layer >= bestLayer) |
| 251 | { |
| 252 | best = entity; |
| 253 | bestLayer = layer; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | return best; |
| 258 | } |
| 259 | |
| 260 | nametagAnchor (entity) |
| 261 | { |
| 262 | const box = this.#entityScreenBox(entity); |
| 263 | if (!box) |
| 264 | { return null; } |
| 265 | return { x: (box.left + box.right) / 2, y: box.top }; |
| 266 | } |
| 267 | |
| 268 | #entityScreenBox (entity) |
| 269 | { |
| 270 | const object = entity?.object; |
| 271 | if (!object) |
| 272 | { return null; } |
| 273 | const size = object.transform.size; |
| 274 | const x = object.transform.position.x + this.scene.cameraPosition.x; |
| 275 | const y = object.transform.position.y + this.scene.cameraPosition.y; |
| 276 | return { |
| 277 | left: x - size.x / 2, |
| 278 | right: x + size.x / 2, |
| 279 | top: y - size.y / 2, |
| 280 | bottom: y + size.y / 2 |
| 281 | }; |
| 282 | } |
| 283 | |
| 284 | update (deltaTime) |
| 285 | { |
| 286 | const now = performance.now(); |
| 287 | const size = this.tileSize(); |
| 288 | for (const entity of this.entities.values()) |
| 289 | { |
| 290 | const target = tilePivot(entity.tileX, entity.tileY, size); |
| 291 | if (entity.lerpStarted != null) |
| 292 | { |
| 293 | const t = Math.min(1, (now - entity.lerpStarted) / entity.lerpMs); |
| 294 | entity.visualX = entity.fromX + (target.x - entity.fromX) * t; |
| 295 | entity.visualY = entity.fromY + (target.y - entity.fromY) * t; |
| 296 | if (t >= 1) |
| 297 | { entity.lerpStarted = null; } |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | entity.visualX = target.x; |
| 302 | entity.visualY = target.y; |
| 303 | } |
| 304 | this.#placeEntity(entity); |
| 305 | } |
| 306 | |
| 307 | this.#centerCamera(); |
| 308 | this.#updateShade(deltaTime); |
| 309 | } |
| 310 | |
| 311 | updateAuthoredTile (chunkX, chunkY, localX, localY, { ground, block, light } = {}) |
| 312 | { |
| 313 | const snapshot = this.chunks.get(`${chunkX},${chunkY}`); |
| 314 | if (!snapshot) |
| 315 | { return; } |
| 316 | const index = localY * CHUNK_SIZE + localX; |
| 317 | if (ground !== undefined) |
| 318 | { snapshot.grounds[index] = ground; } |
| 319 | if (block !== undefined) |
| 320 | { snapshot.blocks[index] = block; } |
| 321 | if (light !== undefined) |
| 322 | { snapshot.light[index] = light; } |
| 323 | this.#drawTile(chunkX * CHUNK_SIZE + localX, chunkY * CHUNK_SIZE + localY); |
| 324 | } |
| 325 | |
| 326 | setTilePreview (tileX, tileY, override) |
| 327 | { |
| 328 | const previous = this.preview; |
| 329 | this.preview = { tileX, tileY, ...override }; |
| 330 | if (previous && (previous.tileX !== tileX || previous.tileY !== tileY)) |
| 331 | { this.#drawTile(previous.tileX, previous.tileY); } |
| 332 | this.#drawTile(tileX, tileY); |
| 333 | } |
| 334 | |
| 335 | clearTilePreview () |
| 336 | { |
| 337 | const previous = this.preview; |
| 338 | this.preview = null; |
| 339 | if (previous) |
| 340 | { this.#drawTile(previous.tileX, previous.tileY); } |
| 341 | } |
| 342 | |
| 343 | #lerpMs (gait) |
| 344 | { |
| 345 | const walk = this.settings.walkTilesPerSecond || 2; |
| 346 | const seconds = gait === GAIT.run ? 1 / (walk * 2) : 1 / walk; |
| 347 | return seconds * 1000; |
| 348 | } |
| 349 | |
| 350 | #centerCamera () |
| 351 | { |
| 352 | if (!this.followPlayer) |
| 353 | { return; } |
| 354 | const local = this.entities.get(this.localEntityId); |
| 355 | if (!local) |
| 356 | { return; } |
| 357 | this.scene.cameraPosition.x = this.scene.real_size.x / 2 - local.visualX; |
| 358 | this.scene.cameraPosition.y = this.scene.real_size.y / 2 - local.visualY; |
| 359 | } |
| 360 | |
| 361 | #upsertEntity (payload) |
| 362 | { |
| 363 | const size = this.tileSize(); |
| 364 | const pivot = tilePivot(payload.tileX, payload.tileY, size); |
| 365 | let entity = this.entities.get(payload.entityId); |
| 366 | if (!entity) |
| 367 | { |
| 368 | const spriteName = payload.layers?.[0]?.sprite |
| 369 | ?? payload.sprite |
| 370 | ?? bodySpriteName(payload.sex); |
| 371 | const object = new GameObject(0, 0, this.game.images[spriteName], SORT_ENTITY); |
| 372 | this.scene.addObject(object); |
| 373 | entity = { object, overlays: [], bloomOverlays: [], visualX: pivot.x, visualY: pivot.y }; |
| 374 | this.entities.set(payload.entityId, entity); |
| 375 | } |
| 376 | entity.entityId = payload.entityId; |
| 377 | entity.tileX = payload.tileX; |
| 378 | entity.tileY = payload.tileY; |
| 379 | entity.gait = payload.gait; |
| 380 | entity.sex = payload.sex ?? entity.sex; |
| 381 | entity.characterId = payload.characterId ?? entity.characterId ?? null; |
| 382 | if (payload.firstName) |
| 383 | { entity.firstName = payload.firstName; } |
| 384 | if (payload.lastName) |
| 385 | { entity.lastName = payload.lastName; } |
| 386 | entity.firstName = entity.firstName ?? ""; |
| 387 | entity.lastName = entity.lastName ?? ""; |
| 388 | entity.visualX = pivot.x; |
| 389 | entity.visualY = pivot.y; |
| 390 | entity.fromX = pivot.x; |
| 391 | entity.fromY = pivot.y; |
| 392 | if (payload.layers) |
| 393 | { this.#applyLayers(entity, payload.layers); } |
| 394 | this.#placeEntity(entity); |
| 395 | } |
| 396 | |
| 397 | #applyLayers (entity, layers) |
| 398 | { |
| 399 | const base = layers[0]?.sprite |
| 400 | ?? bodySpriteName(entity.sex); |
| 401 | if (this.game.images[base]) |
| 402 | { entity.object.renderer.sprite = this.game.images[base]; } |
| 403 | for (const overlay of entity.overlays ?? []) |
| 404 | { overlay.destroy(); } |
| 405 | for (const overlay of entity.bloomOverlays ?? []) |
| 406 | { overlay.destroy(); } |
| 407 | entity.overlays = []; |
| 408 | entity.bloomOverlays = []; |
| 409 | for (const layer of layers.slice(1)) |
| 410 | { |
| 411 | const image = this.game.images[layer.sprite]; |
| 412 | if (!image) |
| 413 | { continue; } |
| 414 | const object = new GameObject(0, 0, image, SORT_ENTITY); |
| 415 | this.scene.addObject(object); |
| 416 | entity.overlays.push(object); |
| 417 | const bloomName = bloomCompanionName(layer.sprite); |
| 418 | const bloomImage = this.game.images[bloomName]; |
| 419 | if (!bloomImage) |
| 420 | { continue; } |
| 421 | const bloom = new GameObject(0, 0, bloomImage, SORT_ENTITY); |
| 422 | bloom.renderer.layer = RENDER_LAYER_BLOOM; |
| 423 | this.scene.addObject(bloom); |
| 424 | entity.bloomOverlays.push(bloom); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | #placeEntity (entity) |
| 429 | { |
| 430 | this.#placePivoted(entity.object, entity.visualX, entity.visualY, entity.visualY, SORT_ENTITY); |
| 431 | for (const overlay of entity.overlays ?? []) |
| 432 | { this.#placePivoted(overlay, entity.visualX, entity.visualY, entity.visualY + 0.001, SORT_ENTITY); } |
| 433 | for (const overlay of entity.bloomOverlays ?? []) |
| 434 | { this.#placePivoted(overlay, entity.visualX, entity.visualY, entity.visualY + 0.002, SORT_ENTITY); } |
| 435 | } |
| 436 | |
| 437 | #rebuildChunk (snapshot) |
| 438 | { |
| 439 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 440 | { |
| 441 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 442 | { this.#drawTile(snapshot.chunkX * CHUNK_SIZE + localX, snapshot.chunkY * CHUNK_SIZE + localY); } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | #redrawPreview () |
| 447 | { |
| 448 | if (this.preview) |
| 449 | { this.#drawTile(this.preview.tileX, this.preview.tileY); } |
| 450 | } |
| 451 | |
| 452 | #drawTile (tileX, tileY) |
| 453 | { |
| 454 | const chunkX = Math.floor(tileX / CHUNK_SIZE); |
| 455 | const chunkY = Math.floor(tileY / CHUNK_SIZE); |
| 456 | const snapshot = this.chunks.get(`${chunkX},${chunkY}`); |
| 457 | if (!snapshot) |
| 458 | { return; } |
| 459 | const localX = tileX - chunkX * CHUNK_SIZE; |
| 460 | const localY = tileY - chunkY * CHUNK_SIZE; |
| 461 | const index = localY * CHUNK_SIZE + localX; |
| 462 | const key = `${tileX},${tileY}`; |
| 463 | let objects = this.tiles.get(key); |
| 464 | if (!objects) |
| 465 | { |
| 466 | objects = { chunkX, chunkY }; |
| 467 | this.tiles.set(key, objects); |
| 468 | } |
| 469 | |
| 470 | let ground = snapshot.grounds[index]; |
| 471 | let block = snapshot.blocks[index]; |
| 472 | const preview = this.preview; |
| 473 | if (preview && preview.tileX === tileX && preview.tileY === tileY) |
| 474 | { |
| 475 | if (preview.ground !== undefined) |
| 476 | { ground = preview.ground; } |
| 477 | if (preview.block !== undefined) |
| 478 | { block = preview.block; } |
| 479 | } |
| 480 | |
| 481 | const size = this.tileSize(); |
| 482 | const pivot = tilePivot(tileX, tileY, size); |
| 483 | const groundName = this.#groundSpriteName(ground); |
| 484 | objects.ground = this.#ensureSprite(objects.ground, groundName, LAYER_GROUND); |
| 485 | if (objects.ground) |
| 486 | { |
| 487 | this.#placePivoted(objects.ground, pivot.x, pivot.y, pivot.y, 0); |
| 488 | objects.ground.layer = LAYER_GROUND; |
| 489 | } |
| 490 | objects.groundBloom = this.#placeBloomOverlay(objects.groundBloom, this.#groundBloomName(ground), pivot); |
| 491 | |
| 492 | const blockName = this.#blockSpriteName(block); |
| 493 | if (blockName) |
| 494 | { |
| 495 | objects.block = this.#ensureSprite(objects.block, blockName, SORT_BLOCK); |
| 496 | if (objects.block) |
| 497 | { this.#placePivoted(objects.block, pivot.x, pivot.y, pivot.y, SORT_BLOCK); } |
| 498 | } |
| 499 | else if (objects.block) |
| 500 | { |
| 501 | objects.block.destroy(); |
| 502 | objects.block = null; |
| 503 | } |
| 504 | objects.blockBloom = this.#placeBloomOverlay(objects.blockBloom, this.#blockBloomName(block), pivot); |
| 505 | |
| 506 | objects.light = snapshot.light[index] ?? 0; |
| 507 | const previousShade = objects.shade; |
| 508 | objects.shade = this.#tileFill(objects.shade, "night-shade", pivot.x, pivot.y, LAYER_SHADE + tileY); |
| 509 | if (objects.shade && objects.shade !== previousShade) |
| 510 | { objects.shade.renderer.alpha = 0; } |
| 511 | } |
| 512 | |
| 513 | #groundSpriteName (ground) |
| 514 | { |
| 515 | const entry = normalizeGround(ground); |
| 516 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 517 | const states = this.catalog.grounds?.[def.key]?.states ?? []; |
| 518 | const state = resolveState(states, entry.state); |
| 519 | return spriteImageName("ground", def.key, state); |
| 520 | } |
| 521 | |
| 522 | #blockSpriteName (block) |
| 523 | { |
| 524 | const entry = normalizeBlock(block); |
| 525 | if (!entry) |
| 526 | { return null; } |
| 527 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 528 | if (!def) |
| 529 | { return null; } |
| 530 | const states = this.catalog.blocks?.[def.key]?.states ?? []; |
| 531 | const state = resolveState(states, entry.state); |
| 532 | return spriteImageName("block", def.key, state); |
| 533 | } |
| 534 | |
| 535 | #groundBloomName (ground) |
| 536 | { |
| 537 | const entry = normalizeGround(ground); |
| 538 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 539 | const type = this.catalog.grounds?.[def.key]; |
| 540 | const state = resolveState(type?.states ?? [], entry.state); |
| 541 | if (!state || !type?.blooms?.[state]) |
| 542 | { return null; } |
| 543 | return bloomImageName("ground", def.key, state); |
| 544 | } |
| 545 | |
| 546 | #blockBloomName (block) |
| 547 | { |
| 548 | const entry = normalizeBlock(block); |
| 549 | if (!entry) |
| 550 | { return null; } |
| 551 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 552 | if (!def) |
| 553 | { return null; } |
| 554 | const type = this.catalog.blocks?.[def.key]; |
| 555 | const state = resolveState(type?.states ?? [], entry.state); |
| 556 | if (!state || !type?.blooms?.[state]) |
| 557 | { return null; } |
| 558 | return bloomImageName("block", def.key, state); |
| 559 | } |
| 560 | |
| 561 | #placeBloomOverlay (existing, name, pivot) |
| 562 | { |
| 563 | if (!name) |
| 564 | { |
| 565 | existing?.destroy(); |
| 566 | return null; |
| 567 | } |
| 568 | const object = this.#ensureSprite(existing, name, SORT_BLOCK); |
| 569 | if (!object) |
| 570 | { return null; } |
| 571 | object.renderer.layer = RENDER_LAYER_BLOOM; |
| 572 | this.#placePivoted(object, pivot.x, pivot.y, pivot.y, SORT_BLOCK); |
| 573 | return object; |
| 574 | } |
| 575 | |
| 576 | #tileFill (existing, name, x, y, layer) |
| 577 | { |
| 578 | existing = this.#ensureSprite(existing, name, layer); |
| 579 | if (!existing) |
| 580 | { return null; } |
| 581 | const size = this.tileSize(); |
| 582 | existing.transform.position.x = x; |
| 583 | existing.transform.position.y = y; |
| 584 | existing.transform.size = new vector(size, size); |
| 585 | existing.layer = layer; |
| 586 | this.#markSortDirty(); |
| 587 | return existing; |
| 588 | } |
| 589 | |
| 590 | #placePivoted (object, pivotX, pivotY, sortY, subOrder) |
| 591 | { |
| 592 | const sprite = object?.renderer?.sprite; |
| 593 | if (!sprite) |
| 594 | { return; } |
| 595 | const zoom = this.zoom; |
| 596 | const draw = spriteDrawSize(sprite.width, sprite.height, zoom); |
| 597 | const center = spriteCenterFromPivot(pivotX, pivotY, sprite.height, zoom); |
| 598 | object.transform.position.x = center.x; |
| 599 | object.transform.position.y = center.y; |
| 600 | object.transform.size = new vector(draw.w, draw.h); |
| 601 | object.layer = ySortLayer(sortY, subOrder); |
| 602 | this.#markSortDirty(); |
| 603 | } |
| 604 | |
| 605 | #ensureSprite (existing, name, layer) |
| 606 | { |
| 607 | const sprite = this.game.images[name]; |
| 608 | if (!sprite) |
| 609 | { |
| 610 | existing?.destroy(); |
| 611 | return null; |
| 612 | } |
| 613 | if (!existing || existing.renderer.sprite !== sprite) |
| 614 | { |
| 615 | existing?.destroy(); |
| 616 | existing = new GameObject(0, 0, sprite, layer); |
| 617 | this.scene.addObject(existing); |
| 618 | if (sprite.hasAnimationCycle?.("idle") && sprite._animationSpeed > 0) |
| 619 | { existing.renderer.setAnimation("idle", 0, sprite._animationSpeed); } |
| 620 | } |
| 621 | return existing; |
| 622 | } |
| 623 | |
| 624 | #neighborClosed (snapshot, dir, dx, dy) |
| 625 | { |
| 626 | const other = this.chunks.get(`${snapshot.chunkX + dx},${snapshot.chunkY + dy}`); |
| 627 | if (other) |
| 628 | { return !other.open; } |
| 629 | return !!snapshot.closedEdges?.[dir]; |
| 630 | } |
| 631 | |
| 632 | #rebuildAllEdges () |
| 633 | { |
| 634 | const seen = new Set(); |
| 635 | for (const snapshot of this.chunks.values()) |
| 636 | { |
| 637 | const key = `${snapshot.chunkX},${snapshot.chunkY}`; |
| 638 | seen.add(key); |
| 639 | this.#rebuildChunkEdges(snapshot); |
| 640 | } |
| 641 | for (const key of [...this.edges.keys()]) |
| 642 | { |
| 643 | if (!seen.has(key)) |
| 644 | { this.#destroyEdges(key); } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | #rebuildChunkEdges (snapshot) |
| 649 | { |
| 650 | const key = `${snapshot.chunkX},${snapshot.chunkY}`; |
| 651 | let bars = this.edges.get(key); |
| 652 | if (!bars) |
| 653 | { |
| 654 | bars = {}; |
| 655 | this.edges.set(key, bars); |
| 656 | } |
| 657 | |
| 658 | const size = this.tileSize(); |
| 659 | const chunkPx = CHUNK_SIZE * size; |
| 660 | const left = snapshot.chunkX * CHUNK_SIZE * size; |
| 661 | const top = snapshot.chunkY * CHUNK_SIZE * size; |
| 662 | const centerX = left + chunkPx / 2; |
| 663 | const centerY = top + chunkPx / 2; |
| 664 | const thickness = Math.max(2, Math.round(2 * this.zoom)); |
| 665 | const draw = snapshot.open === true; |
| 666 | |
| 667 | bars.north = this.#edgeBar(bars.north, draw && this.#neighborClosed(snapshot, "north", 0, -1), |
| 668 | centerX, top, chunkPx, thickness, true); |
| 669 | bars.south = this.#edgeBar(bars.south, draw && this.#neighborClosed(snapshot, "south", 0, 1), |
| 670 | centerX, top + chunkPx, chunkPx, thickness, true); |
| 671 | bars.west = this.#edgeBar(bars.west, draw && this.#neighborClosed(snapshot, "west", -1, 0), |
| 672 | left, centerY, chunkPx, thickness, false); |
| 673 | bars.east = this.#edgeBar(bars.east, draw && this.#neighborClosed(snapshot, "east", 1, 0), |
| 674 | left + chunkPx, centerY, chunkPx, thickness, false); |
| 675 | } |
| 676 | |
| 677 | #edgeBar (existing, visible, x, y, length, thickness, horizontal) |
| 678 | { |
| 679 | if (!visible) |
| 680 | { |
| 681 | if (existing) |
| 682 | { existing.renderer.visible = false; } |
| 683 | return existing; |
| 684 | } |
| 685 | |
| 686 | existing = this.#ensureSprite(existing, "chunk-edge", LAYER_EDGE); |
| 687 | existing.renderer.visible = true; |
| 688 | existing.transform.position.x = x; |
| 689 | existing.transform.position.y = y; |
| 690 | existing.transform.size = horizontal |
| 691 | ? new vector(length, thickness) |
| 692 | : new vector(thickness, length); |
| 693 | existing.layer = LAYER_EDGE; |
| 694 | this.#markSortDirty(); |
| 695 | return existing; |
| 696 | } |
| 697 | |
| 698 | #destroyEdges (key) |
| 699 | { |
| 700 | const bars = this.edges.get(key); |
| 701 | if (!bars) |
| 702 | { return; } |
| 703 | bars.north?.destroy(); |
| 704 | bars.east?.destroy(); |
| 705 | bars.south?.destroy(); |
| 706 | bars.west?.destroy(); |
| 707 | this.edges.delete(key); |
| 708 | } |
| 709 | |
| 710 | #markSortDirty () |
| 711 | { |
| 712 | this.scene._sortDirty = true; |
| 713 | } |
| 714 | |
| 715 | #updateShade () |
| 716 | { |
| 717 | const hour = this.nightPreview ?? this.worldHour; |
| 718 | const ambientDark = nightFactor(hour, this.settings.nightDarkness); |
| 719 | for (const objects of this.tiles.values()) |
| 720 | { |
| 721 | if (!objects.shade) |
| 722 | { continue; } |
| 723 | const intensity = Math.min(1, Math.max(0, (objects.light ?? 0) / 255)); |
| 724 | objects.shade.renderer.alpha = ambientDark * (1 - intensity); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | #syncBloomLayer () |
| 729 | { |
| 730 | const settings = this.settings ?? DEFAULT_WORLD_SETTINGS; |
| 731 | this.scene.setLayers([ |
| 732 | { name: "default" }, |
| 733 | { |
| 734 | name: RENDER_LAYER_BLOOM, |
| 735 | composite: "add", |
| 736 | postShaders: Shader.getBloomPostChain({ |
| 737 | threshold: settings.bloomThreshold, |
| 738 | softKnee: settings.bloomSoftKnee, |
| 739 | intensity: settings.bloomIntensity, |
| 740 | blurSize: settings.bloomBlurSize, |
| 741 | saturation: settings.bloomSaturation, |
| 742 | colorPreserve: settings.bloomColorPreserve, |
| 743 | dilate: settings.bloomDilate, |
| 744 | scales: Math.max(1, Math.round(settings.bloomScales ?? 1)), |
| 745 | halfRes: (settings.bloomHalfRes ?? 1) >= 0.5 |
| 746 | }) |
| 747 | } |
| 748 | ]); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | export function nightFactor (hour, nightDarkness = 0.72) |
| 753 | { |
| 754 | const peak = Math.min(1, Math.max(0, nightDarkness)); |
| 755 | if (hour >= 8 && hour <= 16) |
| 756 | { return 0; } |
| 757 | if (hour >= 22 || hour <= 5) |
| 758 | { return peak; } |
| 759 | if (hour < 8) |
| 760 | { return lerp(peak, 0, (hour - 5) / 3); } |
| 761 | return lerp(0, peak, (hour - 16) / 6); |
| 762 | } |
| 763 | |
| 764 | function bodySpriteName (sex) |
| 765 | { |
| 766 | const key = sex === "female" ? "player-body-female" : "player-body-male"; |
| 767 | return spriteImageName("character", key, key); |
| 768 | } |
| 769 | |
| 770 | function lerp (a, b, t) |
| 771 | { |
| 772 | return a + (b - a) * Math.min(1, Math.max(0, t)); |
| 773 | } |