let EmTurnFlame = 0;    
let _TnEmNo = 0; 
let _SWCngFlg = false;  
let _CmnGoFlg = false;  
let _EmPicNo = -1; 
let _NowTurnEmNo = 0; 
let _TnSkillNo = -1;
let _BtlEnemyTurnSpineFlg = null;
let BtlEnemyCellDmgFlg = false;
Game_Interpreter.prototype.BtlEnemyTurn_Update = function () { 
    //１．最初に点滅させる(5フレ毎)
    if (EmTurnFlame == 0) {
        _EmPicNo = -1;
        _NowTurnEmNo = TurnCard[0]._No;
        _TnSkillNo = -1;
        switch (befTurnType) {
            case EnBefTurn.Mikata: 
                _TnEmNo = -TurnCard[0]._No - 1;
                _EmPicNo = PN_CharacterPL + NPlayer[_TnEmNo]._PicNo;
                _BtlEnemyTurnSpineFlg = NPlayer[_TnEmNo].SpineFlg;
                break;
            case EnBefTurn.Enemy: 
                _TnEmNo = TurnCard[0]._No;
                _EmPicNo = PN_Character + NEnemy[_TnEmNo]._PicNo;
                _BtlEnemyTurnSpineFlg = NEnemy[_TnEmNo].SpineFlg;
                if (NEnemy[_TnEmNo]._hp == 0) { 
                    this.ChangePhase_TurnChange(); 
                    btlWaitFlame = 30; 
                }
                break;
            case EnBefTurn.Skill: 
                _TnSkillNo = TurnCard[0]._No;
                _TnEmNo = -1; 
                _EmPicNo = PN_CharacterPL + NPlayer[0]._PicNo;
                break;
            case EnBefTurn.EmSkill:
                break;
        }
        StateEffectFlame = 0;
    }
    if (EmTurnFlame >= 1) { 
        if (_NowTurnEmNo != TurnCard[0]._No) {
            EmTurnFlame = 0;
            if (TurnCard[0]._No == -1) { 
                this.ChangePhase_TurnChange(true); 
            } else {
                if (NEnemy[_TnEmNo]._hp == 0) {
                    this.ChangePhase_TurnChange(true); 
                }
            }
            btlWaitFlame = 30; 
            return;
        }
    }
    if (EmTurnFlame <= 15 && _EmPicNo != -1) {
        if (EmTurnFlame % 10 == 0) {
            if (!_BtlEnemyTurnSpineFlg)
                this.SpriteCngColor(_EmPicNo, [190, 190, 190, 0]);
            else
                this.SetSpineColor(_EmPicNo , 2,2,2,1);
        }
        if (EmTurnFlame % 10 == 5) {
            if (!_BtlEnemyTurnSpineFlg)
                this.SpriteCngColor(_EmPicNo, [0, 0, 0, 0]);
            else
                this.SetSpineColor(_EmPicNo , 1,1,1,1);
        }
    }
    if (EmTurnFlame >= 20) {
        if (!StateSyoriEnd) {
            if (befTurnType != EnBefTurn.Skill && befTurnType != EnBefTurn.EmSkill) {
                this.StateUpdate(TurnCard[0]._No); 
            } else {
                StateSyoriEnd = true;
            }
        } else {
            if (cardUseEffTimer == 0 && cardUseMoveTimer == 0) {
                if (UseCard_SkillCounter == 0) { 
                    if (TurnCard[0]._Type == EnemyTCType.PlSkill) {
                        this.SkillLogAdd(EnemyDrawSkillText, 1); 
                    } else {
                        this.SkillLogAdd(EnemyDrawSkillText, 2); 
                    }
                    let _skName = "";
                    if (memoSkillArr.length > 0) _skName = memoSkillArr[0];
                    SetEnemyLogText = SetEnemyLogText.replace("Name", _skName); 
                    if (SetEnemyLogText != "") this.BLogAdd(180, SetEnemyLogText);
                    switch (befTurnType) { 
                        case EnBefTurn.Mikata: 
                            GetSkillMemoData(NPlayer[_TnEmNo].enemy(), NPlayer[_TnEmNo]._UsePtnNo);
                            if (NPlayer[_TnEmNo].SpineFlg) { 
                                let memoTag = "Spine_Atk" + (NPlayer[_TnEmNo]._UsePtnNo + 1);
                                let atkAtr = MetaChecker(NPlayer[_TnEmNo].enemy() , memoTag , NPlayer[_TnEmNo].AtkStr);
                                this.SetSpineAnime(_EmPicNo, atkAtr, false, 0, 100);
                                this.AddSpineAnime(_EmPicNo, "Idl", true, 0, 100);
                                if (btl味方VoiceData.isVoice) {
                                    if (atkAtr != "") {
                                        if (btl味方VoiceData.AtkVoice.length > 0) {
                                            this.PRandVoice(btl味方VoiceData.charaName , btl味方VoiceData.AtkVoice);
                                        }
                                    } else {
                                        let voiceTag = "Vo" + (NPlayer[_TnEmNo]._UsePtnNo + 1);
                                        let voTagName = MetaChecker(NPlayer[_TnEmNo].enemy() , memoTag , false);
                                        switch (voTagName) {
                                            case "cng":
                                                this.PRandVoice(btl味方VoiceData.charaName , btl味方VoiceData.CngVoice);
                                                break;
                                            case "heal":
                                                this.PRandVoice(btl味方VoiceData.charaName , btl味方VoiceData.HealVoice);
                                                break;
                                            default:
                                                if (btl味方VoiceData.ExVoice.length > 0) {
                                                    this.PRandVoice(btl味方VoiceData.charaName , btl味方VoiceData.ExVoice);
                                                }
                                                break;
                                        }
                                    }
                                }
                            }
                            break;
                        case EnBefTurn.Enemy: 
                            GetSkillMemoData(NEnemy[_TnEmNo].enemy(), NEnemy[_TnEmNo]._UsePtnNo);
                            if (NEnemy[_TnEmNo].SpineFlg) { 
                                let memoTag = "Spine_Atk" + (NEnemy[_TnEmNo]._UsePtnNo + 1);
                                let atkAtr = MetaChecker(NEnemy[_TnEmNo].enemy() , memoTag , NEnemy[_TnEmNo].AtkStr);
                                this.SetSpineAnime(_EmPicNo, atkAtr, false, 0, 100);
                                this.AddSpineAnime(_EmPicNo, "Idl", true, 0, 100);
                            }
                            break;
                        default:
                            break;
                    }
                }
                if (UseCard_SkillCounter > CardSkills.length - 1) {
                    StateSyoriNo = 0;
                    let ckTurnNo = TurnCard[0]._No;
                    if (NEnemy.length - 1 >= ckTurnNo) {
                        if (ckTurnNo >= 0) {
                            stateData = NEnemy[ckTurnNo]._NState;
                        } else {
                            stateData = NPlayer[-ckTurnNo - 1]._NState;
                        }
                        for (let j = 0; j < stateData.length; j++) {
                            CheckStateData = $dataStates[stateData[j]._No];
                            for (let i = 0; i <= CheckStateData.traits.length - 1; i++) {
                                let stateDamage = 0;
                                switch (CheckStateData.traits[i].dataId) {
                                    case StType.CelDmg:  
                                        this.StateEffectSet(ckTurnNo);
                                        stateDamage = Math.round(stateData[j]._Turn);
                                        if (stateDamage > 0) {
                                            BtlEnemyCellDmgFlg = true;
                                            SetEnemyDmgStac(DType.Dmg, ckTurnNo, 15, stateDamage);     
                                        } else {
                                            SetEnemyDmgStac(DType.HpRecv, ckTurnNo, 15, -stateDamage);  
                                        }
                                        stateData[j]._Turn--;
                                        if (stateData[j]._Turn <= 0) {
                                            stateData.splice(j , 1); j--;
                                        }
                                        break;
                                }
                            }
                            StateSyoriNo++;
                        }
                    }
                    HandStateCng();
                    this.DefBtn_Draw(); 
                    this.EnemySkillEndPicMove(); 
                    switch (befTurnType) {
                        case EnBefTurn.Mikata: 
                        case EnBefTurn.Enemy:  
                            this.SetEnemyNextAction(TurnCard[0]._No); 
                            StateDelCheck_ENo(TurnCard[0]._No);
                            break;
                        default:
                            break;
                    }
                    this.ChangePhase_TurnChange(); 
                    btlWaitFlame = 30; 
                } else {
                    this.EnemySkillUsePicMove(); 
                }
            }
        }
    }
    if (cardUseMoveTimer == 1) {
        this.EnemySkillEffectSet();
    }
    if (cardUseEffTimer == 1) { 
        let _Skill_Id = CardSkills[UseCard_SkillCounter].id; 
        let _CkCmn = MetaChecker(CardSkills[UseCard_SkillCounter], "SetCmn");
        if (_CkCmn != "" && !_CmnGoFlg) {
            this.SetCmnEvent(_CkCmn);
            _CmnGoFlg = true; 
            UseCard_SkillCounter++;
            cardUseEffTimer--;
            return; 
        }
        let _CkSwt = MetaChecker(CardSkills[UseCard_SkillCounter], "SetSwt");
        if (_CkSwt != "" && !_SWCngFlg) {
            $gameSwitches.setValue(Number(_CkSwt), true); 
            _SWCngFlg = true; 
            UseCard_SkillCounter++;
            cardUseEffTimer--;
            return; 
        }
        damageWait = MetaChecker(CardSkills[UseCard_SkillCounter], "DmgWait", 0);
        let setStateArr = GetStateList(CardSkills[UseCard_SkillCounter]); 
        let actionENo = TurnCard[0]._No;
        let skillValue = GetSkillCalcValue(actionENo, CardSkills[UseCard_SkillCounter].id);
        if (befTurnType == EnBefTurn.Skill) actionENo = -1; 
        if (setStateArr.length > 0) {
            for (let i = 0; i <= setStateArr.length - 1; i++) {
                if (setStateArr[i]._Turn == 0) setStateArr[i]._Turn = skillValue;
                AddStateTarget(actionENo, damageWait, setStateArr[i]);
            }
        } else {
            try {
                if (_TnEmNo >= 0) {
                    if (CheckStateInType(NEnemy[_TnEmNo]._NState, StType.DefKantu)) {
                        CardKoukaArr.push(KSkill.防御貫通);
                    }
                } else {
                    if (CheckStateInType(NPlayer[-_TnEmNo - 1]._NState, StType.DefKantu)) {
                        CardKoukaArr.push(KSkill.防御貫通);
                    }
                }
            } catch (e) {
            }
            SetDmgStac_Damage(actionENo, _Skill_Id, damageWait);
        }
        UseCard_SkillCounter++; 
    }
    if (cardUseMoveTimer > 0) {
        cardUseMoveTimer--;
    }
    if (cardUseEffTimer > 0) {
        cardUseEffTimer--;
    }
    EmTurnFlame++;
}
