์๋ ํ์ธ์.
OOP์์ ๋ถ๋ชจ๋ฅผ ์์๋ฐ์ ์์์ ์ ์บ์คํ ์ด๋ ๋ค์ด์บ์คํ ํด์ ์ค๋ฒ๋ผ์ด๋ฉ๋ ๋ณ์๋ฅผ ํ์ฉํ๋ ๋ฐฉ์์ผ๋ก ๋ฌด๊ธฐ ์์ดํ ์ ๋์์ธ ํ๊ณ ์์ต๋๋ค.
์์ปจ๋, Weapon.WeaponClass์ด๋ ๋ถ๋ชจ๋ ์์ดํ ์ ์ด๋ฆ, ์ค๋ช , ์ฝ๋, ํ์ , ๋ฑ๊ธ์ ๋ณ์๋ฅผ ๊ฐ๊ณ ์๊ณ ์. PrintInfo๋ก ์ด๋ฅผ ์ฝ์๋ก ํ์ธํ ์ ์์ต๋๋ค.
Melee, Range๋ Weapon์ ์์๋ฐ์์, ๊ทผ๊ฑฐ๋ฆฌ ์๊ฑฐ๋ฆฌ์ ๋ฐ๋ฅธ ์ถ๊ฐ ํจ๊ณผ๋ค(๊ทผ๊ฑฐ๋ฆฌ ์ ํฌ๋ ฅ, ์๊ฑฐ๋ฆฌ ์ ํฌ๋ ฅ ๊ฐ์)์ ๊ด๋ฆฌํ๋ ๋ณ์๋ฅผ ๊ฐ๊ณ ์ด๋ฅผ ๋ฆฌ์คํธ๋ก ๊ด๋ฆฌํฉ๋๋ค. ๋ํ PrintInfo๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ์ฌ, ๊ทธ๋ฐ ๋ณ์๋ค์ ์ถ๋ ฅํด์ค๋๋ค.
๋ํ Magic๋ Weapon์ ์์๋ฐ์์, ๋ง๋ฒ ์คํฌ์ ๋ฐ๋ฅธ ์ถ๊ฐ ํจ๊ณผ๋ค์ ๊ด๋ฆฌํ๋ ๋ณ์๋ฅผ ๊ฐ๊ณ ์ด๋ฅผ ๋ฆฌ์คํธ๋ก ๊ด๋ผํ๋ฉด, PrintInfo๋ ์ค๋ฒ๋ผ์ด๋ฉ ํ์ต๋๋ค.
์ฌ๊ธฐ์ ์ ๋ Dictonary๋ก key๊ฐ์ weapon.weaponclass์ ์๋ int id๋ฅผ ๋ฐ๊ณ , value๋ก ๋คํ์ฑ์ ์ ๊ณตํ๋ weapon.weaponclass๋ฅผ ์์๋ฐ๋ ์์๋ค์ ๋ฃ์ผ๋ คํ์ต๋๋ค. ๋ค๋ง, ์ด๋ ๊ฒ ํ๋ ์ค๋ฅ๊ฐ ๋จ๋๋ผ๊ตฌ์.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class WeaponManagement : MonoBehaviour
{
[SerializeField] private MeleeWeapon melee;
[SerializeField] private MagicWeapon magic;
[SerializeField] private RangeWeapon range;
public Dictionary<int, Weapon.WeaponClass> weapon;
private void Start()
{
//Melee
for (int i = 0; i < melee.meleeWeaponList.Count; i++)
{
Weapon.WeaponClass wea = new MeleeWeapon.Melee();
wea = melee.meleeWeaponList[i]; ;
int id = melee.meleeWeaponList[i].ID;
wea.PrintInfo(); //๋คํ์ฑ๋ ์ ๋๋ก ์ ์ฉ๋จ.
weapon.Add(id, wea); //์ฌ๊ธฐ์ ์ค๋ฅ ๋ฐ์
}
//Magic
for (int i = 0; i < magic.magicWeaponList.Count; i++)
{
Weapon.WeaponClass wea = magic.magicWeaponList[i];
int id = magic.magicWeaponList[i].ID;
weapon.Add(id, wea);
}
}
}
//MeleeWeapon.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;
[CreateAssetMenu(fileName = "Melee Weapon Data", menuName = "Weapon/Melee Weapon Data", order = 2)]
public class MeleeWeapon : ScriptableObject
{
public List<Melee> meleeWeaponList;
[System.Serializable]
public class Melee : Weapon.WeaponClass
{
public Melee()
{ }
//๋ฐ๋ฆฌ๋ณ์
[SerializeField] protected float physicalCritRate;
public float PhysicalCritRate { get { return physicalCritRate; } }
[SerializeField] protected float physicalCritDmg;
public float PhysicalCritDmg { get { return physicalCritDmg; } }
[SerializeField] protected float physicalSpeed;
public float PhysicalSpeed { get { return physicalSpeed; } }
public override void PrintInfo()
{
base.PrintInfo();
Debug.Log("PhysicalCritRate : " + physicalCritRate + "% PhysicalCritDmg : " + physicalCritDmg + "%");
Debug.Log("Physical Speed : " + physicalSpeed);
}
}
}
์ผ๋จ Melee, Magic์ผ๋ก ํ์ธํด๋ดค๊ตฌ์. weapon.Add(id, wea)์์ ์ค๋ฅ๊ฐ ๋ฉ๋๋ค.
๋จ๋ ์ค๋ฅ๋ ๋๋ฆฌํผ๋ฐ์ค ์ต์ ์ ์ ๋๋ค.. ํน์ ์ฝ๋๊ฐ ์ ๋ชป๋ ๊ฑฐ๋ผ๋ฉด ์ง์ ํด์ฃผ์ญ์ผ.. Melee์ ๊ฐ์ ํ์ ์ ๋น์ด์๋ ์์ฑ์๋ง ์กด์ฌํฉ๋๋ค.
Melee๋ Magic๊ฐ์ ์ค๋ธ์ ํธ๋ค์ Scriptable Object์ด๊ณ , List๊ฐ ์์ด์ ์ธ์คํํฐ๋ก ๊ด๋ฆฌํฉ๋๋ค. ๊ฐ์ด ์ฒจ๋ถํ๊ฒ ์ต๋๋ค.
weapon ๋์ ๋๋ฆฌ๊ฐ ์์ฑ๋์ง ์์์. ๋ ์ต์ ์ ๋จ๋๋ฐ ๋ค์์ฑ์ด์ผ๊ธฐ๋ฅผ ํ๋๊ฑฐ๋ณด๋ ์๋ฌ ๋ฉ์ธ์ง๋ฅผ ์ข ๋ ์์ธํ ๋ค์ฌ๋ค๋ณด๋ ์ต๊ด์ ๊ธฐ๋ฅด๋๋ก ํ์๊ฒ
๊ฐ์ฌ. ์๋์ ๊ฐ์ฌ. ์ ์๋์ ์ฒ์ฌ์ ๋๋ค. ๊ฐ์ฌํฉ๋๋ค. ์ ๋ weapon.Add(id, wea); ์ด ๋ถ๋ถ์์ ์ค๋ฅ๊ฐ ๋ฌ๋ค๊ธธ๋ ๋น์ฐํ ์ธ์ ์ค๋ฅ๊ฒ ๋ค. ์๊ฐํ๋๋ฐ ์ ํ ์๋์๊ตฐ์. ์ ์๋์ ์ฒ์ฌ์ ๋๋ค. ๊ทธ๋ ๊ฒ ์์ญ์์. ์๋ํ ์ ๋ํฐ๋ ๋์ ๋๋ฆฌ์์ ๋คํ์ฑ์ ์ ๊ณตํฉ๋๋ค!
ํด๋น ๋๊ธ์ ์ญ์ ๋์์ต๋๋ค.
๋น ํด๋์ค๋ Debug.Log๋ PrintInfo๋ก ๋๋ ค๋ณด๋๊น ํด๋์ค๋ ๋ฌธ์ ์์๊ณ , ๋คํ์ฑ๋ ์ ๋๋ก ์๋ํ์ต๋๋ค. ๋ฌธ์ ๋ ํด๊ฒฐ๋ ๊ฒ ๊ฐ๋ค์ ๊ฐ์ฌํฉ๋๋ค.
์๋ฐ์ด...