Creating ability objects in Unity

when selecting a character, I currently have a base class

abstract class CharacterClass { public abstract void AbilityOne(); public abstract void AbilityTwo(); } 

And my characters come from this class

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

Finally, I use this code to select Warrior

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

Thus, this method works very well. But when it comes to cooldowns, etc., I want to stay with clean code, so I thought about creating an Ability class.

My abstract parent class

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

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(){} } 

Thus, the warrior will transfer his two skills and create two objects of ability. In the game I could write

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

and this will lead to a break with the cooldown in 3 seconds. Is it possible to implement .. somehow ..?

+5
source share
2 answers

You should 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 } } 
+2
source

Since AbilityOne returns an Ability object, it makes sense to create an infrastructure in the Ability class that handles various logic that might have the ability, for example. starting the clock by the cooldown timer in some Update() method using the ability, etc.

So, the code to call your abilities will look something like this:

 selectedClass.AbilityOne.Trigger(); 

It would also be advisable to save the skill method as System.Action in your class of capabilities.

 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; } 
+3
source

Source: https://habr.com/ru/post/1272149/


All Articles