victor HF Staff commited on
Commit
3eeae2e
·
1 Parent(s): 7847a84

Update UIManager.js

Browse files
Files changed (1) hide show
  1. 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
- // Lazy import to avoid circular deps in some bundlers
225
- const { TOWER_TYPES } = require("../config/gameConfig.js");
 
 
 
 
 
 
 
 
 
 
 
226
  const opts = [];
227
 
228
  const push = (t, extra = {}) => {
@@ -252,11 +263,13 @@ export class UIManager {
252
  });
253
  };
254
 
255
- push(TOWER_TYPES.basic);
256
- push(TOWER_TYPES.slow);
257
- push(TOWER_TYPES.sniper);
258
- // Ensure Electric shows in palette
259
- if (TOWER_TYPES.electric) push(TOWER_TYPES.electric);
 
 
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
- const { TOWER_TYPES } = require("../config/gameConfig.js");
275
- const costByKey = {
276
- basic: TOWER_TYPES.basic?.cost,
277
- slow: TOWER_TYPES.slow?.cost,
278
- sniper: TOWER_TYPES.sniper?.cost,
279
- electric: TOWER_TYPES.electric?.cost,
280
- };
281
 
282
- // Iterate palette items in DOM (each item corresponds to one tower option)
283
- const items = list.querySelectorAll(".palette-item");
284
- items.forEach((item) => {
285
- // Determine tower key for this item by reading its label text
286
- // Labels are created as the first span with the tower name
287
- const labelSpan = item.querySelector("span:first-child");
288
- const name = labelSpan ? labelSpan.textContent : "";
289
- // Map name back to key via config
290
- let key = null;
291
- for (const k of Object.keys(TOWER_TYPES)) {
292
- if (TOWER_TYPES[k]?.name === name) {
293
- key = k;
294
- break;
 
 
 
 
 
 
 
 
 
295
  }
296
- }
297
- if (!key) return;
298
-
299
- const cost = costByKey[key];
300
- const affordable =
301
- typeof cost === "number" ? cost <= currentMoney : false;
302
-
303
- // Disable/enable via aria-disabled like current structure uses
304
- if (affordable) {
305
- item.removeAttribute("aria-disabled");
306
- item.classList.remove("unaffordable");
307
- } else {
308
- item.setAttribute("aria-disabled", "true");
309
- // Optional class; safe to add/remove if styles define it
310
- item.classList.add("unaffordable");
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