| 1 | |
| 2 | /* /workspace/raytracer-work/src/levelFormat.js */ |
| 3 | /** |
| 4 | * Level pack format (schema v1) for raytracer + editor. |
| 5 | * walls: tile type ids; floors/ceilings: texture id string or null (black). |
| 6 | * lights: reserved list (not rendered yet). |
| 7 | */ |
| 8 | var LevelFormat = |
| 9 | { |
| 10 | CURRENT_VERSION: 1, |
| 11 | MAX_SIZE: 128, |
| 12 | MAX_LEVELS: 64, |
| 13 | KNOWN_TEXTURES: ["stone", "wood"], |
| 14 | |
| 15 | emptyLevel: function (name, width, height) |
| 16 | { |
| 17 | width = Math.max(3, Math.min(this.MAX_SIZE, width || 10)); |
| 18 | height = Math.max(3, Math.min(this.MAX_SIZE, height || 10)); |
| 19 | var n = width * height; |
| 20 | var walls = []; |
| 21 | var floors = []; |
| 22 | var ceilings = []; |
| 23 | for (var i = 0; i < n; i++) |
| 24 | { |
| 25 | var x = i % width; |
| 26 | var y = Math.floor(i / width); |
| 27 | var border = x === 0 || y === 0 || x === width - 1 || y === height - 1; |
| 28 | walls.push(border ? 1 : 0); |
| 29 | floors.push(border ? null : "stone"); |
| 30 | ceilings.push(border ? null : "stone"); |
| 31 | } |
| 32 | return { |
| 33 | id: "level-" + Date.now().toString(36), |
| 34 | name: name || "Nieuw level", |
| 35 | width: width, |
| 36 | height: height, |
| 37 | cellSize: 32, |
| 38 | spawn: { tileX: 1, tileY: 1, yawRad: 0 }, |
| 39 | walls: walls, |
| 40 | floors: floors, |
| 41 | ceilings: ceilings, |
| 42 | npcs: [], |
| 43 | lights: [] |
| 44 | }; |
| 45 | }, |
| 46 | |
| 47 | defaultPack: function () |
| 48 | { |
| 49 | return { |
| 50 | format_version: this.CURRENT_VERSION, |
| 51 | textures: [ |
| 52 | { id: "stone", url: "images/stone.png" }, |
| 53 | { id: "wood", url: "images/wood.png" } |
| 54 | ], |
| 55 | levels: [] |
| 56 | }; |
| 57 | }, |
| 58 | |
| 59 | /** Migrate legacy Levels[] (tiles only) into a pack. */ |
| 60 | fromLegacyLevels: function (legacyArr, npcsByLevel) |
| 61 | { |
| 62 | var pack = this.defaultPack(); |
| 63 | for (var i = 0; i < (legacyArr || []).length; i++) |
| 64 | { |
| 65 | var L = legacyArr[i]; |
| 66 | var n = L.width * L.height; |
| 67 | var floors = []; |
| 68 | var ceilings = []; |
| 69 | for (var t = 0; t < n; t++) |
| 70 | { |
| 71 | var solid = L.tiles[t] === 1 || L.tiles[t] === 3; |
| 72 | floors.push(solid ? null : "stone"); |
| 73 | ceilings.push(solid ? null : "stone"); |
| 74 | } |
| 75 | pack.levels.push({ |
| 76 | id: "level-" + (i + 1), |
| 77 | name: L.name || ("Level " + (i + 1)), |
| 78 | width: L.width, |
| 79 | height: L.height, |
| 80 | cellSize: 32, |
| 81 | spawn: { |
| 82 | tileX: L.spawnX, |
| 83 | tileY: L.spawnY, |
| 84 | yawRad: 0 |
| 85 | }, |
| 86 | walls: L.tiles.slice(), |
| 87 | floors: floors, |
| 88 | ceilings: ceilings, |
| 89 | npcs: (npcsByLevel && npcsByLevel[i]) || [], |
| 90 | lights: [] |
| 91 | }); |
| 92 | } |
| 93 | return pack; |
| 94 | }, |
| 95 | |
| 96 | validateTextureId: function (tex, allowNull) |
| 97 | { |
| 98 | if (tex == null || tex === "") |
| 99 | { return !!allowNull; } |
| 100 | return this.KNOWN_TEXTURES.indexOf(tex) >= 0; |
| 101 | }, |
| 102 | |
| 103 | validateLevel: function (level, index) |
| 104 | { |
| 105 | var prefix = "level[" + (index == null ? "?" : index) + "]"; |
| 106 | if (!level || typeof level !== "object") |
| 107 | { throw new Error(prefix + ": moet een object zijn"); } |
| 108 | if (!level.id || typeof level.id !== "string") |
| 109 | { throw new Error(prefix + ": id verplicht"); } |
| 110 | if (!level.name || typeof level.name !== "string") |
| 111 | { throw new Error(prefix + ": name verplicht"); } |
| 112 | var w = level.width | 0; |
| 113 | var h = level.height | 0; |
| 114 | if (w < 3 || h < 3 || w > this.MAX_SIZE || h > this.MAX_SIZE) |
| 115 | { throw new Error(prefix + ": width/height 3.." + this.MAX_SIZE); } |
| 116 | var n = w * h; |
| 117 | if (!Array.isArray(level.walls) || level.walls.length !== n) |
| 118 | { throw new Error(prefix + ": walls lengte ≠ width*height"); } |
| 119 | if (!Array.isArray(level.floors) || level.floors.length !== n) |
| 120 | { throw new Error(prefix + ": floors lengte ≠ width*height"); } |
| 121 | if (!Array.isArray(level.ceilings) || level.ceilings.length !== n) |
| 122 | { throw new Error(prefix + ": ceilings lengte ≠ width*height"); } |
| 123 | for (var i = 0; i < n; i++) |
| 124 | { |
| 125 | var wall = level.walls[i]; |
| 126 | if (typeof wall !== "number" || wall !== (wall | 0) || wall < 0 || wall > 3) |
| 127 | { throw new Error(prefix + ": walls[" + i + "] ongeldig"); } |
| 128 | if (!this.validateTextureId(level.floors[i], true)) |
| 129 | { throw new Error(prefix + ": floors[" + i + "] onbekende textuur"); } |
| 130 | if (!this.validateTextureId(level.ceilings[i], true)) |
| 131 | { throw new Error(prefix + ": ceilings[" + i + "] onbekende textuur"); } |
| 132 | } |
| 133 | if (!level.spawn || typeof level.spawn.tileX !== "number" || typeof level.spawn.tileY !== "number") |
| 134 | { throw new Error(prefix + ": spawn.tileX/tileY verplicht"); } |
| 135 | if (level.spawn.tileX < 0 || level.spawn.tileY < 0 || level.spawn.tileX >= w || level.spawn.tileY >= h) |
| 136 | { throw new Error(prefix + ": spawn buiten de map"); } |
| 137 | if (typeof level.spawn.yawRad !== "number") |
| 138 | { level.spawn.yawRad = 0; } |
| 139 | if (!Array.isArray(level.npcs)) |
| 140 | { throw new Error(prefix + ": npcs moet een array zijn"); } |
| 141 | if (!Array.isArray(level.lights)) |
| 142 | { level.lights = []; } |
| 143 | if (level.cellSize == null) |
| 144 | { level.cellSize = 32; } |
| 145 | return level; |
| 146 | }, |
| 147 | |
| 148 | validatePack: function (pack) |
| 149 | { |
| 150 | if (!pack || typeof pack !== "object") |
| 151 | { throw new Error("pack: root moet een object zijn"); } |
| 152 | var ver = pack.format_version; |
| 153 | if (ver !== 1) |
| 154 | { throw new Error("pack: format_version " + ver + " niet ondersteund (wel: 1)"); } |
| 155 | if (!Array.isArray(pack.levels)) |
| 156 | { throw new Error("pack: levels moet een array zijn"); } |
| 157 | if (pack.levels.length > this.MAX_LEVELS) |
| 158 | { throw new Error("pack: te veel levels"); } |
| 159 | if (pack.levels.length < 1) |
| 160 | { throw new Error("pack: minstens één level"); } |
| 161 | var ids = {}; |
| 162 | for (var i = 0; i < pack.levels.length; i++) |
| 163 | { |
| 164 | this.validateLevel(pack.levels[i], i); |
| 165 | if (ids[pack.levels[i].id]) |
| 166 | { throw new Error("pack: dubbele level-id " + pack.levels[i].id); } |
| 167 | ids[pack.levels[i].id] = true; |
| 168 | } |
| 169 | if (!Array.isArray(pack.textures)) |
| 170 | { pack.textures = this.defaultPack().textures; } |
| 171 | return pack; |
| 172 | }, |
| 173 | |
| 174 | parse: function (jsonText) |
| 175 | { |
| 176 | var doc; |
| 177 | try |
| 178 | { doc = typeof jsonText === "string" ? JSON.parse(jsonText) : jsonText; } |
| 179 | catch (e) |
| 180 | { throw new Error("levels.json: ongeldige JSON (" + e.message + ")"); } |
| 181 | return this.validatePack(doc); |
| 182 | }, |
| 183 | |
| 184 | /** Runtime view used by World (tiles = walls). */ |
| 185 | toRuntimeLevel: function (level) |
| 186 | { |
| 187 | return { |
| 188 | id: level.id, |
| 189 | name: level.name, |
| 190 | width: level.width, |
| 191 | height: level.height, |
| 192 | cellSize: level.cellSize || 32, |
| 193 | spawnX: level.spawn.tileX, |
| 194 | spawnY: level.spawn.tileY, |
| 195 | spawnYaw: level.spawn.yawRad || 0, |
| 196 | tiles: level.walls, |
| 197 | floors: level.floors, |
| 198 | ceilings: level.ceilings, |
| 199 | npcs: level.npcs || [], |
| 200 | lights: level.lights || [] |
| 201 | }; |
| 202 | }, |
| 203 | |
| 204 | findLevelIndex: function (pack, query) |
| 205 | { |
| 206 | if (query == null || query === "") |
| 207 | { return 0; } |
| 208 | var asNum = Number(query); |
| 209 | if (!isNaN(asNum) && asNum === (asNum | 0) && asNum >= 0 && asNum < pack.levels.length) |
| 210 | { return asNum; } |
| 211 | for (var i = 0; i < pack.levels.length; i++) |
| 212 | { |
| 213 | if (pack.levels[i].id === query || pack.levels[i].name === query) |
| 214 | { return i; } |
| 215 | } |
| 216 | return -1; |
| 217 | }, |
| 218 | |
| 219 | cloneLevel: function (level) |
| 220 | { |
| 221 | return JSON.parse(JSON.stringify(level)); |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | if (typeof module !== "undefined" && module.exports) |
| 226 | { module.exports = LevelFormat; } |
| 227 | |
| 228 | /* /workspace/raytracer-work/src/editorApp.js */ |
| 229 | /** |
| 230 | * Simple grid level editor (2D canvas). Uses LevelFormat for validation. |
| 231 | */ |
| 232 | (function () |
| 233 | { |
| 234 | var STORAGE_KEY = "raytracer-levels-v1"; |
| 235 | var pack = null; |
| 236 | var levelIndex = 0; |
| 237 | var tool = "wall"; // wall | floor | ceiling | spawn | npc |
| 238 | var brushWall = 1; |
| 239 | var brushTex = "stone"; // or null via "none" |
| 240 | var cellPx = 28; |
| 241 | var painting = false; |
| 242 | var selectedNpc = -1; |
| 243 | |
| 244 | var canvas = null; |
| 245 | var ctx = null; |
| 246 | var texPreviews = {}; |
| 247 | |
| 248 | function level () |
| 249 | { return pack.levels[levelIndex]; } |
| 250 | |
| 251 | function idx (x, y) |
| 252 | { return x + y * level().width; } |
| 253 | |
| 254 | function loadPreviews () |
| 255 | { |
| 256 | ["stone", "wood"].forEach(function (id) |
| 257 | { |
| 258 | var img = new Image(); |
| 259 | img.onload = function () |
| 260 | { texPreviews[id] = img; redraw(); }; |
| 261 | img.src = "images/" + id + ".png"; |
| 262 | }); |
| 263 | } |
| 264 | |
| 265 | function ensurePack (raw) |
| 266 | { |
| 267 | pack = LevelFormat.validatePack(raw); |
| 268 | if (levelIndex >= pack.levels.length) |
| 269 | { levelIndex = 0; } |
| 270 | refreshLevelSelect(); |
| 271 | redraw(); |
| 272 | saveLocal(); |
| 273 | } |
| 274 | |
| 275 | function saveLocal () |
| 276 | { |
| 277 | try |
| 278 | { localStorage.setItem(STORAGE_KEY, JSON.stringify(pack)); } |
| 279 | catch (e) { /* ignore */ } |
| 280 | } |
| 281 | |
| 282 | function refreshLevelSelect () |
| 283 | { |
| 284 | var sel = document.getElementById("level-select"); |
| 285 | sel.innerHTML = ""; |
| 286 | pack.levels.forEach(function (L, i) |
| 287 | { |
| 288 | var o = document.createElement("option"); |
| 289 | o.value = String(i); |
| 290 | o.textContent = L.name; |
| 291 | if (i === levelIndex) |
| 292 | { o.selected = true; } |
| 293 | sel.appendChild(o); |
| 294 | }); |
| 295 | document.getElementById("level-name").value = level().name; |
| 296 | document.getElementById("level-w").value = level().width; |
| 297 | document.getElementById("level-h").value = level().height; |
| 298 | refreshNpcList(); |
| 299 | } |
| 300 | |
| 301 | function refreshNpcList () |
| 302 | { |
| 303 | var ul = document.getElementById("npc-list"); |
| 304 | ul.innerHTML = ""; |
| 305 | (level().npcs || []).forEach(function (n, i) |
| 306 | { |
| 307 | var li = document.createElement("li"); |
| 308 | li.textContent = (n.label || "npc") + " @" + Math.round(n.x) + "," + Math.round(n.y) + " yaw " + n.facingDeg; |
| 309 | li.style.cursor = "pointer"; |
| 310 | if (i === selectedNpc) |
| 311 | { li.style.background = "#345"; } |
| 312 | li.onclick = function () |
| 313 | { |
| 314 | selectedNpc = i; |
| 315 | document.getElementById("npc-yaw").value = n.facingDeg; |
| 316 | document.getElementById("npc-clip").value = n.clip || "walk"; |
| 317 | document.getElementById("npc-anim").checked = !!n.animating; |
| 318 | refreshNpcList(); |
| 319 | redraw(); |
| 320 | }; |
| 321 | ul.appendChild(li); |
| 322 | }); |
| 323 | } |
| 324 | |
| 325 | function redraw () |
| 326 | { |
| 327 | var L = level(); |
| 328 | canvas.width = L.width * cellPx; |
| 329 | canvas.height = L.height * cellPx; |
| 330 | ctx.fillStyle = "#111"; |
| 331 | ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 332 | for (var y = 0; y < L.height; y++) |
| 333 | { |
| 334 | for (var x = 0; x < L.width; x++) |
| 335 | { |
| 336 | var i = idx(x, y); |
| 337 | var px = x * cellPx; |
| 338 | var py = y * cellPx; |
| 339 | var floor = L.floors[i]; |
| 340 | if (floor && texPreviews[floor]) |
| 341 | { ctx.drawImage(texPreviews[floor], px, py, cellPx, cellPx); } |
| 342 | else if (floor) |
| 343 | { |
| 344 | ctx.fillStyle = floor === "wood" ? "#a66" : "#888"; |
| 345 | ctx.fillRect(px, py, cellPx, cellPx); |
| 346 | } |
| 347 | var wall = L.walls[i]; |
| 348 | if (wall === 1 || wall === 3) |
| 349 | { |
| 350 | ctx.globalAlpha = 0.85; |
| 351 | var tid = wall === 3 ? "wood" : "stone"; |
| 352 | if (texPreviews[tid]) |
| 353 | { ctx.drawImage(texPreviews[tid], px, py, cellPx, cellPx); } |
| 354 | else |
| 355 | { |
| 356 | ctx.fillStyle = wall === 3 ? "#84a" : "#555"; |
| 357 | ctx.fillRect(px, py, cellPx, cellPx); |
| 358 | } |
| 359 | ctx.globalAlpha = 1; |
| 360 | ctx.strokeStyle = "#fff"; |
| 361 | ctx.strokeRect(px + 0.5, py + 0.5, cellPx - 1, cellPx - 1); |
| 362 | } |
| 363 | else if (wall === 2) |
| 364 | { |
| 365 | ctx.fillStyle = "rgba(255,220,0,0.35)"; |
| 366 | ctx.fillRect(px, py, cellPx, cellPx); |
| 367 | } |
| 368 | if (L.ceilings[i]) |
| 369 | { |
| 370 | ctx.fillStyle = "rgba(100,160,255,0.15)"; |
| 371 | ctx.fillRect(px, py, cellPx, 4); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | // spawn |
| 376 | ctx.fillStyle = "#0f0"; |
| 377 | ctx.beginPath(); |
| 378 | ctx.arc(L.spawn.tileX * cellPx + cellPx / 2, L.spawn.tileY * cellPx + cellPx / 2, 6, 0, Math.PI * 2); |
| 379 | ctx.fill(); |
| 380 | var ang = L.spawn.yawRad || 0; |
| 381 | ctx.strokeStyle = "#0f0"; |
| 382 | ctx.beginPath(); |
| 383 | ctx.moveTo(L.spawn.tileX * cellPx + cellPx / 2, L.spawn.tileY * cellPx + cellPx / 2); |
| 384 | ctx.lineTo( |
| 385 | L.spawn.tileX * cellPx + cellPx / 2 + Math.cos(ang) * 14, |
| 386 | L.spawn.tileY * cellPx + cellPx / 2 + Math.sin(ang) * 14 |
| 387 | ); |
| 388 | ctx.stroke(); |
| 389 | // npcs |
| 390 | (L.npcs || []).forEach(function (n, i) |
| 391 | { |
| 392 | var nx = (n.x / (L.cellSize || 32)) * cellPx; |
| 393 | var ny = (n.y / (L.cellSize || 32)) * cellPx; |
| 394 | ctx.fillStyle = i === selectedNpc ? "#f80" : "#f44"; |
| 395 | ctx.fillRect(nx - 5, ny - 5, 10, 10); |
| 396 | }); |
| 397 | ctx.strokeStyle = "#333"; |
| 398 | for (var gx = 0; gx <= L.width; gx++) |
| 399 | { |
| 400 | ctx.beginPath(); |
| 401 | ctx.moveTo(gx * cellPx + 0.5, 0); |
| 402 | ctx.lineTo(gx * cellPx + 0.5, canvas.height); |
| 403 | ctx.stroke(); |
| 404 | } |
| 405 | for (var gy = 0; gy <= L.height; gy++) |
| 406 | { |
| 407 | ctx.beginPath(); |
| 408 | ctx.moveTo(0, gy * cellPx + 0.5); |
| 409 | ctx.lineTo(canvas.width, gy * cellPx + 0.5); |
| 410 | ctx.stroke(); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | function cellFromEvent (e) |
| 415 | { |
| 416 | var r = canvas.getBoundingClientRect(); |
| 417 | var x = Math.floor((e.clientX - r.left) / cellPx); |
| 418 | var y = Math.floor((e.clientY - r.top) / cellPx); |
| 419 | if (x < 0 || y < 0 || x >= level().width || y >= level().height) |
| 420 | { return null; } |
| 421 | return { x: x, y: y }; |
| 422 | } |
| 423 | |
| 424 | function paintAt (x, y) |
| 425 | { |
| 426 | var L = level(); |
| 427 | var i = idx(x, y); |
| 428 | if (tool === "wall") |
| 429 | { L.walls[i] = brushWall; } |
| 430 | else if (tool === "floor") |
| 431 | { L.floors[i] = brushTex === "none" ? null : brushTex; } |
| 432 | else if (tool === "ceiling") |
| 433 | { L.ceilings[i] = brushTex === "none" ? null : brushTex; } |
| 434 | else if (tool === "spawn") |
| 435 | { |
| 436 | L.spawn.tileX = x; |
| 437 | L.spawn.tileY = y; |
| 438 | } |
| 439 | else if (tool === "npc") |
| 440 | { |
| 441 | var cs = L.cellSize || 32; |
| 442 | L.npcs.push({ |
| 443 | x: x * cs + cs / 2, |
| 444 | y: y * cs + cs / 2, |
| 445 | facingDeg: Number(document.getElementById("npc-yaw").value) || 0, |
| 446 | clip: document.getElementById("npc-clip").value || "walk", |
| 447 | animating: document.getElementById("npc-anim").checked, |
| 448 | label: "npc" + (L.npcs.length + 1), |
| 449 | body: "paperdolls/male-body/", |
| 450 | bodyPpda: "male-body-atlas.ppda", |
| 451 | gear: "paperdolls/male-sword/", |
| 452 | gearPpda: "male-sword-atlas.ppda" |
| 453 | }); |
| 454 | selectedNpc = L.npcs.length - 1; |
| 455 | refreshNpcList(); |
| 456 | } |
| 457 | redraw(); |
| 458 | saveLocal(); |
| 459 | } |
| 460 | |
| 461 | function resizeLevel (nw, nh) |
| 462 | { |
| 463 | var L = level(); |
| 464 | nw = Math.max(3, Math.min(LevelFormat.MAX_SIZE, nw | 0)); |
| 465 | nh = Math.max(3, Math.min(LevelFormat.MAX_SIZE, nh | 0)); |
| 466 | var walls = []; |
| 467 | var floors = []; |
| 468 | var ceilings = []; |
| 469 | for (var y = 0; y < nh; y++) |
| 470 | { |
| 471 | for (var x = 0; x < nw; x++) |
| 472 | { |
| 473 | if (x < L.width && y < L.height) |
| 474 | { |
| 475 | var i = idx(x, y); |
| 476 | walls.push(L.walls[i]); |
| 477 | floors.push(L.floors[i]); |
| 478 | ceilings.push(L.ceilings[i]); |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | var border = x === 0 || y === 0 || x === nw - 1 || y === nh - 1; |
| 483 | walls.push(border ? 1 : 0); |
| 484 | floors.push(border ? null : "stone"); |
| 485 | ceilings.push(border ? null : "stone"); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | L.width = nw; |
| 490 | L.height = nh; |
| 491 | L.walls = walls; |
| 492 | L.floors = floors; |
| 493 | L.ceilings = ceilings; |
| 494 | if (L.spawn.tileX >= nw) L.spawn.tileX = nw - 2; |
| 495 | if (L.spawn.tileY >= nh) L.spawn.tileY = nh - 2; |
| 496 | refreshLevelSelect(); |
| 497 | redraw(); |
| 498 | saveLocal(); |
| 499 | } |
| 500 | |
| 501 | function boot (initial) |
| 502 | { |
| 503 | canvas = document.getElementById("grid"); |
| 504 | ctx = canvas.getContext("2d"); |
| 505 | ensurePack(initial); |
| 506 | loadPreviews(); |
| 507 | |
| 508 | document.getElementById("level-select").onchange = function (e) |
| 509 | { |
| 510 | levelIndex = Number(e.target.value) | 0; |
| 511 | selectedNpc = -1; |
| 512 | refreshLevelSelect(); |
| 513 | redraw(); |
| 514 | }; |
| 515 | document.getElementById("level-name").onchange = function (e) |
| 516 | { |
| 517 | level().name = e.target.value || level().name; |
| 518 | refreshLevelSelect(); |
| 519 | saveLocal(); |
| 520 | }; |
| 521 | document.getElementById("btn-add-level").onclick = function () |
| 522 | { |
| 523 | pack.levels.push(LevelFormat.emptyLevel("Level " + (pack.levels.length + 1), 10, 10)); |
| 524 | levelIndex = pack.levels.length - 1; |
| 525 | refreshLevelSelect(); |
| 526 | redraw(); |
| 527 | saveLocal(); |
| 528 | }; |
| 529 | document.getElementById("btn-dup-level").onclick = function () |
| 530 | { |
| 531 | var c = LevelFormat.cloneLevel(level()); |
| 532 | c.id = "level-" + Date.now().toString(36); |
| 533 | c.name = level().name + " (kopie)"; |
| 534 | pack.levels.push(c); |
| 535 | levelIndex = pack.levels.length - 1; |
| 536 | refreshLevelSelect(); |
| 537 | redraw(); |
| 538 | saveLocal(); |
| 539 | }; |
| 540 | document.getElementById("btn-del-level").onclick = function () |
| 541 | { |
| 542 | if (pack.levels.length <= 1) |
| 543 | { return alert("Minstens één level houden"); } |
| 544 | if (!confirm("Level verwijderen?")) |
| 545 | { return; } |
| 546 | pack.levels.splice(levelIndex, 1); |
| 547 | levelIndex = Math.min(levelIndex, pack.levels.length - 1); |
| 548 | refreshLevelSelect(); |
| 549 | redraw(); |
| 550 | saveLocal(); |
| 551 | }; |
| 552 | document.getElementById("btn-resize").onclick = function () |
| 553 | { |
| 554 | resizeLevel(Number(document.getElementById("level-w").value), Number(document.getElementById("level-h").value)); |
| 555 | }; |
| 556 | |
| 557 | document.querySelectorAll("[data-tool]").forEach(function (el) |
| 558 | { |
| 559 | el.onclick = function () |
| 560 | { |
| 561 | tool = el.getAttribute("data-tool"); |
| 562 | document.querySelectorAll("[data-tool]").forEach(function (b) |
| 563 | { b.classList.toggle("active", b === el); }); |
| 564 | }; |
| 565 | }); |
| 566 | document.querySelectorAll("[data-wall]").forEach(function (el) |
| 567 | { |
| 568 | el.onclick = function () |
| 569 | { |
| 570 | brushWall = Number(el.getAttribute("data-wall")); |
| 571 | tool = "wall"; |
| 572 | }; |
| 573 | }); |
| 574 | document.querySelectorAll("[data-tex]").forEach(function (el) |
| 575 | { |
| 576 | el.onclick = function () |
| 577 | { |
| 578 | brushTex = el.getAttribute("data-tex"); |
| 579 | if (brushTex === "none") |
| 580 | { brushTex = "none"; } |
| 581 | }; |
| 582 | }); |
| 583 | |
| 584 | canvas.onmousedown = function (e) |
| 585 | { |
| 586 | painting = true; |
| 587 | var c = cellFromEvent(e); |
| 588 | if (c) |
| 589 | { paintAt(c.x, c.y); } |
| 590 | }; |
| 591 | canvas.onmousemove = function (e) |
| 592 | { |
| 593 | if (!painting || tool === "spawn" || tool === "npc") |
| 594 | { return; } |
| 595 | var c = cellFromEvent(e); |
| 596 | if (c) |
| 597 | { paintAt(c.x, c.y); } |
| 598 | }; |
| 599 | window.onmouseup = function () |
| 600 | { painting = false; }; |
| 601 | |
| 602 | document.getElementById("spawn-yaw").onchange = function (e) |
| 603 | { |
| 604 | level().spawn.yawRad = (Number(e.target.value) || 0) * Math.PI / 180; |
| 605 | redraw(); |
| 606 | saveLocal(); |
| 607 | }; |
| 608 | document.getElementById("btn-del-npc").onclick = function () |
| 609 | { |
| 610 | if (selectedNpc < 0) |
| 611 | { return; } |
| 612 | level().npcs.splice(selectedNpc, 1); |
| 613 | selectedNpc = -1; |
| 614 | refreshNpcList(); |
| 615 | redraw(); |
| 616 | saveLocal(); |
| 617 | }; |
| 618 | document.getElementById("npc-yaw").onchange = function (e) |
| 619 | { |
| 620 | if (selectedNpc >= 0) |
| 621 | { |
| 622 | level().npcs[selectedNpc].facingDeg = Number(e.target.value) || 0; |
| 623 | refreshNpcList(); |
| 624 | redraw(); |
| 625 | saveLocal(); |
| 626 | } |
| 627 | }; |
| 628 | document.getElementById("npc-clip").onchange = function (e) |
| 629 | { |
| 630 | if (selectedNpc >= 0) |
| 631 | { |
| 632 | level().npcs[selectedNpc].clip = e.target.value; |
| 633 | saveLocal(); |
| 634 | } |
| 635 | }; |
| 636 | document.getElementById("npc-anim").onchange = function (e) |
| 637 | { |
| 638 | if (selectedNpc >= 0) |
| 639 | { |
| 640 | level().npcs[selectedNpc].animating = e.target.checked; |
| 641 | saveLocal(); |
| 642 | } |
| 643 | }; |
| 644 | |
| 645 | document.getElementById("btn-download").onclick = function () |
| 646 | { |
| 647 | try |
| 648 | { LevelFormat.validatePack(pack); } |
| 649 | catch (err) |
| 650 | { return alert(err.message); } |
| 651 | var blob = new Blob([JSON.stringify(pack, null, 2)], { type: "application/json" }); |
| 652 | var a = document.createElement("a"); |
| 653 | a.href = URL.createObjectURL(blob); |
| 654 | a.download = "levels.json"; |
| 655 | a.click(); |
| 656 | }; |
| 657 | document.getElementById("file-upload").onchange = function (e) |
| 658 | { |
| 659 | var f = e.target.files[0]; |
| 660 | if (!f) |
| 661 | { return; } |
| 662 | var reader = new FileReader(); |
| 663 | reader.onload = function () |
| 664 | { |
| 665 | try |
| 666 | { ensurePack(LevelFormat.parse(String(reader.result))); } |
| 667 | catch (err) |
| 668 | { alert(err.message); } |
| 669 | }; |
| 670 | reader.readAsText(f); |
| 671 | }; |
| 672 | document.getElementById("btn-play").onclick = function () |
| 673 | { |
| 674 | try |
| 675 | { LevelFormat.validatePack(pack); } |
| 676 | catch (err) |
| 677 | { return alert(err.message); } |
| 678 | saveLocal(); |
| 679 | window.location.href = "index.html?playtest=1&level=" + encodeURIComponent(level().id); |
| 680 | }; |
| 681 | document.getElementById("btn-reload-repo").onclick = function () |
| 682 | { |
| 683 | fetch("levels.json").then(function (r) |
| 684 | { return r.text(); }).then(function (t) |
| 685 | { ensurePack(LevelFormat.parse(t)); }).catch(function (e) |
| 686 | { alert(e.message); }); |
| 687 | }; |
| 688 | } |
| 689 | |
| 690 | window.EditorApp = { |
| 691 | start: function () |
| 692 | { |
| 693 | var local = null; |
| 694 | try |
| 695 | { local = localStorage.getItem(STORAGE_KEY); } |
| 696 | catch (e) { /* */ } |
| 697 | var bootFrom = function (text) |
| 698 | { boot(LevelFormat.parse(text)); }; |
| 699 | if (local) |
| 700 | { |
| 701 | try |
| 702 | { return boot(LevelFormat.parse(local)); } |
| 703 | catch (e) |
| 704 | { console.warn("localStorage levels ongeldig, repo laden", e); } |
| 705 | } |
| 706 | fetch("levels.json").then(function (r) |
| 707 | { return r.text(); }).then(bootFrom).catch(function (e) |
| 708 | { |
| 709 | alert("Kon levels niet laden: " + e.message); |
| 710 | boot(LevelFormat.defaultPack()); |
| 711 | pack.levels.push(LevelFormat.emptyLevel("Level 1", 10, 10)); |
| 712 | ensurePack(pack); |
| 713 | }); |
| 714 | } |
| 715 | }; |
| 716 | })(); |