1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using UnityEngine.Events; using UnityEngine.UI; public class ExperienceManager : Singleton<ExperienceManager> { #region [Tooltip("ํ์ฌ ๋ ๋ฒจ์ ํ์ํ UI ์ปดํฌ๋ํธ")] #endregion [SerializeField] TextMeshProUGUI CurrentLevelText; #region [Tooltip("ํ์ฌ XP๋ฅผ ํ์ํ UI ์ปดํฌ๋ํธ")] #endregion [SerializeField] TextMeshProUGUI CurrentXPText; #region [Tooltip("๋ค์ ๋ ๋ฒจ๊น์ง ํ์ํ XP๋ฅผ ํ์ํ UI ์ปดํฌ๋ํธ")] #endregion [SerializeField] TextMeshProUGUI XPToNextLevelText; #region [Tooltip("XP ๊ณ์ฐ ๋ฐฉ์์ ๊ฒฐ์ ํ ScriptableObject")] #endregion [SerializeField] BaseXPTranslation XPTranslationType; // C# ์ด๋ฒคํธ ์ ์ธ public event Action<int, int> OnXpChanged; public Slider expBarSlider; // ๋ฐํ์์ ์ฌ์ฉ๋ XP ๊ณ์ฐ ์ธ์คํด์ค BaseXPTranslation XPTranslation; private void Awake() { //Debug.Log(XPTranslationType); XPTranslation = ScriptableObject.Instantiate(XPTranslationType); } // ๊ฒฝํ์น ์ถ๊ฐ ๋ฐ UI ์
๋ฐ์ดํธ ๋ฉ์๋, EXP ๋งค๋์ ๋ฅผ ํตํด์ XP Translation์ ์ฐธ์กฐํจ public void AddXP(int amount) { int previousLevel = XPTranslation.CurrentLevel; if (XPTranslation.AddXP(amount)) { OnXpChanged?.Invoke(previousLevel, XPTranslation.CurrentLevel); } RefreshDisplays(); } // ๋ ๋ฒจ ์ค์ ๋ฐ UI ์
๋ฐ์ดํธ ๋ฉ์๋ public void SetLevel(int level) { int previousLevel = XPTranslation.CurrentLevel; XPTranslation.SetLevel(level); if (previousLevel != XPTranslation.CurrentLevel) { OnXpChanged?.Invoke(previousLevel, XPTranslation.CurrentLevel); } RefreshDisplays(); } // Start is called before the first frame update void Start() { RefreshDisplays(); //Debug.Log("์คํํธ!"); //Debug.Log($"Start: OnXpChanged = {OnXpChanged}"); OnXpChanged?.Invoke(0, XPTranslation.CurrentLevel); } // Update is called once per frame void Update() { } // UI๋ฅผ ์ต์ ์ํ๋ก ๊ฐฑ์ ํ๋ ๋ฉ์๋ void RefreshDisplays() { #region ํ
์คํธ ํํธ CurrentLevelText.text = $"{XPTranslation.CurrentLevel}"; CurrentXPText.text = $"{XPTranslation.CurrentXP}"; if (!XPTranslation.AtLevelCap) { XPToNextLevelText.text = $"{XPTranslation.XPRequiredForNextLevel}"; } else { CurrentXPText.text = $"Max"; XPToNextLevelText.text = $"Max"; } #endregion ํ
์คํธ ํํธ //์ฌ๋ผ์ด๋ UI if (expBarSlider != null) { //๋ง๋ ์ด ์๋๋ if(!XPTranslation.AtLevelCap) { //ํ์ฌ ์บ๋ฆญํฐ์ ๋ฅ๋ ฅ์น๋ก ๋ฐ๊ธฐ๋ก ๊ณง ๊ต์ฒดํ๊ธฐ expBarSlider.maxValue = XPTranslation.XPRequiredForNextLevel; expBarSlider.value = XPTranslation.CurrentXP; } //๋ง๋ ์ผ๋ else { expBarSlider.maxValue = XPTranslation.XPRequiredForNextLevel; expBarSlider.value = XPTranslation.XPRequiredForNextLevel; } } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class XPTranslationTableEntry { public int Level; //ํด๋น ์ํธ๋ฆฌ์ ๋ ๋ฒจ public int XPRequired;//ํด๋น ๋ ๋ฒจ์ ๋๋ฌํ๊ธฐ ์ํด ํ์ํ ๊ฒฝํ์น์ ์ } [CreateAssetMenu(fileName = "XPTranslation_Table" , menuName = "Scriptable Objects/Exp/XPTable")] public class XPTranslation_Table : BaseXPTranslation { //SO๋ฐ์ดํฐ [SerializeField] List<XPTranslationTableEntry> Table; // ๋ ๋ฒจ๊ณผ ํ์ํ XP๋ฅผ ๋งคํํ ํ
์ด๋ธ // ๊ฒฝํ์น๋ฅผ ์ถ๊ฐํ๊ณ ๋ ๋ฒจ ์
์ ์ฒ๋ฆฌํ๋ ๋ฉ์๋ public override bool AddXP(int amount) { if (AtLevelCap) { return false; } CurrentXP += amount; bool levelChanged = false; // ํ์ฌ XP๊ฐ ๋ค์ ๋ ๋ฒจ์ ํ์ํ XP๋ฅผ ์ด๊ณผํ ๋๊น์ง ๋ฐ๋ณต while (!AtLevelCap && CurrentXP >= Table[CurrentLevel].XPRequired) { // ๋ค์ ๋ ๋ฒจ๋ก ์
๋ฐ์ดํธ CurrentXP -= Table[CurrentLevel].XPRequired; // ๋ค์ ๋ ๋ฒจ๋ก ๊ฐ๊ธฐ ์ํด ํ์ํ XP๋ฅผ ํ์ฌ XP์์ ๋นผ๊ธฐ CurrentLevel++; levelChanged = true; // ๋ ๋ฒจ ์
ํ ์ต์ข
๋ ๋ฒจ์ ๋๋ฌํ๋์ง ํ์ธ AtLevelCap = CurrentLevel >= Table.Count; if (AtLevelCap) { CurrentXP = 0; // ์ต์ข
๋ ๋ฒจ์์๋ XP๋ฅผ 0์ผ๋ก ์ค์ break; // ๋ฐ๋ณต ์ข
๋ฃ } } return levelChanged; } // ํน์ ๋ ๋ฒจ๋ก ์ค์ ํ๋ ๋ฉ์๋ public override void SetLevel(int level) { CurrentXP = 0; CurrentLevel = 1; AtLevelCap = false; foreach (var entry in Table) { if (entry.Level == level) { AddXP(entry.XPRequired); return; } } throw new System.ArgumentOutOfRangeException($"๋ ๋ฒจ์ ๋ํ ํญ๋ชฉ ์ฐพ์ ์ ์์ {level}"); } // ๋ค์ ๋ ๋ฒจ์ ํ์ํXP๋ฅผ ๋ฐํํ๋ ๋ฉ์๋ protected override int GetXPRequiredForNextLevel() { if (AtLevelCap) { return int.MaxValue; } for (int index = 0; index < Table.Count; ++index) { var entry = Table[index]; if (entry.Level == CurrentLevel) { return Table[index + 1].XPRequired; } } throw new System.ArgumentOutOfRangeException($"ํ์ฌ ๋ ๋ฒจ์ ๋ํ ํญ๋ชฉ ์ฐพ์ ์ ์์ {CurrentLevel}"); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using UnityEngine; public abstract class BaseXPTranslation : ScriptableObject { // ํ์ฌ ์บ๋ฆญํฐ์ XP public int CurrentXP { get; protected set; } = 0; // ํ์ฌ ์บ๋ฆญํฐ์ ๋ ๋ฒจ public int CurrentLevel { get; protected set; } = 1; // ๋ค์ ๋ ๋ฒจ์ ํ์ํ XP public int XPRequiredForNextLevel => GetXPRequiredForNextLevel(); // ๋ ๋ฒจ ์ํ์ ๋๋ฌํ๋์ง ์ฌ๋ถ public bool AtLevelCap { get; protected set; } = false; // ๊ฒฝํ์น ์ถ๊ฐ ์ถ์ ๋ฉ์๋ public abstract bool AddXP(int amount); // ๋ ๋ฒจ ์ค์ ์ถ์ ๋ฉ์๋ public abstract void SetLevel(int level); // ๋ค์ ๋ ๋ฒจ์ ํ์ํ XP ๊ณ์ฐ ์ถ์ ๋ฉ์๋ protected abstract int GetXPRequiredForNextLevel(); } | cs |
ํ์ฌ๋ SO์์ ๋ฐ์ดํฐ ๋งคํํด์ ๊ตฌํํ๋๋ฐ
๋ด๊ฐ ์๊ฐํด๋ ์ฝ๋๋ฅผ ๊ทธ๋ ๊ฒ ์ ์ง์ง ์์์, ์ข ๊ณ ๋ฏผ ์ค์ด๋ค.
์ฝ๋๊ฐ ์ด์์ง ์๋ค๊ณ ์๊ฐํ๋๋ฐ,
์ด๋ ๊ฒ ๋ ธ๋ ๊ทธ๋ํ๋ฅผ ๋ง๋๋ ์ค์ธ๋ฐ, SO์ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐํ์ผ๋ก ๋ ธ๋๋ฅผ ๋ง๋ค๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น.
์ด๋ ๊ฒ ํ๋ฉด ๋์ค์ ํ์ ํ ๋ ๊ธฐํํ๋ฉด ์์๊ฐ์ ๊ธ๋ฐฉ ๊ณ ์ณ๋ ๋ ๊ฒ๊ฐ์์ ํธํ ๊ฑฐ๊ฐ์๋ฐ
๊ทธ๋์ ๋ด ๋ชฉํ๊ฐ ์ ๋ฐ์ดํฐ ํ
์ด๋ธ์ ๋
ธ๋๋ก ๋ง๋ค๊ณ ์ถ์๋ฐ ์ด๋ป๊ฒ ํด์ผ ํ ๊น?
์ฆ ๋ ๋ฒจ์ -๋ฐ์ดํฐ ํ ์ด๋ธ ๋ ธ๋ ์ง๋๊ฐ->๊ฒฐ๊ณผ๊ฐ
์ด๋ ๊ฒ ๋์ผํ๋๋ฐ, ๋ฐ์ดํฐ ํ ์ด๋ธ ๋ ธ๋์์ ๋ค์ ๋ ๋ฒจ์ ์ ๋ณด๋ฅผ ๋ฐ์ผ๋ ค๋ฉด ์ด๋ป๊ฒ ์ง์ผํ์ง.
์ฝ๋ ์์ค์ด ๋ฎ์๊ฑด ์ดํดํด์ฃผ์ . ๊ทธ๋ค์ง ์ฝ๋๋ฅผ ์ ์ง์ง ๋ชปํด์
๋๊ธ 0