Tweaking - Adding refilling PowerUps
Out of ammo or low on lives? Get one of these!
In this post, we will go over some improvements and tweaks for the Space Shooter game.
I. Adding a “Ammo+” PowerUp
Just recently, we limited the amount of available ammo for the player. However, there is no ability to refill the ammo. Therefore, the game would be limited to 15 shots and would be a plain survive-and-dodge afterwards… Until now. The player now has the ability to refill the ammo with the help of a new powerup.
As there was no pre-built ready-to-use powerup sprite for this one, I just created one by using the provided assets and playing around with them.
The logic implementation is done pretty easy and quick:
- We need to add a function to reassign the current ammo variable. However, we want to make this method modular as there could be another powerup (or situation) that will add less or more ammo.
- If the reassigned ammo value exceeds the maximum allowed ammo value, we should reset it to avoid any issues or bugs.
The first function is done with just two lines of code:
As we call the method, we decide how much ammo will be added in this call. Then, we simply adding the amount we chose to the current available ammo counter.
To prevent the current ammo exceeding the maximum allowed ammo, we need another small method:
And finally, we simply need to call the AddAmmo() method inside the PowerUp.cs script. Here is the fully working implementation inside the game:
II. Adding a “HP+” PowerUp
The implementation of this one is pretty much the same as from the Ammo+ powerup:
- If the player has less than three lives remaining, add one life
- Update the live visualizer shown on screen to represent the current lives
- Make sure to also update the damage visualizer
The first method will be created inside the Player.cs script. The latter two methods inside the PowerUp.cs script inside the powerup ID switch statement.
Here’s what’s happening when the PowerUp gets collected:
- The player will get another life
- The UI element representing the current lives gets updated by asking the Player.cs script how many lives the player has left. The returned number will be the sprite loaded
- The damage visualizer on the player will be updated to fit the current available lives
Here is the working end result: