UnityGameProjectsCode / DiceAndMenGame / UnitSelectionControl.cs
UnitSelectionControl.cs
Raw
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class UnitSelectionControl : MonoBehaviour
{
    //hold functions for use with the unit selection screen.
    [SerializeField] private Transform selectedUnitListContent, reserveUnitListContent;
    [SerializeField] private int maxUnitSelection;
    private int currentlySelectedUnits;
    [SerializeField] private TextMeshProUGUI selectedUnitsText;
    [SerializeField] private GameObject soldierPrefab, warriorPrefab, gruntPrefab;

    private void Awake()
    {
        selectedUnitsText.text = 0 + " / " + maxUnitSelection;

        AddRosterUnitsToReserveList();
    }

    public void AddRosterUnitsToReserveList()
    {
        List<UnitStatBlock> allPlayerUnits = Singleton.Instance.ReserveUnitRoster.units;

        for (int i = 0; i < allPlayerUnits.Count; i++)
        {
            if (allPlayerUnits[i].name == "Grunt")
            {
                Instantiate(gruntPrefab, reserveUnitListContent);
            }

            if (allPlayerUnits[i].name == "Soldier")
            {
                Instantiate(soldierPrefab, reserveUnitListContent);
            }

            if (allPlayerUnits[i].name == "Warrior")
            {
                Instantiate(warriorPrefab, reserveUnitListContent);
            }
        }
    }

    public bool MoveToSelectedUnitList(GameObject targetObject)
    {
        if(currentlySelectedUnits < maxUnitSelection)
        {
            Singleton.Instance.ReserveUnitRoster.units.Remove(targetObject.GetComponent<UnitIconControl>().UnitStats);

            targetObject.transform.SetParent(selectedUnitListContent);
            currentlySelectedUnits++;
            selectedUnitsText.text = currentlySelectedUnits + " / " + maxUnitSelection;

            return false;
        }
        else
        {
            Debug.LogError("Max units already selected.");

            return true;
        }
    }

    public void CreateStatBlock(UnitStatBlock newUnit)
    {
        Singleton.Instance.UnitRoster.units.Add(newUnit);
    }
}