/** * Play spells panel: known spells, favourites bar, arm / cast intents. */ import { spellMindPowerFromTier } from "/shared/definitions/spells.js"; export function initSpells ({ socket, MessageType }) { const panel = document.getElementById("spells-panel"); const list = document.getElementById("spells-list"); const bar = document.getElementById("spell-favorites-bar"); const autoCastToggle = document.getElementById("spell-autocast-toggle"); let spells = []; let autoCast = false; let armedSpellId = null; document.getElementById("spells-toggle")?.addEventListener("click", () => { panel.hidden = !panel.hidden; }); autoCastToggle?.addEventListener("change", () => { socket.send({ type: MessageType.setSpellAutoCast, enabled: autoCastToggle.checked }); }); function disarmLocal () { if (!armedSpellId) { return; } armedSpellId = null; renderList(); renderBar(); } function render (payload) { spells = payload?.spells ?? []; autoCast = !!payload?.autoCast; if (payload?.armedSpellId !== undefined) { armedSpellId = payload.armedSpellId; } if (autoCastToggle) { autoCastToggle.checked = autoCast; } renderList(); renderBar(); } function renderList () { list.replaceChildren(); for (const spell of spells) { list.append(renderSpellRow(spell)); } } function renderSpellRow (spell) { const row = document.createElement("div"); row.className = "spell-row"; if (spell.spellId === armedSpellId) { row.classList.add("armed"); } const icon = document.createElement("img"); icon.className = "spell-icon"; icon.src = spell.icon; icon.alt = ""; icon.loading = "lazy"; const body = document.createElement("div"); body.className = "spell-body"; const title = document.createElement("div"); title.className = "spell-title-row"; const name = document.createElement("span"); name.className = "spell-title"; name.textContent = spell.displayName; const meta = document.createElement("span"); meta.className = "spell-meta"; const level = spell.tier | 0 || 1; const power = spell.mindPower != null ? spell.mindPower | 0 : spellMindPowerFromTier(level); meta.textContent = `Lv. ${level} Pwr: ${power}`; title.append(name, meta); const desc = document.createElement("div"); desc.className = "spell-desc hint"; desc.textContent = spell.description ?? ""; const actions = document.createElement("div"); actions.className = "spell-actions"; const favorite = document.createElement("button"); favorite.type = "button"; favorite.className = "spell-favorite-btn"; favorite.textContent = spell.favorite ? "★" : "☆"; favorite.title = spell.canFavorite ? (spell.favorite ? "Remove from favourites" : "Add to favourites") : "Magic level too low to favourite"; favorite.disabled = !spell.canFavorite; favorite.addEventListener("click", () => { socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId }); }); const arm = document.createElement("button"); arm.type = "button"; arm.className = "spell-arm-btn"; arm.textContent = spell.spellId === armedSpellId ? "Armed" : "Arm"; arm.disabled = !spell.canCast; arm.addEventListener("click", () => { if (spell.spellId === armedSpellId) { cancelTargeting(); } else { socket.send({ type: MessageType.armSpell, spellId: spell.spellId }); } }); actions.append(favorite, arm); body.append(title, desc, actions); row.append(icon, body); return row; } function renderBar () { const favorites = spells.filter((entry) => entry.favorite); bar.replaceChildren(); bar.hidden = favorites.length === 0; for (const spell of favorites) { const btn = document.createElement("button"); btn.type = "button"; btn.className = "spell-favorite-slot"; if (spell.spellId === armedSpellId) { btn.classList.add("armed"); } btn.title = spell.displayName; btn.disabled = !spell.canCast; const icon = document.createElement("img"); icon.src = spell.icon; icon.alt = ""; btn.append(icon); btn.addEventListener("click", () => { if (spell.spellId === armedSpellId) { return; } socket.send({ type: MessageType.armSpell, spellId: spell.spellId }); }); btn.addEventListener("contextmenu", (event) => { event.preventDefault(); socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId }); }); bar.append(btn); } } function getArmedSpell () { if (!armedSpellId) { return null; } return spells.find((entry) => entry.spellId === armedSpellId) ?? null; } function tryCastAtTile (tileX, tileY) { const armed = getArmedSpell(); if (!armed || armed.target !== "tile" || !armed.canCast) { return false; } socket.send({ type: MessageType.castSpellTile, spellId: armed.spellId, tileX, tileY }); return true; } function tryCastAtEntity (targetEntityId) { const armed = getArmedSpell(); if (!armed || armed.target !== "entity" || !armed.canCast) { return false; } socket.send({ type: MessageType.castSpellEntity, spellId: armed.spellId, targetEntityId }); return true; } function cancelTargeting () { if (!armedSpellId) { return; } socket.send({ type: MessageType.cancelArmSpell }); disarmLocal(); } return { render, getArmedSpell, tryCastAtTile, tryCastAtEntity, cancelTargeting, isBlocking () { return panel != null && !panel.hidden; }, isTargeting () { return armedSpellId != null; }, handleSpellArmed (message) { armedSpellId = message.spellId ?? null; renderList(); renderBar(); }, handleSpellDisarmed () { disarmLocal(); }, hide () { panel.hidden = true; spells = []; autoCast = false; armedSpellId = null; if (autoCastToggle) { autoCastToggle.checked = false; } list.replaceChildren(); bar.replaceChildren(); bar.hidden = true; } }; }