/* /workspace/raytracer-work/src/levelFormat.js */ /** * Level pack format (schema v1) for raytracer + editor. * walls: tile type ids; floors/ceilings: texture id string or null (black). * lights: reserved list (not rendered yet). */ var LevelFormat = { CURRENT_VERSION: 1, MAX_SIZE: 128, MAX_LEVELS: 64, KNOWN_TEXTURES: ["stone", "wood"], emptyLevel: function (name, width, height) { width = Math.max(3, Math.min(this.MAX_SIZE, width || 10)); height = Math.max(3, Math.min(this.MAX_SIZE, height || 10)); var n = width * height; var walls = []; var floors = []; var ceilings = []; for (var i = 0; i < n; i++) { var x = i % width; var y = Math.floor(i / width); var border = x === 0 || y === 0 || x === width - 1 || y === height - 1; walls.push(border ? 1 : 0); floors.push(border ? null : "stone"); ceilings.push(border ? null : "stone"); } return { id: "level-" + Date.now().toString(36), name: name || "Nieuw level", width: width, height: height, cellSize: 32, spawn: { tileX: 1, tileY: 1, yawRad: 0 }, walls: walls, floors: floors, ceilings: ceilings, npcs: [], lights: [] }; }, defaultPack: function () { return { format_version: this.CURRENT_VERSION, textures: [ { id: "stone", url: "images/stone.png" }, { id: "wood", url: "images/wood.png" } ], levels: [] }; }, /** Migrate legacy Levels[] (tiles only) into a pack. */ fromLegacyLevels: function (legacyArr, npcsByLevel) { var pack = this.defaultPack(); for (var i = 0; i < (legacyArr || []).length; i++) { var L = legacyArr[i]; var n = L.width * L.height; var floors = []; var ceilings = []; for (var t = 0; t < n; t++) { var solid = L.tiles[t] === 1 || L.tiles[t] === 3; floors.push(solid ? null : "stone"); ceilings.push(solid ? null : "stone"); } pack.levels.push({ id: "level-" + (i + 1), name: L.name || ("Level " + (i + 1)), width: L.width, height: L.height, cellSize: 32, spawn: { tileX: L.spawnX, tileY: L.spawnY, yawRad: 0 }, walls: L.tiles.slice(), floors: floors, ceilings: ceilings, npcs: (npcsByLevel && npcsByLevel[i]) || [], lights: [] }); } return pack; }, validateTextureId: function (tex, allowNull) { if (tex == null || tex === "") { return !!allowNull; } return this.KNOWN_TEXTURES.indexOf(tex) >= 0; }, validateLevel: function (level, index) { var prefix = "level[" + (index == null ? "?" : index) + "]"; if (!level || typeof level !== "object") { throw new Error(prefix + ": moet een object zijn"); } if (!level.id || typeof level.id !== "string") { throw new Error(prefix + ": id verplicht"); } if (!level.name || typeof level.name !== "string") { throw new Error(prefix + ": name verplicht"); } var w = level.width | 0; var h = level.height | 0; if (w < 3 || h < 3 || w > this.MAX_SIZE || h > this.MAX_SIZE) { throw new Error(prefix + ": width/height 3.." + this.MAX_SIZE); } var n = w * h; if (!Array.isArray(level.walls) || level.walls.length !== n) { throw new Error(prefix + ": walls lengte ≠ width*height"); } if (!Array.isArray(level.floors) || level.floors.length !== n) { throw new Error(prefix + ": floors lengte ≠ width*height"); } if (!Array.isArray(level.ceilings) || level.ceilings.length !== n) { throw new Error(prefix + ": ceilings lengte ≠ width*height"); } for (var i = 0; i < n; i++) { var wall = level.walls[i]; if (typeof wall !== "number" || wall !== (wall | 0) || wall < 0 || wall > 3) { throw new Error(prefix + ": walls[" + i + "] ongeldig"); } if (!this.validateTextureId(level.floors[i], true)) { throw new Error(prefix + ": floors[" + i + "] onbekende textuur"); } if (!this.validateTextureId(level.ceilings[i], true)) { throw new Error(prefix + ": ceilings[" + i + "] onbekende textuur"); } } if (!level.spawn || typeof level.spawn.tileX !== "number" || typeof level.spawn.tileY !== "number") { throw new Error(prefix + ": spawn.tileX/tileY verplicht"); } if (level.spawn.tileX < 0 || level.spawn.tileY < 0 || level.spawn.tileX >= w || level.spawn.tileY >= h) { throw new Error(prefix + ": spawn buiten de map"); } if (typeof level.spawn.yawRad !== "number") { level.spawn.yawRad = 0; } if (!Array.isArray(level.npcs)) { throw new Error(prefix + ": npcs moet een array zijn"); } if (!Array.isArray(level.lights)) { level.lights = []; } if (level.cellSize == null) { level.cellSize = 32; } return level; }, validatePack: function (pack) { if (!pack || typeof pack !== "object") { throw new Error("pack: root moet een object zijn"); } var ver = pack.format_version; if (ver !== 1) { throw new Error("pack: format_version " + ver + " niet ondersteund (wel: 1)"); } if (!Array.isArray(pack.levels)) { throw new Error("pack: levels moet een array zijn"); } if (pack.levels.length > this.MAX_LEVELS) { throw new Error("pack: te veel levels"); } if (pack.levels.length < 1) { throw new Error("pack: minstens één level"); } var ids = {}; for (var i = 0; i < pack.levels.length; i++) { this.validateLevel(pack.levels[i], i); if (ids[pack.levels[i].id]) { throw new Error("pack: dubbele level-id " + pack.levels[i].id); } ids[pack.levels[i].id] = true; } if (!Array.isArray(pack.textures)) { pack.textures = this.defaultPack().textures; } return pack; }, parse: function (jsonText) { var doc; try { doc = typeof jsonText === "string" ? JSON.parse(jsonText) : jsonText; } catch (e) { throw new Error("levels.json: ongeldige JSON (" + e.message + ")"); } return this.validatePack(doc); }, /** Runtime view used by World (tiles = walls). */ toRuntimeLevel: function (level) { return { id: level.id, name: level.name, width: level.width, height: level.height, cellSize: level.cellSize || 32, spawnX: level.spawn.tileX, spawnY: level.spawn.tileY, spawnYaw: level.spawn.yawRad || 0, tiles: level.walls, floors: level.floors, ceilings: level.ceilings, npcs: level.npcs || [], lights: level.lights || [] }; }, findLevelIndex: function (pack, query) { if (query == null || query === "") { return 0; } var asNum = Number(query); if (!isNaN(asNum) && asNum === (asNum | 0) && asNum >= 0 && asNum < pack.levels.length) { return asNum; } for (var i = 0; i < pack.levels.length; i++) { if (pack.levels[i].id === query || pack.levels[i].name === query) { return i; } } return -1; }, cloneLevel: function (level) { return JSON.parse(JSON.stringify(level)); } }; if (typeof module !== "undefined" && module.exports) { module.exports = LevelFormat; } /* /workspace/raytracer-work/src/editorApp.js */ /** * Simple grid level editor (2D canvas). Uses LevelFormat for validation. */ (function () { var STORAGE_KEY = "raytracer-levels-v1"; var pack = null; var levelIndex = 0; var tool = "wall"; // wall | floor | ceiling | spawn | npc var brushWall = 1; var brushTex = "stone"; // or null via "none" var cellPx = 28; var painting = false; var selectedNpc = -1; var canvas = null; var ctx = null; var texPreviews = {}; function level () { return pack.levels[levelIndex]; } function idx (x, y) { return x + y * level().width; } function loadPreviews () { ["stone", "wood"].forEach(function (id) { var img = new Image(); img.onload = function () { texPreviews[id] = img; redraw(); }; img.src = "images/" + id + ".png"; }); } function ensurePack (raw) { pack = LevelFormat.validatePack(raw); if (levelIndex >= pack.levels.length) { levelIndex = 0; } refreshLevelSelect(); redraw(); saveLocal(); } function saveLocal () { try { localStorage.setItem(STORAGE_KEY, JSON.stringify(pack)); } catch (e) { /* ignore */ } } function refreshLevelSelect () { var sel = document.getElementById("level-select"); sel.innerHTML = ""; pack.levels.forEach(function (L, i) { var o = document.createElement("option"); o.value = String(i); o.textContent = L.name; if (i === levelIndex) { o.selected = true; } sel.appendChild(o); }); document.getElementById("level-name").value = level().name; document.getElementById("level-w").value = level().width; document.getElementById("level-h").value = level().height; refreshNpcList(); } function refreshNpcList () { var ul = document.getElementById("npc-list"); ul.innerHTML = ""; (level().npcs || []).forEach(function (n, i) { var li = document.createElement("li"); li.textContent = (n.label || "npc") + " @" + Math.round(n.x) + "," + Math.round(n.y) + " yaw " + n.facingDeg; li.style.cursor = "pointer"; if (i === selectedNpc) { li.style.background = "#345"; } li.onclick = function () { selectedNpc = i; document.getElementById("npc-yaw").value = n.facingDeg; document.getElementById("npc-clip").value = n.clip || "walk"; document.getElementById("npc-anim").checked = !!n.animating; refreshNpcList(); redraw(); }; ul.appendChild(li); }); } function redraw () { var L = level(); canvas.width = L.width * cellPx; canvas.height = L.height * cellPx; ctx.fillStyle = "#111"; ctx.fillRect(0, 0, canvas.width, canvas.height); for (var y = 0; y < L.height; y++) { for (var x = 0; x < L.width; x++) { var i = idx(x, y); var px = x * cellPx; var py = y * cellPx; var floor = L.floors[i]; if (floor && texPreviews[floor]) { ctx.drawImage(texPreviews[floor], px, py, cellPx, cellPx); } else if (floor) { ctx.fillStyle = floor === "wood" ? "#a66" : "#888"; ctx.fillRect(px, py, cellPx, cellPx); } var wall = L.walls[i]; if (wall === 1 || wall === 3) { ctx.globalAlpha = 0.85; var tid = wall === 3 ? "wood" : "stone"; if (texPreviews[tid]) { ctx.drawImage(texPreviews[tid], px, py, cellPx, cellPx); } else { ctx.fillStyle = wall === 3 ? "#84a" : "#555"; ctx.fillRect(px, py, cellPx, cellPx); } ctx.globalAlpha = 1; ctx.strokeStyle = "#fff"; ctx.strokeRect(px + 0.5, py + 0.5, cellPx - 1, cellPx - 1); } else if (wall === 2) { ctx.fillStyle = "rgba(255,220,0,0.35)"; ctx.fillRect(px, py, cellPx, cellPx); } if (L.ceilings[i]) { ctx.fillStyle = "rgba(100,160,255,0.15)"; ctx.fillRect(px, py, cellPx, 4); } } } // spawn ctx.fillStyle = "#0f0"; ctx.beginPath(); ctx.arc(L.spawn.tileX * cellPx + cellPx / 2, L.spawn.tileY * cellPx + cellPx / 2, 6, 0, Math.PI * 2); ctx.fill(); var ang = L.spawn.yawRad || 0; ctx.strokeStyle = "#0f0"; ctx.beginPath(); ctx.moveTo(L.spawn.tileX * cellPx + cellPx / 2, L.spawn.tileY * cellPx + cellPx / 2); ctx.lineTo( L.spawn.tileX * cellPx + cellPx / 2 + Math.cos(ang) * 14, L.spawn.tileY * cellPx + cellPx / 2 + Math.sin(ang) * 14 ); ctx.stroke(); // npcs (L.npcs || []).forEach(function (n, i) { var nx = (n.x / (L.cellSize || 32)) * cellPx; var ny = (n.y / (L.cellSize || 32)) * cellPx; ctx.fillStyle = i === selectedNpc ? "#f80" : "#f44"; ctx.fillRect(nx - 5, ny - 5, 10, 10); }); ctx.strokeStyle = "#333"; for (var gx = 0; gx <= L.width; gx++) { ctx.beginPath(); ctx.moveTo(gx * cellPx + 0.5, 0); ctx.lineTo(gx * cellPx + 0.5, canvas.height); ctx.stroke(); } for (var gy = 0; gy <= L.height; gy++) { ctx.beginPath(); ctx.moveTo(0, gy * cellPx + 0.5); ctx.lineTo(canvas.width, gy * cellPx + 0.5); ctx.stroke(); } } function cellFromEvent (e) { var r = canvas.getBoundingClientRect(); var x = Math.floor((e.clientX - r.left) / cellPx); var y = Math.floor((e.clientY - r.top) / cellPx); if (x < 0 || y < 0 || x >= level().width || y >= level().height) { return null; } return { x: x, y: y }; } function paintAt (x, y) { var L = level(); var i = idx(x, y); if (tool === "wall") { L.walls[i] = brushWall; } else if (tool === "floor") { L.floors[i] = brushTex === "none" ? null : brushTex; } else if (tool === "ceiling") { L.ceilings[i] = brushTex === "none" ? null : brushTex; } else if (tool === "spawn") { L.spawn.tileX = x; L.spawn.tileY = y; } else if (tool === "npc") { var cs = L.cellSize || 32; L.npcs.push({ x: x * cs + cs / 2, y: y * cs + cs / 2, facingDeg: Number(document.getElementById("npc-yaw").value) || 0, clip: document.getElementById("npc-clip").value || "walk", animating: document.getElementById("npc-anim").checked, label: "npc" + (L.npcs.length + 1), body: "paperdolls/male-body/", bodyPpda: "male-body-atlas.ppda", gear: "paperdolls/male-sword/", gearPpda: "male-sword-atlas.ppda" }); selectedNpc = L.npcs.length - 1; refreshNpcList(); } redraw(); saveLocal(); } function resizeLevel (nw, nh) { var L = level(); nw = Math.max(3, Math.min(LevelFormat.MAX_SIZE, nw | 0)); nh = Math.max(3, Math.min(LevelFormat.MAX_SIZE, nh | 0)); var walls = []; var floors = []; var ceilings = []; for (var y = 0; y < nh; y++) { for (var x = 0; x < nw; x++) { if (x < L.width && y < L.height) { var i = idx(x, y); walls.push(L.walls[i]); floors.push(L.floors[i]); ceilings.push(L.ceilings[i]); } else { var border = x === 0 || y === 0 || x === nw - 1 || y === nh - 1; walls.push(border ? 1 : 0); floors.push(border ? null : "stone"); ceilings.push(border ? null : "stone"); } } } L.width = nw; L.height = nh; L.walls = walls; L.floors = floors; L.ceilings = ceilings; if (L.spawn.tileX >= nw) L.spawn.tileX = nw - 2; if (L.spawn.tileY >= nh) L.spawn.tileY = nh - 2; refreshLevelSelect(); redraw(); saveLocal(); } function boot (initial) { canvas = document.getElementById("grid"); ctx = canvas.getContext("2d"); ensurePack(initial); loadPreviews(); document.getElementById("level-select").onchange = function (e) { levelIndex = Number(e.target.value) | 0; selectedNpc = -1; refreshLevelSelect(); redraw(); }; document.getElementById("level-name").onchange = function (e) { level().name = e.target.value || level().name; refreshLevelSelect(); saveLocal(); }; document.getElementById("btn-add-level").onclick = function () { pack.levels.push(LevelFormat.emptyLevel("Level " + (pack.levels.length + 1), 10, 10)); levelIndex = pack.levels.length - 1; refreshLevelSelect(); redraw(); saveLocal(); }; document.getElementById("btn-dup-level").onclick = function () { var c = LevelFormat.cloneLevel(level()); c.id = "level-" + Date.now().toString(36); c.name = level().name + " (kopie)"; pack.levels.push(c); levelIndex = pack.levels.length - 1; refreshLevelSelect(); redraw(); saveLocal(); }; document.getElementById("btn-del-level").onclick = function () { if (pack.levels.length <= 1) { return alert("Minstens één level houden"); } if (!confirm("Level verwijderen?")) { return; } pack.levels.splice(levelIndex, 1); levelIndex = Math.min(levelIndex, pack.levels.length - 1); refreshLevelSelect(); redraw(); saveLocal(); }; document.getElementById("btn-resize").onclick = function () { resizeLevel(Number(document.getElementById("level-w").value), Number(document.getElementById("level-h").value)); }; document.querySelectorAll("[data-tool]").forEach(function (el) { el.onclick = function () { tool = el.getAttribute("data-tool"); document.querySelectorAll("[data-tool]").forEach(function (b) { b.classList.toggle("active", b === el); }); }; }); document.querySelectorAll("[data-wall]").forEach(function (el) { el.onclick = function () { brushWall = Number(el.getAttribute("data-wall")); tool = "wall"; }; }); document.querySelectorAll("[data-tex]").forEach(function (el) { el.onclick = function () { brushTex = el.getAttribute("data-tex"); if (brushTex === "none") { brushTex = "none"; } }; }); canvas.onmousedown = function (e) { painting = true; var c = cellFromEvent(e); if (c) { paintAt(c.x, c.y); } }; canvas.onmousemove = function (e) { if (!painting || tool === "spawn" || tool === "npc") { return; } var c = cellFromEvent(e); if (c) { paintAt(c.x, c.y); } }; window.onmouseup = function () { painting = false; }; document.getElementById("spawn-yaw").onchange = function (e) { level().spawn.yawRad = (Number(e.target.value) || 0) * Math.PI / 180; redraw(); saveLocal(); }; document.getElementById("btn-del-npc").onclick = function () { if (selectedNpc < 0) { return; } level().npcs.splice(selectedNpc, 1); selectedNpc = -1; refreshNpcList(); redraw(); saveLocal(); }; document.getElementById("npc-yaw").onchange = function (e) { if (selectedNpc >= 0) { level().npcs[selectedNpc].facingDeg = Number(e.target.value) || 0; refreshNpcList(); redraw(); saveLocal(); } }; document.getElementById("npc-clip").onchange = function (e) { if (selectedNpc >= 0) { level().npcs[selectedNpc].clip = e.target.value; saveLocal(); } }; document.getElementById("npc-anim").onchange = function (e) { if (selectedNpc >= 0) { level().npcs[selectedNpc].animating = e.target.checked; saveLocal(); } }; document.getElementById("btn-download").onclick = function () { try { LevelFormat.validatePack(pack); } catch (err) { return alert(err.message); } var blob = new Blob([JSON.stringify(pack, null, 2)], { type: "application/json" }); var a = document.createElement("a"); a.href = URL.createObjectURL(blob); a.download = "levels.json"; a.click(); }; document.getElementById("file-upload").onchange = function (e) { var f = e.target.files[0]; if (!f) { return; } var reader = new FileReader(); reader.onload = function () { try { ensurePack(LevelFormat.parse(String(reader.result))); } catch (err) { alert(err.message); } }; reader.readAsText(f); }; document.getElementById("btn-play").onclick = function () { try { LevelFormat.validatePack(pack); } catch (err) { return alert(err.message); } saveLocal(); window.location.href = "index.html?playtest=1&level=" + encodeURIComponent(level().id); }; document.getElementById("btn-reload-repo").onclick = function () { fetch("levels.json").then(function (r) { return r.text(); }).then(function (t) { ensurePack(LevelFormat.parse(t)); }).catch(function (e) { alert(e.message); }); }; } window.EditorApp = { start: function () { var local = null; try { local = localStorage.getItem(STORAGE_KEY); } catch (e) { /* */ } var bootFrom = function (text) { boot(LevelFormat.parse(text)); }; if (local) { try { return boot(LevelFormat.parse(local)); } catch (e) { console.warn("localStorage levels ongeldig, repo laden", e); } } fetch("levels.json").then(function (r) { return r.text(); }).then(bootFrom).catch(function (e) { alert("Kon levels niet laden: " + e.message); boot(LevelFormat.defaultPack()); pack.levels.push(LevelFormat.emptyLevel("Level 1", 10, 10)); ensurePack(pack); }); } }; })();