File size: 5,107 Bytes
b29710c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import {
  INITIAL_MONEY,
  INITIAL_LIVES,
  getWaveParams,
} from "../config/gameConfig.js";

/**
 * Minimal event emitter for local use.
 * Backward-compatible and small-footprint; no external deps.
 */
class SimpleEventEmitter {
  constructor() {
    this._events = new Map();
  }

  on(type, handler) {
    if (!this._events.has(type)) this._events.set(type, new Set());
    this._events.get(type).add(handler);
  }

  off(type, handler) {
    const set = this._events.get(type);
    if (!set) return;
    set.delete(handler);
    if (set.size === 0) this._events.delete(type);
  }

  emit(type, ...args) {
    const set = this._events.get(type);
    if (!set) return;
    // Copy to array to avoid mutation issues during emit
    [...set].forEach((h) => {
      try {
        h(...args);
      } catch {
        // swallow to avoid breaking game loop
      }
    });
  }
}

export class GameState {
  constructor() {
    // Internal event bus
    this._events = new SimpleEventEmitter();
    this.reset();
  }

  /**
   * Subscribe to GameState events.
   * Usage: gameState.on('moneyChanged', (newMoney, prevMoney) => {})
   */
  on(type, handler) {
    this._events.on(type, handler);
  }

  /**
   * Unsubscribe from GameState events.
   */
  off(type, handler) {
    this._events.off(type, handler);
  }

  /**
   * Convenience subscription helpers for moneyChanged event.
   */
  subscribeMoneyChanged(handler) {
    this.on("moneyChanged", handler);
  }

  unsubscribeMoneyChanged(handler) {
    this.off("moneyChanged", handler);
  }

  reset() {
    this.money = INITIAL_MONEY;
    this.lives = INITIAL_LIVES;
    this.waveIndex = 0; // 0-based; wave number = waveIndex + 1
    this.gameOver = false;
    this.gameWon = false; // no longer used for wave completion, kept for compatibility
    this.totalWaves = Infinity; // for compatibility with any UI that reads it

    // Wave spawning state (accumulator-based; in seconds)
    this.lastSpawnTime = 0; // kept for compatibility but unused by new accumulator
    this.spawnAccum = 0;
    this.spawnedThisWave = 0;
    this.waveActive = false;

    // Gameplay speed (1x or 2x)
    this.gameSpeed = 1;

    // Entity arrays
    this.enemies = [];
    this.towers = [];
    this.projectiles = [];

    // Selection state
    this.selectedTower = null;
  }

  setGameSpeed(speed) {
    const s = speed === 2 ? 2 : 1;
    this.gameSpeed = s;
  }

  getGameSpeed() {
    return this.gameSpeed;
  }

  startWave() {
    if (this.gameOver) return false;
    this.waveActive = true;
    // reset accumulator timing
    this.lastSpawnTime = performance.now() / 1000;
    this.spawnAccum = 0;
    this.spawnedThisWave = 0;
    return true;
  }

  getCurrentWave() {
    // wave number is 1-based
    const waveNum = this.waveIndex + 1;
    return getWaveParams(waveNum);
  }

  nextWave() {
    this.waveIndex++;
    // prepare next wave accumulator and state
    this.spawnAccum = 0;
    this.spawnedThisWave = 0;
    this.waveActive = false;
    // never set gameWon due to infinite waves
  }

  takeDamage(amount = 1) {
    this.lives -= amount;
    if (this.lives <= 0) {
      this.gameOver = true;
    }
  }

  /**
   * Increment money and emit moneyChanged if value changed.
   */
  addMoney(amount) {
    if (!amount) return;
    const prev = this.money;
    this.money += amount;
    if (this.money !== prev) {
      this._events.emit("moneyChanged", this.money, prev);
    }
  }

  /**
   * Spend money if enough funds; emits moneyChanged on success.
   * Returns true if spent, false otherwise.
   */
  spendMoney(amount) {
    if (this.money >= amount) {
      const prev = this.money;
      this.money -= amount;
      if (this.money !== prev) {
        this._events.emit("moneyChanged", this.money, prev);
      }
      return true;
    }
    return false;
  }

  /**
   * Set absolute money value; emits moneyChanged if changed.
   */
  setMoney(amount) {
    const prev = this.money;
    this.money = amount;
    if (this.money !== prev) {
      this._events.emit("moneyChanged", this.money, prev);
    }
  }

  /**
   * Returns whether there is enough money for amount.
   */
  canAfford(amount) {
    return this.money >= amount;
  }

  // TODO(deprecation): Avoid direct assignments to `money` outside GameState.
  // Migrate any external direct writes to use addMoney/spendMoney/setMoney.

  addEnemy(enemy) {
    this.enemies.push(enemy);
  }

  removeEnemy(enemy) {
    const index = this.enemies.indexOf(enemy);
    if (index > -1) {
      this.enemies.splice(index, 1);
    }
  }

  addTower(tower) {
    this.towers.push(tower);
  }

  removeTower(tower) {
    const index = this.towers.indexOf(tower);
    if (index > -1) {
      this.towers.splice(index, 1);
    }
  }

  addProjectile(projectile) {
    this.projectiles.push(projectile);
  }

  removeProjectile(projectile) {
    const index = this.projectiles.indexOf(projectile);
    if (index > -1) {
      this.projectiles.splice(index, 1);
    }
  }

  setSelectedTower(tower) {
    this.selectedTower = tower;
  }

  isGameActive() {
    return !this.gameOver;
  }
}