import { CHUNK_SIZE, SPRITE_SIZE_PX, GAIT } from "/shared/protocol/constants.js"; import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS } from "/shared/definitions/tiles.js"; export class WorldView { constructor (game, scene) { this.game = game; this.scene = scene; this.zoom = 2; this.chunks = new Map(); this.tiles = new Map(); this.entities = new Map(); this.localEntityId = null; this.settings = { walkTilesPerSecond: 2 }; this.worldHour = 12; this.nightPreview = null; this.followPlayer = true; } setLocalEntity (entityId, tileX, tileY, gait, sex) { this.localEntityId = entityId; this.#upsertEntity({ entityId, tileX, tileY, gait, sex, firstName: "", lastName: "" }); } applySnapshot (snapshot) { const key = `${snapshot.chunkX},${snapshot.chunkY}`; this.chunks.set(key, snapshot); this.#rebuildChunk(snapshot); for (const entity of snapshot.entities) { this.#upsertEntity(entity); } } removeChunk (chunkX, chunkY) { const key = `${chunkX},${chunkY}`; this.chunks.delete(key); for (const [tileKey, objects] of [...this.tiles.entries()]) { if (objects.chunkX === chunkX && objects.chunkY === chunkY) { objects.ground?.destroy(); objects.block?.destroy(); objects.shade?.destroy(); this.tiles.delete(tileKey); } } } moveEntity (payload) { const existing = this.entities.get(payload.entityId); if (!existing) { this.#upsertEntity({ entityId: payload.entityId, tileX: payload.tileX, tileY: payload.tileY, gait: payload.gait, sex: "male" }); return; } existing.fromX = existing.visualX; existing.fromY = existing.visualY; existing.tileX = payload.tileX; existing.tileY = payload.tileY; existing.gait = payload.gait; existing.lerpStarted = performance.now(); existing.lerpMs = this.#lerpMs(payload.gait); } despawnEntity (entityId) { const existing = this.entities.get(entityId); if (!existing) { return; } existing.object?.destroy(); this.entities.delete(entityId); } setZoom (zoom) { this.zoom = Math.min(4, Math.max(1, zoom)); for (const snapshot of this.chunks.values()) { this.#rebuildChunk(snapshot); } const size = this.tileSize(); for (const entity of this.entities.values()) { entity.visualX = entity.tileX * size + size / 2; entity.visualY = entity.tileY * size + size / 2; entity.fromX = entity.visualX; entity.fromY = entity.visualY; entity.lerpStarted = null; if (entity.object) { entity.object.transform.size = new vector(size, size); } } } tileSize () { return SPRITE_SIZE_PX * this.zoom; } screenToTile (mouseX, mouseY) { const size = this.tileSize(); const worldX = mouseX - this.scene.cameraPosition.x; const worldY = mouseY - this.scene.cameraPosition.y; return { tileX: Math.floor(worldX / size), tileY: Math.floor(worldY / size) }; } update (deltaTime) { const now = performance.now(); const size = this.tileSize(); for (const entity of this.entities.values()) { const targetX = entity.tileX * size + size / 2; const targetY = entity.tileY * size + size / 2; if (entity.lerpStarted != null) { const t = Math.min(1, (now - entity.lerpStarted) / entity.lerpMs); entity.visualX = entity.fromX + (targetX - entity.fromX) * t; entity.visualY = entity.fromY + (targetY - entity.fromY) * t; if (t >= 1) { entity.lerpStarted = null; } } else { entity.visualX = targetX; entity.visualY = targetY; } if (entity.object) { entity.object.transform.position.x = entity.visualX; entity.object.transform.position.y = entity.visualY; entity.object.transform.size = new vector(size, size); } } this.#centerCamera(); this.#updateShade(deltaTime); } #lerpMs (gait) { const walk = this.settings.walkTilesPerSecond || 2; const seconds = gait === GAIT.run ? 1 / (walk * 2) : 1 / walk; return seconds * 1000; } #centerCamera () { if (!this.followPlayer) { return; } const local = this.entities.get(this.localEntityId); if (!local) { return; } this.scene.cameraPosition.x = this.scene.real_size.x / 2 - local.visualX; this.scene.cameraPosition.y = this.scene.real_size.y / 2 - local.visualY; } #upsertEntity (payload) { const size = this.tileSize(); let entity = this.entities.get(payload.entityId); if (!entity) { const spriteName = payload.sex === "female" ? "player-body-female" : "player-body-male"; const object = new GameObject(0, 0, this.game.images[spriteName], 2); this.scene.addObject(object); entity = { object, visualX: 0, visualY: 0 }; this.entities.set(payload.entityId, entity); } entity.tileX = payload.tileX; entity.tileY = payload.tileY; entity.gait = payload.gait; entity.sex = payload.sex; entity.visualX = payload.tileX * size + size / 2; entity.visualY = payload.tileY * size + size / 2; entity.fromX = entity.visualX; entity.fromY = entity.visualY; entity.object.transform.position.x = entity.visualX; entity.object.transform.position.y = entity.visualY; entity.object.transform.size = new vector(size, size); } #rebuildChunk (snapshot) { const size = this.tileSize(); for (let localY = 0; localY < CHUNK_SIZE; localY++) { for (let localX = 0; localX < CHUNK_SIZE; localX++) { const index = localY * CHUNK_SIZE + localX; const tileX = snapshot.chunkX * CHUNK_SIZE + localX; const tileY = snapshot.chunkY * CHUNK_SIZE + localY; const key = `${tileX},${tileY}`; let objects = this.tiles.get(key); if (!objects) { objects = { chunkX: snapshot.chunkX, chunkY: snapshot.chunkY }; this.tiles.set(key, objects); } const px = tileX * size + size / 2; const py = tileY * size + size / 2; const groundName = GROUND_DEFINITIONS[snapshot.grounds[index]]?.sprite ?? "ground-grass"; objects.ground = this.#sprite(objects.ground, groundName, px, py, 0, size); const block = snapshot.blocks[index]; const blockName = block ? BLOCK_DEFINITIONS[block.typeId]?.sprite : null; if (blockName) { objects.block = this.#sprite(objects.block, blockName, px, py, 1, size); } else if (objects.block) { objects.block.destroy(); objects.block = null; } objects.light = snapshot.light[index] ?? 0; objects.shade = this.#sprite(objects.shade, "night-shade", px, py, 3, size); objects.shade.renderer.alpha = 0; } } } #sprite (existing, name, x, y, layer, size) { if (!existing || existing.renderer.sprite !== this.game.images[name]) { existing?.destroy(); existing = new GameObject(x, y, this.game.images[name], layer); this.scene.addObject(existing); } existing.transform.position.x = x; existing.transform.position.y = y; existing.transform.size = new vector(size, size); existing.layer = layer; return existing; } #updateShade () { const hour = this.nightPreview ?? this.worldHour; const ambientDark = nightFactor(hour); for (const objects of this.tiles.values()) { if (!objects.shade) { continue; } const lit = (objects.light ?? 0) > 0; objects.shade.renderer.alpha = lit ? ambientDark * 0.12 : ambientDark; } } } export function nightFactor (hour) { if (hour >= 8 && hour <= 16) { return 0; } if (hour >= 22 || hour <= 5) { return 0.72; } if (hour < 8) { return lerp(0.72, 0, (hour - 5) / 3); } return lerp(0, 0.72, (hour - 16) / 6); } function lerp (a, b, t) { return a + (b - a) * Math.min(1, Math.max(0, t)); }