Devlog 28: Added A Modular System To Spawn and Respawn Enemies In “Formation”
As a kid playing Gradius and R-type, I was always fascinated how the enemies would always come in as a formation. As I moved from the Nes to games like Halo. I’ve noticed that Halo 1 , 2 , shared the same recipe. The same recipe that was implemented again in halo infinite.
They both communicated to the player that the enemy has built up some kind of intelligible defense or order.
You the player will bring chaos to order.
So today, I wanted to replicate that design to my game by implementing a way for the enemies to spawn as a convoy or formation.
For now, they will spawn in a line and shoot at the players general direction. Once they have gone out of the camera, they’ll reappear opposite of their coordinates.
A list was created so I can be able to spawn any enemy prefabs and set its unique values upon instantiation. This list is made in a way to spawn specific enemies and have them move in a specific formation.
max number to spawn: max number of enemies that can spawn in this formation
current number: by set to the max number to spawn and will decrement to zero when spawning
number to respawn: increments when the enemy goes out of bounds
common enemy: the prefab that will be instantiated in the spawn coroutine
Elite Enemy: Another prefab to possibly spawn in this formation
spawn position: The initial spawn position
respawn position: multiplies into spawn position to change direction
This is the initial function that will be called first. It checks if there's a prefab to spawn and then determines how many enemies that can spawn
it'll then call the spawn coroutine which will be passed a integer variable that reflects the index of the _formationList. This insures that we know what specific list part of the list we are using.
This coroutine will follow up. It'll instantiate the common enemy and set its values from the list to the enemy script itself.
At the moment, there's no randomizer for the formation selection. A Wave Spawning system will be made and will be another article.
that being said, the enemy is in the game now.
Enemies reaching out of bounds ( Respawning )
ReSpawner will be called from the enemy script when it is less than -35y.
The concept of respawning is that the game object gets destroyed and we keep score of how many went out of bounds via integer variable.
we keep track of how many enemies are still on screen and off screen via EnemiesInPlay and NumberToRespawn.
Once all enemies are out of play two conditional statements will be checked. A boolean named Type which dictates whether the enemy moves vertically across screen or horizontally.
if true then vertical, false then horizontal
we set the current number to spawn to the tally of enemies that went off screen ( number to respawn ).
we check if the X position is positive or negative using Mathf.Sign. Then flip the values to their opposite by adding or subtracting the x position by twice its value.
The initial spawn position is 30x so to reach the opposite side I would need -60.
The same is done for things that move horizontally. The only difference is that the Y value is modified.
Respawn uses the same Spawn coroutine
so this bit in the tail end prevents the ships from continuously streaming to one side to the next. I wanted them to spawn until all ships are out of bounds.