| 1 | import { MessageType, CHUNK_SIZE, SPRITE_SIZE_PX, VISIBLE_CHUNK_RADIUS } from "/shared/protocol/constants.js"; |
| 2 | import { |
| 3 | GROUND_DEFINITIONS, |
| 4 | BLOCK_DEFINITIONS, |
| 5 | GroundType, |
| 6 | BlockType, |
| 7 | normalizeGround, |
| 8 | normalizeBlock, |
| 9 | listPlaceableBlockPaletteEntries |
| 10 | } from "/shared/definitions/tiles.js"; |
| 11 | import { cycleState, defaultStateForType, resolveState, statesForType } from "/shared/definitions/tileSprites.js"; |
| 12 | import { defaultSpawnerConfig, normalizeSpawnerConfig, mobDefinition, mobSpriteName } from "/shared/definitions/mobs.js"; |
| 13 | import { GameSocket, defaultSocketUrl } from "../net/socket.js"; |
| 14 | import { loadSpriteCatalog, preloadCatalogImages, applySpriteSheets, useNearestNeighbour } from "../render/sprites.js"; |
| 15 | import { WorldView } from "../render/worldView.js"; |
| 16 | import { initSettingsModal } from "./settingsModal.js"; |
| 17 | import { initPaletteModal } from "./paletteModal.js"; |
| 18 | import { initTileConfigModal } from "./tileConfigModal.js"; |
| 19 | |
| 20 | const game = new Game(); |
| 21 | let socket; |
| 22 | let scene; |
| 23 | let view; |
| 24 | let draft = null; |
| 25 | let chunkX = 0; |
| 26 | let chunkY = 0; |
| 27 | let tool = { kind: "ground", typeId: GroundType.grass, state: null }; |
| 28 | let settingsModal; |
| 29 | let paletteModal; |
| 30 | let tileConfigModal; |
| 31 | let catalog = { grounds: {}, blocks: {}, images: [] }; |
| 32 | let imageUrlByName = new Map(); |
| 33 | let hoverLocal = null; |
| 34 | let editorZoom = null; |
| 35 | let activeRevisionId = null; |
| 36 | let pendingRevisionId = null; |
| 37 | let applyInFlight = false; |
| 38 | const EDITOR_ZOOM_MIN = 0.25; |
| 39 | const EDITOR_ZOOM_MAX = 8; |
| 40 | const EDITOR_ZOOM_STEP = 1.12; |
| 41 | |
| 42 | function $ (id) |
| 43 | { |
| 44 | return document.getElementById(id); |
| 45 | } |
| 46 | |
| 47 | function selectedToolLabel () |
| 48 | { |
| 49 | if (tool.kind === "erase") |
| 50 | { return "Erase block"; } |
| 51 | if (tool.kind === "spawner") |
| 52 | { |
| 53 | const def = mobDefinition(tool.mobTypeId); |
| 54 | return `Spawner · ${def?.displayName ?? tool.mobTypeId}`; |
| 55 | } |
| 56 | const def = tool.kind === "ground" ? GROUND_DEFINITIONS[tool.typeId] : BLOCK_DEFINITIONS[tool.typeId]; |
| 57 | const layer = tool.kind === "ground" ? "ground" : "block"; |
| 58 | const states = statesForType(catalog, layer, def.key); |
| 59 | const state = resolveState(states, tool.state); |
| 60 | if (tool.kind === "block" && def.id === BlockType.boulder) |
| 61 | { |
| 62 | const entry = listPlaceableBlockPaletteEntries(catalog) |
| 63 | .find((row) => row.typeId === def.id && row.state === state); |
| 64 | if (entry) |
| 65 | { return entry.displayName; } |
| 66 | } |
| 67 | if (states.length > 1) |
| 68 | { return `${def.displayName} (${state})`; } |
| 69 | return def.displayName; |
| 70 | } |
| 71 | |
| 72 | function selectTool (next) |
| 73 | { |
| 74 | if (next.kind === "erase") |
| 75 | { |
| 76 | tool = { kind: "erase" }; |
| 77 | return; |
| 78 | } |
| 79 | if (next.kind === "spawner") |
| 80 | { |
| 81 | tool = { kind: "spawner", mobTypeId: next.mobTypeId }; |
| 82 | return; |
| 83 | } |
| 84 | const layer = next.kind === "ground" ? "ground" : "block"; |
| 85 | const def = next.kind === "ground" ? GROUND_DEFINITIONS[next.typeId] : BLOCK_DEFINITIONS[next.typeId]; |
| 86 | tool = { |
| 87 | kind: next.kind, |
| 88 | typeId: next.typeId, |
| 89 | state: next.state ?? defaultStateForType(catalog, layer, def.key) |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | function placeableToolState () |
| 94 | { |
| 95 | if (tool.kind === "erase" || tool.kind === "spawner") |
| 96 | { return null; } |
| 97 | const layer = tool.kind === "ground" ? "ground" : "block"; |
| 98 | const def = tool.kind === "ground" ? GROUND_DEFINITIONS[tool.typeId] : BLOCK_DEFINITIONS[tool.typeId]; |
| 99 | return resolveState(statesForType(catalog, layer, def.key), tool.state); |
| 100 | } |
| 101 | |
| 102 | function previewFromTool () |
| 103 | { |
| 104 | if (tool.kind === "erase") |
| 105 | { return { block: null }; } |
| 106 | if (tool.kind === "spawner") |
| 107 | { return {}; } |
| 108 | const state = placeableToolState(); |
| 109 | if (tool.kind === "ground") |
| 110 | { return { ground: { typeId: tool.typeId, state } }; } |
| 111 | return { block: { typeId: tool.typeId, state, config: null } }; |
| 112 | } |
| 113 | |
| 114 | function inEditorView (x, y) |
| 115 | { |
| 116 | return Math.abs(x - chunkX) <= VISIBLE_CHUNK_RADIUS && Math.abs(y - chunkY) <= VISIBLE_CHUNK_RADIUS; |
| 117 | } |
| 118 | |
| 119 | function neighborhoodCoords (centerX, centerY) |
| 120 | { |
| 121 | const coords = []; |
| 122 | for (let dy = -VISIBLE_CHUNK_RADIUS; dy <= VISIBLE_CHUNK_RADIUS; dy++) |
| 123 | { |
| 124 | for (let dx = -VISIBLE_CHUNK_RADIUS; dx <= VISIBLE_CHUNK_RADIUS; dx++) |
| 125 | { coords.push({ chunkX: centerX + dx, chunkY: centerY + dy }); } |
| 126 | } |
| 127 | return coords; |
| 128 | } |
| 129 | |
| 130 | function snapshotFromPayload (payload) |
| 131 | { |
| 132 | return { |
| 133 | chunkX: payload.chunkX, |
| 134 | chunkY: payload.chunkY, |
| 135 | open: payload.data.open, |
| 136 | closedEdges: payload.closedEdges, |
| 137 | grounds: payload.data.grounds, |
| 138 | blocks: payload.data.blocks, |
| 139 | light: payload.data.light, |
| 140 | entities: [] |
| 141 | }; |
| 142 | } |
| 143 | |
| 144 | function syncSpawnersOverlay (cx, cy, spawners) |
| 145 | { |
| 146 | view.setEditorSpawners(cx, cy, spawners ?? [], (mobTypeId) => mobSpriteName(mobTypeId)); |
| 147 | } |
| 148 | |
| 149 | function spawnerAt (localX, localY) |
| 150 | { |
| 151 | return (draft?.spawners ?? []).find((entry) => entry.localX === localX && entry.localY === localY) ?? null; |
| 152 | } |
| 153 | |
| 154 | function paintAt (localX, localY) |
| 155 | { |
| 156 | if (!draft || localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE) |
| 157 | { return; } |
| 158 | const index = localY * CHUNK_SIZE + localX; |
| 159 | const state = placeableToolState(); |
| 160 | if (tool.kind === "ground") |
| 161 | { draft.grounds[index] = { typeId: tool.typeId, state }; } |
| 162 | else if (tool.kind === "erase") |
| 163 | { draft.blocks[index] = null; } |
| 164 | else if (tool.kind === "spawner") |
| 165 | { |
| 166 | const existing = spawnerAt(localX, localY); |
| 167 | const spawnerId = existing?.config?.spawnerId ?? `${chunkX}:${chunkY}:${index}`; |
| 168 | const entry = { |
| 169 | localX, |
| 170 | localY, |
| 171 | config: normalizeSpawnerConfig({ |
| 172 | ...defaultSpawnerConfig(spawnerId, tool.mobTypeId), |
| 173 | ...(existing?.config ?? {}), |
| 174 | spawnerId, |
| 175 | mobTypeId: tool.mobTypeId |
| 176 | }, spawnerId) |
| 177 | }; |
| 178 | draft.spawners = (draft.spawners ?? []).filter((s) => s.localX !== localX || s.localY !== localY); |
| 179 | draft.spawners.push(entry); |
| 180 | syncSpawnersOverlay(chunkX, chunkY, draft.spawners); |
| 181 | return; |
| 182 | } |
| 183 | else |
| 184 | { draft.blocks[index] = { typeId: tool.typeId, state, config: null }; } |
| 185 | view.updateAuthoredTile(chunkX, chunkY, localX, localY, { |
| 186 | ground: draft.grounds[index], |
| 187 | block: draft.blocks[index], |
| 188 | light: draft.light[index] |
| 189 | }); |
| 190 | } |
| 191 | |
| 192 | function updateRevisionUI () |
| 193 | { |
| 194 | const applyBtn = $("apply"); |
| 195 | $("revision").textContent = `active ${activeRevisionId ?? "?"}` + |
| 196 | (pendingRevisionId ? ` · pending ${pendingRevisionId}` : " · no pending revision"); |
| 197 | if (applyBtn) |
| 198 | { |
| 199 | applyBtn.disabled = !pendingRevisionId || applyInFlight; |
| 200 | applyBtn.title = pendingRevisionId |
| 201 | ? "Reload the live world with the pending map revision" |
| 202 | : "Save a chunk first to create a pending revision"; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | function setRevisionState ({ activeRevisionId: active, pendingRevisionId: pending }) |
| 207 | { |
| 208 | if (active != null) |
| 209 | { activeRevisionId = active; } |
| 210 | pendingRevisionId = pending ?? null; |
| 211 | updateRevisionUI(); |
| 212 | } |
| 213 | |
| 214 | function showSelectedChunk (payload) |
| 215 | { |
| 216 | draft = { |
| 217 | open: payload.data.open, |
| 218 | grounds: payload.data.grounds.map((ground) => normalizeGround(ground)), |
| 219 | blocks: payload.data.blocks.map((block) => (block ? { ...block, config: block.config ?? null } : null)), |
| 220 | light: payload.data.light.slice(), |
| 221 | spawners: (payload.data.spawners ?? []).map((entry) => ({ |
| 222 | localX: entry.localX, |
| 223 | localY: entry.localY, |
| 224 | config: { ...entry.config } |
| 225 | })) |
| 226 | }; |
| 227 | $("coords").textContent = `${payload.chunkX}, ${payload.chunkY}`; |
| 228 | $("open").checked = draft.open; |
| 229 | setRevisionState({ |
| 230 | activeRevisionId: payload.activeRevisionId, |
| 231 | pendingRevisionId: payload.pendingRevisionId |
| 232 | }); |
| 233 | $("authored").textContent = payload.authored ? "authored" : "default grass (closed until saved open)"; |
| 234 | const log = $("changelog"); |
| 235 | log.innerHTML = ""; |
| 236 | for (const entry of payload.changelog ?? []) |
| 237 | { |
| 238 | const item = document.createElement("article"); |
| 239 | item.innerHTML = `<header>${entry.editor_admin_id} · ${new Date(entry.edited_at).toLocaleString()}</header>`; |
| 240 | const list = document.createElement("ul"); |
| 241 | for (const bullet of entry.diffEntries) |
| 242 | { |
| 243 | const li = document.createElement("li"); |
| 244 | li.textContent = bullet; |
| 245 | list.append(li); |
| 246 | } |
| 247 | item.append(list); |
| 248 | log.append(item); |
| 249 | } |
| 250 | view.applySnapshot(snapshotFromPayload(payload)); |
| 251 | syncSpawnersOverlay(payload.chunkX, payload.chunkY, draft.spawners); |
| 252 | } |
| 253 | |
| 254 | function pruneView () |
| 255 | { |
| 256 | for (const key of [...view.chunks.keys()]) |
| 257 | { |
| 258 | const [x, y] = key.split(",").map(Number); |
| 259 | if (!inEditorView(x, y)) |
| 260 | { view.removeChunk(x, y); } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | function frameNeighborhood () |
| 265 | { |
| 266 | const size = view.tileSize(); |
| 267 | view.followPlayer = false; |
| 268 | const centerX = (chunkX + 0.5) * CHUNK_SIZE * size; |
| 269 | const centerY = (chunkY + 0.5) * CHUNK_SIZE * size; |
| 270 | view.scene.cameraPosition.x = view.scene.real_size.x / 2 - centerX; |
| 271 | view.scene.cameraPosition.y = view.scene.real_size.y / 2 - centerY; |
| 272 | updateSelectionBorder(); |
| 273 | } |
| 274 | |
| 275 | function updateSelectionBorder () |
| 276 | { |
| 277 | const border = $("selection-border"); |
| 278 | const canvas = scene?.canvas; |
| 279 | if (!border || !canvas) |
| 280 | { return; } |
| 281 | |
| 282 | const rect = canvas.getBoundingClientRect(); |
| 283 | const size = view.tileSize(); |
| 284 | const chunkPx = CHUNK_SIZE * size; |
| 285 | const screenLeft = chunkX * CHUNK_SIZE * size + scene.cameraPosition.x; |
| 286 | const screenTop = chunkY * CHUNK_SIZE * size + scene.cameraPosition.y; |
| 287 | border.style.left = `${rect.left + screenLeft}px`; |
| 288 | border.style.top = `${rect.top + screenTop}px`; |
| 289 | border.style.width = `${chunkPx}px`; |
| 290 | border.style.height = `${chunkPx}px`; |
| 291 | } |
| 292 | |
| 293 | function layoutScene () |
| 294 | { |
| 295 | const header = document.querySelector("body > header"); |
| 296 | const aside = document.querySelector("body > aside"); |
| 297 | const changelog = $("changelog-wrap"); |
| 298 | const top = header.offsetHeight; |
| 299 | aside.style.top = `${top}px`; |
| 300 | changelog.style.top = `${top}px`; |
| 301 | const left = aside.offsetWidth; |
| 302 | const right = changelog.offsetWidth; |
| 303 | const width = Math.max(64, window.innerWidth - left - right); |
| 304 | const height = Math.max(64, window.innerHeight - top); |
| 305 | scene.position.x = left; |
| 306 | scene.position.y = top; |
| 307 | scene.size.x = width; |
| 308 | scene.size.y = height; |
| 309 | scene.g_fixSize(); |
| 310 | useNearestNeighbour(scene, game.images); |
| 311 | const fit = Math.min(width, height) / (CHUNK_SIZE * 3 * SPRITE_SIZE_PX); |
| 312 | if (editorZoom == null) |
| 313 | { editorZoom = fit; } |
| 314 | else |
| 315 | { editorZoom = Math.min(EDITOR_ZOOM_MAX, Math.max(EDITOR_ZOOM_MIN, editorZoom)); } |
| 316 | view.setZoom(editorZoom, { min: EDITOR_ZOOM_MIN, max: EDITOR_ZOOM_MAX }); |
| 317 | frameNeighborhood(); |
| 318 | } |
| 319 | |
| 320 | function zoomEditor (direction) |
| 321 | { |
| 322 | if (!view) |
| 323 | { return; } |
| 324 | const factor = direction > 0 ? EDITOR_ZOOM_STEP : 1 / EDITOR_ZOOM_STEP; |
| 325 | editorZoom = Math.min(EDITOR_ZOOM_MAX, Math.max(EDITOR_ZOOM_MIN, (editorZoom ?? view.zoom) * factor)); |
| 326 | view.setZoom(editorZoom, { min: EDITOR_ZOOM_MIN, max: EDITOR_ZOOM_MAX }); |
| 327 | useNearestNeighbour(scene, game.images); |
| 328 | frameNeighborhood(); |
| 329 | } |
| 330 | |
| 331 | function requestChunks (coords) |
| 332 | { |
| 333 | for (const coord of coords) |
| 334 | { |
| 335 | socket.send({ type: MessageType.editorLoadChunk, chunkX: coord.chunkX, chunkY: coord.chunkY }); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | function loadNeighborhood (x, y) |
| 340 | { |
| 341 | chunkX = x; |
| 342 | chunkY = y; |
| 343 | $("coords").textContent = `${chunkX}, ${chunkY}`; |
| 344 | requestChunks(neighborhoodCoords(x, y)); |
| 345 | } |
| 346 | |
| 347 | function requestNeighbors () |
| 348 | { |
| 349 | requestChunks(neighborhoodCoords(chunkX, chunkY).filter((coord) => coord.chunkX !== chunkX || coord.chunkY !== chunkY)); |
| 350 | } |
| 351 | |
| 352 | function anyModalOpen () |
| 353 | { |
| 354 | return settingsModal?.isOpen() || paletteModal?.isOpen() || tileConfigModal?.isOpen(); |
| 355 | } |
| 356 | |
| 357 | function updateTooltip (local) |
| 358 | { |
| 359 | const tip = $("tile-tooltip"); |
| 360 | if (!tip || !local || !draft) |
| 361 | { |
| 362 | if (tip) |
| 363 | { tip.hidden = true; } |
| 364 | view?.clearSpawnRangeHighlight(); |
| 365 | return; |
| 366 | } |
| 367 | const block = normalizeBlock(draft.blocks[local.y * CHUNK_SIZE + local.x]); |
| 368 | const spawner = spawnerAt(local.x, local.y); |
| 369 | const lines = []; |
| 370 | if (block) |
| 371 | { |
| 372 | const def = BLOCK_DEFINITIONS[block.typeId]; |
| 373 | lines.push(def?.displayName ?? `Block ${block.typeId}`); |
| 374 | if (block.config && Object.keys(block.config).length) |
| 375 | { |
| 376 | for (const [key, value] of Object.entries(block.config)) |
| 377 | { lines.push(`${key}: ${value}`); } |
| 378 | } |
| 379 | } |
| 380 | if (spawner) |
| 381 | { |
| 382 | const config = normalizeSpawnerConfig(spawner.config, spawner.config.spawnerId); |
| 383 | const mob = mobDefinition(config.mobTypeId); |
| 384 | lines.push(`Spawner · ${mob?.displayName ?? config.mobTypeId}`); |
| 385 | lines.push(`maxAlive: ${config.maxAlive}`); |
| 386 | lines.push(`spawnRange: ${config.spawnRangeTiles}`); |
| 387 | lines.push(`wanderRange: ${config.maxWanderRangeTiles}`); |
| 388 | lines.push(`id: ${config.spawnerId}`); |
| 389 | view.setSpawnRangeHighlight( |
| 390 | local.tileX, |
| 391 | local.tileY, |
| 392 | config.spawnRangeTiles, |
| 393 | config.maxWanderRangeTiles |
| 394 | ); |
| 395 | } |
| 396 | else |
| 397 | { view.clearSpawnRangeHighlight(); } |
| 398 | if (!lines.length) |
| 399 | { |
| 400 | tip.hidden = true; |
| 401 | return; |
| 402 | } |
| 403 | tip.hidden = false; |
| 404 | tip.textContent = lines.join("\n"); |
| 405 | const canvas = scene.canvas; |
| 406 | const rect = canvas.getBoundingClientRect(); |
| 407 | const size = view.tileSize(); |
| 408 | const screenX = local.tileX * size + scene.cameraPosition.x + rect.left + size; |
| 409 | const screenY = local.tileY * size + scene.cameraPosition.y + rect.top; |
| 410 | tip.style.left = `${screenX}px`; |
| 411 | tip.style.top = `${screenY}px`; |
| 412 | } |
| 413 | |
| 414 | async function main () |
| 415 | { |
| 416 | catalog = await loadSpriteCatalog(); |
| 417 | imageUrlByName = new Map((catalog.images ?? []).map((entry) => [entry.name, entry.url])); |
| 418 | await preloadCatalogImages(game, catalog); |
| 419 | applySpriteSheets(game.images, catalog); |
| 420 | scene = new Scene(250, 56, 64, 64); |
| 421 | game.loadScene(scene); |
| 422 | useNearestNeighbour(scene, game.images); |
| 423 | view = new WorldView(game, scene, catalog); |
| 424 | view.followPlayer = false; |
| 425 | layoutScene(); |
| 426 | selectTool(tool); |
| 427 | $("tool").textContent = selectedToolLabel(); |
| 428 | |
| 429 | paletteModal = initPaletteModal({ |
| 430 | catalog, |
| 431 | imageUrlFor (name) |
| 432 | { |
| 433 | const sprite = game.images[name]; |
| 434 | const html = sprite?.g_image; |
| 435 | if (html && typeof html.toDataURL === "function") |
| 436 | { |
| 437 | try |
| 438 | { return html.toDataURL("image/png"); } |
| 439 | catch (_) |
| 440 | { /* fall through */ } |
| 441 | } |
| 442 | return imageUrlByName.get(name) ?? null; |
| 443 | }, |
| 444 | onSelect (next) |
| 445 | { |
| 446 | selectTool(next); |
| 447 | $("tool").textContent = selectedToolLabel(); |
| 448 | } |
| 449 | }); |
| 450 | |
| 451 | tileConfigModal = initTileConfigModal({ |
| 452 | onSave (ctx, config) |
| 453 | { |
| 454 | if (!draft) |
| 455 | { return; } |
| 456 | draft.spawners = (draft.spawners ?? []).filter((s) => s.localX !== ctx.localX || s.localY !== ctx.localY); |
| 457 | draft.spawners.push({ localX: ctx.localX, localY: ctx.localY, config }); |
| 458 | syncSpawnersOverlay(chunkX, chunkY, draft.spawners); |
| 459 | }, |
| 460 | onRemove (ctx) |
| 461 | { |
| 462 | if (!draft) |
| 463 | { return; } |
| 464 | draft.spawners = (draft.spawners ?? []).filter((s) => s.localX !== ctx.localX || s.localY !== ctx.localY); |
| 465 | syncSpawnersOverlay(chunkX, chunkY, draft.spawners); |
| 466 | } |
| 467 | }); |
| 468 | |
| 469 | const ticker = new GameObject(0, 0, null, 10); |
| 470 | ticker.update = (dt) => |
| 471 | { |
| 472 | hoverLocal = null; |
| 473 | const canvas = scene.canvas; |
| 474 | const rect = canvas.getBoundingClientRect(); |
| 475 | const mouseX = GameInput.mousePosition.x - rect.left; |
| 476 | const mouseY = GameInput.mousePosition.y - rect.top; |
| 477 | const overCanvas = mouseX >= 0 && mouseY >= 0 && mouseX < rect.width && mouseY < rect.height; |
| 478 | if (overCanvas && draft && !anyModalOpen()) |
| 479 | { |
| 480 | const tile = view.screenToTile(mouseX, mouseY); |
| 481 | const localX = tile.tileX - chunkX * CHUNK_SIZE; |
| 482 | const localY = tile.tileY - chunkY * CHUNK_SIZE; |
| 483 | if (localX >= 0 && localY >= 0 && localX < CHUNK_SIZE && localY < CHUNK_SIZE) |
| 484 | { |
| 485 | hoverLocal = { x: localX, y: localY, tileX: tile.tileX, tileY: tile.tileY }; |
| 486 | view.setTilePreview(tile.tileX, tile.tileY, previewFromTool()); |
| 487 | updateTooltip(hoverLocal); |
| 488 | if (GameInput.mouseHeld(GameInput.MouseButtons.Left)) |
| 489 | { paintAt(localX, localY); } |
| 490 | } |
| 491 | else |
| 492 | { |
| 493 | view.clearTilePreview(); |
| 494 | updateTooltip(null); |
| 495 | } |
| 496 | } |
| 497 | else |
| 498 | { |
| 499 | view.clearTilePreview(); |
| 500 | updateTooltip(null); |
| 501 | } |
| 502 | view.update(dt); |
| 503 | frameNeighborhood(); |
| 504 | }; |
| 505 | scene.addObject(ticker); |
| 506 | window.addEventListener("resize", layoutScene); |
| 507 | |
| 508 | socket = new GameSocket(defaultSocketUrl(), (message) => |
| 509 | { |
| 510 | if (message.type === MessageType.session) |
| 511 | { |
| 512 | $("who").textContent = `${message.authSubject}${message.isAdmin ? " · admin" : ""}`; |
| 513 | socket.send({ type: MessageType.editorLoadSettings }); |
| 514 | loadNeighborhood(0, 0); |
| 515 | } |
| 516 | else if (message.type === MessageType.editorSettings) |
| 517 | { settingsModal.handleMessage(message); } |
| 518 | else if (message.type === MessageType.editorChunk) |
| 519 | { |
| 520 | if (message.settings) |
| 521 | { view.setSettings(message.settings); } |
| 522 | if (!inEditorView(message.chunkX, message.chunkY)) |
| 523 | { return; } |
| 524 | if (message.chunkX === chunkX && message.chunkY === chunkY) |
| 525 | { showSelectedChunk(message); } |
| 526 | else |
| 527 | { |
| 528 | view.applySnapshot(snapshotFromPayload(message)); |
| 529 | syncSpawnersOverlay(message.chunkX, message.chunkY, message.data.spawners); |
| 530 | } |
| 531 | pruneView(); |
| 532 | } |
| 533 | else if (message.type === MessageType.editorRevisionApplied) |
| 534 | { |
| 535 | applyInFlight = false; |
| 536 | setRevisionState({ |
| 537 | activeRevisionId: message.activeRevisionId, |
| 538 | pendingRevisionId: message.pendingRevisionId |
| 539 | }); |
| 540 | const status = $("apply-status"); |
| 541 | status.textContent = `Applied revision ${message.appliedRevisionId}. Live world reloaded.`; |
| 542 | status.className = "hint ok"; |
| 543 | requestChunks(neighborhoodCoords(chunkX, chunkY)); |
| 544 | } |
| 545 | else if (message.type === MessageType.error) |
| 546 | { |
| 547 | $("error").textContent = message.message; |
| 548 | if (applyInFlight) |
| 549 | { |
| 550 | applyInFlight = false; |
| 551 | $("apply-status").textContent = ""; |
| 552 | $("apply-status").className = "hint"; |
| 553 | updateRevisionUI(); |
| 554 | } |
| 555 | } |
| 556 | }); |
| 557 | settingsModal = initSettingsModal({ |
| 558 | socket, |
| 559 | MessageType, |
| 560 | onApplied (settings) |
| 561 | { |
| 562 | view.setSettings(settings); |
| 563 | }, |
| 564 | onPreviewNight (hour) |
| 565 | { |
| 566 | view.nightPreview = hour; |
| 567 | $("night").value = String(hour); |
| 568 | $("night-label").textContent = Number(hour).toFixed(1); |
| 569 | } |
| 570 | }); |
| 571 | game.startGame(); |
| 572 | await socket.connect(); |
| 573 | } |
| 574 | |
| 575 | $("tool").textContent = selectedToolLabel(); |
| 576 | $("tool-erase").addEventListener("click", () => |
| 577 | { |
| 578 | selectTool({ kind: "erase" }); |
| 579 | $("tool").textContent = selectedToolLabel(); |
| 580 | }); |
| 581 | |
| 582 | $("north").addEventListener("click", () => loadNeighborhood(chunkX, chunkY - 1)); |
| 583 | $("south").addEventListener("click", () => loadNeighborhood(chunkX, chunkY + 1)); |
| 584 | $("west").addEventListener("click", () => loadNeighborhood(chunkX - 1, chunkY)); |
| 585 | $("east").addEventListener("click", () => loadNeighborhood(chunkX + 1, chunkY)); |
| 586 | $("open").addEventListener("change", () => |
| 587 | { |
| 588 | if (draft) |
| 589 | { |
| 590 | draft.open = $("open").checked; |
| 591 | view.setChunkOpen(chunkX, chunkY, draft.open); |
| 592 | } |
| 593 | }); |
| 594 | $("save").addEventListener("click", () => |
| 595 | { |
| 596 | $("error").textContent = ""; |
| 597 | $("apply-status").textContent = ""; |
| 598 | $("apply-status").className = "hint"; |
| 599 | socket.send({ |
| 600 | type: MessageType.editorSaveChunk, |
| 601 | chunkX, |
| 602 | chunkY, |
| 603 | draft: { |
| 604 | open: $("open").checked, |
| 605 | grounds: draft.grounds, |
| 606 | blocks: draft.blocks, |
| 607 | spawners: draft.spawners ?? [] |
| 608 | } |
| 609 | }); |
| 610 | requestNeighbors(); |
| 611 | }); |
| 612 | $("apply").addEventListener("click", () => |
| 613 | { |
| 614 | if (!pendingRevisionId || applyInFlight) |
| 615 | { return; } |
| 616 | $("error").textContent = ""; |
| 617 | const status = $("apply-status"); |
| 618 | status.textContent = "Applying…"; |
| 619 | status.className = "hint"; |
| 620 | applyInFlight = true; |
| 621 | updateRevisionUI(); |
| 622 | socket.send({ type: MessageType.editorApplyRevision }); |
| 623 | }); |
| 624 | $("night").addEventListener("input", () => |
| 625 | { |
| 626 | view.nightPreview = Number($("night").value); |
| 627 | $("night-label").textContent = view.nightPreview.toFixed(1); |
| 628 | }); |
| 629 | |
| 630 | function cycleToolState (direction) |
| 631 | { |
| 632 | if (tool.kind === "erase" || tool.kind === "spawner") |
| 633 | { return; } |
| 634 | const layer = tool.kind === "ground" ? "ground" : "block"; |
| 635 | const def = tool.kind === "ground" ? GROUND_DEFINITIONS[tool.typeId] : BLOCK_DEFINITIONS[tool.typeId]; |
| 636 | const states = statesForType(catalog, layer, def.key); |
| 637 | if (states.length < 2) |
| 638 | { return; } |
| 639 | tool.state = cycleState(states, tool.state, direction); |
| 640 | $("tool").textContent = selectedToolLabel(); |
| 641 | if (!draft || !hoverLocal) |
| 642 | { return; } |
| 643 | const index = hoverLocal.y * CHUNK_SIZE + hoverLocal.x; |
| 644 | if (tool.kind === "ground" && normalizeGround(draft.grounds[index]).typeId === tool.typeId) |
| 645 | { paintAt(hoverLocal.x, hoverLocal.y); } |
| 646 | else if (tool.kind === "block" && draft.blocks[index]?.typeId === tool.typeId) |
| 647 | { paintAt(hoverLocal.x, hoverLocal.y); } |
| 648 | } |
| 649 | |
| 650 | document.addEventListener("wheel", (event) => |
| 651 | { |
| 652 | if (!scene?.canvas || anyModalOpen()) |
| 653 | { return; } |
| 654 | const rect = scene.canvas.getBoundingClientRect(); |
| 655 | if (event.clientX < rect.left || event.clientX >= rect.right || event.clientY < rect.top || event.clientY >= rect.bottom) |
| 656 | { return; } |
| 657 | event.preventDefault(); |
| 658 | if (event.shiftKey) |
| 659 | { |
| 660 | cycleToolState(event.deltaY < 0 ? 1 : -1); |
| 661 | if (hoverLocal) |
| 662 | { view.setTilePreview(hoverLocal.tileX, hoverLocal.tileY, previewFromTool()); } |
| 663 | return; |
| 664 | } |
| 665 | zoomEditor(event.deltaY < 0 ? 1 : -1); |
| 666 | }, { passive: false }); |
| 667 | |
| 668 | document.addEventListener("contextmenu", (event) => |
| 669 | { |
| 670 | if (!scene?.canvas || anyModalOpen() || !draft) |
| 671 | { return; } |
| 672 | const rect = scene.canvas.getBoundingClientRect(); |
| 673 | if (event.clientX < rect.left || event.clientX >= rect.right || event.clientY < rect.top || event.clientY >= rect.bottom) |
| 674 | { return; } |
| 675 | event.preventDefault(); |
| 676 | const tile = view.screenToTile(event.clientX - rect.left, event.clientY - rect.top); |
| 677 | const localX = tile.tileX - chunkX * CHUNK_SIZE; |
| 678 | const localY = tile.tileY - chunkY * CHUNK_SIZE; |
| 679 | if (localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE) |
| 680 | { return; } |
| 681 | const spawner = spawnerAt(localX, localY); |
| 682 | if (!spawner) |
| 683 | { return; } |
| 684 | tileConfigModal.openSpawner({ |
| 685 | localX, |
| 686 | localY, |
| 687 | tileX: tile.tileX, |
| 688 | tileY: tile.tileY, |
| 689 | config: { ...spawner.config } |
| 690 | }); |
| 691 | }); |
| 692 | |
| 693 | main().catch((error) => |
| 694 | { |
| 695 | $("error").textContent = error.message; |
| 696 | console.error(error); |
| 697 | }); |