114 lines
3.6 KiB
C#
114 lines
3.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Kryz.CharacterStats.Examples
|
|
{
|
|
public class StatPanel : MonoBehaviour
|
|
{
|
|
[SerializeField] private Transform statDisplaysParent;
|
|
[SerializeField] StatDisplay[] statDisplays;
|
|
[SerializeField] string[] statNames;
|
|
[SerializeField] Button[] addStatButtons;
|
|
[SerializeField] private Text characterName;
|
|
[SerializeField] private Text characterLevel;
|
|
[SerializeField] private StatDisplay unallocated;
|
|
private CharacterStat[] stats;
|
|
private PlayerCharacterStats playerStats;
|
|
|
|
private bool InitializedListeners = false;
|
|
|
|
private void OnValidate()
|
|
{
|
|
if (statDisplaysParent != null)
|
|
{
|
|
statDisplays = statDisplaysParent.GetComponentsInChildren<StatDisplay>();
|
|
|
|
UpdateStatNames();
|
|
}
|
|
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
public void AllocateStat(int index)
|
|
{
|
|
if (playerStats.AvailablePointsToAllocate <= 0) return;
|
|
|
|
playerStats.AllocatedStatPoints[index]++;
|
|
playerStats.AvailablePointsToAllocate--;
|
|
playerStats.UpdateAllocatedStats();
|
|
|
|
ToggleAllocateButtonsInteractable(playerStats.AvailablePointsToAllocate > 0);
|
|
}
|
|
|
|
public void SetCharacterInfo(string name, string level)
|
|
{
|
|
characterName.text = name;
|
|
characterLevel.text = level;
|
|
}
|
|
public void UpdateLevelInfo(string level)
|
|
{
|
|
characterLevel.text = level;
|
|
}
|
|
|
|
public void UpdateStatValues()
|
|
{
|
|
for (int i = 0; i < stats.Length; i++)
|
|
{
|
|
statDisplays[i].ValueText.text = stats[i].Value.ToString();
|
|
}
|
|
}
|
|
|
|
public void UpdateStatNames()
|
|
{
|
|
for (int i = 0; i < statNames.Length; i++)
|
|
{
|
|
statDisplays[i].NameText.text = statNames[i];
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|