BlueGit

timfa questward

Files

questward / client / src / play / spells.js

spells.js

spells.js 7.5 KiB Download Raw
1/**
2 * Play spells panel: known spells, favourites bar, arm / cast intents.
3 */
4import { spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "/shared/definitions/spells.js";
5import { SkillId } from "/shared/definitions/skills.js";
6import { spellMaxHitDisplay } from "/shared/combat/maxHit.js";
7
8export function initSpells ({
9 socket,
10 MessageType,
11 getSkillLevel,
12 getSkillLevels = null,
13 getEquipmentSlots = null,
14 getSettings = null
15} = {})
16{
17 const panel = document.getElementById("spells-panel");
18 const list = document.getElementById("spells-list");
19 const bar = document.getElementById("spell-favorites-bar");
20 const autoCastToggle = document.getElementById("spell-autocast-toggle");
21 let spells = [];
22 let autoCast = false;
23 let armedSpellId = null;
24
25 document.getElementById("spells-toggle")?.addEventListener("click", () =>
26 {
27 panel.hidden = !panel.hidden;
28 if (!panel.hidden)
29 { refreshCastGates(); }
30 });
31
32 autoCastToggle?.addEventListener("change", () =>
33 {
34 socket.send({
35 type: MessageType.setSpellAutoCast,
36 enabled: autoCastToggle.checked
37 });
38 });
39
40 /** Re-evaluate Magic gates from the latest client skill levels (e.g. after level-up). */
41 function refreshCastGates ()
42 {
43 if (!spells.length)
44 { return; }
45 const magicLevel = Math.max(1, getSkillLevel?.(SkillId.magic) | 0 || 1);
46 let changed = false;
47 for (const spell of spells)
48 {
49 const need = spellMinMagicLevel(spellDefinition(spell.spellId));
50 const ok = magicLevel >= need;
51 if (spell.canCast !== ok || spell.canFavorite !== ok)
52 {
53 spell.canCast = ok;
54 spell.canFavorite = ok;
55 changed = true;
56 }
57 }
58 if (changed || !panel.hidden)
59 { renderList(); }
60 /** Favourites sit outside the panel — always sync so they show after login. */
61 renderBar();
62 }
63
64 function refreshMaxHitDisplay ()
65 {
66 if (!panel.hidden)
67 { renderList(); }
68 }
69
70 function disarmLocal ()
71 {
72 if (!armedSpellId)
73 { return; }
74 armedSpellId = null;
75 renderList();
76 renderBar();
77 }
78
79 function render (payload)
80 {
81 spells = payload?.spells ?? [];
82 autoCast = !!payload?.autoCast;
83 if (payload?.armedSpellId !== undefined)
84 { armedSpellId = payload.armedSpellId; }
85 if (autoCastToggle)
86 { autoCastToggle.checked = autoCast; }
87 refreshCastGates();
88 }
89
90 function renderList ()
91 {
92 list.replaceChildren();
93 for (const spell of spells)
94 {
95 list.append(renderSpellRow(spell));
96 }
97 }
98
99 function renderSpellRow (spell)
100 {
101 const row = document.createElement("div");
102 row.className = "spell-row";
103 if (spell.spellId === armedSpellId)
104 { row.classList.add("armed"); }
105
106 const icon = document.createElement("img");
107 icon.className = "spell-icon";
108 icon.src = spell.icon;
109 icon.alt = "";
110 icon.loading = "lazy";
111
112 const body = document.createElement("div");
113 body.className = "spell-body";
114
115 const title = document.createElement("div");
116 title.className = "spell-title-row";
117 const name = document.createElement("span");
118 name.className = "spell-title";
119 name.textContent = spell.displayName;
120 const meta = document.createElement("span");
121 meta.className = "spell-meta";
122 const level = spell.tier | 0 || 1;
123 const power = spell.mindPower != null
124 ? spell.mindPower | 0
125 : spellMindPowerFromTier(level);
126 meta.textContent = `Lv. ${level} Pwr: ${power}`;
127 title.append(name, meta);
128
129 const desc = document.createElement("div");
130 desc.className = "spell-desc hint";
131 desc.textContent = spell.description ?? "";
132
133 const maxHit = spellMaxHitDisplay({
134 spellId: spell.spellId,
135 skillLevels: getSkillLevels?.() ?? {},
136 equipmentSlots: getEquipmentSlots?.() ?? {},
137 settings: getSettings?.() ?? null
138 });
139 const maxHitEl = maxHit != null
140 ? (() =>
141 {
142 const line = document.createElement("div");
143 line.className = "spell-max-hit hint";
144 line.textContent = `Max hit: ${maxHit}`;
145 return line;
146 })()
147 : null;
148
149 const actions = document.createElement("div");
150 actions.className = "spell-actions";
151
152 const favorite = document.createElement("button");
153 favorite.type = "button";
154 favorite.className = "spell-favorite-btn";
155 favorite.textContent = spell.favorite ? "★" : "☆";
156 favorite.title = spell.canFavorite
157 ? (spell.favorite ? "Remove from favourites" : "Add to favourites")
158 : "Magic level too low to favourite";
159 favorite.disabled = !spell.canFavorite;
160 favorite.addEventListener("click", () =>
161 {
162 socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId });
163 });
164
165 const arm = document.createElement("button");
166 arm.type = "button";
167 arm.className = "spell-arm-btn";
168 arm.textContent = spell.spellId === armedSpellId ? "Armed" : "Arm";
169 arm.disabled = !spell.canCast;
170 arm.addEventListener("click", () =>
171 {
172 if (spell.spellId === armedSpellId)
173 { cancelTargeting(); }
174 else
175 { socket.send({ type: MessageType.armSpell, spellId: spell.spellId }); }
176 });
177
178 actions.append(favorite, arm);
179 body.append(title, desc);
180 if (maxHitEl)
181 { body.append(maxHitEl); }
182 body.append(actions);
183 row.append(icon, body);
184 return row;
185 }
186
187 function renderBar ()
188 {
189 const favorites = spells.filter((entry) => entry.favorite);
190 bar.replaceChildren();
191 bar.hidden = favorites.length === 0;
192 for (const spell of favorites)
193 {
194 const btn = document.createElement("button");
195 btn.type = "button";
196 btn.className = "spell-favorite-slot";
197 if (spell.spellId === armedSpellId)
198 { btn.classList.add("armed"); }
199 btn.title = spell.displayName;
200 btn.disabled = !spell.canCast;
201
202 const icon = document.createElement("img");
203 icon.src = spell.icon;
204 icon.alt = "";
205 btn.append(icon);
206
207 btn.addEventListener("click", () =>
208 {
209 if (spell.spellId === armedSpellId)
210 { return; }
211 socket.send({ type: MessageType.armSpell, spellId: spell.spellId });
212 });
213 btn.addEventListener("contextmenu", (event) =>
214 {
215 event.preventDefault();
216 socket.send({ type: MessageType.setSpellFavorite, spellId: spell.spellId });
217 });
218 bar.append(btn);
219 }
220 }
221
222 function getArmedSpell ()
223 {
224 if (!armedSpellId)
225 { return null; }
226 return spells.find((entry) => entry.spellId === armedSpellId) ?? null;
227 }
228
229 function tryCastAtTile (tileX, tileY)
230 {
231 const armed = getArmedSpell();
232 if (!armed || armed.target !== "tile" || !armed.canCast)
233 { return false; }
234 socket.send({
235 type: MessageType.castSpellTile,
236 spellId: armed.spellId,
237 tileX,
238 tileY
239 });
240 return true;
241 }
242
243 function tryCastAtEntity (targetEntityId)
244 {
245 const armed = getArmedSpell();
246 if (!armed || armed.target !== "entity" || !armed.canCast)
247 { return false; }
248 socket.send({
249 type: MessageType.castSpellEntity,
250 spellId: armed.spellId,
251 targetEntityId
252 });
253 return true;
254 }
255
256 function cancelTargeting ()
257 {
258 if (!armedSpellId)
259 { return; }
260 socket.send({ type: MessageType.cancelArmSpell });
261 disarmLocal();
262 }
263
264 return {
265 render,
266 refreshCastGates,
267 getArmedSpell,
268 tryCastAtTile,
269 tryCastAtEntity,
270 cancelTargeting,
271 isBlocking ()
272 {
273 return panel != null && !panel.hidden;
274 },
275 isTargeting ()
276 {
277 return armedSpellId != null;
278 },
279 handleSpellArmed (message)
280 {
281 armedSpellId = message.spellId ?? null;
282 renderList();
283 renderBar();
284 },
285 knowsSpell (spellId)
286 {
287 return spells.some((spell) => spell.spellId === spellId);
288 },
289 getKnownSpellIds ()
290 {
291 return spells.map((spell) => spell.spellId);
292 },
293 refreshMaxHitDisplay,
294 handleSpellDisarmed ()
295 {
296 disarmLocal();
297 },
298 hide ()
299 {
300 panel.hidden = true;
301 spells = [];
302 autoCast = false;
303 armedSpellId = null;
304 if (autoCastToggle)
305 { autoCastToggle.checked = false; }
306 list.replaceChildren();
307 bar.replaceChildren();
308 bar.hidden = true;
309 }
310 };
311}