| 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 | |
| 4 | export 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) |
| 93 | { |
| 94 | this.zoom = Math.min(4, Math.max(1, zoom)); |
| 95 | for (const snapshot of this.chunks.values()) |
| 96 | { this.#rebuildChunk(snapshot); } |
| 97 | const size = this.tileSize(); |
| 98 | for (const entity of this.entities.values()) |
| 99 | { |
| 100 | entity.visualX = entity.tileX * size + size / 2; |
| 101 | entity.visualY = entity.tileY * size + size / 2; |
| 102 | entity.fromX = entity.visualX; |
| 103 | entity.fromY = entity.visualY; |
| 104 | entity.lerpStarted = null; |
| 105 | if (entity.object) |
| 106 | { entity.object.transform.size = new vector(size, size); } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | tileSize () |
| 111 | { |
| 112 | return SPRITE_SIZE_PX * this.zoom; |
| 113 | } |
| 114 | |
| 115 | screenToTile (mouseX, mouseY) |
| 116 | { |
| 117 | const size = this.tileSize(); |
| 118 | const worldX = mouseX - this.scene.cameraPosition.x; |
| 119 | const worldY = mouseY - this.scene.cameraPosition.y; |
| 120 | return { |
| 121 | tileX: Math.floor(worldX / size), |
| 122 | tileY: Math.floor(worldY / size) |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | update (deltaTime) |
| 127 | { |
| 128 | const now = performance.now(); |
| 129 | const size = this.tileSize(); |
| 130 | for (const entity of this.entities.values()) |
| 131 | { |
| 132 | const targetX = entity.tileX * size + size / 2; |
| 133 | const targetY = entity.tileY * size + size / 2; |
| 134 | if (entity.lerpStarted != null) |
| 135 | { |
| 136 | const t = Math.min(1, (now - entity.lerpStarted) / entity.lerpMs); |
| 137 | entity.visualX = entity.fromX + (targetX - entity.fromX) * t; |
| 138 | entity.visualY = entity.fromY + (targetY - entity.fromY) * t; |
| 139 | if (t >= 1) |
| 140 | { entity.lerpStarted = null; } |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | entity.visualX = targetX; |
| 145 | entity.visualY = targetY; |
| 146 | } |
| 147 | if (entity.object) |
| 148 | { |
| 149 | entity.object.transform.position.x = entity.visualX; |
| 150 | entity.object.transform.position.y = entity.visualY; |
| 151 | entity.object.transform.size = new vector(size, size); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | this.#centerCamera(); |
| 156 | this.#updateShade(deltaTime); |
| 157 | } |
| 158 | |
| 159 | #lerpMs (gait) |
| 160 | { |
| 161 | const walk = this.settings.walkTilesPerSecond || 2; |
| 162 | const seconds = gait === GAIT.run ? 1 / (walk * 2) : 1 / walk; |
| 163 | return seconds * 1000; |
| 164 | } |
| 165 | |
| 166 | #centerCamera () |
| 167 | { |
| 168 | if (!this.followPlayer) |
| 169 | { return; } |
| 170 | const local = this.entities.get(this.localEntityId); |
| 171 | if (!local) |
| 172 | { return; } |
| 173 | this.scene.cameraPosition.x = this.scene.real_size.x / 2 - local.visualX; |
| 174 | this.scene.cameraPosition.y = this.scene.real_size.y / 2 - local.visualY; |
| 175 | } |
| 176 | |
| 177 | #upsertEntity (payload) |
| 178 | { |
| 179 | const size = this.tileSize(); |
| 180 | let entity = this.entities.get(payload.entityId); |
| 181 | if (!entity) |
| 182 | { |
| 183 | const spriteName = payload.sex === "female" ? "player-body-female" : "player-body-male"; |
| 184 | const object = new GameObject(0, 0, this.game.images[spriteName], 2); |
| 185 | this.scene.addObject(object); |
| 186 | entity = { object, visualX: 0, visualY: 0 }; |
| 187 | this.entities.set(payload.entityId, entity); |
| 188 | } |
| 189 | entity.tileX = payload.tileX; |
| 190 | entity.tileY = payload.tileY; |
| 191 | entity.gait = payload.gait; |
| 192 | entity.sex = payload.sex; |
| 193 | entity.visualX = payload.tileX * size + size / 2; |
| 194 | entity.visualY = payload.tileY * size + size / 2; |
| 195 | entity.fromX = entity.visualX; |
| 196 | entity.fromY = entity.visualY; |
| 197 | entity.object.transform.position.x = entity.visualX; |
| 198 | entity.object.transform.position.y = entity.visualY; |
| 199 | entity.object.transform.size = new vector(size, size); |
| 200 | } |
| 201 | |
| 202 | #rebuildChunk (snapshot) |
| 203 | { |
| 204 | const size = this.tileSize(); |
| 205 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 206 | { |
| 207 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 208 | { |
| 209 | const index = localY * CHUNK_SIZE + localX; |
| 210 | const tileX = snapshot.chunkX * CHUNK_SIZE + localX; |
| 211 | const tileY = snapshot.chunkY * CHUNK_SIZE + localY; |
| 212 | const key = `${tileX},${tileY}`; |
| 213 | let objects = this.tiles.get(key); |
| 214 | if (!objects) |
| 215 | { |
| 216 | objects = { chunkX: snapshot.chunkX, chunkY: snapshot.chunkY }; |
| 217 | this.tiles.set(key, objects); |
| 218 | } |
| 219 | const px = tileX * size + size / 2; |
| 220 | const py = tileY * size + size / 2; |
| 221 | const groundName = GROUND_DEFINITIONS[snapshot.grounds[index]]?.sprite ?? "ground-grass"; |
| 222 | objects.ground = this.#sprite(objects.ground, groundName, px, py, 0, size); |
| 223 | const block = snapshot.blocks[index]; |
| 224 | const blockName = block ? BLOCK_DEFINITIONS[block.typeId]?.sprite : null; |
| 225 | if (blockName) |
| 226 | { objects.block = this.#sprite(objects.block, blockName, px, py, 1, size); } |
| 227 | else if (objects.block) |
| 228 | { |
| 229 | objects.block.destroy(); |
| 230 | objects.block = null; |
| 231 | } |
| 232 | objects.light = snapshot.light[index] ?? 0; |
| 233 | objects.shade = this.#sprite(objects.shade, "night-shade", px, py, 3, size); |
| 234 | objects.shade.renderer.alpha = 0; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | #sprite (existing, name, x, y, layer, size) |
| 240 | { |
| 241 | if (!existing || existing.renderer.sprite !== this.game.images[name]) |
| 242 | { |
| 243 | existing?.destroy(); |
| 244 | existing = new GameObject(x, y, this.game.images[name], layer); |
| 245 | this.scene.addObject(existing); |
| 246 | } |
| 247 | existing.transform.position.x = x; |
| 248 | existing.transform.position.y = y; |
| 249 | existing.transform.size = new vector(size, size); |
| 250 | existing.layer = layer; |
| 251 | return existing; |
| 252 | } |
| 253 | |
| 254 | #updateShade () |
| 255 | { |
| 256 | const hour = this.nightPreview ?? this.worldHour; |
| 257 | const ambientDark = nightFactor(hour); |
| 258 | for (const objects of this.tiles.values()) |
| 259 | { |
| 260 | if (!objects.shade) |
| 261 | { continue; } |
| 262 | const lit = (objects.light ?? 0) > 0; |
| 263 | objects.shade.renderer.alpha = lit ? ambientDark * 0.12 : ambientDark; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | export function nightFactor (hour) |
| 269 | { |
| 270 | if (hour >= 8 && hour <= 16) |
| 271 | { return 0; } |
| 272 | if (hour >= 22 || hour <= 5) |
| 273 | { return 0.72; } |
| 274 | if (hour < 8) |
| 275 | { return lerp(0.72, 0, (hour - 5) / 3); } |
| 276 | return lerp(0, 0.72, (hour - 16) / 6); |
| 277 | } |
| 278 | |
| 279 | function lerp (a, b, t) |
| 280 | { |
| 281 | return a + (b - a) * Math.min(1, Math.max(0, t)); |
| 282 | } |