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

/// <summary>
/// The main component to the GUI prefab that links all of the necessary information about an aspect for the
/// player to gather information easily
/// </summary>
public class AspectHelperUI : MonoBehaviour
{
    [Header("GUI Components")]
    [SerializeField] private TextMeshProUGUI aspectName;                // The name location of the aspect helper
    [SerializeField] private TextMeshProUGUI aspectDesc;                // The description location of the aspect helper
    [SerializeField] private TextMeshProUGUI aspectCost;                // The cost location of the aspect helper
    [SerializeField] private TextMeshProUGUI aspectValue;               // The value location of the aspect helper
    [SerializeField] private Image aspectImage;                         // The sprite location of the aspect helper
    [SerializeField] private AutoScroller scroller;                     // The reference to the auto scroller functionality 
    [SerializeField] private GameObject textObject;                     // The reference to the text prefab for the aspect effect value listings
    [SerializeField] private Transform effectField;                     // The reference to the transform field for the helper effects to be placed

    private bool IsAbility;
    private bool IsSpell;
    private bool IsItem;

    private List<string> aspectEffectValues;
    private List<GameObject> effectObjects;

    private object Aspect;

    /// <summary>
    /// Retrieves the number of effects that are present for this aspect
    /// </summary>
    /// <returns>Effect list count</returns>
    public int GetEffectCount()
        => aspectEffectValues.Count;
    public void InjectAspectInformation(object aspectInfo)
    {
        // Clear the field of any potential garbage
        foreach (Transform child in effectField)
            Destroy(child.gameObject);

        // Determine what type the aspect helper shoudl be
        if (aspectInfo is Weapon || aspectInfo is Apparel || aspectInfo is Item || aspectInfo is Consumable)
            IsItem          = true;
        else if (aspectInfo is Ability)
            IsAbility       = true;
        else if (aspectInfo is Spell)
            IsSpell         = true;
        else
            throw new InvalidDataException($"The object that was injected into the helper shows to be not any of the four classifications. Type {typeof(object)}");

        // Fill in the information and activate the scroll functionality for the helper effects
        Aspect = aspectInfo;
        FillInformation();
        scroller.enabled = true;
    }

    private void FillInformation()
    {
        if (IsItem)
        {
            Item item                       = Aspect as Item;

            aspectName.text                 = item.ItemName;
            aspectDesc.text                 = item.ItemDescription;
            aspectImage.sprite              = item.ItemSprite;
            aspectCost.text                 = $"Cost\n{item.ItemCost}";
            aspectValue.text                = $"Value\n{item.ItemValue}";

            aspectEffectValues              = new List<string>(item.HelperEffects);
            effectObjects                   = new List<GameObject>();

            for (int i = 0; i < aspectEffectValues.Count; i++)
            {
                GameObject textField = Instantiate(textObject, effectField);
                textField.GetComponent<TextMeshProUGUI>().text = aspectEffectValues[i];
                effectObjects.Add(textField);
            }
        }
        else if (IsAbility)
        {
            Ability ability                 = Aspect as Ability;

            aspectCost.enabled              = false;
            aspectValue.enabled             = false;
            aspectName.text                 = ability.AbilityName;
            aspectDesc.text                 = ability.AbilityDescription;
            aspectImage.sprite              = ability.AbilitySprite;

            aspectEffectValues              = new List<string>(ability.HelperEffects);
            effectObjects                   = new List<GameObject>();

            for (int i = 0; i < aspectEffectValues.Count; i++)
            {
                GameObject textField = Instantiate(textObject, effectField);
                textField.GetComponent<TextMeshProUGUI>().text = aspectEffectValues[i];
                effectObjects.Add(textField);
            }
        }
        else if (IsSpell)
        {
            Spell spell                 = Aspect as Spell;

            aspectCost.enabled          = true;
            aspectCost.text             = $"Mana Cost\n{spell.SpellCost}";  
            aspectValue.enabled         = false;
            aspectName.text             = spell.SpellName;
            aspectDesc.text             = spell.SpellDescription;
            aspectImage.sprite          = spell.SpellSprite;

            aspectEffectValues          = new List<string>(spell.HelperEffects);
            effectObjects               = new List<GameObject>();

            for (int i = 0; i < aspectEffectValues.Count; i++)
            {
                GameObject textField = Instantiate(textObject, effectField);
                textField.GetComponent<TextMeshProUGUI>().text = aspectEffectValues[i];
                effectObjects.Add(textField);
            }
        }
    }
}