BlueGit

timfa questward

Files

questward / client / src / play / spells.js

spells.js

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