BlueGit

timfa questward

Files

questward / client / src / play / spells.js

spells.js

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