peterHasemann
peterHasemann

Reputation: 1590

creating ability objects in Unity

when choosing a character I currently have a base class

abstract class CharacterClass
    {
        public abstract void AbilityOne();

        public abstract void AbilityTwo();
    }

And my characters derive from this class

class Warrior : CharacterClass
{
    public override void AbilityOne()
    {
        // implement ability one here
    }

    public override void AbilityTwo()
    {
        // implement ability two here
    }
}

Lastly I use this code to select the Warrior

CharacterClass selectedClass = new Warrior(); // start the game as a Warrior

So this way works pretty fine. But when it comes to cooldowns etc. I want to stay with a clean code so I thought about creating a Ability class.

My abstract parent class

abstract class CharacterClass
{
    public Ability AbilityOne { get; set; }

    public Ability AbilityTwo { get; set; }
}

The Warrior class

class Warrior : CharacterClass
{
    public Warrior()
    {
        // Set the new Abilities
        AbilityOne = new Ability(3, Smash()); // pass in the method as a parameter ??
        AbilityTwo = new Ability(7, Shieldblock());
    }

            private void Smash()
    {
        // Ability 1
    }

    private void ShieldBlock()
    {
        // Ability 2
    }
}

And my skill class

class Ability
    {
        public Ability(int cooldown, [the ability method here])
        {
            CoolDown = cooldown;
            TheAbility = ? the ability parameter ?
        }

        public int CoolDown { get; set; }

        public void TheAbility(){}
    }

So the warrior will pass in his two skills and creates two ability objects. In the game I could write

CharacterClass selectedClass = new Warrior();
selectedClass.AbilityOne();

and this would result into a smash with a cooldown of 3 seconds. Is this possible to implement .. somehow .. ?

Upvotes: 4

Views: 978

Answers (2)

Fredrik
Fredrik

Reputation: 5108

You should be able to do something like this:

class Ability
{
    public Ability(int cooldown, Action ab)
    {
        CoolDown = cooldown;
        TheAbility = ab;
    }

    public int CoolDown { get; set; }

    public Action TheAbility;
}


class Warrior : CharacterClass
{
    public Warrior()
    {
        // Set the new Abilities
        AbilityOne = new Ability(3, Smash);
        AbilityTwo = new Ability(7, ShieldBlock);

        AbilityOne.TheAbility(); // Will run Smash() method
    }

    private void Smash()
    {
        // Ability 1
    }

    private void ShieldBlock()
    {
        // Ability 2
    }
}

Upvotes: 2

Epnk
Epnk

Reputation: 93

Since AbilityOne returns an Ability object it would make sense to create the infrastructure within the Ability class that handles different logic that an ability might have, e.g. running the clock on the cooldown timer in some Update() method, using the ability, etc.

So the code for calling your ability would look something like:

selectedClass.AbilityOne.Trigger();

It would also make sense to store the ability method as a System.Action within your ability class.

class Ability
{
    public Ability(int cooldown, System.Action _abilityMethod)
    {
        CoolDown = cooldown;
        abilityMethod = _abilityMethod;
    }

    public int CoolDown { get; set; }

    public void Trigger()
    {
        if( abilityMethod != null )
        {
            abilityMethod();
        }
    }

    private System.Action abilityMethod;
}

Upvotes: 3

Related Questions