Enemy Wave Spawner
A wave spawner sends enemies in rounds. A break counts down, the enemies of the wave arrive one by one, and the next break starts once the player has cleared them. Each wave is bigger, only so many may be alive at once, and a wave that drags on cannot stop the game.

Paste this to your AI
Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.
Add "Enemy Wave Spawner" to my game.
What it should do: Waves give a fight a rhythm of pressure and relief. The player gets time to breathe, spend and prepare, then faces a harder round, and the game can grow in difficulty without anyone placing enemies by hand.
Build it exactly as this state machine. First check that it fits my game, then build it like this:
```mermaid
stateDiagram-v2
[*] --> resting
resting: Break
spawning: Enemies arriving
fighting: Wave under way
won: All waves cleared
resting --> spawning: countdown is over
spawning --> fighting: last enemy is in
fighting --> resting: cleared or too slow
fighting --> won: last wave cleared
won --> [*]
```
Use these starting values:
- totalWaves: 5 waves
- firstWaveSize: 4 enemies
- sizeGrowth: 2 enemies
- restTime: 8 s
- spawnInterval: 0.6 s
- maxAlive: 12 enemies
- fightTimeout: 90 s
Watch out for these common mistakes:
- One enemy holds up the whole game: Tell your AI: give each wave a time limit. When it runs out, start the next break anyway, and keep counting the leftover enemy. For the last wave, make the game hunt the leftovers down or show where they are.
- The whole wave arrives in one moment: Tell your AI: let enemies arrive one at a time with a set time between them, and also cap how many may be alive at once.
- The pace depends on the frame rate: Tell your AI: when the timer runs out, keep the time that ran past and add it to the next wait. If a long tick covers two arrivals, create both.
- The next enemy comes in a rush when room opens: Tell your AI: while the limit holds the wave back, do not let the timer run into the past. Bring in one enemy as soon as there is room, then wait the normal time.
- The wave ends while enemies are still arriving: Tell your AI: a wave is only cleared when every enemy has arrived and none is alive.
- Leftovers add to the next wave: Tell your AI: keep one count of all living enemies and check the limit against it, whichever wave they belong to.
- More deaths reported than enemies: Tell your AI: never remove more enemies from the count than are alive.
Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP
What it does
Waves give a fight a rhythm of pressure and relief. The player gets time to breathe, spend and prepare, then faces a harder round, and the game can grow in difficulty without anyone placing enemies by hand.
How it runs, as a diagram
Everything it can be in, and what moves it on. Your AI builds from exactly this.
How it runs, as a diagram
- Break → Enemies arriving: countdown is over
- Enemies arriving → Wave under way: last enemy is in
- Wave under way → Break: cleared or too slow
- Wave under way → All waves cleared: last wave cleared
Good starting values
| Value | Start with | What it changes |
|---|---|---|
| totalWaves | 5 waves | How many waves there are. The game is won when the last one is cleared. Five is a short session; endless play needs a different ending. |
| firstWaveSize | 4 enemies | How many enemies the first wave has. Small enough that a new player can win it without knowing the game yet. |
| sizeGrowth | 2 enemies | How many enemies each wave adds to the one before. Zero makes every wave the same size, which suits games where the enemies themselves get tougher. |
| restTime | 8 s | The break before each wave. Long enough to collect, heal and rebuild, short enough that the player does not get bored. |
| spawnInterval | 0.6 s | The time between two enemies arriving. It is never zero, because a whole wave in one moment is a wall and not a trickle. |
| maxAlive | 12 enemies | The most enemies alive at once. It keeps the screen readable and the game fast. It is never zero, because with no room no wave could ever start. |
| fightTimeout | 90 s | After this long a wave stops holding up the next one. An enemy stuck behind a wall or hiding would otherwise stop the game for ever. The last wave has no limit: it must really be cleared. |
What usually goes wrong
One enemy holds up the whole game
An enemy gets stuck behind a wall, falls out of reach or hides. The wave never counts as cleared, the break never starts and the player waits for nothing.
Fix: Tell your AI: give each wave a time limit. When it runs out, start the next break anyway, and keep counting the leftover enemy. For the last wave, make the game hunt the leftovers down or show where they are.
The whole wave arrives in one moment
The game creates every enemy of the wave on the same tick. The screen floods, the game stutters and the player cannot react.
Fix: Tell your AI: let enemies arrive one at a time with a set time between them, and also cap how many may be alive at once.
The pace depends on the frame rate
The game creates one enemy per tick when the timer runs out and throws away the time that ran past. On a slow device the waves arrive slower, on a fast one faster.
Fix: Tell your AI: when the timer runs out, keep the time that ran past and add it to the next wait. If a long tick covers two arrivals, create both.
The next enemy comes in a rush when room opens
The wave is full to the limit and its timer keeps running. When the player kills three enemies at once, all three waiting enemies arrive on the same tick.
Fix: Tell your AI: while the limit holds the wave back, do not let the timer run into the past. Bring in one enemy as soon as there is room, then wait the normal time.
The wave ends while enemies are still arriving
The player kills the first enemies faster than new ones arrive. The count of living enemies hits zero and the game thinks the wave is cleared, although half of it has not come in yet.
Fix: Tell your AI: a wave is only cleared when every enemy has arrived and none is alive.
Leftovers add to the next wave
An enemy left over from a timed-out wave is still alive when the next wave starts. If the game does not count it, the limit is passed and the screen holds more enemies than intended.
Fix: Tell your AI: keep one count of all living enemies and check the limit against it, whichever wave they belong to.
More deaths reported than enemies
One explosion reports the same enemy dying twice. The count of living enemies goes below zero, the wave looks cleared too early, and later checks go wrong.
Fix: Tell your AI: never remove more enemies from the count than are alive.
How to check it works
- A new game with a break of 8 seconds. 8 seconds pass: wave 1 begins, and no enemy has arrived yet.
- 4 enemies in the first wave, and 2 more in each after. you look at waves 1 to 5: they have 4, 6, 8, 10 and 12 enemies, and wave 1 sends exactly 4.
- A wave of 4 with 0.6 seconds between enemies. it arrives: the enemies arrive at 0, 0.6, 1.2 and 1.8 seconds.
- A big wave and four different frame rates. 6 seconds pass at each: 10 or 11 enemies have arrived at each rate, and the counts differ by at most 1.
- A limit of 3 alive and a wave of 6. 30 seconds pass with no kills, then 2 enemies die: 3 stayed alive and 3 waited, and after the deaths only one comes in at once.
- Wave 2 is under way with 3 enemies alive. all 3 die: an 8 second break starts, and wave 3 begins after it.
- Wave 1 is under way and one enemy never dies. 90 seconds pass: the break starts anyway, wave 2 begins, and the leftover enemy still counts as alive.
- The last wave has been under way for far longer than the time limit with 2 enemies alive. time passes, and then both die: the game is not won until the second one dies.
- A wave of 4 where only 1 has arrived. that 1 dies: the wave is not cleared, and it goes on until all 4 have come in.
- 2 enemies alive. the game reports 10 deaths: 0 are alive and 2 kills are counted.
- The game is won. time passes and the game reports more deaths: it stays won, and no more enemies come.
- A break time of 0. a wave is cleared: the break is shown for one tick, then the next wave begins.
Works well with
- Enemy Patrol and Chase: Arriving enemies can start already chasing, or walk in along a patrol. Decide which, so a wave does not feel like it appears from nowhere. Give each arriving enemy its behaviour as it is created.
- Loot Table: A big wave multiplies the drops. Set the odds with the wave size in mind. Roll the drops when an enemy dies, not when it arrives.
- Dynamic Difficulty: A wave that shrinks or grows in front of the player feels unfair. The break is the place to adjust. Change the size of the next wave, never the wave that has already begun.
- Boss Phases: Count the boss as one living enemy, and decide if the wave is cleared when only its helpers are gone. Make the last wave the boss, or send a boss as one enemy of a wave.
Common questions
how do i add enemy waves to my game
Paste this: Add enemy waves. Start with a break of 8 seconds. Then send 4 enemies one by one, 0.6 seconds apart. When the player has killed all of them, start the next break, and make each wave 2 enemies bigger. No more than 12 may be alive at once. After the 5th wave is cleared, the player wins.
how fast should waves grow
Add a few enemies each wave and stop at a limit the screen can hold. Growth that is too steep makes wave 4 impossible and wave 3 boring. As a start, add about half of the first wave's size each time.
why does my wave never end
One enemy is stuck, hiding or out of reach, so the count never hits zero. Give every wave a time limit, and for the last one show the player where the leftover enemies are.
should the player be able to call the next wave early
It is a good extra: skipping the rest of the break can give a small bonus. Only allow it between waves, never while enemies are still arriving.
Where it comes from
We could not find a reliable origin for this one yet.
What was checked
- 15 tests pass on the reference code.
- The state diagram was checked against the code, at the default values and at every listed low and high value.
Your AI gets the code
Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it