using UnityEngine; using UnityEngine.UI; namespace Kryz.CharacterStats.Examples { public class StatPanel : MonoBehaviour { [SerializeField] private Transform statDisplaysParent; [SerializeField] StatDisplay[] statDisplays; [SerializeField] private StatDisplay[] secondaryStatDisplays; [SerializeField] string[] statNames; [SerializeField] Button[] addStatButtons; [SerializeField] private string[] secondaryStatNames; [SerializeField] private Text characterName; [SerializeField] private Text characterLevel; [SerializeField] private Text characterRepLevel; [SerializeField] private StatDisplay unallocated; [SerializeField] private Button resetAllocated; private CharacterStat[] stats; private CharacterStat[] secondaryStats; private PlayerCharacterStats playerStats; private bool InitializedListeners = false; private void OnValidate() { if (statDisplaysParent != null) { statDisplays = statDisplaysParent.GetComponentsInChildren(); UpdateStatNames(); UpdateSecondaryStatNames(); } } public void SetPlayerStats(PlayerCharacterStats playerStats) { this.playerStats = playerStats; } public void SetStats(params CharacterStat[] charStats) { stats = charStats; if (stats.Length > statDisplays.Length) { Debug.LogError("Not Enough Stat Displays!"); return; } for (int i = 0; i < statDisplays.Length; i++) { statDisplays[i].Stat = i < stats.Length ? stats[i] : null; statDisplays[i].gameObject.SetActive(i < stats.Length); } unallocated.ValueText.text = playerStats.AvailablePointsToAllocate.ToString(); unallocated.gameObject.SetActive(playerStats.AvailablePointsToAllocate > 0); if (InitializedListeners) return; InitializedListeners = true; addStatButtons[0].onClick.AddListener(() => AllocateStat(0)); addStatButtons[1].onClick.AddListener(() => AllocateStat(1)); addStatButtons[2].onClick.AddListener(() => AllocateStat(2)); addStatButtons[3].onClick.AddListener(() => AllocateStat(3)); addStatButtons[4].onClick.AddListener(() => AllocateStat(4)); resetAllocated.onClick.AddListener(ResetAllocatedStats); } public void SetSecondaryStats(params CharacterStat[] secondaryStats) { this.secondaryStats = secondaryStats; if (secondaryStats.Length > secondaryStatDisplays.Length) { Debug.LogError("Not Enough Stat Displays!"); return; } for (int i = 0; i < secondaryStatDisplays.Length; i++) { secondaryStatDisplays[i].Stat = secondaryStats[i]; } } int pointsToAllocate; public void AllocateStat(int index) { if (playerStats.AvailablePointsToAllocate <= 0) return; // Determine points to allocate based on shift key pointsToAllocate = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift) ? 10 : 1; pointsToAllocate = Mathf.Min(pointsToAllocate, playerStats.AvailablePointsToAllocate); playerStats.AllocatedStatPoints[index] += pointsToAllocate; playerStats.AvailablePointsToAllocate -= pointsToAllocate; playerStats.UpdateAllocatedStats(); ToggleAllocateButtonsInteractable(playerStats.AvailablePointsToAllocate > 0); } public void ResetAllocatedStats() { for (int i = 0; i < playerStats.AllocatedStatPoints.Count; i++) { playerStats.AvailablePointsToAllocate += playerStats.AllocatedStatPoints[i]; playerStats.AllocatedStatPoints[i] = 0; } playerStats.UpdateAllocatedStats(); ToggleAllocateButtonsInteractable(playerStats.AvailablePointsToAllocate > 0); } public void SetCharacterInfo(string name, string level, string repLevel) { characterName.text = name; characterLevel.text = level; characterRepLevel.text = repLevel; } public void UpdateLevelInfo(string level, string repLevel) { characterLevel.text = level; characterRepLevel.text = repLevel; } public void UpdateStatValues() { for (int i = 0; i < stats.Length; i++) { statDisplays[i].ValueText.text = stats[i].Value.ToString(); } UpdateSecondaryStatValues(); } public void UpdateStatNames() { for (int i = 0; i < statNames.Length; i++) { statDisplays[i].NameText.text = statNames[i]; } } public void UpdateSecondaryStatNames() { for (int i = 0; i < secondaryStatNames.Length; i++) { secondaryStatDisplays[i].NameText.text = secondaryStatNames[i]; } } public void UpdateSecondaryStatValues() { for (int i = 0; i < secondaryStats.Length; i++) { secondaryStatDisplays[i].ValueText.text = secondaryStatDisplays[i].NameText.text.Contains("crit") ? secondaryStats[i].Value.ToString() + " %" : secondaryStats[i].Value.ToString(); } } public void ToggleAllocateButtonsInteractable(bool interactable) { unallocated.ValueText.text = playerStats.AvailablePointsToAllocate.ToString(); unallocated.gameObject.SetActive(playerStats.AvailablePointsToAllocate > 0); for (int i = 0; i < addStatButtons.Length; i++) { addStatButtons[i].gameObject.SetActive(interactable); } } } }