/** * 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 | light var selectedLight = -1; 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(); refreshLightList(); } function refreshLightList () { var ul = document.getElementById("light-list"); if (!ul) return; ul.innerHTML = ""; (level().lights || []).forEach(function (lamp, i) { var li = document.createElement("li"); li.textContent = (lamp.id || "light") + " @" + Math.round(lamp.x) + "," + Math.round(lamp.y) + " r" + Math.round(lamp.range || 0); li.style.cursor = "pointer"; if (i === selectedLight) li.style.background = "#543"; li.onclick = function () { selectedLight = i; document.getElementById("light-z").value = lamp.z; document.getElementById("light-intensity").value = lamp.intensity; document.getElementById("light-range").value = lamp.range; var c = lamp.color || [1, 0.9, 0.7]; document.getElementById("light-cr").value = c[0]; document.getElementById("light-cg").value = c[1]; document.getElementById("light-cb").value = c[2]; refreshLightList(); redraw(); }; ul.appendChild(li); }); } 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); }); (L.lights || []).forEach(function (lamp, i) { var lx = (lamp.x / (L.cellSize || 32)) * cellPx; var ly = (lamp.y / (L.cellSize || 32)) * cellPx; var rr = ((lamp.range || 64) / (L.cellSize || 32)) * cellPx; ctx.strokeStyle = i === selectedLight ? "#ff0" : "rgba(255,220,80,0.45)"; ctx.beginPath(); ctx.arc(lx, ly, rr, 0, Math.PI * 2); ctx.stroke(); var col = lamp.color || [1, 0.9, 0.5]; var R = Math.round((col[0] > 1 ? col[0] : col[0] * 255)); var G = Math.round((col[1] > 1 ? col[1] : col[1] * 255)); var B = Math.round((col[2] > 1 ? col[2] : col[2] * 255)); ctx.fillStyle = "rgb(" + R + "," + G + "," + B + ")"; ctx.beginPath(); ctx.arc(lx, ly, i === selectedLight ? 8 : 6, 0, Math.PI * 2); ctx.fill(); ctx.strokeStyle = "#000"; ctx.stroke(); }); 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 === "light") { var csL = L.cellSize || 32; if (!L.lights) L.lights = []; var lamp = LevelFormat.emptyLight(x * csL + csL / 2, y * csL + csL / 2); lamp.z = Number(document.getElementById("light-z").value) || 20; lamp.intensity = Number(document.getElementById("light-intensity").value) || 1.2; lamp.range = Number(document.getElementById("light-range").value) || 96; lamp.color = [ Number(document.getElementById("light-cr").value) || 1, Number(document.getElementById("light-cg").value) || 0.9, Number(document.getElementById("light-cb").value) || 0.7 ]; L.lights.push(lamp); selectedLight = L.lights.length - 1; refreshLightList(); } 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"; } }; }); var draggingLight = false; canvas.onmousedown = function (e) { var c = cellFromEvent(e); if (!c) return; if (tool === "light") { var cs = level().cellSize || 32; var wx = c.x * cs + cs / 2; var wy = c.y * cs + cs / 2; var hit = -1; (level().lights || []).forEach(function (lamp, i) { var dx = lamp.x - wx, dy = lamp.y - wy; if (dx * dx + dy * dy < (cs * 0.6) * (cs * 0.6)) hit = i; }); if (hit >= 0) { selectedLight = hit; draggingLight = true; refreshLightList(); redraw(); return; } } painting = true; paintAt(c.x, c.y); }; canvas.onmousemove = function (e) { var c = cellFromEvent(e); if (!c) return; if (draggingLight && selectedLight >= 0 && level().lights[selectedLight]) { var cs = level().cellSize || 32; level().lights[selectedLight].x = c.x * cs + cs / 2; level().lights[selectedLight].y = c.y * cs + cs / 2; redraw(); return; } if (!painting || tool === "spawn" || tool === "npc" || tool === "light") { return; } paintAt(c.x, c.y); }; window.onmouseup = function () { if (draggingLight) { saveLocal(); refreshLightList(); } painting = false; draggingLight = 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(); } }; var ambEl = document.getElementById("level-ambient"); if (ambEl) { ambEl.value = level().ambient != null ? level().ambient : 0.38; ambEl.onchange = function (e) { level().ambient = Number(e.target.value); saveLocal(); }; } document.getElementById("btn-del-light").onclick = function () { if (selectedLight < 0 || !level().lights) return; level().lights.splice(selectedLight, 1); selectedLight = -1; refreshLightList(); redraw(); saveLocal(); }; function applyLightProps () { if (selectedLight < 0 || !level().lights[selectedLight]) return; var lamp = level().lights[selectedLight]; lamp.z = Number(document.getElementById("light-z").value) || 20; lamp.intensity = Number(document.getElementById("light-intensity").value) || 1; lamp.range = Number(document.getElementById("light-range").value) || 96; lamp.color = [ Number(document.getElementById("light-cr").value) || 1, Number(document.getElementById("light-cg").value) || 0.9, Number(document.getElementById("light-cb").value) || 0.7 ]; redraw(); saveLocal(); } ["light-z","light-intensity","light-range","light-cr","light-cg","light-cb"].forEach(function (id) { var el = document.getElementById(id); if (el) el.onchange = applyLightProps; }); 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); }); } }; })();