유닛에 스텟이 자그마치 변수가 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);
}
}
스텟이 아무때나 마구잡이로 생성될 클래스도 아니고, 엄청나게 빠른 반응을 요구하지도 않는 클래스이므로 빌더패턴을 적용하였다.
나쁘지 않은 선택일까나
'Develop_Log' 카테고리의 다른 글
[ScriptableObject Loader] Excel에서 SO로 만들기 (0) | 2024.11.24 |
---|---|
[팀플젝 회고] 유니티 심화주차 (0) | 2024.11.22 |
[TrobleShooting] ww로 달리기 구현 (0) | 2024.10.24 |
유니티 입문 주차 회고 (2) | 2024.10.22 |
아이템에 커맨드 패턴 적용 (1) | 2024.10.18 |