选择角色时我目前有基类
abstract class CharacterClass
{
public abstract void AbilityOne();
public abstract void AbilityTwo();
}
我的角色来自这个班级
class Warrior : CharacterClass
{
public override void AbilityOne()
{
// implement ability one here
}
public override void AbilityTwo()
{
// implement ability two here
}
}
最后,我使用此代码选择战士
CharacterClass selectedClass = new Warrior(); // start the game as a Warrior
所以这种方式非常好。但是当谈到冷却等时,我希望保持干净的代码,所以我考虑创建一个Ability
类。
我的抽象父类
abstract class CharacterClass
{
public Ability AbilityOne { get; set; }
public Ability AbilityTwo { get; set; }
}
战士班
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
}
}
我的技能课
class Ability
{
public Ability(int cooldown, [the ability method here])
{
CoolDown = cooldown;
TheAbility = ? the ability parameter ?
}
public int CoolDown { get; set; }
public void TheAbility(){}
}
所以战士将传递他的两种技能并创造两个技能对象。在游戏中我可以写
CharacterClass selectedClass = new Warrior();
selectedClass.AbilityOne();
这将导致粉碎,冷却时间为3秒。这有可能实现......某种程度上......?
答案 0 :(得分:3)
由于AbilityOne
返回Ability
个对象,因此在Ability
类中创建处理能力可能具有的不同逻辑的基础结构是有意义的,例如:在某种Update()
方法中使用能力等在冷却计时器上运行时钟
所以调用你的能力的代码看起来像是:
selectedClass.AbilityOne.Trigger();
将能力方法存储为能力等级中的System.Action
也是有意义的。
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;
}
答案 1 :(得分:2)
你应该可以这样做:
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
}
}