Develop_Log

유닛 스텟에 빌더패턴 적용

리네엔 2024. 10. 25. 22:42

유닛에 스텟이 자그마치 변수가 10개다.
아이템은 이 스텟들 중에 6개만을 상승 시킬 수 있다.
아이템이 아닌 다른 무언가가 이 6개 스텟말고 다른 스텟을 증가 시킬 경우의 수도 있다.
이걸 디폴트 매개변수 생성자를 쓰자니 인생이 너무 힘들어질 것 같았다.
해결방안으로 빌더 패턴을 적용했다.


public enum StatsChangeType
{
    Add,
    Multiple,
    Override,
}

[Serializable]
public class UnitStat
{
    [SerializeField] private StatsChangeType _type;
    public StatsChangeType Type => _type;

    [SerializeField] private int _health;
    [SerializeField] private int _hunger;
    [SerializeField] private int _stamina;

    [SerializeField] private int _passiveHealthRecovery;
    [SerializeField] private int _passiveHealthDecline;
    [SerializeField] private int _passiveHungerDecline;
    [SerializeField] private int _passiveStaminaRecovery;

    [SerializeField] private int _walkSpeed;
    [SerializeField] private int _runSpeed;
    [SerializeField] private int _jumpForce;


    public int Health => _health;
    public int Hunger => _hunger;
    public int Stamina => _stamina;

    public int PassiveHealthRecovery => PassiveHealthRecovery;
    public int PassiveHealthDecline => PassiveHealthDecline;
    public int PassiveHungerDecline => PassiveHungerDecline;
    public int passiveStaminaRecovery => passiveStaminaRecovery;

    public int WalkSpeed => _walkSpeed;
    public int RunSpeed => _runSpeed;
    public int JumpForce => _jumpForce;

    public UnitStat() { }

    public UnitStat(StatsChangeType type, int health, int hunger, int stamina, int passiveHealthRecovery, int passiveHealthDecline, int passiveHungerDecline, int passiveStaminaRecovery, int walkSpeed, int runSpeed, int jumpForce)
    {
        _type = type;
        _health = health;
        _hunger = hunger;
        _stamina = stamina;
        _passiveHealthRecovery = passiveHealthRecovery;
        _passiveHealthDecline = passiveHealthDecline;
        _passiveHungerDecline = passiveHungerDecline;
        _passiveStaminaRecovery = passiveStaminaRecovery;
        _walkSpeed = walkSpeed;
        _runSpeed = runSpeed;
        _jumpForce = jumpForce;
    }
}

public class UnitStatBuilder
{
    private StatsChangeType _type;

    private int _health;
    private int _hunger;
    private int _stamina;

    private int _passiveHealthRecovery;
    private int _passiveHealthDecline;
    private int _passiveHungerDecline;
    private int _passiveStaminaRecovery;

    private int _walkSpeed;
    private int _runSpeed;
    private int _jumpForce;

    public UnitStatBuilder Type(StatsChangeType value)
    {
        this._type = value;
        return this;
    }

    public UnitStatBuilder Health(int value)
    {
        this._health = value;
        return this;
    }
    public UnitStatBuilder Hunger(int value)
    {
        this._hunger = value;
        return this;
    }
    public UnitStatBuilder Stamina(int value)
    {
        this._stamina = value;
        return this;
    }

    public UnitStatBuilder PassiveHealthRecovery(int value)
    {
        this._passiveHealthRecovery = value;
        return this;
    }
    public UnitStatBuilder PassiveHealthDecline(int value)
    {
        this._passiveHealthDecline = value;
        return this;
    }
    public UnitStatBuilder PassiveHungerDecline(int value)
    {
        this._passiveHungerDecline = value;
        return this;
    }
    public UnitStatBuilder PssiveStaminaRecovery(int value)
    {
        this._passiveStaminaRecovery = value;
        return this;
    }

    public UnitStatBuilder WalkSpeed(int value)
    {
        this._walkSpeed = value;
        return this;
    }
    public UnitStatBuilder RunSpeed(int value)
    {
        this._runSpeed = value;
        return this;
    }
    public UnitStatBuilder JumpForce(int value)
    {
        this._jumpForce = value;
        return this;
    }

    public UnitStat Build()
    {
        return new UnitStat(_type, _health, _hunger, _stamina, _passiveHealthRecovery, _passiveHealthDecline, _passiveHungerDecline, _passiveStaminaRecovery, _walkSpeed, _runSpeed, _jumpForce);
    }

}

스텟이 아무때나 마구잡이로 생성될 클래스도 아니고, 엄청나게 빠른 반응을 요구하지도 않는 클래스이므로 빌더패턴을 적용하였다.
나쁘지 않은 선택일까나