스킬

Skill 클래스는 플레이어(Player)가 사용할 수 있는 스킬을 정의하는 클래스다.

스킬은 이름, 설명, MP 소모량, 타입 및 실행 로직(action)을 포함한다.

이를 통해 다양한 스킬을 게임 내에서 구현할 수 있으며, action을 활용하여 개별 스킬의 동작을 유연하게 설정할 수 있다.

클래스 정의

public class Skill
{
    public string name { get; set; }
    public string description { get; set; }
    public int mp { get; set; }
    public int type { get; set; }
    Func<Player, Monster, int> action { get; set; }

    // action 포함 생성자
    public Skill(string name, string description, int mp, int type, Func<Player, Monster, int> action)
    {
        this.name = name;
        this.description = description;
        this.mp = mp;
        this.type = type;
        this.action = action;
    }
    // action 미포함 생성자
    public Skill(string name, string description, int mp, int type)
    {
        this.name = name;
        this.description = description;
        this.mp = mp;
        this.type = type;
    }

    // action을 나중에 설정 가능
    public void SetAction(Func<Player, Monster, int> action)
    {
        this.action = action;
    }

    public int Invoke(Player player, Monster monster)
    {
        int result = 0;

        if (action != null)
        {
            result = action(player, monster);
        }
        else
        {
            Console.WriteLine("Action 값이 없다.");  // Debug용
        }
        return result;
    }

    public bool CanUse(Player player)
    {
        if (player.mp - mp < 0)
        {
            return false;
        }
        return true;
    }
}

스킬 생성 (Action 포함)

Skill fireball = new Skill(
    "Fireball",
    "A powerful fire attack",
    10,
    1,
    (player, monster) => {
        monster.hp -= 30;
        player.mp -= 10;
        return 30; // 데미지 반환
    }
);

이 방식으로 생성된 스킬은 즉시 사용할 수 있으며, Invoke()를 호출하면 action에 정의된 효과가 적용된다.

스킬 생성 후 Action 설정

Skill heal = new Skill("Heal", "Restores health", 5, 2);
heal.SetAction((player, monster) => {
    player.hp += 20;
    player.mp -= 5;
    return 20; // 회복량 반환
});

이러한 방식으로 SetAction()을 사용하면, 초기 생성 시 동작을 정의하지 않고 나중에 설정할 수도 있다.

추가 메서드와 스킬 생성

// SkillManager.cs
public static int AlphaStrike(Player player, Monster monster)
{
    int damage = UtilManager.GetCeiling(player.atk) * 2;
    monster.hp = UtilManager.CalcDamage(monster.hp, damage);
    return damage;
}

// 사용될 스킬
Skill AlphaStrike = new Skill("AlphaStrike", "AlphaStrike.", 5, 1, SkillManager.AlphaStrike );

스킬 사용

if (fireball.CanUse(player))
{
    int damage = fireball.Invoke(player, monster);
    Console.WriteLine($"Fireball dealt {damage} damage!");
}
else
{
    Console.WriteLine("Not enough MP to use Fireball!");
}

스킬을 사용하기 전에 CanUse()를 호출하여 MP가 충분한지 확인해야한다.

스킬 목록 관리

플레이어가 여러 개의 스킬을 보유하도록 하기 위해 리스트를 활용할 수도 있다.

List<Skill> playerSkills = new List<Skill>();
playerSkills.Add(fireball);
playerSkills.Add(heal);

이렇게 하면 플레이어가 다양한 스킬을 보유하고 게임 내에서 선택적으로 사용할 수 있다.


주의 사항

  • Invoke() 호출 전에 SetAction()으로 동작을 설정.
  • CanUse()를 통해 MP가 충분한지 확인 후 Invoke()를 실행.
  • actionnull이면 Invoke() 실행 시 오류가 발생할 수 있다.
  • 스킬을 추가할 때 중복되지 않도록 관리하는 것이 중요.
  • 플레이어의 MP가 부족한 상태에서 스킬을 사용하려 하면 실행되지 않는다.

댓글남기기