Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating ability objects in Unity

Tags:

c#

unity3d

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 .. ?

like image 906
peterHasemann Avatar asked Sep 27 '17 08:09

peterHasemann


People also ask

What are scriptable objects?

What are ScriptableObjects? ScriptableObject is a serializable Unity class that allows you to store large quantities of shared data independent from script instances. Using ScriptableObjects makes it easier to manage changes and debugging.


1 Answers

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;
}
like image 153
Esper Avatar answered Oct 13 '22 03:10

Esper