Implementing enemy wave system — Part 1

Samarth Dhroov
4 min readJun 7, 2021

--

The exercise here demonstrates a solution that took a while to come to its full completion. The objective is to introduce a wave system that depicts game levels with each increment.

Please make sure that the previous articles written by myself contain the foundation building which is majorly used here to achieve the task at hand.

The solution is in two parts. The first one contains the journey to come to this solution and the second one contains the implementation.

Let us get to the problem first.

The game currently spawns infinite enemies until the player dies. An ideal game seeks a controlled environment where each and every game object exhibits regulated behavior. So, the infinite run needs to be brought down to a set of runs, and each set must be having a pre-assigned set of properties.

When I say, pre-assigned set of properties, I am talking about game stats (data). The data must not be hardcoded. On the other hand, If we use code to keep swapping data, too many data swaps introduce a number of loop statements that make the code less readable and far less manageable.

Do not look through the OOPS lens because the implementation does not use any of its’ principles. I’ll post an OOP based solution in the future :)

So at the most abstract level, one could think that we need something similar to a boarding pass. A pass contains a set of details that allows both parties, the traveler and the issuer a way to come to a contract. the passenger knows that the given ticket will take him/her to a pre-determined destination and the issuer can confirm all the necessary details beforehand are fulfilled so that when the traveler comes for departures, the authorities can run a smooth check.

I wanted to implement a boarding pass mechanism that remains an external entity and one has to bring it to the system to use it until the task is over.

While I saw multiple options that can create such a tool, I could not find something that remains detached from the game until assigned manually. Upon spending a few days of research, I finally found a resembling tool named “Scriptable Object”.

A scriptable object does not come out of a mono behavior class hence the transform component gets away immediately. It does not remain in the game scene. Rather, it works as an asset. But instead of any audio-visual asset, it’s a data asset.

Please take a look at the talk here to gain a deeper understanding of this tool.

I’d also recommend a thorough read of this crystal clear article.

How to Use Scriptable Objects in Unity — Simple Talk

So, the main tool that is going to help the task accomplishment is in hands.

Let us see through the widest pseudo code to see the journey ahead.

If you closely observe above, there are only a few things changing.

  • Number of Enemies
  • Type of Enemies
  • Speed of Enemies
  • Level Number

The core idea is to extract these DATA out of the program and put in different boarding passes. The game must have a scanner coded that checks the details on each pass and performs relative to the details written on it.

For instance, When you are standing at departure gates and if your boarding pass says from Berlin to NYC, the only journey you can get is from Berlin to NYC with written allowances of luggage and food. Nothing less or more :)

On the other side, once you are onboarded, the airport authorities will continue addressing with thousands of other boarding passes.

Below is a list of needed items for the implementation stage further. I have them in place and if you are following up so far, you’d need them working fine.

  • There are two types of enemy with their separate instantiation, movement, destruction code.
  • There is a player with its’ separate instantiation, movement, and destruction code.
  • There is a spawn manager object with a script that will remain the pilot of the game level runs.
  • There is a UI Manager that runs the UI text displays.
  • Lastly, there is a game manager who stops the game if the player wins and loops back to the main menu page.

Let us see through a more elaborate pseudo-code that will drive us further.

That seems enough for this article. The next one will have implementation. Stay tuned :)

Thank you very much

--

--