Прежде всего, ваша реализация Use
, как
void Use()
{
...
}
автоматически делает его private
, поэтому он не может быть вызван из другого класса. Сделай это
public void Use()
{
...
}
Функция всегда называется «Использовать».
Это точный пример использования интерфейса
public interface IAbility
{
// interface method are automatically public
void Use();
}
Теперь каждый класс, реализующий этот интерфейс , должен реализовать метод Use()
, как
[System.Serializable]
public class PartAttack, IAbility
{
public Targeting target_manager;
public int block_attack_damage = 0;
public int block_attack_repeats = 0;
// has to implement this because of IAbility
public override void Use()
{
foreach (Combatant enemy in target_manager.list_of_targetable)
{
int repeats = block_attack_repeats;
while (repeats >= 0)
{
enemy.health = enemy.health - block_attack_damage;
block_attack_repeats -= 1;
}
}
}
}
Если вы уже хотите иметь возможность иметь некоторые предопределенные методы с определенной функциональностью и сделать так, чтобы классы также использовали одни и те же поля, сделайте его абстрактным классом
[System.Serializable]
public abstract class Ability
{
// this can not be changed or accessed not even by any inheritor
private float aValueOnlyThisClassCanSee;
// this can changed/accessed by inheritors but not from the outside
protected string SomeProtectedValue;
// this can accessed/changed by everyone
public Targeting target_manager;
// the same keywords can be used on methods ... I skip this ;)
// This method HAS TO BE implemented by every inheritor
public abstract void Use();
// This method CAN BE implemented in order to extend or overwrite it by inhertors
public virtual void SaySomething()
{
Debug.Log("I basically do nothing.");
}
}
Теперь вы наследуете как
[System.Serializable]
public class PartAttack : Ability
{
// inherit target_manager and can use it in every inheritor
public int block_attack_damage = 0;
public int block_attack_repeats = 0;
// Has to be implemented otherwise throws compiler-error
public override void Use()
{
foreach (Combatant enemy in target_manager.list_of_targetable)
{
int repeats = block_attack_repeats;
while (repeats >= 0)
{
enemy.health = enemy.health - block_attack_damage;
block_attack_repeats -= 1;
}
}
}
// CAN BE implemented in order to extend
public override void SaySomething()
{
Debug.Log("I'm creating some debug logs but ..");
base.SaySomething();
}
}
Теперь в зависимости от того, с кем вы идете, ваш словарь либо
public Dictionary<string, IAbility> abilities_parts = new Dictionary<string, IAbility>();
или
public Dictionary<string, Ability> abilities_parts = new Dictionary<string, Ability>();