Galaxy Shooter 2D — Modularity #9
Adding two new powerups: Speed Boost and Shield!
So far, I have only implemented the Triple Shot powerup. It works really well, but it’s not enough to make the game enjoyable. That’s why, today, I will talk about how I have programmed a modular powerup system and the two new powerups for my game:
Speed Boost and Shield!
Time to refactor
One of the ways to modularize the use of powerups has been to enable in the same script to choose what type of powerup it is. That is, the Powerup script will be a container that contains all the references to the powerups and triggers those abilities that are programmed inside the player’s ship script.
This approach is fine if all the powerups behave the same way (movement, interaction… have no difference between powerups). However, if I wanted to truly modularize those powerups, I would’ve made a base class where all the powerups inherit that class and all the Trigger calls would be made through that base. For now, it will stay as it is.
Also, see that SetPowerupType(Type newType) method? It has a line that access and sets an integer in the animator. Because every powerup has it’s own sprites, I have to set the proper animation in the Animator, and I do that like this:
Whenever my SpawnManager spawns a powerup, it will randomly select a powerup within the Type enum Length. That way, if I ever add new powerups, I don’t have to change any line of code to suit any change.
Implementing the two new Powerups
These two powerups are pretty much self-explanatory, so, I will briefly comment on how I implemented them in the ship’s script.
With the use of a ternary operator, depending on the state of the Speed Boost powerup, I can dynamically set the speed of the ship.
Whenever the shield is active, if an enemy or a laser has collided with the player, the shield will take that damage, preventing the ship from being damaged.
The next two articles will be about the Unity UI, and how you can create your own HUD and menus with it!