Component Based Ability Part I : Base Setup

To start off, what should an ability do? At the most basic level when a player casts an ability in a game like DOTA or Diablo, something happens. What this “something” is varies from ability to ability but, we can say for sure that an ability will execute. What I did for my system is set up an interface which defines what an ability is.

public interface IAbility
 {
    string AbilityName { get; }

    bool CanExecute { get; set; }
    Unit Owner { get; set; }

    void Execute();
}

As seen above my ability will, at the very least, have a name and a function called Execute where all the logic for that ability will go. Here owner is the player which owns the ability (In my case called a Unit). The CanExecute boolean comes in handy to check for situations where the player is unable to cast the ability (e.g. if the player is stunned or the ability is on cool-down etc.)

Using this we can create the actual ability class. This class can then be treated as the base class for any ability we create.

public class Ability : IAbility
 {

    public string AbilityName { get { return name; } protected set { name = value; } }

    public bool CanExecute { get { return canExecute; } set { canExecute = value; } }

    public Unit Owner { get { return owner; } set { owner = value; } }

    public virtual void Execute()
    {
        if (owner == null)
        {
            Debug.Log("Ability : " + abilityName + " has no owner!");
            canExecute = false;
        }
        else
            canExecute = true;
        

        if (!Owner.PlayerController.IsControllable)
        {
            Debug.Log("Can't control player yet!");
            canExecute = false;
        }
        else
            canExecute = true;
    }

    private string abilityName = "default";

    private bool canExecute = true;

    private Unit owner = null;
}

Here I’ve added a few basic checks inside the base Execute function to make sure the ability is not cast without all the necessary information. If anything is missing the canExecute boolean is set to false. Another thing to notice is that my Owner (of type Unit) has a player controller. This is another class I have created where I keep all the control logic for the player. You may or may not have this in your project.

Up until now we don’t have any component based stuff, just an interface and a base class with everything based around inheritance. But this is the extent of inheritance I will be using here. In my next blog post I will discuss how I use components for different parts of my abilities. Moreover, these components will be reusable across different abilities.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s