This site requires JavaScript, please enable it in your browser!
Greenfoot back
Deepfist
Deepfist wrote ...

2023/5/12

Enemy wave system

Deepfist Deepfist

2023/5/12

#
I am currently making a game where, once you kill every enemy on screen, enemies spawn by waves. I don't know how to make such a system though. I have 4 types of enemies and they all extend to the class Enemies. Once all enemies are killed a new wave will spawn (with a delay in-between, but I can probably figure that out myself) and this wave will have a few more enemies then previously. Once a certain wave is reached, let's say 4 in this case, a new enemy will be introduced into the fray, once this happens there will be a randomizer so that the game spices up a bit. I'd appreciate any help, I have no code for any of this yet as I have no clue how to implement something like this.
danpost danpost

2023/5/12

#
Deepfist wrote...
I am currently making a game where, once you kill every enemy on screen, enemies spawn by waves. I don't know how to make such a system though. I have 4 types of enemies and they all extend to the class Enemies. Once all enemies are killed a new wave will spawn (with a delay in-between, but I can probably figure that out myself) and this wave will have a few more enemies then previously. Once a certain wave is reached, let's say 4 in this case, a new enemy will be introduced into the fray, once this happens there will be a randomizer so that the game spices up a bit. I'd appreciate any help, I have no code for any of this yet as I have no clue how to implement something like this.
Sounds like you need to start with an int wave counter:
public int waveNum;
It can be used to set the number of enemies to spawn for that wave:
private int enemiesToSpawn;
with, for example (when beginning or changing waves):
enemiesToSpawn = 10+3*waveNum;
enemiesToSpawn should be decreaed when spawning an enemy and it should be used to determine spawning at all:
if (enemiesToSpawn > 0 && Greenfoot.getRandomNumber(100) == 0) spawnAnEnemy();
For adding extras to the fray, you could use, for example:
if (waveNum > 3 && Greenfoot.getRandomNumber(10000/waveNum) == 0) spawnAnExtra();
By dividing by waveNum, there will be an increased chance of spawning an extra for each successive wave. For the delay between waves, you will need an int timer:
private int delayTimer;
In act of world, as first lines in act, use:
if (delayTimer > 0) {
    if (--delayTimer > 0) return;
    else spawnAnEnemy();
}
to run the delay. Setting the delay can be done immediately after this:
if (getObjects(Enemies.class).isEmpty()) {
    waveNum++;
    enemiesToSpawn = 10+3*waveNum;
    delayTimer = 90;
}
Hope this helped.
Deepfist Deepfist

2023/5/17

#
The code is amazing, my DelayTimer is a bit different however. Nonetheless it's still wonderful and it has helped a lot (was stuck on this for a while). Thx a lot :D
You need to login to post a reply.