Encounter / Assets / Scripts / GUIHandlers / SkillButton.cs
SkillButton.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(SkillGUIManager))]
/// <summary>
/// The GUI formatted button that contains the SkillSO data 
/// </summary>
public class SkillButton : MonoBehaviour
{
    [Header("Button Elements")]
    [SerializeField] private GraphicalSkillTree skillTreeComp;
    [SerializeField] private Button buttonRef;          // Reference to the button component itself
    [SerializeField] private Image spriteImage;         // Reference to the sprite that will showcase the skill sprite
    [SerializeField] private bool isButtonTowardsRight;
    [SerializeField] private bool isButtonTowardsLeft;
    [SerializeField] private bool isButtonTowardsTop;
    [SerializeField] private bool isButtonTowardsBottom;

    public bool IsRight 
    {
        get => isButtonTowardsRight;
    }
    public bool IsLeft
    {
        get => isButtonTowardsLeft;
    }
    public bool IsTop
    {
        get => isButtonTowardsTop;
    }
    public bool IsBottom
    {
        get => isButtonTowardsBottom;
    }

    private SkillSO skillData;
    private Entity entityAffected;

    private bool isActivated;
    private bool? active = null;

    public void CallInitialization(Entity entityRef, SkillSO skillData, bool hasBeenActivated, bool? isEnabled = null)
    {
        this.skillData = skillData;
        this.entityAffected = entityRef;

        spriteImage.sprite = skillData.SkillSprite;
        isActivated = hasBeenActivated;

        // Auto set to true for when the skill is actually activated to and fall in line as needed
        if (skillData.IsEnablingSkill && isEnabled is null)
            active = true;
        // When there is data present indicating enabled/disabled, set it to whatever state it was previously
        else if (skillData.IsEnablingSkill && isEnabled is not null)
            active = isEnabled;
    }
    public void TryActivateSkill()
    {
        if (isActivated && active is not null)
        {
            active = !active;
            if (active is true)
                ReapplySkill();
            else if (active is false)
                TakeAwaySkill();
        }

        // Guard clause for if the skill has already been activated
        if (isActivated)
            return;

        if (entityAffected.TryUseSkillPoint(skillData.SkillPointCost))
        {
            if (ActivateSkill())
            {
                skillTreeComp.SwitchOnSkill(skillData);
                SkillTreeManager.Instance.SignalPointUsage();
                isActivated = true;

                skillTreeComp.UpdateSkillTreeVisually(skillData, GetComponent<Image>());
            }
            else
                entityAffected.RefundSkillPoints(skillData.SkillPointCost);
        }
        else
        {
            // Add a pop up that the skill was unable to be unlocked
            Debug.Log("Skill was unable to be unlocked due to not enough points!");
        }
    }
    public bool ActivateSkill()
    {
        if (skillTreeComp.TryActivateSkill(skillData))
        {
            if (skillData.IsEnablingSkill)
                active = true;

            return true;
        }
        else
            return false;
    }
    private void ReapplySkill()
    {
        skillTreeComp.ReactivateSkill(skillData, GetComponent<Image>());
    }
    private void TakeAwaySkill()
    {
        skillTreeComp.DisableSkill(skillData, GetComponent<Image>());
    }
    public SkillSO GatherData()
        => skillData;
}