Encounter / Assets / Scripts / GUIHandlers / SpellGUI.cs
SpellGUI.cs
Raw
using System.Collections.Generic;
using System.ComponentModel;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using static Dice;

public class SpellGUI : MonoBehaviour
{
    [Header("UI Components")]
    [SerializeField] private Image spell_Image;
    [SerializeField] private TextMeshProUGUI spell_Cost_Text;
    [SerializeField] private TextMeshProUGUI spell_Desc_Text;
    [SerializeField] private TextMeshProUGUI spell_Dice_Text;

    /// <summary>
    /// Injects the spell information into the GUI
    /// </summary>
    /// <param name="spell">The spell to take information from</param>
    public void InjectSpellInformation(Spell spell)
    {
        // General information to take from the spell
        spell_Image.sprite          = spell.SpellSprite;
        spell_Cost_Text.text        = spell.SpellCost.ToString();
        spell_Desc_Text.text        = spell.SpellDescription;

        // If not a diceless spell indicate the dice used
        if (!spell.DicelessSpell)
        {
            spell_Dice_Text.text = string.Empty;
            Dictionary<string, int> diceCount = new Dictionary<string, int>();

            for (int i = 0; i < spell.Dice.Count; i++)
            {
                string dice = DiceFormatter(spell.Dice[i]);
                
                // Checks if a key is already present for the dice string and if not creates it or if it is increments the value
                if (!diceCount.ContainsKey(dice))
                    diceCount.Add(dice, 1);
                else
                    diceCount[dice]++;
            }

            foreach (var pair in diceCount)
                spell_Dice_Text.text += pair.Value + pair.Key + '\n';
        }
        // Otherwise indicate no dice is necessary for the spell
        else
            spell_Dice_Text.text    = "No\nDice";
    }

    private string DiceFormatter(DiceR dice)
    {
        return dice switch
        {
            DiceR.Four         => "D4",
            DiceR.Six          => "D6",
            DiceR.Eight        => "D8",
            DiceR.Ten          => "D10",
            DiceR.Twelve       => "D12",
            DiceR.Twenty       => "D20",
            DiceR.Hundred      => "D100",

            _                       => throw new InvalidEnumArgumentException(nameof(dice))
        };
    }
}