2D Mobile Game: Damage Interface
In this article, I will cover how to create an Interface that will handle the Damageable object in the game and serve bit of an intro to Interfaces.
An Interface is like a contract that will need to be fulfilled by any scripts that use it. This interface will be for any game objects that can be damaged. I will be using it with enemies, but this again could be used with any object that can take damage. For this reason, we want to opt for an interface instead of including in the Enemy Class script because a script can only inherit one Class but can use as many Interfaces they want.
In making a script an interface, we change it from a public class to public interface. The naming conventions for interfaces uses camel casing but the name starts with an uppercase I for interface and typically is something-able.
Thinking about the Damagable Interface, we are going to want to keep track of the health of the object to know when to destroy it and a Damage behavior to implement the health logic among other things. We will create what seems like an int variable, but is a property because we will need to get and set it. If get and set are not declared, we will get an error. Next, we will create a Damage method.
Integrating the Interface
Time to integrate the Damageable Interface with the Enemy scripts. Next to the Enemy Class, the syntax to include an Interface is a comma (,) followed by the name of the Interface. Since Interfaces are like a contract, we will need to meet the obligations of the Damageable contract or we will get errors.
This means we need to get & set the Health int property and create the Damage method.
Enemy Takes Damage
Making use of the Health property, we will set it to the base health of the Enemy Class, which we can assign the amount in the Inspector.
The Health property will track how many hits the Enemy can take and in the Damage method, we will subtract 1 from the current Health amount and check to see if the Health has gone below 1. If this is true, we destroy the game object the script is attached.
The Attack script is attached to the Player’s sword and can detect when the sword has collided with another object possessing a collider. This other collider is stored in the OnTriggerEnter2D Collider2D parameter other. When the Sword collides with the Enemy collider, we will want to cache the Interface Damageable (hit) and grab the Interface script through the other parameter. After a null check to cached the Damageable script in hit, can access the Damage method from the Enemy script we are colliding with.
We have a bit of a bug. The Enemy’s health was set to 3, so it should be able to take 3 hits before being destroyed. Currently, it is taking 2 hits before being destroyed. This is due to the Sword collider entering the Enemy collider more than once in the Swing animation.
We want to isolate the hit.Damage() method and be able to switch it on and off. This requires a bool (_canDamage) and we will set it true to start.
In the OnTriggerEnter, we only want the Damage method to be called when _canDamage true. Once the Damage is called, we will set the bool false so it will not be called again.
We will need a way to reset the bool back to true again and we will use a Coroutine (DamageReset) that will wait some time before setting _canDamage true.
This method gives the animation time to play through while only calling the Damage method once per swing.