Health Restoration and Ammo Count
Health and ammo may be a strange combination to talk about, but they are very similar to set up.
To collection of more health and ammo is the same functionality as the other powerups in this space shooter game and so we are going to add them to the switch statement (check out my article on switch statements).
For the health case, we call the heal method which checks if the current number of lives is less than the max life count, three in this case.
If it is, we add one to the life count and then raise an event that lets the UIManager know the current life count. The UIManager updates the display of how many lives you have.
Then the Heal() method checks how many lives you have and adjusts the visible damage to match that. The right engine will show damage if you are less than full health and the left engine will show damage if you have one life left. If you receive more health and your life count goes up to two then the left engine will no longer show damage. Same with the right engine if you go back to full health.
And the ammo is even easier to set up.
The ammo method just resets the ammo count to max ammo, which is 15 for this game, and then lets the UIManager know.
Same as the health, the UIManager has a reference to the display component and changes that based on the value it receives. It has fixed values of “Ammo: “ and “ / 15” and it updates the currant ammo by changing the integer value if receives to a string to be displayed.
And whenever the player fires, the ammo count is decreased by one and that new value is passed to the UIManager to display the current ammo. As long as the ammo count is not zero, the player can fire and the ammo count will be updated.
And that handles the addition of restoring health and ammo to the player in this space shooter.