class BtlBit_Class {
    constructor() {
        this.picCounter = 0;
        this.befSelectNo = -1;
        this.delDatas = [];
        this.doCount = 0;   
        this.picArr = [];
        for (let i = 0; i <= 4 - 1; i++) this.picArr.push(PN_BtlBit + i);
        this.PicPath = BtlPicPath + "/BtlBit/";
        this.StartPos = new Pos2D(781, 356);
        this.PosArr = [new Pos2D(680, 324), new Pos2D(754, 225), new Pos2D(851, 264)];
        this.moveFlame = 10;
        this.textSize = 22;
        this.textZure = 20;
    }
}
let BtlBit = new BtlBit_Class();
let EnBitType = {
    Atk: "Atk",
    Def: "Def",
    Sup: "Sup"
}
class BtlBitData {
    constructor(image, _Var, _Turn) {
        this._PicNo = -1;
        this._Img = image;
        this._Var = _Var;       
        this._Turn = _Turn;     
        this._flame = 0;        
    }
}
class BtlBit_Setu {
    constructor(_Img, _Name, _Setu1, _Setu2) {
        this._Img = _Img;
        this._Name = _Name;
        this._Setu1 = _Setu1;
        this._Setu2 = _Setu2;
    }
}
let Bit_Setumei = [
    new BtlBit_Setu(EnBitType.Prt, "プロトフェアリー", "能力なし", ""),
    new BtlBit_Setu(EnBitType.Atk, "攻撃型フェアリー", "ターン終了時にランダムな", "敵にVar点ダメージを与える"),
    new BtlBit_Setu(EnBitType.Def, "防御型フェアリー", "ターン終了時に防御値を", "Var点得る。"),
    new BtlBit_Setu(EnBitType.Sup, "支援型フェアリー", "ターン終了時に体力を", "Var点回復させる")
];
Game_Interpreter.prototype.AllBitDelete = function () {
    N_Sprite_DLayer = 1;
    NPlayer[0]._BtlBit = []; 
    this.DelSpriteSpan(PN_BtlBit , PN_BtlBitTurn + 3); 
    N_Sprite_DLayer = 0;
}
Game_Interpreter.prototype.BtlBitUpdate = function () {
    N_Sprite_DLayer = 1;
    for (let i = 0; i <= BtlBit.delDatas.length - 1; i++) {
        BtlBit.delDatas[i]._flame--;
        if (BtlBit.delDatas[i]._flame <= 0) {
            this.DelSprite(BtlBit.delDatas[i]._PicNo);
            BtlBit.delDatas.splice(i, 1);
            i--;
        }
    }
    let usePicArr = [0,1,2,3];
    for (let i = 0; i <= NPlayer[0]._BtlBit.length - 1; i++) {
        let delPicNo = NPlayer[0]._BtlBit[i]._PicNo - PN_BtlBit;
        let delIndex = usePicArr.indexOf(delPicNo);
        if (delIndex != -1) usePicArr.splice(delIndex, 1);
    }
    if (usePicArr.length == 0) usePicArr.push(0); 
    D_Text_Cng_font = "Makinas"; 
    for (let i = 0; i <= NPlayer[0]._BtlBit.length - 1; i++) {
        if (NPlayer[0]._BtlBit[i]._flame == 0) { 
            NPlayer[0]._BtlBit[i]._PicNo = PN_BtlBit + usePicArr[0];
            this.PlaySpriteAnimeC(NPlayer[0]._BtlBit[i]._PicNo, BtlBit.PicPath + NPlayer[0]._BtlBit[i]._Img, 10, 3,
                BtlBit.PosArr[0]._x, BtlBit.PosArr[0]._y, 0);
            this.MoveSprite(NPlayer[0]._BtlBit[i]._PicNo, BtlBit.PosArr[0]._x, BtlBit.PosArr[0]._y, BtlBit.moveFlame);
            this.SpriteStrC(NPlayer[0]._BtlBit[i]._PicNo + 4, "\\oc[rgb(0,0,0)]" + NPlayer[0]._BtlBit[i]._Turn, BtlBit.textSize,
                BtlBit.PosArr[0]._x + BtlBit.textZure, BtlBit.PosArr[0]._y + BtlBit.textZure, 0);
            this.MoveSprite(NPlayer[0]._BtlBit[i]._PicNo + 4, true, true, BtlBit.moveFlame);
            this.PlayPtcPic("BtlBitOn", BtlBit.PosArr[0]._x, BtlBit.PosArr[0]._y);
            EasingStr = "easeOutQuad";
            for (let j = 0; j <= NPlayer[0]._BtlBit.length - 1; j++) {
                switch (j) {
                    case 1:
                        this.MoveSprite(NPlayer[0]._BtlBit[j]._PicNo, BtlBit.PosArr[1]._x, BtlBit.PosArr[1]._y, BtlBit.moveFlame);
                        this.MoveSprite(NPlayer[0]._BtlBit[j]._PicNo + 4,
                            BtlBit.PosArr[1]._x + BtlBit.textZure, BtlBit.PosArr[1]._y + BtlBit.textZure, BtlBit.moveFlame);
                        break;
                    case 2:
                        this.MoveSprite(NPlayer[0]._BtlBit[j]._PicNo, BtlBit.PosArr[2]._x, BtlBit.PosArr[2]._y, BtlBit.moveFlame);
                        this.MoveSprite(NPlayer[0]._BtlBit[j]._PicNo + 4,
                            BtlBit.PosArr[2]._x + BtlBit.textZure, BtlBit.PosArr[2]._y + BtlBit.textZure, BtlBit.moveFlame);
                        break;
                    case 3: 
                        this.MoveSprite(NPlayer[0]._BtlBit[j]._PicNo, BtlBit.StartPos._x, BtlBit.StartPos._y, BtlBit.moveFlame, 0);
                        this.MoveSprite(NPlayer[0]._BtlBit[j]._PicNo + 4,
                            BtlBit.StartPos._x + BtlBit.textZure, BtlBit.StartPos._y + BtlBit.textZure, BtlBit.moveFlame, 0);
                        break;
                }
            }
        }
        while (NPlayer[0]._BtlBit.length >= 4) {
            NPlayer[0]._BtlBit.pop();
        }
        for (let j = 0; j <= NPlayer[0]._BtlBit.length - 1; j++) {
            NPlayer[0]._BtlBit[j]._flame++;
        }
    }
    if (BtlData.Mode == EnBtlMode.CardSelect) {
        let _CNo = overPointerSpCkArr(BtlBit.picArr);
        if (_CNo != -1) {
            for (let i = 0; i <= NPlayer[0]._BtlBit.length - 1; i++) {
                if (NPlayer[0]._BtlBit[i]._PicNo == _CNo) {
                    if (i != BtlBit.befSelectNo) {
                        BtlBit.befSelectNo = i;
                        this.BitStateViews(i);
                    }
                }
            }
        } else {
            if (BtlBit.befSelectNo != -1) {
                this.DelSpriteSpan(PN_SetuStateWaku, PN_BNameStr + 3);
                BtlBit.befSelectNo = -1;
            }
        }
    } else {
        if (BtlBit.befSelectNo != -1) {
            this.DelSpriteSpan(PN_SetuStateWaku, PN_BNameStr + 3);
            BtlBit.befSelectNo = -1;
        }
    }
    D_Text_Cng_font = ""; 
    N_Sprite_DLayer = 0;
}
Game_Interpreter.prototype.BtlBit_PLTurnEnd = function () {
    BtlBit.doCount = 0;
}
Game_Interpreter.prototype.BtlBit_PLTurnEnd_Update = function () {
    if (CardSelecter_Endflame == 1) {
        if (NPlayer[0]._BtlBit.length > BtlBit.doCount) {
            N_Sprite_DLayer = 1; 
            this.BtlBit_Do(); 
            CardSelecter_Endflame = 20;
            BtlBit.doCount++;
            N_Sprite_DLayer = 0;
        } else {
            N_Sprite_DLayer = 1;
            for (let i = 0; i <= NPlayer[0]._BtlBit.length - 1; i++) {
                if (NPlayer[0]._BtlBit[i]._Turn <= 0) {
                    this.MoveSprite(NPlayer[0]._BtlBit[i]._PicNo, true, true, 5, 0);
                    this.MoveSprite(NPlayer[0]._BtlBit[i]._PicNo + 4, true, true, 5, 0);
                    let ckSprite = GetSpriteData(NPlayer[0]._BtlBit[i]._PicNo);
                    this.PlayPtcPic("BtlBitOn", ckSprite._x, ckSprite._y);
                    NPlayer[0]._BtlBit.splice(i, 1);
                    i--;
                }
            }
            N_Sprite_DLayer = 0;
        }
    }
}
class BitDelData {
    constructor(picNo, flame) {
        this._PicNo = picNo;
        this._flame = flame;
    }
}
Game_Interpreter.prototype.BtlBit_突撃 = function (moveFlg) {
    N_Sprite_DLayer = 1;
    let ckTurn = 999;
    let ck_i = -1;
    for (let i = 0; i <= NPlayer[0]._BtlBit.length - 1; i++) {
        if (NPlayer[0]._BtlBit[i]._Turn <= ckTurn) {
            ckTurn = NPlayer[0]._BtlBit[i]._Turn;
            ck_i = i;
        }
    }
    if (ck_i != -1) {
        let atkENo = GetTargetArr_Enemy距離()[0]; 
        let DelBitPicNo = NPlayer[0]._BtlBit[ck_i]._PicNo;
        let EmPsX = NEnemy[atkENo]._screenX;
        let EmPsY = NEnemy[atkENo]._screenY;
        if (moveFlg) {
            this.MoveSprite(DelBitPicNo, EmPsX, EmPsY, 10);
        } else {
            this.MoveSprite(DelBitPicNo, true, true, 10 , 0);
        }
        BtlBit.delDatas.push(new BitDelData(DelBitPicNo, 10));
        this.DelSprite(DelBitPicNo + 4);
        NPlayer[0]._BtlBit.splice(ck_i, 1);
    }
    N_Sprite_DLayer = 0;
}
Game_Interpreter.prototype.BtlBit_Do = function () {
    D_Text_Cng_font = "Makinas"; 
    let ckSprite = GetSpriteData(NPlayer[0]._BtlBit[BtlBit.doCount]._PicNo);
    NPlayer[0]._BtlBit[BtlBit.doCount]._Turn--;
    this.SpriteStrC(NPlayer[0]._BtlBit[BtlBit.doCount]._PicNo + 4,
        "\\oc[rgb(0,0,0)]" + NPlayer[0]._BtlBit[BtlBit.doCount]._Turn, BtlBit.textSize,
        ckSprite._x + BtlBit.textZure, ckSprite._y + BtlBit.textZure);
    switch (NPlayer[0]._BtlBit[BtlBit.doCount]._Img) {
        case EnBitType.Atk:
            this.PlayPtcPic("BtlBitAtk", ckSprite.x, ckSprite.y); 
            let setDmage = NPlayer[0]._BtlBit[BtlBit.doCount]._Var;
            let setENo = GetENo_Ramdom();
            SetEnemyDmgStac(DType.Dmg, setENo, 10, setDmage);     
            //this.PlayAnimeTopBtlChara(setENo, 18, 0, 0); //銃アニメ(18)を流す
            this.PlayPtcAnimeEnemy("BtlBitAtk" , false , setENo , 0 , 0);
            break;
        case EnBitType.Def:
            break;
        case EnBitType.Sup:
            break;
    }
    D_Text_Cng_font = ""; 
}
Game_Interpreter.prototype.BtlBit_効果複製 = function () {
    let CpyArr = CardSkills.slice();
    for (let i = 0; i <= NPlayer[0]._BtlBit.length - 1; i++) {
        for (let j = 0; j <= CpyArr.length - 1; j++) {
            CardSkills.push(CpyArr[j]);
        }
    }
}
let _BitFontSize = 16;
Game_Interpreter.prototype.BitStateViews = function (bitNo) {
    D_Text_Cng_font = "Makinas";
    let ckSprite = GetSpriteData(NPlayer[0]._BtlBit[bitNo]._PicNo);
    let setX = ckSprite._x - 101;
    let setY = ckSprite._y - 149;
    this.SetSprite(PN_SetuStateWaku, BtlPicPath + "PN_SetuStateWaku", setX, setY);
    this.SetSpriteCellC(PN_BBitPict, BtlBit.PicPath + NPlayer[0]._BtlBit[bitNo]._Img, 10, 1, setX + 34, setY + 50);
    let setuData = null;
    for (let i = 0; i <= Bit_Setumei.length - 1; i++) {
        if (Bit_Setumei[i]._Img == NPlayer[0]._BtlBit[bitNo]._Img) {
            setuData = Bit_Setumei[i];
        }
    }
    let _WCStr = "\\oc[rgb(0,0,0)]";
    this.SpriteStr(PN_BNameStr + 0, _WCStr + setuData._Name, _BitFontSize, setX + 73, setY + 27);
    this.SpriteStrC(PN_BNameStr + 1, _WCStr + "（残り" + NPlayer[0]._BtlBit[bitNo]._Turn + "ターン）", _BitFontSize,
        setX + 129, setY + 56);
    this.SpriteStr(PN_BNameStr + 2, _WCStr + setuData._Setu1.replace("Var", NPlayer[0]._BtlBit[bitNo]._Var), _BitFontSize,
        setX + 9, setY + 79);
    this.SpriteStr(PN_BNameStr + 3, _WCStr + setuData._Setu2.replace("Var", NPlayer[0]._BtlBit[bitNo]._Var), _BitFontSize,
        setX + 9, setY + 99);
    D_Text_Cng_font = "";
}
Game_Interpreter.prototype.Bit_TurnCng = function (value) {
    N_Sprite_DLayer = 1;
    D_Text_Cng_font = "Makinas"; 
    for (let i = 0; i <= NPlayer[0]._BtlBit.length - 1; i++) {
        NPlayer[0]._BtlBit[i]._Turn += value;
        let ckSprite = GetSpriteData(NPlayer[0]._BtlBit[i]._PicNo);
        this.SpriteStrC(NPlayer[0]._BtlBit[i]._PicNo + 4,
            "\\oc[rgb(0,0,0)]" + NPlayer[0]._BtlBit[i]._Turn, BtlBit.textSize,
            ckSprite._x + BtlBit.textZure, ckSprite._y + BtlBit.textZure);
        this.PlayPtcPic("BtlBitAtk", ckSprite.x, ckSprite.y); 
    }
    D_Text_Cng_font = ""; 
    N_Sprite_DLayer = 0;
}
Game_Interpreter.prototype.Bit_VarCng = function (value) {
    N_Sprite_DLayer = 1;
    D_Text_Cng_font = "Makinas"; 
    for (let i = 0; i <= NPlayer[0]._BtlBit.length - 1; i++) {
        NPlayer[0]._BtlBit[i]._Var += value;
        let _CkSp = GetSpriteData(NPlayer[0]._BtlBit[i]._PicNo);
        this.PlayPtcPic("BtlBitAtk", _CkSp.x, _CkSp.y); 
    }
    D_Text_Cng_font = ""; 
    N_Sprite_DLayer = 0;
}
