import { MessageType, CHUNK_SIZE } from "/shared/protocol/constants.js"; import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, BlockType } from "/shared/definitions/tiles.js"; import { GameSocket, defaultSocketUrl } from "../net/socket.js"; import { PLAY_IMAGES, assertSpriteSizes, useNearestNeighbour } from "../render/sprites.js"; import { WorldView } from "../render/worldView.js"; const game = new Game(); let socket; let scene; let view; let draft = null; let chunkX = 0; let chunkY = 0; let tool = { kind: "ground", typeId: GroundType.grass }; function $(id) { return document.getElementById(id); } function selectedToolLabel () { if (tool.kind === "erase") { return "Erase block"; } if (tool.kind === "ground") { return GROUND_DEFINITIONS[tool.typeId].displayName; } return BLOCK_DEFINITIONS[tool.typeId].displayName; } function paintAt (localX, localY) { if (!draft || localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE) { return; } const index = localY * CHUNK_SIZE + localX; if (tool.kind === "ground") { draft.grounds[index] = tool.typeId; } else if (tool.kind === "erase") { draft.blocks[index] = null; } else { draft.blocks[index] = { typeId: tool.typeId, config: null }; } view.applySnapshot({ chunkX, chunkY, grounds: draft.grounds, blocks: draft.blocks, light: draft.light, entities: [] }); } function showChunk (payload) { draft = { open: payload.data.open, grounds: payload.data.grounds.slice(), blocks: payload.data.blocks.map((block) => (block ? { ...block } : null)), light: payload.data.light.slice() }; chunkX = payload.chunkX; chunkY = payload.chunkY; $("coords").textContent = `${chunkX}, ${chunkY}`; $("open").checked = draft.open; $("revision").textContent = `active ${payload.activeRevisionId}` + (payload.pendingRevisionId ? ` · pending ${payload.pendingRevisionId}` : " · no pending revision"); $("authored").textContent = payload.authored ? "authored" : "default grass (closed until saved open)"; const log = $("changelog"); log.innerHTML = ""; for (const entry of payload.changelog ?? []) { const item = document.createElement("article"); item.innerHTML = `
${entry.editor_admin_id} · ${new Date(entry.edited_at).toLocaleString()}
`; const list = document.createElement("ul"); for (const bullet of entry.diffEntries) { const li = document.createElement("li"); li.textContent = bullet; list.append(li); } item.append(list); log.append(item); } view.applySnapshot({ chunkX, chunkY, grounds: draft.grounds, blocks: draft.blocks, light: draft.light, entities: [] }); frameChunk(); } function frameChunk () { const size = view.tileSize(); view.followPlayer = false; view.scene.cameraPosition.x = 24 - (chunkX * CHUNK_SIZE * size); view.scene.cameraPosition.y = 24 - (chunkY * CHUNK_SIZE * size); } function loadChunk (x, y) { socket.send({ type: MessageType.editorLoadChunk, chunkX: x, chunkY: y }); } async function main () { await game.preloadImages(PLAY_IMAGES); assertSpriteSizes(game.images); const canvasSize = CHUNK_SIZE * 16 * 2 + 48; scene = new Scene(260, 56, canvasSize, canvasSize); game.loadScene(scene); useNearestNeighbour(scene, game.images); view = new WorldView(game, scene); view.followPlayer = false; view.setZoom(2); const ticker = new GameObject(0, 0, null, 10); ticker.update = (dt) => { if (GameInput.mouseHeld(GameInput.MouseButtons.Left) && draft) { const canvas = scene.canvas; const rect = canvas.getBoundingClientRect(); const mouseX = GameInput.mousePosition.x - rect.left; const mouseY = GameInput.mousePosition.y - rect.top; if (mouseX >= 0 && mouseY >= 0 && mouseX < rect.width && mouseY < rect.height) { const tile = view.screenToTile(mouseX, mouseY); paintAt(tile.tileX - chunkX * CHUNK_SIZE, tile.tileY - chunkY * CHUNK_SIZE); } } view.update(dt); frameChunk(); }; scene.addObject(ticker); game.startGame(); socket = new GameSocket(defaultSocketUrl(), (message) => { if (message.type === MessageType.session) { $("who").textContent = `${message.authSubject}${message.isAdmin ? " · admin" : ""}`; loadChunk(0, 0); } else if (message.type === MessageType.editorChunk) { if (message.settingsSaved) { $("revision").textContent = `settings saved on pending ${message.settingsSaved.revisionId}`; return; } showChunk(message); } else if (message.type === MessageType.error) { $("error").textContent = message.message; } }); await socket.connect(); } $("tool").textContent = selectedToolLabel(); document.querySelectorAll("[data-tool]").forEach((button) => { button.addEventListener("click", () => { tool = JSON.parse(button.dataset.tool); $("tool").textContent = selectedToolLabel(); }); }); $("north").addEventListener("click", () => loadChunk(chunkX, chunkY - 1)); $("south").addEventListener("click", () => loadChunk(chunkX, chunkY + 1)); $("west").addEventListener("click", () => loadChunk(chunkX - 1, chunkY)); $("east").addEventListener("click", () => loadChunk(chunkX + 1, chunkY)); $("open").addEventListener("change", () => { if (draft) { draft.open = $("open").checked; } }); $("save").addEventListener("click", () => { $("error").textContent = ""; socket.send({ type: MessageType.editorSaveChunk, chunkX, chunkY, draft: { open: $("open").checked, grounds: draft.grounds, blocks: draft.blocks } }); }); $("apply").addEventListener("click", () => socket.send({ type: MessageType.editorApplyRevision })); $("night").addEventListener("input", () => { view.nightPreview = Number($("night").value); $("night-label").textContent = view.nightPreview.toFixed(1); }); $("save-settings").addEventListener("click", () => { socket.send({ type: MessageType.editorSaveSettings, settings: { walkTilesPerSecond: Number($("walk-speed").value), simulationTickHz: Number($("tick-hz").value) } }); }); main().catch((error) => { $("error").textContent = error.message; console.error(error); });