using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
///
/// Manages the UI display for the skill tree, dynamically creating buttons
/// and updating their state based on player progression.
///
public class SkillTreeUI : MonoBehaviour
{
[Header("UI References")]
[Tooltip("The parent transform where all skill buttons will be instantiated.")]
public Transform skillButtonParent;
[Tooltip("The prefab for a single skill button.")]
public GameObject skillButtonPrefab;
[Tooltip("Text element to display the player's available skill points.")]
public TextMeshProUGUI availableSkillPointsText;
[Header("Skill Info Pop-up")]
[Tooltip("The panel that displays the skill information.")]
public GameObject skillInfoPopupPanel;
[Tooltip("Text element for the skill's name in the pop-up.")]
public TextMeshProUGUI skillNameText;
[Tooltip("Text element for the skill's description in the pop-up.")]
public TextMeshProUGUI skillDescriptionText;
[Tooltip("Text element for the skill's cost in the pop-up.")]
public TextMeshProUGUI skillCostText;
[Tooltip("The button to learn the skill from the pop-up.")]
public Button learnSkillButton;
private SkillTreeManager skillTreeManager;
private PlayerProgression playerProgression;
private Dictionary skillButtons = new Dictionary();
private string activeSkillID;
private void Start()
{
// Get references to the singleton managers
skillTreeManager = SkillTreeManager.Instance;
playerProgression = PlayerProgression.Instance;
if (skillTreeManager == null || playerProgression == null)
{
Debug.LogError("Required managers not found. Ensure SkillTreeManager and PlayerProgression are in the scene.");
return;
}
// Subscribe to events to refresh the UI
skillTreeManager.OnSkillLearned.AddListener(RefreshUI);
playerProgression.OnSkillPointChanged.AddListener(RefreshUI);
PopulateSkillTreeUI();
RefreshUI();
// Hide the pop-up panel at the start
if (skillInfoPopupPanel != null)
{
skillInfoPopupPanel.SetActive(false);
}
}
private void OnDestroy()
{
// Unsubscribe from events to prevent memory leaks
if (skillTreeManager != null)
{
skillTreeManager.OnSkillLearned.RemoveListener(RefreshUI);
}
if (playerProgression != null)
{
playerProgression.OnSkillPointChanged.RemoveListener(RefreshUI);
}
}
///
/// Instantiates buttons for all skills defined in the Skill Tree ScriptableObject.
///
private void PopulateSkillTreeUI()
{
// This line was updated to get the ScriptableObject directly from the manager.
SkillTree skillTreeData = skillTreeManager.GetComponent().skillTreeData;
foreach (Skill skill in skillTreeData.skills)
{
GameObject newButton = Instantiate(skillButtonPrefab, skillButtonParent);
skillButtons[skill.skillID] = newButton;
// Get the button's components
Button buttonComponent = newButton.GetComponent