I set up my player’s ammunition mechanic last time. The next step is to create an Ammunition PoweUp, that the player can collect. I want the player to choose when to fire more carefully. However, I don't want to deprive them of being able to do it all together. Enter a new PowerUp.
Create an Ammunition PowerUp that will reload the player’s current weapon.
How I went about it:
On the surface, this seemed like a simple thing to implement. I thought of creating new art for my powerup and adding some code to my Player and PowerUp scripts.
The first thing I did was add a variable to hold my player’s current ammunition amount. I used this variable in tandem with the fire rate check to see if the player was allowed to shoot.
This method was working well enough until I realized that I have different weapon types in my game.
Why does that matter? I mentioned in an earlier article that I want the player to treat ammunition as a valuable resource. With different weapon types, I implemented different ammo values. Each weapon should have a way of keeping track of ammunition for itself. I also have to keep track of unused ammunition when the player changes weapons. One variable can’t hold all of the information that I need.
Maybe two variables would do it.
I wanted to keep track of the main ammunition separately. The idea was that after the player had exhausted their power-up ammunition, they would still have the leftover main ammunition.
This mechanic created another problem. What if there are more weapon types? The plan is to include different powerups to spice up the game. I would have needed a variable for each ammunition type.
Luckily, the solution was right under my nose. From the start, I had planned to create different weapons for the player. The shooting method already takes advantage of this modular system. All I had to do was include a method for updating the ammunition values.
Each weapon can now store the value of its ammunition independently. I can reference these values and alter them with the
UpdateAmmo() method. This system will be expandable and will support any number of weapons.
This story may very well be a case of not seeing the forest for the trees. I had planned for this eventuality but had somehow forgotten my previous work. In my defense, I have been working long hours. I am not complaining. I love working on games, and that is why I work long hours. Sometimes it’s hard to tear me away from the computer. However, all’s well that ends well.