Encounter / Assets / Scripts / GUIHandlers / HelperUI.cs
HelperUI.cs
Raw
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// UI Class for the spell text boxes that pop up when in the Magic menu during fights
/// </summary>
public class HelperUI : MonoBehaviour
{
    [Header("UI Components")]
    [SerializeField] private TextMeshProUGUI nameField;             // Reference for the Text of the spells name in the UI
    [SerializeField] private GameObject costObject;                 // Reference to the game object of the helper to be switched on
    [SerializeField] private TextMeshProUGUI spellCostField;        // Reference for the Text of the spells cost in the UI
    [SerializeField] private TextMeshProUGUI descriptionField;      // Reference for the Text of the spells description in the UI
    [SerializeField] private Image aspectSprite;                    // Reference for the Image field fo the spells Sprite in the UI

    private Spell spell;                                            // Container for the spell in the Helper UI
    private Modifier modifier;                                      // Container for the modifier in the Helper UI
    private int turnsLeft;
    /// <summary>
    /// Spell information is injected into the spell helper UI for the necessary requirements
    /// </summary>
    /// <param name="spell">Spell to show in the helper UI</param>
    public void InjectSpellInfo(Spell spell) 
        => this.spell = spell;
    /// <summary>
    /// Injects the Modifier information into the Helper UI Modifier section
    /// </summary>
    /// <param name="modifier">Modifier to show</param>
    public void InjectModifierInfo(Modifier modifier, int turnsLeft)
    {
        this.modifier = modifier;
        this.turnsLeft = turnsLeft;
    }
    private async void OnEnable()
    {
        await Info();

        if (spell is not null)
        {
            nameField.text                  = spell.SpellName;
            costObject.SetActive(true);
            spellCostField.text             = "Mana: " + spell.SpellCost.ToString();
            descriptionField.text           = spell.SpellDescription;
            aspectSprite.sprite             = spell.SpellSprite;
        }
        else if (modifier is not null)
        {
            nameField.text                  = modifier.Name;
            descriptionField.text           = modifier.Description;
            aspectSprite.sprite             = modifier.ModSprite;
            costObject.SetActive(true);
            spellCostField.text             = $"Turns Left: {this.turnsLeft}";
        }

        async Task Info()
        {
            if (spell is null && modifier is null)
                await Task.Yield();

            await Task.CompletedTask;
        }
    }
}