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

/// <summary>
/// Handles the GUI interface for showing party members on the left side of the screen
/// </summary>
public class PartyMemberGUI : MonoBehaviour
{
    [Header("UI Components")]
    [SerializeField] private TextMeshProUGUI allyName;                      // Ally name information
    [SerializeField] private TextMeshProUGUI allyLevel;                     // Ally level information
    [SerializeField] private TextMeshProUGUI healthPositionTextOne;         // Ally max HP information
    [SerializeField] private TextMeshProUGUI healthPositionTextTwo;         // Ally current HP information
    [SerializeField] private TextMeshProUGUI manaPositionTextOne;           // Ally max MP information
    [SerializeField] private TextMeshProUGUI manaPositionTextTwo;           // Ally current MP information
    [SerializeField] private Slider hpSlider;                               // Ally HP slider object
    [SerializeField] private Slider mpSlider;                               // Ally MP slider object
    [SerializeField] private Image allySprite;                              // Ally sprite information

    [Header("Modifier UI Components")]
    [SerializeField] private GameObject modPrefab;             // Prefab of the Modifier Object
    [SerializeField] private Transform modField;               // The field the mod prefab will be instantiated within

    private Ally allyInfo;                                     // Ally information within the GUI system to act as a reference call

    public Ally GetAllyInfo() 
        => allyInfo;
    /// <summary>
    /// When the GUI is instantiated the information is dumped into the method from the Ally information
    /// </summary>
    /// <param name="allyInformation">The collection of information about the ally</param>
    /// <returns>The instance of the GUI</returns>
    public PartyMemberGUI CallCreateGUI(Ally allyInformation)
    {
        allyInfo               = allyInformation;
        
        allyName.text          = allyInfo.GetName();
        allySprite.sprite      = allyInfo.GetSprite();
        allyLevel.text         = allyInfo.GetLevel().ToString();

        allySprite.GetComponent<IconHandler>().HandleOrientation();
        

        int maxHP = allyInfo.RetrieveMaxHealth();
        int curHP = allyInfo.RetrieveHealth();
        int maxMP = allyInfo.RetrieveMaxMana();
        int curMP = allyInfo.RetrieveMana();

        hpSlider.maxValue      = maxHP;
        mpSlider.maxValue      = maxMP;

        ShowMpAndHpStats(maxHP, curHP, maxMP, curMP);

        hpSlider.value = curHP;
        mpSlider.value = curMP;

        return this;
    }
    /// <summary>
    /// Adds the mod image and helper to the GUI of the party member
    /// </summary>
    /// <param name="mod">The modifier to input into the components for the instantiated object</param>
    public void UpdateModifiers()
    {
        foreach (KeyValuePair<Modifier, int> mod in allyInfo.GatherModList())
        {
            GameObject modRef = Instantiate(modPrefab, modField);
            modRef.GetComponent<Image>().sprite = mod.Key.ModSprite;
            modRef.GetComponent<ModifierHelper>().InjectModifier(mod.Key, mod.Value);
        }
    }
    public void RemoveModifiers()
    {
        foreach (Transform t in modField)
            Destroy(t.gameObject);
    }
    /// <summary>
    /// Caller for updating the stats of the ally when damaged or using mana
    /// </summary>
    public void UpdateStats(Ally newInfo)
    {
        allyInfo               = newInfo;

        int maxHP               = allyInfo.RetrieveMaxHealth();
        int curHP               = allyInfo.RetrieveHealth();
        int maxMP               = allyInfo.RetrieveMaxMana();
        int curMP               = allyInfo.RetrieveMana();

        allyLevel.text = allyInfo.GetLevel().ToString();

        hpSlider.maxValue      = maxHP;
        mpSlider.maxValue      = maxMP;

        ShowMpAndHpStats(maxHP, curHP, maxMP, curMP);

        hpSlider.value         = curHP;
        mpSlider.value         = curMP;
    }
    private void ShowMpAndHpStats(int maxHP, int curHP, int maxMP, int curMP)
    {
        if (Globals.CurMaxPositionSwitch) // Enabled is CUR / MAX
        {
            healthPositionTextOne.text  = curHP.ToString();
            healthPositionTextTwo.text  = maxHP.ToString();
            manaPositionTextOne.text    = curMP.ToString();
            manaPositionTextTwo.text    = maxMP.ToString();
        }
        else                              // Disabled is MAX / CUR
        {
            healthPositionTextOne.text  = maxHP.ToString();
            healthPositionTextTwo.text  = curHP.ToString();
            manaPositionTextOne.text    = maxMP.ToString();
            manaPositionTextTwo.text    = curMP.ToString();
        }
    }
    public void SwitchHPandMPStats()
        => UpdateStats(allyInfo);
}