Energy Ammo Component
First, this thing probably needs to have most of its functionality abstracted into a base class called Ammo. There’s a lot going on here that any kind of ammo could use. Of note are the exposures of several private fields via public getter properties. This was necessary because the Weapon is being dynamically created, so we are going to need to make some runtime hookups of these events to the Ammo HUD. More on this later.
The concept here is that the laser rifle burns energy while it is in use but replenishes its energy over time when it’s not in use. To accomplish this I use two coroutines: Replenish Ammo Over Time and Deplete Ammo Over Time. I use a boolean flag called Using Ammo to ensure that only one of the two coroutines can run at any time.
Laser Rifle Variant Prefab
The Laser Rifle Variant Prefab has three components at this time: Basic Attack, Energy Ammo, and Laser Beam. Energy Ammo is new and there is some refactoring of Basic Attack to accommodate the new functionality provided by the Energy Ammo component.
The Basic Attack component still has the two events: Attack Started and Attack Ended. The big change is that the Attack Started event no longer turns the beam on, instead leaving that to the Energy Ammo component. Attack Ended still turns the beam off though.
The Energy Ammo component has four events but only two are hooked up in the inspector at this time. We want to turn the Laser Beam off if we run out of ammo and we need to turn it on when we consume ammo.
Nothing has changed here but I’m including it out of a sense of completeness.
Ammo HUD Component
The Ammo HUD component right now is simply a text field. The Ammo HUD script takes a reference to the Text Mesh Pro object which is on the same component. I don’t usually like to do this, preferring to look up attached components in the Start method rather than having them exposed in the inspector, but I think the text field may end up on a different object in the future.
The Ammo HUD script updates the text field according to the current ammo passed in. There is also a max ammo field which can be used if we are dealing with an energy bar and we need to display ammo as a percentage.
Linking the Ammo HUD and Energy Ammo Components
Since the Weapon is dynamically created and Prefabs don’t save Serialized fields that are exist outside the prefab, I needed a way to link up the Ammo HUD with the Laser Rifle when it was created. To do this I exposed the Ammo Consumed and Ammo Replenished events as public properties of the Energy Ammo component. Then I was able to write Lambda functions inside the Equip script that weakly couples the Ammo HUD to the Energy Ammo at runtime (though at the expense of a strong coupling of the Ammo HUD to the Equip object).
If I wanted to avoid this coupling then I could define a custom Unity Event that passes the Energy Ammo component of the created weapon as a parameter to the event. This would need to be Serialized and then the Ammo HUD could be hooked up inside the inspector. I imagine this change will be effected when I add additional weapons down the line.