void WaitEndTurn()
 {
     if ((_battleState == _BattleState.Start || _battleState == _BattleState.Finish) && _waitEndTurn && _monster.ToList().Count > 0 && _hero.ToList().Count > 0)
     {
         _waitEndTurn = false;
         _battleState = _BattleState.Start;
         StartCoroutine(EndTurn());
     }
 }
        void ShowPopupText(string dmg, Vector3 pos, PopupText obj)
        {
            PopupText damage = Instantiate(obj);

            damage.transform.SetParent(GameObject.Find("Canvas").transform, false);
            damage.transform.localScale = new Vector3(1, 1, 1);
            damage.SetPopupText(dmg.ToString());
            damage.transform.position = new Vector3(pos.x, pos.y + 0.25f, pos.z);
            _battleState = _BattleState.Finish;
        }
        public IEnumerator PlayAnimation(int damage, _Model target, float delay = 0.1f)
        {
            _battleState = _BattleState.Wait;
            yield return(new WaitForSeconds(delay));

            if (target == _Model.PLAYER)
            {
                GetHeroFocus().PlayInjurious(damage);
            }
            else
            {
                GetMonFocus().PlayInjurious(damage);
            }
        }
        IEnumerator RunMonsterAI(int delay = 1)
        {
            if (_crystalMon > 0)
            {
                _battleState = _BattleState.Wait;
            }
            yield return(new WaitForSeconds(delay));

            //Debug.Log("hero count " + _hero.Count+" focus "+_heroFocus);
            if (_monster.ToList().Count > 0 && _hero.ToList().Count > 0)
            {
                _monFocus = Random.Range(0, _monster.ToList().Count);
                GetMonFocus().Attack(GetHeroFocus());
            }
        }
        void OnEnable()
        {
            Camera.main.orthographicSize = 1f;
            _monster = new List <Monster>();
            foreach (Monster mon in _mapCon.monsterList)
            {
                mon._icon = _monCom.LoadMonIcon(mon);
                _monster.Add(mon);
            }
            for (int i = _mapCon.monsterList.Length; i < 5; i++)
            {
                _monCom._monAvatarList[i].gameObject.SetActive(false);
            }
            _hero = new List <Hero>();
            for (int i = 0; i < _mapCon._teamList.Count; i++)
            {
                if (_mapCon._teamList[i].id == -1)
                {
                    _heroCom._heroAvatarList[i].gameObject.SetActive(false);
                }
                else
                {
                    Hero newHero = new Hero(i);
                    newHero.hero  = _mapCon._teamList[i];
                    newHero._icon = _heroCom.LoadHeroIcon(newHero.hero);
                    _hero.Add(newHero);
                }
            }
            _heroFocus = 0;
            _monFocus  = 0;

            _monData = new Monster[_monster.Count];
            for (int i = 0; i < _monster.Count; i++)
            {
                _monster[i].LoadSprite();
                _monData[i] = _monster[i];
            }
            _heroData = new Hero[_hero.Count];
            for (int i = 0; i < _hero.Count; i++)
            {
                _hero[i].LoadSprite();
                _heroData[i] = _hero[i];
            }

            UpdateMonsterHP();
            _bgSprite = _core._bgList[Random.Range(0, _core._bgList.Length)];
            transform.Find("BG").GetComponent <SpriteRenderer>().sprite      = _bgSprite;
            transform.Find("BGLeft").GetComponent <SpriteRenderer>().sprite  = _bgSprite;
            transform.Find("BGRight").GetComponent <SpriteRenderer>().sprite = _bgSprite;
            _battleState = _BattleState.Start;
            _core._heroPanel.SetActive(true);
            _isEscape    = false;
            _roundBattle = _RoundBattle.PLAYER;
            _monsterList = _core._currentMonsterBattle;
            _teamList    = _core._teamSetup[_core._currentTeamIsSelect - 1];
            _turnAround  = 0;
            _eventAround = 0;
            _selectATKCon._blockCount = 0;
            Crystal     = 1;
            _crystalMon = 1;
            SetPanel(true);
            _selectATKCon.UpdateAttackSlot();
            _damage_of_each_hero = new int[_monData.Length, _heroData.Length];
            LoadEvent();
            CreateFocusEffect(GetMonFocus()._avatar.transform);
            ShowTurnBattleNotify();
            if (_core._cutscene != null)
            {
                _core._cutscene.GetComponent <Cutscene>().TutorialPlay(_core._attackPanel.transform.Find("ActionMask").Find("GridView").GetChild(0), true,
                                                                       "ในโหมดต่อสู้นี้ถ้าเจ้ากำจัดมอนสเตอร์หมดก็จะชนะ_แต่ถ้าทีมเจ้าแพ้จะโดนดาเมจตามจำนวนมอนสเตอร์ที่เหลืออยู่บนสนาม_หากหลอดเลือดที่มุมล่างจอเหลือศูนย์เจ้าคงรู้นะว่าจะเกิดอะไรขึ้น!");
            }
        }
        public void AttackEffect(Skill skill, int damage, _Model target)
        {
            bool NoBuff = false;

            if (target == _Model.MONSTER)
            {
                if (loadEffectPlayer == null)
                {
                    loadEffectPlayer = Resources.LoadAll <GameObject>("Effects/Effect_Player/");
                }

                for (int i = 0; i < loadEffectPlayer.Length; i++)
                {
                    if (skill.skill.effect == loadEffectPlayer[i].name)
                    {
                        GameObject effect = Instantiate(loadEffectPlayer[i]);
                        if (skill.skill.effect == "Slash")
                        {
                            effect.transform.SetParent(GetMonFocus()._avatar.transform.parent);
                            effect.transform.localPosition = new Vector3(GetMonFocus()._avatar.transform.localPosition.x, GetMonFocus()._avatar.transform.localPosition.y + 0.25f, GetMonFocus()._avatar.transform.localPosition.z);
                        }
                        effect.AddComponent <ParticleDestroy>();
                        if (skill.skill.type != _Attack.BUFF)
                        {
                            StartCoroutine(PlayAnimation(damage, _Model.MONSTER, skill.skill.delay));
                            NoBuff = true;
                        }
                        break;
                    }
                }
                if (!NoBuff)
                {
                    _battleState = _BattleState.Finish;
                }
            }
            else
            {
                if (loadEffectMonster == null)
                {
                    loadEffectMonster = Resources.LoadAll <GameObject>("Effects/Effect_Monster/");
                }

                for (int i = 0; i < loadEffectMonster.Length; i++)
                {
                    if (skill.skill.effect == loadEffectMonster[i].name)
                    {
                        GameObject effect = Instantiate(loadEffectMonster[i]);
                        if (skill.skill.effect == "Slash")
                        {
                            effect.transform.SetParent(GetHeroFocus()._avatar.transform.parent);
                            effect.transform.localPosition = new Vector3(GetHeroFocus()._avatar.transform.localPosition.x, GetHeroFocus()._avatar.transform.localPosition.y + 0.25f, GetHeroFocus()._avatar.transform.localPosition.z);
                        }
                        effect.AddComponent <ParticleDestroy>();
                        if (skill.skill.type != _Attack.BUFF)
                        {
                            StartCoroutine(PlayAnimation(damage, _Model.PLAYER, skill.skill.delay));
                            NoBuff = true;
                        }
                        break;
                    }
                }
                if (!NoBuff)
                {
                    Debug.Log("end 1");
                    _waitEndTurn = true;
                }
            }
        }