// Start is called before the first frame update
 void Start()
 {
     LoadCharacters();
     LoadAbilities();
     HideAbilitiesPanel();
     m_currFace = CombatFace.PlayerSelect;
 }
    void ResetCombatTurn()
    {
        foreach (GameObject character in m_party)
        {
            character.GetComponent <Character>().NewCombatTurn();
        }

        m_currFace = CombatFace.PlayerSelect;
    }
    void EndCombat()
    {
        HideAbilitiesPanel();

        foreach (GameObject character in m_party)
        {
            character.GetComponent <Character>().EndCombat();
        }

        m_currFace = CombatFace.PlayerSelect;

        LoadMainMap();
    }
    void ShowAbilitiesPanel()
    {
        abilitiesPanel.SetActive(true);

        foreach (Ability ability in m_selectedCharacter.abilities)
        {
            GameObject button = Instantiate(abilityButton, abilitiesPanel.transform);

            button.GetComponentInChildren <TextMeshProUGUI>().text = ability.type.ToString();

            button.GetComponent <Button>().onClick.AddListener(() =>
            {
                if (ability.damage != 0)
                {
                    if (m_selectedEnemy == null)
                    {
                        StartCoroutine(GameManager.PrintText(stateText, "You need to select a taget!"));
                        return;
                    }
                }

                m_selectedCharacter.UseAbility(ability, m_selectedEnemy);
                m_currFace = CombatFace.PlayerSelect;
                UnselectCharacter(m_selectedCharacter);
                UnselectCharacter(m_selectedEnemy);

                CheckIfCombatHasEneded();

                bool partyFinished = true;
                foreach (GameObject character in m_party)
                {
                    if (!character.GetComponent <Character>().hasAttacked)
                    {
                        partyFinished = false;
                        break;
                    }
                }
                if (partyFinished)
                {
                    m_currFace = CombatFace.AIFace;
                }

                HideAbilitiesPanel();
            });
        }
    }
    void HandleAI()
    {
        foreach (GameObject enemy in m_enemies)
        {
            Character enemyC = enemy.GetComponent <Character>();

            if (!enemyC.isDead)
            {
                Character target  = m_party[UnityEngine.Random.Range(0, m_party.Count)].GetComponent <Character>();
                Ability   ability = enemyC.abilities[UnityEngine.Random.Range(0, enemyC.abilities.Count)];

                enemyC.UseAbility(ability, target);

                //CheckIfCombatHasEneded();
            }
        }
        m_currFace = CombatFace.ResetFace;
    }
 public void EndTurn()
 {
     endTurnButton.interactable = false;
     m_currFace = CombatFace.AIFace;
 }
 void StartTurn()
 {
     endTurnButton.interactable = true;
     m_currFace = CombatFace.PlayerSelect;
 }
    void HandleCombatState()
    {
        switch (m_currFace)
        {
        default:        // Playerface is the default state
        case CombatFace.PlayerSelect:
            if (Input.GetMouseButtonUp(0))
            {
                m_hit = Physics2D.Raycast(m_cam.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

                HideAbilitiesPanel();
                try
                {
                    if (m_hit.collider.gameObject.GetComponentInParent <Player>() != null)
                    {
                        m_selectedCharacter = m_hit.collider.gameObject.GetComponentInParent <Character>();
                        if (m_selectedCharacter.hasAttacked)
                        {
                            StartCoroutine(GameManager.PrintText(stateText, "This unit has already attacked!"));
                            m_selectedCharacter = null;
                        }
                        else
                        {
                            m_currFace = CombatFace.PlayerAttack;
                            ShowAbilitiesPanel();
                            SelectCharacter(m_selectedCharacter);
                        }
                    }
                }
                catch (Exception e) { GameManager.PrintException(e); }
            }
            break;

        case CombatFace.PlayerAttack:
            if (Input.GetMouseButtonUp(0))
            {
                m_hit = Physics2D.Raycast(m_cam.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

                try
                {
                    if (m_hit.collider.gameObject.GetComponentInParent <Enemy>() != null)
                    {
                        m_selectedEnemy = m_hit.collider.gameObject.GetComponentInParent <Character>();
                        SelectCharacter(m_selectedEnemy);
                    }
                    else if (m_hit.collider.gameObject.GetComponentInParent <Character>() == m_selectedCharacter)
                    {
                        UnselectCharacter(m_selectedCharacter);
                        UnselectCharacter(m_selectedEnemy);
                        HideAbilitiesPanel();
                        m_currFace = CombatFace.PlayerSelect;
                    }
                }
                catch (Exception e) { GameManager.PrintException(e); }
            }
            break;

        case CombatFace.AIFace:
            HandleAI();
            break;

        case CombatFace.ResetFace:
            CheckIfCombatHasEneded();
            ResetCombatTurn();
            break;
        }
    }