| 1 | /** |
| 2 | * Right-click config modal for editor tiles that have configurable data (spawners). |
| 3 | */ |
| 4 | import { listSpawnableMobTypes, normalizeSpawnerConfig } from "/shared/definitions/mobs.js"; |
| 5 | |
| 6 | export function initTileConfigModal ({ onSave, onRemove }) |
| 7 | { |
| 8 | const backdrop = document.getElementById("tile-config-backdrop"); |
| 9 | const modal = document.getElementById("tile-config-modal"); |
| 10 | const form = document.getElementById("tile-config-form"); |
| 11 | const title = document.getElementById("tile-config-title"); |
| 12 | let open = false; |
| 13 | let context = null; |
| 14 | |
| 15 | function show (visible) |
| 16 | { |
| 17 | open = visible; |
| 18 | backdrop.hidden = !visible; |
| 19 | modal.hidden = !visible; |
| 20 | if (!visible) |
| 21 | { context = null; } |
| 22 | } |
| 23 | |
| 24 | function openSpawner (ctx) |
| 25 | { |
| 26 | context = { kind: "spawner", ...ctx }; |
| 27 | const config = normalizeSpawnerConfig(ctx.config, ctx.config.spawnerId); |
| 28 | title.textContent = `Spawner · ${ctx.tileX}, ${ctx.tileY}`; |
| 29 | form.innerHTML = ""; |
| 30 | form.append( |
| 31 | field("Mob type", mobSelect("mobTypeId", config.mobTypeId)), |
| 32 | field("Max alive", numberInput("maxAlive", config.maxAlive, 1, 20)), |
| 33 | field("Spawn range", numberInput("spawnRangeTiles", config.spawnRangeTiles, 1, 32)), |
| 34 | field("Max wander range", numberInput("maxWanderRangeTiles", config.maxWanderRangeTiles, 1, 64)), |
| 35 | field("Spawner id", textInput("spawnerId", config.spawnerId, true)) |
| 36 | ); |
| 37 | const spawnInput = form.querySelector('[name="spawnRangeTiles"]'); |
| 38 | const wanderInput = form.querySelector('[name="maxWanderRangeTiles"]'); |
| 39 | const syncWanderMin = () => |
| 40 | { |
| 41 | const spawn = Number(spawnInput.value) || 1; |
| 42 | wanderInput.min = String(spawn); |
| 43 | if (Number(wanderInput.value) < spawn) |
| 44 | { wanderInput.value = String(spawn); } |
| 45 | }; |
| 46 | spawnInput.addEventListener("input", syncWanderMin); |
| 47 | syncWanderMin(); |
| 48 | show(true); |
| 49 | } |
| 50 | |
| 51 | document.getElementById("tile-config-close").addEventListener("click", () => show(false)); |
| 52 | document.getElementById("tile-config-cancel").addEventListener("click", () => show(false)); |
| 53 | backdrop.addEventListener("click", () => show(false)); |
| 54 | document.getElementById("tile-config-save").addEventListener("click", () => |
| 55 | { |
| 56 | if (!context) |
| 57 | { return; } |
| 58 | const data = new FormData(form); |
| 59 | onSave?.(context, normalizeSpawnerConfig({ |
| 60 | mobTypeId: String(data.get("mobTypeId") || "deer"), |
| 61 | maxAlive: Number(data.get("maxAlive") || 3), |
| 62 | spawnRangeTiles: Number(data.get("spawnRangeTiles") || 4), |
| 63 | maxWanderRangeTiles: Number(data.get("maxWanderRangeTiles") || 4), |
| 64 | spawnerId: String(data.get("spawnerId") || context.config.spawnerId) |
| 65 | }, context.config.spawnerId)); |
| 66 | show(false); |
| 67 | }); |
| 68 | document.getElementById("tile-config-remove").addEventListener("click", () => |
| 69 | { |
| 70 | if (!context) |
| 71 | { return; } |
| 72 | onRemove?.(context); |
| 73 | show(false); |
| 74 | }); |
| 75 | |
| 76 | return { |
| 77 | isOpen: () => open, |
| 78 | openSpawner, |
| 79 | close: () => show(false) |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | function field (label, input) |
| 84 | { |
| 85 | const wrap = document.createElement("label"); |
| 86 | wrap.append(document.createTextNode(label), input); |
| 87 | return wrap; |
| 88 | } |
| 89 | |
| 90 | function numberInput (name, value, min, max) |
| 91 | { |
| 92 | const input = document.createElement("input"); |
| 93 | input.type = "number"; |
| 94 | input.name = name; |
| 95 | input.min = String(min); |
| 96 | input.max = String(max); |
| 97 | input.value = String(value); |
| 98 | return input; |
| 99 | } |
| 100 | |
| 101 | function textInput (name, value, readOnly = false) |
| 102 | { |
| 103 | const input = document.createElement("input"); |
| 104 | input.type = "text"; |
| 105 | input.name = name; |
| 106 | input.value = String(value ?? ""); |
| 107 | input.readOnly = readOnly; |
| 108 | return input; |
| 109 | } |
| 110 | |
| 111 | function mobSelect (name, value) |
| 112 | { |
| 113 | const select = document.createElement("select"); |
| 114 | select.name = name; |
| 115 | for (const def of listSpawnableMobTypes()) |
| 116 | { |
| 117 | const option = document.createElement("option"); |
| 118 | option.value = def.mobTypeId; |
| 119 | option.textContent = def.displayName; |
| 120 | if (def.mobTypeId === value) |
| 121 | { option.selected = true; } |
| 122 | select.append(option); |
| 123 | } |
| 124 | return select; |
| 125 | } |