Unity/Unity 프로그래밍

[C#/Unity] Builder 패턴을 이용해서 캐릭터 기술 만들기!

DoubleAAa 2025. 3. 24. 23:18

 

게임에서 플레이어 캐릭터들은 자신만의 기술을 가지고 있다.

베기, 찌르기, 이동하기, 총으로 공격하기 등등... 많은 기술들을 관리하기 위해

Builder 패턴을 활용해서 만들어보자!

 

 Bilder 패턴이 무엇인지는 이 글에서 확인해보자!

https://twilightchicken.tistory.com/entry/c-%EA%B2%8C%EC%9E%84%EC%9C%BC%EB%A1%9C-%EC%9D%B4%ED%95%B4%ED%95%98%EB%8A%94-%EC%83%9D%EC%84%B1-%ED%8C%A8%ED%84%B4-04-%EB%B9%8C%EB%8D%94Builder


1. 우선 캐릭터  스킬의 타입을 정의한다! 

public enum Player_SkillType
{
    type_Melee,  // 근접
    type_Range,  // 원거리
    type_Heal,   // 치유
    type_Buff    // 버프
}

 

캐릭터 기술의 타입은 근접형, 원거리형, 치유형, 버프형이 있다.


2. 캐릭터 스킬에 필요한 변수들을 정의한다!

    public bool skill_Seleted = false;  // 선택 유무
    public string skill_Name;   // 스킬명
    public int skill_Damage;    // 데미지
    public int skill_Crit;      // 치명타율
    public float skill_Heal;    // 힐량

    public Player_SkillType skill_Type; // 스킬 타입
    public Sprite skill_sprite;     // 스킬 아이콘

    public List<Vector2Int> skill_useablePos = new List<Vector2Int>(); // 사용 위치 리스트
    public List<Vector2Int> skill_targetPos = new List<Vector2Int>();  // 공격 대상 리스트

 

레퍼런스가 다키스트 던전이므로 필요한 변수들만 정의해보았다!


3-1. 빌더(Builder) 예시 코드

public skill_Builder SetName(string name) { skill.skill_Name = name; return this; }
public skill_Builder SetDamage(int damage) { skill.skill_Damage = damage; return this; }

이런식으로 스킬 변수들을 빌더 패턴에 적용시키면 된다!

 

3-1. 스킬 빌더(skill_Builder) 코드

// 스킬 빌더 패턴
public class skill_Builder 
{ 
    private Player_SkillData skill = new Player_SkillData();

    public skill_Builder SetName(string name) { skill.skill_Name = name; return this; }
    public skill_Builder SetDamage(int damage) { skill.skill_Damage = damage; return this; }
    public skill_Builder SetHeal(float heal) { skill.skill_Heal = heal; return this; }
    public skill_Builder SetCrit(int crit) { skill.skill_Crit = crit; return this; }
    public skill_Builder SetType(Player_SkillType type) { skill.skill_Type = type; return this; }
    public skill_Builder SetIcon(Sprite icon) { skill.skill_sprite = icon; return this; }

    public skill_Builder SetUseablePositions(params Vector2Int[] positions)
    {
        skill.skill_useablePos.AddRange(positions);
        return this;
    }

    public skill_Builder SetTargetPositions(params Vector2Int[] positions)
    {
        skill.skill_targetPos.AddRange(positions);
        return this;
    }

    public Player_SkillData Build() { return skill; }
}

4. 플레이어 캐릭터 클래스에서 빌더 패턴으로 스킬을 추가해보자!

 // 테스트용 기술 생성해보기
 public void InitializeSkills()
 {
     allSkills.Add(new Player_SkillData.skill_Builder()
         .SetName("내려찍기")
         .SetDamage(10)
         .SetCrit(15)
         .SetType(Player_SkillType.type_Melee)
         .SetUseablePositions(new Vector2Int(0, 1), new Vector2Int(1, 1))  // 사용 가능 위치
         .SetTargetPositions(new Vector2Int(0, 2), new Vector2Int(1, 2))   // 두 위치 중 지정 가능
         .Build()
         );

     allSkills.Add(new Player_SkillData.skill_Builder()
         .SetName("Gun 공격")
         .SetDamage(12)
         .SetCrit(10)
         .SetType(Player_SkillType.type_Range)
         .SetUseablePositions(new Vector2Int(0, 0), new Vector2Int(1, 0))  // 사용 가능 위치
         .SetTargetPositions(new Vector2Int(0, 2), new Vector2Int(0, 3), new Vector2Int(1, 2), new Vector2Int(1, 3)) // 대상 위치
         .Build()
         );

     allSkills.Add(new Player_SkillData.skill_Builder()
        .SetName("자힐")
        .SetHeal(10)
        .SetCrit(5)
        .SetType(Player_SkillType.type_Heal)
        .SetUseablePositions(new Vector2Int(0, 0), new Vector2Int(0, 1), new Vector2Int(1, 0), new Vector2Int(1, 1))
        .Build()
        );

     allSkills.Add(new Player_SkillData.skill_Builder()
       .SetName("자가 버프형")
       .SetDamage(0)
       .SetCrit(0)
       .SetType(Player_SkillType.type_Buff)
       .SetUseablePositions(new Vector2Int(0, 0), new Vector2Int(1, 0), new Vector2Int(0, 1), new Vector2Int(1, 1))
       .Build()
       );

     allSkills.Add(new Player_SkillData.skill_Builder()
      .SetName("필살기 전체공격형")
      .SetDamage(13)
      .SetCrit(20)
      .SetType(Player_SkillType.type_Melee)
      .SetUseablePositions(new Vector2Int(0, 1), new Vector2Int(1, 1))
      .SetTargetPositions(  // 광역기: 전체 공격 가능
         new Vector2Int(0, 2), new Vector2Int(0, 3), new Vector2Int(1, 2), new Vector2Int(1, 3))
      .Build()
      );

 

빌더 패턴의 장점은 변수가 아직 생성되지 않아도 테스트가 가능하다

(스킬 아이콘 변수를 아직 사용하지 않았다!)


- 테스트 결과

빌더 테스트 결과

플레이어에게 스킬이 등록된 것을 확인할 수 있었다!