Ammo Count, Ammo Collectable, Life Collectable
Ammo Count
I going to Limit the number of laser fired by the player to be only 15 shots, providing feed back through on-screen ammo count.
I added a Stat Variable for the Ammo Count. In the Start Method I Reset the Ammo Count so that the Player has Ammo. In the Should Fire Laser Method I add a check if the player has ammo. In the Fire Laser I removed ammo before Fireing
Creating the UI
I duplicated the Shield Slider. Renamed it to Ammo Count. I changed the slider to use the ammo count instead of the shield Health.
I moved the Ammo Count Slider Down Below the Shields
I changed the Text display color to an orange color and the Fill Color to an orange color.
Now the Game Displays the Amount of ammo the Player has.
Ammo Collectable
Just like the rest of the power ups I put this in the Player Behavior.
I duplicated the shield power up code and renamed it to ammo. When the Ammo Power up is collected I reset the ammo count.
Now every time the Ammo Power Up Collected Event is raised the ammo count now resets back to the 15.
To Create the Collectable For use in Game I added a Square Sprite then Added Several Capsule Sprites to the Square Sprite. I then Animated it using the Unity Animation Tools. I created an Animation Override Controller, to Override the Power Up Controller with the new Animation. I Copied the Move and Damageable Components on the Speed Power Up and Pasted them on the Ammo Collectable. I Added a Box Collider 2D Trigger. I then made the new Ammo Power Up a Prefab.
I duplicated the Shield Spawner, renamed it to Ammo Power Up Spawner, and changed the values to spawn ammo power ups.
I added the Ammo Spawner to the Spawn Manager.
Now the Game Spawns Ammo Power Ups and the Player can continue firing.
Life Collectable
Just like the rest of the power ups I put this in the Player Behavior.
I duplicated the ammo power up code and renamed it to Life. When the Ammo Power up is collected I add 1 to the Player Lives.
To take care of the visuals it is almost the same code as the Received Damage Method.
I have a Disable Left Engine and Disable Right Engine Methods that set the engine to inactive and sets the bool
to be false so the engine is no longer enabled.
In the Life Collected Method I get the current player lives and cast it to an int
. Using a switch statement if the current lives equals 2 (both engines are enabled) I randomly pick an engine fire to disable. If the current lives equals 3 only one of the engine fires is enabled, I disable the one that is currently enabled.
When the player receives damage the Damageable Behavior removes a life and the Stat Variable fires the Player Damage Event. The Player responds to the Player Damage Event and randomly picks one of the engines to Enable the fire. The Player receives Damage again and the other Engine gets the fire Enabled. The Player has 1 life and collects a life collectable, the Life Collected Event happens. The Player responds by Adding a life, the Stat Variable then fires the Player Damage Event which randomly picks an engine to Enable the fire on (Both Engines already have a fire). So I have to make sure that the removing of the fires happens after the life is added and not before.
Now when the Life Collected Event is raised the Game Updates correctly.
Life Collectable Prefab
I created a Square Sprite and changed its color to a blue color, renaming it to Player Life Power Up. Then I added the Player ship Sprite, adjusted the values so that it fit inside the square. I added a Box Collider 2D trigger.
I then copied the Movable and Damageable Component on the from the Ammo Power Up Prefab and Pasted the Components as new on the Player Life Power Up.
I then changed the Event that fires when the Game Object gets destroyed to be the Life Collected Game Event.
I made sure that All the settings are correct and mad it a prefab.
I created a spawner for it by duplicating the Ammo Spawner, renaming and changing the Parameters.
I then added the Spawner to the Spawn Manager.
Now using Ammo and Restoring Lives.