Feature: Boss Enemy — Part 2
In this article, we will finish the Boss Enemy started in the previous article where we created and adjusted the Boss weapons.
Making the Enemy
The Boss will start off the screen and move to sit top center of the screen and attack the Player.
Boss Enemy
Since the behavior is different from the other Enemies, we won’t inherit from the Enemy class. The BossAI script will require a Rigidbody2D to know when the reactor core child is hit. For the movement variables we need, a float for the speed the Boss moves, a float for the rotation speed the Boss will spin at, a Vector2 for the position the Boss will stop, and a bool to know when the position is reached and to start spinning.
The weapon variables are a float for the duration before switching from Laser Beams to Missiles or vice versa, a float for the fire rate of the Missiles, and another for the time that has to pass before Missiles can fire again. Now we need a GameObject for the Missile Prefab, an array of Transforms for the Missile spawn points, a List of BeamEmitters to turn on the Laser Beams and keep track of how many are left, and a bool to check if the Beams are active.
The health variables are int for the starting health of the reactor core, another int for the current reactor health, a GameObject for the reactor core object, and another GameObject for the Explosion Prefab.
The remaining variables are a bool to know when the Boss enters the final stage, a reference to the Player, and an enum that controls the Enemy state.
In Start, we get the Player reference, check the number of elements in the BeamEmitter List, remove any empty elements, and activate them. Then we set the current reactor health and disable the reactor core GameObject.
This method is called when the List of BeamEmitters beams need to be turned on or off. It also checks the remaining BeamEmitters and removes any empty elements. If all of the Emitters are destroyed, then we set the final stage to true.
This method is called during the FiringMissiles state when the firing cool-down system is finished. It restarts the cool-down, spawns the Missile Prefab at the Missile spawn points or point if there is only one in the array, sets the instantiated Missile as an Enemy Missile gives it the Player’s Transform if the Player exists.
The FinalStage method is called when the Beam Emitters have been destroyed and when the inFinalStage bool is true. The method sets the Enemy State to FiringMissiles and enables the reactor core so that the Player can kill the Boss.
The DamageCore method adjusts the current reactor health by the amount passed in, and if the reactor is out of health, we instantiate a large explosion and destroy the Boss Enemy.
This Coroutine switches the Boss to the different Enemy States after the stage duration as long as the Player is alive and the Boss has not entered the final stage.
In Update, we move the Boss until it is in the top center of the screen and then rotate it at the rotation speed. Then we check what state the Boss is in and activate the Beams if they are not active or fire Missiles and deactivate the Beams if the cool-down is up.
The script also needs an OnTriggerEnter2D to check if the collision with the reactor core is from the Player’s Laser or Omni-Shot and then damage the core.
Next time, in the last of this three-part series, we will make the Boss spawn after the Last Wave.