Encounter / Assets / Scripts / GUIHandlers / StatusScreenHandler.cs
StatusScreenHandler.cs
Raw
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

/// <summary>
/// Handles the general status screen, shows stats, permanent modifiers, and other gizmos the player may be interested in,
/// also provides a link to the entities skill tree screen
/// </summary>
public class StatusScreenHandler : MonoBehaviour
{
    [Header("UI Components")]
    [SerializeField] private TextMeshProUGUI entityName;
    [SerializeField] private TextMeshProUGUI strengthText;
    [SerializeField] private TextMeshProUGUI intelliText;
    [SerializeField] private TextMeshProUGUI dexterityText;
    [SerializeField] private TextMeshProUGUI constiText;
    [SerializeField] private TextMeshProUGUI wisdomText;
    [SerializeField] private TextMeshProUGUI charismaText;
    [SerializeField] private TextMeshProUGUI luckText;
    [SerializeField] private TextMeshProUGUI healthStat;
    [SerializeField] private TextMeshProUGUI manaStat;
    [SerializeField] private TextMeshProUGUI armorStat;
    [SerializeField] private TextMeshProUGUI skillPointText;
    [SerializeField] private GameObject[] modSectionObjects;
    [SerializeField] private GameObject statusScreenObject;         // Reference to the general status screen object to be switched on and off when activating the skill tree
    [SerializeField] private GameObject skillTreeObject;
    [SerializeField] private GameObject skillAvailEffect;
    [SerializeField] private Transform permanentEffectTransform;
    [SerializeField] private TextMeshProUGUI entityStorySoFarText;
    [SerializeField] private TextMeshProUGUI entityStoryDescription;

    private Entity activeEntity;
    private List<GameObject> skillTrees;

    private void OnEnable()
    {
        // Retrieves the current entity component that is being shown
        activeEntity = InventoryManipulator.Instance.GetCurrentEntity();
        skillTrees = new List<GameObject>(activeEntity.GetSkillTrees());

        DisplayStats();
        DisplayModifiersForStats();

        IndicateSkillPoints();

        activeEntity.HasIntegratedLevel();
    }

    private void IndicateSkillPoints()
    {
        if (activeEntity.HasSkillPoints())
        {
            skillPointText.gameObject.SetActive(true);
            skillAvailEffect.SetActive(true);

            if (activeEntity.GetSkillPoints() is 1)
                skillPointText.text = $"{activeEntity.GetSkillPoints()} Skill Point Available";
            else
                skillPointText.text = $"{activeEntity.GetSkillPoints()} Skill Points Available";
        }
        else
        {
            skillPointText.gameObject.SetActive(false);
            skillAvailEffect.SetActive(false);
        }
    }

    /// <summary>
    /// Displays all of the needed stats from the entity component retrieved upon enable
    /// </summary>
    private void DisplayStats()
    {
        entityName.text             = activeEntity.GetName();
        entityStorySoFarText.text   = $"{activeEntity.GetName()}'s Story so far...";

        StorySoFarGeneration();

        healthStat.text             = activeEntity.RetrieveMaxHealth().ToString();
        manaStat.text               = activeEntity.RetrieveMaxMana().ToString();
        armorStat.text              = activeEntity.RetrieveMaxArmor().ToString();

        strengthText.text           = activeEntity.GatherStats().GetBaseStat(EntityStats.Stat.Strength).ToString();
        wisdomText.text             = activeEntity.GatherStats().GetBaseStat(EntityStats.Stat.Wisdom).ToString();
        intelliText.text            = activeEntity.GatherStats().GetBaseStat(EntityStats.Stat.Intelligence).ToString();
        dexterityText.text          = activeEntity.GatherStats().GetBaseStat(EntityStats.Stat.Dexterity).ToString();
        charismaText.text           = activeEntity.GatherStats().GetBaseStat(EntityStats.Stat.Charisma).ToString();
        constiText.text             = activeEntity.GatherStats().GetBaseStat(EntityStats.Stat.Constitution).ToString();
        luckText.text               = activeEntity.GatherStats().GetBaseStat(EntityStats.Stat.Luck).ToString();
    }

    private void StorySoFarGeneration()
    {
        if (activeEntity is Player)
        {
            entityStoryDescription.text = $"As you have heard of the Hero's legacy and stories throughout the realm, you have decided to go on your own adventure. " +
                $"However, the perilous nature of adventuring has led to the demise of many. You seek to gain powerful allies and trinkets to embark on a journey " +
                $"that will be told in legends...";
        }
        else if (activeEntity.GetName().Equals("Phyll"))
        {
            entityStoryDescription.text = $"Phyll is a strange entity, many would agree from simply glancing at the sentient blade of grass. However, " +
                $"Phyll is a jolly and friendly entity in a world full of despair, making his nature of needing to help others naieve to some. " +
                $"Eventually, he met an adventurer trying to create a name for themselves and volunteered to join them in their quest to find new friends!" +
                $" The name Phyll was given to him by a long lost friend who he calls 'Thora'...";
        }
    }

    /// <summary>
    /// Checks each section of modifiers to see if they need to be activated
    /// </summary>
    private void DisplayModifiersForStats()
    {
        for (int i = 0; i < 7; ++i)
        {
            int baseStat = activeEntity.GatherStats().GetBaseStat((EntityStats.Stat)i);
            int modification = activeEntity.GatherStats().GetStat((EntityStats.Stat)i, baseStat) - Globals.GatherModifier(baseStat);

            // Positive modification
            if (modification > 0)
            {
                modSectionObjects[i].SetActive(true);

                TextMeshProUGUI additiveText = modSectionObjects[i].transform.GetChild(0).GetComponent<TextMeshProUGUI>();
                TextMeshProUGUI modText = modSectionObjects[i].transform.GetChild(1).GetComponent<TextMeshProUGUI>();

                additiveText.text = "+";
                modText.text = $"{modification}";
            }
            // Negative modification
            else if (modification < 0)
            {
                modSectionObjects[i].SetActive(true);

                TextMeshProUGUI differenceText = modSectionObjects[i].transform.GetChild(0).GetComponent<TextMeshProUGUI>();
                TextMeshProUGUI modText = modSectionObjects[i].transform.GetChild(1).GetComponent<TextMeshProUGUI>();

                differenceText.text = "-";
                modText.text = $"{modification}";
            }
            // No modification
            else
                modSectionObjects[i].SetActive(false);
        }
    }
    public async void ActivateSkillTreeScreen()
    {
        await skillTreeObject.GetComponent<SkillTreeManager>().Initialize(skillTrees, activeEntity, statusScreenObject);

        // Turn off the general stat screen while skill tree screen is active
        statusScreenObject.SetActive(false);
        skillAvailEffect.SetActive(false);
    }
    public void UpdateStatusScreen()
    {
        IndicateSkillPoints();
        DisplayModifiersForStats();
        DisplayStats();
        InventoryManipulator.Instance.AdjustItems();
    }
}