BlueGit

timfa questward

Files

questward / client / src / play / spells.js

spells.js

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