Understanding Abstract Class
Now that we are working with our enemy game objects and since we are going to be using several different types of enemies (ie. Moss Giants | Giant Spider | Skeletons) rewriting the scripts several times over is not the most optimal method. Therefore we can use something called an “Abstract Class”.
Understanding Classes Inheritance:
To understand what an abstract class is, first we need to break down what the enemy requires and what each individual enemy has in common. We know that each enemy is going to have an “Health, movement Speed, Gem drops” so instead of defining each variable for each different enemy. We can placed these base information as an “Abstract Class” (ie. basic player stats in RPG). That way each individual script will contain a value for Health, speed and gems as a base blueprint.
The beauty of abstract classes is that even thought the individual enemies that have inherit the base stats, each enemy is allowed to have their values unique them them. (ie. the Moss Giant can have an Health value of 100 while the Giant Spider can have a value of 50 for Health).
Now that we understand what class inheritance is, it is time to discuss Virtual methods. Virtual Methods are override methods which allow the individual class that inherits the abstract class to have a customize trait. An Example would be all enemies inherit the “Attack” method, but I want the Moss Giant to have more than just the base dmg that is being inherited. I want the Moss Giant to have extra poison damage along with the regular attack. What I would use is an override method which keeps the original inherited stat but also be able get customized.
In the example above, we have an public attack method which does the base damage of -10.
Then in the Moss Giant script we are going to add an “Override” ability to our Attack method. This will not only include the base value of our inherited stat, but also customize it with our newly appointed Poison damage.
During the play session, you will see in the console that the Moss Giant does both the base damage and also the extra Poison damage. This poison damage is only unique to the Moss Giant. Other enemies would not include this bonus poison damage.
With this technique we will be able to formulate a proper blueprint for our enemies which will result in an organized and clean layout.