Spaces:
Running
Running
Update UIManager.js
Browse files- src/ui/UIManager.js +66 -44
src/ui/UIManager.js
CHANGED
@@ -221,8 +221,19 @@ export class UIManager {
|
|
221 |
|
222 |
// Utility: build default palette options using game config
|
223 |
_defaultTowerOptions() {
|
224 |
-
//
|
225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
const opts = [];
|
227 |
|
228 |
const push = (t, extra = {}) => {
|
@@ -252,11 +263,13 @@ export class UIManager {
|
|
252 |
});
|
253 |
};
|
254 |
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
|
|
260 |
return opts;
|
261 |
}
|
262 |
|
@@ -270,45 +283,54 @@ export class UIManager {
|
|
270 |
const list = this.palette.querySelector(".palette-list");
|
271 |
if (!list) return;
|
272 |
|
273 |
-
// Read costs from config
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
}
|
281 |
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
295 |
}
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
}
|
312 |
});
|
313 |
}
|
314 |
|
|
|
221 |
|
222 |
// Utility: build default palette options using game config
|
223 |
_defaultTowerOptions() {
|
224 |
+
// Prefer static ESM import at top of module for browser compatibility.
|
225 |
+
// We cache config on the instance if dynamic import is needed elsewhere.
|
226 |
+
if (!this._cfgSync) {
|
227 |
+
// Fallback in case not yet set by updateTowerAffordability's dynamic import
|
228 |
+
// but since gameConfig is an ESM and likely already evaluated via imports,
|
229 |
+
// we can safely rely on synchronous import by hoisting at top-level if desired.
|
230 |
+
}
|
231 |
+
// Use a synchronous named import by referencing cached module if present;
|
232 |
+
// otherwise import statically at top of file would be ideal.
|
233 |
+
const TOWER_TYPES = (this._cfg && this._cfg.TOWER_TYPES) || undefined;
|
234 |
+
// If not cached yet, we can safely reference a top-level import added by bundlers.
|
235 |
+
// To avoid runtime errors, guard and build using an empty array if config missing temporarily.
|
236 |
+
const types = TOWER_TYPES;
|
237 |
const opts = [];
|
238 |
|
239 |
const push = (t, extra = {}) => {
|
|
|
263 |
});
|
264 |
};
|
265 |
|
266 |
+
if (types) {
|
267 |
+
push(types.basic);
|
268 |
+
push(types.slow);
|
269 |
+
push(types.sniper);
|
270 |
+
// Ensure Electric shows in palette
|
271 |
+
if (types.electric) push(types.electric);
|
272 |
+
}
|
273 |
return opts;
|
274 |
}
|
275 |
|
|
|
283 |
const list = this.palette.querySelector(".palette-list");
|
284 |
if (!list) return;
|
285 |
|
286 |
+
// Read costs from config via dynamic ESM import for browser compatibility
|
287 |
+
// Cache the loaded module on the instance to avoid re-fetching.
|
288 |
+
if (!this._cfgPromise) {
|
289 |
+
this._cfgPromise = import("../config/gameConfig.js").then((m) => {
|
290 |
+
this._cfg = m;
|
291 |
+
return m;
|
292 |
+
});
|
293 |
+
}
|
294 |
|
295 |
+
this._cfgPromise.then(({ TOWER_TYPES }) => {
|
296 |
+
const costByKey = {
|
297 |
+
basic: TOWER_TYPES.basic?.cost,
|
298 |
+
slow: TOWER_TYPES.slow?.cost,
|
299 |
+
sniper: TOWER_TYPES.sniper?.cost,
|
300 |
+
electric: TOWER_TYPES.electric?.cost,
|
301 |
+
};
|
302 |
+
|
303 |
+
// Iterate palette items in DOM (each item corresponds to one tower option)
|
304 |
+
const items = list.querySelectorAll(".palette-item");
|
305 |
+
items.forEach((item) => {
|
306 |
+
// Determine tower key for this item by reading its label text
|
307 |
+
// Labels are created as the first span with the tower name
|
308 |
+
const labelSpan = item.querySelector("span:first-child");
|
309 |
+
const name = labelSpan ? labelSpan.textContent : "";
|
310 |
+
// Map name back to key via config
|
311 |
+
let key = null;
|
312 |
+
for (const k of Object.keys(TOWER_TYPES)) {
|
313 |
+
if (TOWER_TYPES[k]?.name === name) {
|
314 |
+
key = k;
|
315 |
+
break;
|
316 |
+
}
|
317 |
}
|
318 |
+
if (!key) return;
|
319 |
+
|
320 |
+
const cost = costByKey[key];
|
321 |
+
const affordable =
|
322 |
+
typeof cost === "number" ? cost <= currentMoney : false;
|
323 |
+
|
324 |
+
// Disable/enable via aria-disabled like current structure uses
|
325 |
+
if (affordable) {
|
326 |
+
item.removeAttribute("aria-disabled");
|
327 |
+
item.classList.remove("unaffordable");
|
328 |
+
} else {
|
329 |
+
item.setAttribute("aria-disabled", "true");
|
330 |
+
// Optional class; safe to add/remove if styles define it
|
331 |
+
item.classList.add("unaffordable");
|
332 |
+
}
|
333 |
+
});
|
334 |
});
|
335 |
}
|
336 |
|