/*:ja
 * @plugindesc 
 * @author Nupuryu
 *
 * @help 
 */
if(DbgLive2D == undefined) DbgLive2D = []; 
let L2D_Dlayer = 5;         
let L2D_LeftPos = 300;      
let L2D_RightPos = 1100;    
let PN_MdlLoadPic = 100;    
let PN_L2DView = 90;        
let L2DLayerPicCnt = 10;    
let L2D_FadeCmpTimer = 0;   
let N_CkPicContainer = [];  
let L2D_SetParams_Arr = []; 
let L2D_DelParams_Arr = []; 
class L2D_SetParams {
    constructor() {
        this._modelId = "";
        this._ids = [];
        this._values = [];
    }
}
let L2D_MdlNameCus = function(_MdlName) {
    return _MdlName;
}
let _L2d_MapMoveCnt = 0;
let _L2d_MapMoveAftCnt = 0;
let L2D_cmd201 = Game_Interpreter.prototype.command201;
Game_Interpreter.prototype.command201 = function() {
    if(_L2d_MapMoveCnt < 5) {
        _L2d_MapMoveCnt++;
        return false;
    }
    if(_L2d_MapMoveCnt == 4) {
        let _bef_DLayerId = N_Sprite_DLayer;
        N_Sprite_DLayer = L2D_Dlayer;
        for (let i = PN_L2DView; i <= L2DLayerPicCnt - 1; i++) {
            let _CkGameLive2D = L2D_GetGameLive2D_PicNo(i);
            if(_CkGameLive2D._TgtAlpha == 0) {
                DelSprite(i); 
            }
        }
        N_Sprite_DLayer = _bef_DLayerId;
    }
    _L2d_MapMoveCnt = 0;
    //L2D_cmd201.call(this);
    if (!$gameParty.inBattle() && !$gameMessage.isBusy() && _L2d_MapMoveAftCnt == 0) {
        let mapId, x, y;
        if (this._params[0] === 0) {  
            mapId = this._params[1];
            x = this._params[2];
            y = this._params[3];
        } else {  
            mapId = $gameVariables.value(this._params[1]);
            x = $gameVariables.value(this._params[2]);
            y = $gameVariables.value(this._params[3]);
        }
        $gamePlayer.reserveTransfer(mapId, x, y, this._params[4], this._params[5]);
        this.setWaitMode('transfer');
    }
    if(_L2d_MapMoveAftCnt < 5) {
        _L2d_MapMoveAftCnt++;
        return false;
    }
    _L2d_MapMoveAftCnt = 0;
    this._index++; 
    return false;
}
let DbgLive2D_Check = function (_MdlName , _BefStr) {
    if(DbgLive2D.indexOf(_MdlName) != -1) {
        //console.log(_BefStr + "::" + _MdlName);
    }
}
let L2D_KeepArr = [];   
let L2D_LoadTask = [];
let L2D_NowLoadMdlName = "";
let L2ModelLoadMdl = function(_MdlName) {
    console.log("L2ModelLoadMdl::" + _MdlName);
    DbgLive2D_Check(_MdlName , "L2ModelLoadMdl");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    if(L2D_NowLoadMdlName != ""){ 
        L2D_LoadTask.push(_MdlName);
        N_Sprite_DLayer = _bef_DLayerId;
        return;
    }
    if(L2D_KeepArr.indexOf(_MdlName) == -1){
        SetSpriteLive2D(PN_L2DView ,_MdlName , -2000 , -2000 );
        L2D_NowLoadMdlName = _MdlName; 
        L2D_KeepArr.push(_MdlName);
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2ModelSetting = function(_MdlName) {
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    DbgLive2D_Check(_MdlName , "L2ModelSetting");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    L2ModelLoadMdl(_MdlName);
    N_Sprite_DLayer = _bef_DLayerId;
}
let Live2DMdl_FastLoadFlg = false;
let L2DExp_KeepArr = []; 
class L2DExp_Keep {
    constructor(_MdlName , _ExpName , _trackNo){
        this._MdlName = _MdlName;
        this._ExpName = _ExpName;
        this._trackNo = _trackNo;
        this._DelTime = 180; 
    }
}
let Live2D_KeepLoading = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    Live2D_KeepLoading.call(this);
    if (!Live2DMdl_FastLoadFlg) {
        modelSetting.forEach(_mSetting => {
            if (_mSetting._StartLoad) {
                L2ModelLoadMdl(_mSetting._MdlName);
            }
        });
        Live2DMdl_FastLoadFlg = true;
    }
    if (L2D_NowLoadMdlName != "") {
        let _bef_DLayerId = N_Sprite_DLayer;
        N_Sprite_DLayer = L2D_Dlayer;
        if(GetSpriteData(PN_L2DView) == undefined) {
            console.log("Live2D_ロード中断::" + L2D_NowLoadMdlName);
            L2D_NowLoadMdlName = "";
        }
        if (L2D_GetLive2D(L2D_NowLoadMdlName).length > 0) {
            if(L2D_GetLive2D(L2D_NowLoadMdlName)[0] != undefined) {
                console.log("Live2D_ロード成功::" + L2D_NowLoadMdlName);
                L2D_NowLoadMdlName = "";
            }
        }
        if(L2D_NowLoadMdlName == "") {
            this.DelSprite(PN_L2DView);
            this.DelPict(PN_L2DView);
            if (L2D_LoadTask.length > 0) {
                L2ModelLoadMdl(L2D_LoadTask[0]);
                L2D_LoadTask.shift();
            }
        }
        N_Sprite_DLayer = _bef_DLayerId;
    }
    L2dSetExp_KeepFlg = true;
    for (let i = 0; i <= L2DExp_KeepArr.length - 1; i++) {
        L2DExp_KeepArr[i]._DelTime--;
        if(L2DExp_KeepArr[i]._DelTime <= 0) {
            L2DExp_KeepArr.splice(i , 1); i--;
        } else {
            if(L2dSetExp(L2DExp_KeepArr[i]._MdlName , L2DExp_KeepArr[i]._ExpName , L2DExp_KeepArr[i]._trackNo)) {
                L2DExp_KeepArr.splice(i , 1); i--;
            }
        }
    }
    L2dSetExp_KeepFlg = false;
}
let L2D_PFSMode = {
    Nomal : 1,  
    FPS30 : 2,  
    FPS60 : 3   
}
let L2D_Mode = L2D_PFSMode.Nomal;
let _NupuL2D_Game_System_initialize = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () {
    _NupuL2D_Game_System_initialize.call(this);
    this.L2D_ParamSave = [];      
    this.Live2D_ViewSave = [];    
}
class L2D_ViewSave_Data {
    constructor() {
        this._PicNo = 0;
        this._MdlName = "";
        this._IdlMtn = "";
        this._ZureX = 0;
        this._ZureY = 0;
        this._PsStr = "";
        this._ScaleX = 0;
        this._ScaleY = 0;
    }
}
Live2D_Save = function () {
    $gameSystem.Live2D_ViewSave = [];
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    for (let i = 0; i <= L2DLayerPicCnt; i++) {
        let _MdlArr = L2D_GetLive2D_PicNo(PN_L2DView + i);
        if(_MdlArr.length > 0) {
            if (GetSpriteData(PN_L2DView + i) == null) continue;
            if (_MdlArr[0].parent._mdlAlpha < 0.01) continue;
            let _SaveViewData = new L2D_ViewSave_Data(); 
            _SaveViewData._PicNo = PN_L2DView + i;
            _SaveViewData._MdlName = _MdlArr[0].parent._name;
            if (_MdlArr[0].parent._track[0] != undefined) {
                let _tCnt = _MdlArr[0].parent._track[0].list.length;
                if (_tCnt > 0) {
                    _SaveViewData._IdlMtn = _MdlArr[0].parent._track[0].list[_tCnt - 1].name
                }
            }
            _SaveViewData._PsStr = L2D_GetGameLive2D_PicNo(PN_L2DView + i)._FadePs;
            _SaveViewData._ZureX = L2D_GetGameLive2D_PicNo(PN_L2DView + i)._ZureX;
            if(_SaveViewData._ZureX == undefined) _SaveViewData._ZureX = 0;
            _SaveViewData._ZureY = L2D_GetGameLive2D_PicNo(PN_L2DView + i)._ZureY;
            if(_SaveViewData._ZureY == undefined) _SaveViewData._ZureY = 0;
            _SaveViewData._ScaleX = GetSpriteData(PN_L2DView + i)._scaleX / 100;
            _SaveViewData._ScaleY = GetSpriteData(PN_L2DView + i)._scaleY / 100;
            $gameSystem.Live2D_ViewSave.push (_SaveViewData);
        }
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let _skilLive2DLoad = []; 
Live2D_Load = function (inFrame) {
    inFrame = typeof inFrame !== 'undefined' ? inFrame : 10;
    for (let i = 0; i <= $gameSystem.Live2D_ViewSave.length - 1; i++) {
        if(_skilLive2DLoad.indexOf($gameSystem.Live2D_ViewSave[i]._MdlName) != -1) {
            $gameSystem.Live2D_ViewSave.splice(i , 1);
            i--;
            continue;
        }
        let ckMdlName = $gameSystem.Live2D_ViewSave[i]._MdlName;
        if (ckMdlName != "romasha_talk" && ckMdlName != "romasha_battale" && ckMdlName != "spina_talk") {
            $gameSystem.Live2D_ViewSave.splice(i , 1);
            i--;
            continue;
        }
        if (ckMdlName == "spina_talk" && !$gameSwitches.value(SW_スピナ操作モード)) {
            $gameSystem.Live2D_ViewSave.splice(i , 1);
            i--;
            continue;
        }
    }
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    for (let i = 0; i <= $gameSystem.L2D_ParamSave.length - 1; i++) {
        L2D_ParamSet($gameSystem.L2D_ParamSave[i][0] , $gameSystem.L2D_ParamSave[i][1] ,$gameSystem.L2D_ParamSave[i][2]);
    }
    let ViewedMdlNameArr = [];
    for (let i = 0; i <= $gameSystem.Live2D_ViewSave.length - 1; i++) {
        let _mdlName = $gameSystem.Live2D_ViewSave[i]._MdlName;
        let _PicNo = $gameSystem.Live2D_ViewSave[i]._PicNo;
        if(L2D_GetPicNo(_mdlName).length > 0) { 
            _PicNo = L2D_GetPicNo(_mdlName)[0];
        }
        if(ViewedMdlNameArr.indexOf(_mdlName) != -1) continue;
        let _MtnName = $gameSystem.Live2D_ViewSave[i]._IdlMtn;
        let _ZureX = $gameSystem.Live2D_ViewSave[i]._ZureX;
        let _ZureY = $gameSystem.Live2D_ViewSave[i]._ZureY;
        let SwPos = $gameSystem.Live2D_ViewSave[i]._PsStr;
        let _ScaleX = $gameSystem.Live2D_ViewSave[i]._ScaleX;
        let _ScaleY = $gameSystem.Live2D_ViewSave[i]._ScaleY;
        let _SetPsX = 0;
        let _ScaleXZure = -(Graphics.width / 2) * (_ScaleX - 1); 
        let _ScaleYZure = -(Graphics.height / 2) * (_ScaleY - 1); 
        switch (SwPos) {
            case "left":
                _SetPsX = L2D_LeftPos + _ZureX;
                break;
            case "right":
                _SetPsX = L2D_RightPos + _ZureX;
                break;
            case "center":
                break;
        }
        _SetPsX += _ScaleXZure;
        let _mdlSettingNo = -1;
        for (let i = 0; i <= modelSetting.length - 1; i++) {
            if(modelSetting[i]._MdlName == _mdlName) {
                _mdlSettingNo = i;
                break;
            }
        }
        if(_ZureX == undefined) {
            _ZureX = 0;
            if(_mdlSettingNo != -1) _ZureX = modelSetting[i]._ZureX;
        }
        if(_ZureY == undefined) {
            _ZureY = 0;
            if(_mdlSettingNo != -1) _ZureY = modelSetting[i]._ZureY;
        }
        let _SetPsY = 0 + _ZureY;
        _SetPsY += _ScaleYZure;
        L2dFadeMdl(_mdlName, SwPos + "in" , inFrame , _MtnName , _ScaleX , _ScaleY);
        if (_MtnName != "") {
            L2dIdleStart(_mdlName , _MtnName);
        }
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2DShowModel = function(_PicNo , _MdlName , _PosX , _PosY , _ScaleX , _ScaleY) {
    DbgLive2D_Check(_MdlName , "L2DShowModel");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    SetSpriteLive2D(_PicNo, _MdlName, _PosX, _PosY , _ScaleX , _ScaleY);
}
let L2D_SetMix = function (_MdlName , _MixVar) {
    _MixVar = typeof _MixVar !== 'undefined' ? _MixVar : 0;
    DbgLive2D_Check(_MdlName , "L2D_SetMix");
    let _CkPicArr = L2D_GetPicNo(_MdlName);
    if(_CkPicArr.length > 0) {
        L2D_GetGameLive2D_PicNo(_CkPicArr[0]).setMix(_MixVar);
    }
}
let DelMdlPict = function (_MdlName) {
    let _CkPicArr = L2D_GetPicNo(_MdlName);
    DelSprite(_CkPicArr[0]);
}
let L2D_ViewSaveArr = [];
class L2D_ViewSave {
    constructor(_MdlName , _IdleMtn){
        this._MdlName = _MdlName;
        this._IdleMtn = _IdleMtn;
        this._ExpTrack = []; 
    }
}
let L2D_ViewSave_Save = function (_MdlName) {
    _MdlName = L2D_MdlNameCus(_MdlName); 
    DbgLive2D_Check(_MdlName , "L2D_ViewSave_Save");
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _ckArr = L2D_GetGameLive2D(_MdlName);
    if(_ckArr.length > 0) {
        let _AddFlg = true;
        for (let i = 0; i <= L2D_ViewSaveArr.length - 1; i++) {
            if(L2D_ViewSaveArr[i]._MdlName == _MdlName) {
                _AddFlg = false;
                L2D_ViewSaveArr[i]._IdleMtn = _ckArr[0]._IdleMtn;
                L2D_ViewSaveArr[i]._ExpTrack = [];
                let _ckL2DArr = L2D_GetLive2D(_MdlName);
                if(_ckL2DArr.length > 0) {
                    for (let j = 0; j <= _ckL2DArr[0]._exp_tracks.length - 1; j++) {
                        if(_ckL2DArr[0]._exp_tracks[j] != null) {
                            L2D_ViewSaveArr[i]._ExpTrack.push(_ckL2DArr[0]._exp_tracks[j].Name);
                        } else {
                            L2D_ViewSaveArr[i]._ExpTrack.push(null);
                        }
                    }
                }
            }
        }
        if(_AddFlg) {
            let _setSaveData = new L2D_ViewSave(_MdlName , _ckArr[0]._IdleMtn);
            let _ckL2DArr = L2D_GetLive2D(_MdlName);
            if(_ckL2DArr.length > 0) {
                for (let i = 0; i <= _ckL2DArr[0]._exp_tracks.length - 1; i++) {
                    if(_ckL2DArr[0]._exp_tracks[i] != null) {
                        _setSaveData._ExpTrack.push(_ckL2DArr[0]._exp_tracks[i].Name);
                    } else {
                        _setSaveData._ExpTrack.push(null);
                    }
                }
            }
            L2D_ViewSaveArr.push(_setSaveData);
        }
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2TopViewModel = function() { }
let L2dFExp = ""; 
let L2dPicNo = -1; 
let L2dFadeMdl_FadeZureX = 0; 
let L2dFadeMdl_FadeZureY = 0; 
let notRomaFadeOutCount = 0;
let notSpinaFadeOutCount = 0;
let L2dFadeMdl = function (_MdlName, PosStr, SetTime, _MtnName, _ScaleX, _ScaleY, _ZureX, _ZureY , _NoMaskFlg) {
    let _FadeZureX = L2dFadeMdl_FadeZureX; L2dFadeMdl_FadeZureX = 0;
    let _FadeZureY = L2dFadeMdl_FadeZureY; L2dFadeMdl_FadeZureY = 0;
    DbgLive2D_Check(_MdlName , "L2dFadeMdl:" + _MtnName);
    if (PosStr.indexOf("out") != -1) {
        if (_MdlName == "romasha_talk") {
            notRomaFadeOutCount = 0;
        } else {
            let ckArr = L2D_GetPicNo("romasha_talk")
            if (ckArr.length > 0) {
                notRomaFadeOutCount++;
            }
        }
        if (_MdlName == "spina_talk") {
            notSpinaFadeOutCount = 0;
        } else {
            let ckArr = L2D_GetPicNo("spina_talk")
            if (ckArr.length > 0) {
                notSpinaFadeOutCount++;
            }
        }
    }
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    SetTime = typeof SetTime !== 'undefined' ? SetTime : 15;
    _MtnName = typeof _MtnName !== 'undefined' ? _MtnName : "";
    _ScaleX = typeof _ScaleX !== 'undefined' ? _ScaleX : null;
    _ScaleY = typeof _ScaleY !== 'undefined' ? _ScaleY : null;
    _ZureX = typeof _ZureX !== 'undefined' ? _ZureX : null;
    _ZureY = typeof _ZureY !== 'undefined' ? _ZureY : null;
    _NoMaskFlg = typeof _NoMaskFlg !== 'undefined' ? _NoMaskFlg : false;
    if(_ScaleX == null) _ScaleX = 1;
    if(_ScaleY == null) _ScaleY = 1;
    if(_ZureX == null) _ZureX = 0;
    if(_ZureY == null) _ZureY = 0;
    let _ckL2dDtl = null;
    modelSetting.forEach(_ckL2dDtlSet => {
        if(_ckL2dDtlSet._MdlName == _MdlName) {
            _ckL2dDtl = _ckL2dDtlSet;
        }
    });
    if(_ckL2dDtl != null) _ZureX += _ckL2dDtl._ZureX;
    if(_ckL2dDtl != null) _ZureY += _ckL2dDtl._ZureY;
    let SwPos = "";     
    let INOutStr = "";  
    let ALFlg = false;     
    let MZure = 0;
    if (PosStr.indexOf("left") != -1) {
        SwPos = "left";
        if (PosStr.indexOf("lleft") != -1) {
            MZure = -100;
        }
        if (PosStr.indexOf("rleft") != -1) {
            MZure = +100;
        }
    }
    if (PosStr.indexOf("right") != -1) {
        SwPos = "right";
        if (PosStr.indexOf("lright") != -1) {
            MZure = -100;
        }
        if (PosStr.indexOf("rright") != -1) {
            MZure = +100;
        }
    }
    if (PosStr.indexOf("center") != -1) {
        SwPos = "center"; MZure = 0;
    }
    if (PosStr.indexOf("in") != -1) INOutStr = "in";
    if (PosStr.indexOf("out") != -1) INOutStr = "out";
    if (PosStr.indexOf("AL") != -1) ALFlg = true;
    let _SetPsX = 0;
    let _SetPsY = 0;
    let _FadeZure = 0; 
    switch (SwPos) {
        case "left":
            _SetPsX = L2D_LeftPos + MZure;
            _FadeZure = -30;
            break;
        case "right":
            _SetPsX = L2D_RightPos + MZure;
            _FadeZure = 30;
            break;
        case "center":
            break;
    }
    let _CkPicArr = L2D_GetPicNo(_MdlName); 
    let _setPicNo = PN_L2DView + L2DLayerPicCnt; 
    let _ScaleXZure = -(Graphics.width / 2) * (_ScaleX - 1); 
    let _ScaleYZure = -(Graphics.height / 2) * (_ScaleY - 1); 
    let _SetZureX = _ZureX + _ScaleXZure;
    let _SetZureY = _ZureY + _ScaleYZure;
    if(L2dPicNo != -1) { 
        _setPicNo = L2dPicNo;
    }
    if(INOutStr == "in") {
        let _StartPsX = _SetPsX + _SetZureX + _FadeZure + _FadeZureX;
        if(SetTime == 0) _StartPsX = _SetPsX + _SetZureX + _FadeZureY; 
        let _StartPsY = _SetZureY;
        if(_CkPicArr.length > 0 && L2dPicNo == -1) {
            if(_CkPicArr[0] != _setPicNo) {
                L2DPicZurashi(); 
                let _CkPicArr = L2D_GetPicNo(_MdlName);
                SpriteNoCng(_CkPicArr[0], _setPicNo);
            }
            let _SpriteObj = GetSpriteData(_setPicNo);
            _SpriteObj._x = _StartPsX;
            _SpriteObj._y = _StartPsY;
            _SpriteObj._scaleX = _ScaleX * 100;
            _SpriteObj._scaleY = _ScaleY * 100;
        } else {
            if (L2dPicNo == -1) { 
                L2DPicZurashi(); 
            }
            SetSpriteLive2D(_setPicNo, _MdlName, _StartPsX, _StartPsY, _ScaleX * 100 , _ScaleY * 100);
            for (let i = 0; i <= L2D_ViewSaveArr.length - 1; i++) {
                if(L2D_ViewSaveArr[i]._MdlName == _MdlName){
                    if (L2D_ViewSaveArr[i]._IdleMtn == "") continue;
                    L2dIdleStart(_MdlName , L2D_ViewSaveArr[i]._IdleMtn);
                    for (let j = 0; j <= L2D_ViewSaveArr[i]._ExpTrack.length - 1; j++) {
                        if (L2D_ViewSaveArr[i]._ExpTrack[j] != null) {
                            L2dSetExp2(_MdlName , L2D_ViewSaveArr[i]._ExpTrack[j] , j);
                        }
                    }
                    break;
                }
            }
        }
        if (SetTime != 0) {
            L2D_GetGameLive2D_PicNo(_setPicNo).setColor(1, 1, 1, 0);
            let _TgtAlpha = 1;
            if (ALFlg) _TgtAlpha = 1 / 255 / 10;
            L2D_GetGameLive2D_PicNo(_setPicNo)._TgtAlpha= _TgtAlpha;
            L2D_GetGameLive2D_PicNo(_setPicNo)._TgtAlphaCng = _TgtAlpha / SetTime;
            L2D_GetGameLive2D_PicNo(_setPicNo)._TgtAlphaFlame = SetTime;
            MoveSprite(_setPicNo, _SetPsX + _SetZureX, _SetPsY + _SetZureY , SetTime, 255, _ScaleX * 100 , _ScaleY * 100);
        } else {
            L2D_GetGameLive2D_PicNo(_setPicNo).setColor(1, 1, 1, 1);
        }
        if (_MtnName != "") {
            if(L2D_MotionIndexGet(_MdlName , _MtnName) != -1) {
                L2D_GetGameLive2D_PicNo(_setPicNo).TrackCkSkipFlg = true;
                L2D_GetGameLive2D_PicNo(_setPicNo).setAnimation(0, [_MtnName],'sequential','none');
                Add_L2D_MotionKpArr(_MdlName , _MtnName , 0);
            }
        }
        Motion_EyeSetting(_MdlName , _MtnName);
        if(L2dFExp != "") {
            L2D_GetGameLive2D_PicNo(_setPicNo).setExpression(0 , L2dFExp);
        }
        L2D_GetGameLive2D_PicNo(_setPicNo)._FadePs = SwPos;  
        L2D_GetGameLive2D_PicNo(_setPicNo)._ZureX = _ZureX;  
        L2D_GetGameLive2D_PicNo(_setPicNo)._ZureY = _ZureY;  
        if(!_NoMaskFlg) {
            let _psWidth = Graphics.width * (_ScaleX);
            let _psHeight = Graphics.height * (_ScaleY);
            let _psLeft = (Graphics.width - _psWidth) / 2;
            let _psTop = (Graphics.height - _psHeight) / 2;
            _psLeft += _ZureX;
            _psTop += _ZureY;
            L2D_GetGameLive2D_PicNo(_setPicNo)._SetMask =
                new PIXI.Graphics()
                    .beginFill(0xffffff)
                    .drawRect (_psLeft , _psTop, _psWidth , _psHeight)
                    .endFill();
        } else {
            L2D_GetGameLive2D_PicNo(_setPicNo)._SetMask = null;
        }
    } else if (INOutStr == "out") {
        if(_CkPicArr.length > 0) {
            if(SetTime != 0) {
                MoveSprite(_CkPicArr[0], _SetPsX + _SetZureX, _SetPsY + _SetZureY , SetTime , 255, _ScaleX * 100 , _ScaleY * 100);
                L2D_GetGameLive2D_PicNo(_CkPicArr[0])._TgtAlpha = 0;
                L2D_GetGameLive2D_PicNo(_CkPicArr[0])._TgtAlphaCng = -1 / SetTime;
                L2D_GetGameLive2D_PicNo(_CkPicArr[0])._TgtAlphaFlame = SetTime;
            } else {
                DelSprite(_CkPicArr[0]);
            }
        }
    }
    if(L2D_KeepArr.indexOf(_MdlName) != -1) {
        L2D_KeepArr.push(_MdlName);
    }
    N_Sprite_DLayer = _bef_DLayerId;
    L2dFExp = ""; L2dPicNo = -1;
}
let L2dFadeMdl_Pic = function (_PicNo , _MdlName, PosStr, SetTime, _MtnName, _ScaleX, _ScaleY, _ZureX, _ZureY , _NoMaskFlg) {
    DbgLive2D_Check(_MdlName , "L2dFadeMdl_Pic:" + _MtnName);
    L2dPicNo = _PicNo;
    _MdlName = L2D_MdlNameCus(_MdlName); 
    L2dFadeMdl(_MdlName, PosStr, SetTime, _MtnName, _ScaleX, _ScaleY, _ZureX, _ZureY , _NoMaskFlg);
}
let L2D_MotionIndexGet = function (_MdlName , _MtnName) {
    DbgLive2D_Check(_MdlName , "L2D_MotionIndexGet:" + _MtnName);
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _RntVar = -1;
    try {
        let _ckLive2D_Motion = Live2D_JsonKeep["live2d data"][_MdlName + "/" + _MdlName].Motions[""];
        for (let i = 0; i <= _ckLive2D_Motion.length - 1; i++) {
            if(_ckLive2D_Motion[i].Name == _MtnName) {
                return  i;
            }
        }
    } catch (e) {
    }
    console.log("◆存在しないモーションを呼び出してるよ!!:" + _MdlName + ":" + _MtnName);
    return _RntVar;
}
let L2D_ScaleX = function (_MdlName, SetScale) {
    DbgLive2D_Check(_MdlName , "L2D_ScaleX");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    SetScale = typeof SetScale !== 'undefined' ? SetScale : 1;
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _CkPicArr = L2D_GetPicNo(_MdlName);
    if (_CkPicArr.length > 0) { 
        let _SpriteObj = GetSpriteData(_CkPicArr[0]);
        _SpriteObj._scaleX = SetScale * 100;
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2D_SetScale = function (_MdlName, _SetScaleX, _SetScaleY) {
    DbgLive2D_Check(_MdlName , "L2D_SetScale");
    _SetScaleX = typeof _SetScaleX !== 'undefined' ? _SetScaleX : 1;
    _SetScaleY = typeof _SetScaleY !== 'undefined' ? _SetScaleY : 1;
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _CkPicArr = L2D_GetPicNo(_MdlName);
    if (_CkPicArr.length > 0) { 
        let _SpriteObj = GetSpriteData(_CkPicArr[0]);
        _SpriteObj._scaleX = _SetScaleX * 100;
        _SpriteObj._scaleY = _SetScaleY * 100;
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2D_SetZure = function (_MdlName, _SetZureX, _SetZureY) {
    _SetZureX = typeof _SetZureX !== 'undefined' ? _SetZureX : 0;
    _SetZureY = typeof _SetZureY !== 'undefined' ? _SetZureY : 0;
    DbgLive2D_Check(_MdlName , "L2D_SetZure");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _CkPicArr = L2D_GetPicNo(_MdlName);
    if (_CkPicArr.length > 0) { 
        let _SetPsX = 0;
        let _SetPsY = 0;
        switch (L2D_GetGameLive2D_PicNo(_CkPicArr[0])._FadePs){
            case "left":
                _SetPsX = L2D_LeftPos;
                break;
            case "right":
                _SetPsX = L2D_RightPos;
                break;
            case "center":
                break;
        }
        let _SpriteObj = GetSpriteData(_CkPicArr[0]);
        _SpriteObj._x = _SetPsX + _SetZureX;
        _SpriteObj._y = _SetPsY + _SetZureY;
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2D_View_Update = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    L2D_View_Update.call(this);
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    for (let i = 0; i <= L2DLayerPicCnt; i++) {
        if(GetSpriteData(PN_L2DView + i) == null) continue;
        let _CkArr = L2D_GetLive2D_PicNo(PN_L2DView + i);
        if(_CkArr.length > 0) {
            if(L2D_GetGameLive2D_PicNo(PN_L2DView + i) == null) continue;
            if(_CkArr[0].parent._mdlAlpha < 0.00001) {
                L2D_ViewSave_Save(_CkArr[0]._data.name);
                DelSprite(PN_L2DView + i); 
            }
        }
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2DPicZurashi = function() {
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    if(GetSpriteData(PN_L2DView + 10) == null) {
    } else {
        let _PNullNo = 0;
        for (let i = L2DLayerPicCnt; i >= 1; i--) {
            if(GetSpriteData(PN_L2DView + i) == null) {
                _PNullNo = i;
                break;
            }
        }
        for (let i = _PNullNo + 1; i <= L2DLayerPicCnt; i++) {
            if(i == 1) {
                DelSprite(PN_L2DView + 0); 
            }
            if(GetSpriteData(PN_L2DView + i) != undefined){
                SpriteNoCng(PN_L2DView + i, PN_L2DView + i - 1);
            }
        }
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2dTop = function(_MdlName) {
    DbgLive2D_Check(_MdlName , "L2dTop");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    if(L2D_GetPicNo(_MdlName).length == 0){
        N_Sprite_DLayer = _bef_DLayerId;
        return false; 
    } else {
        if(L2D_GetPicNo(_MdlName) == PN_L2DView + L2DLayerPicCnt) {
            N_Sprite_DLayer = _bef_DLayerId;
            return false; 
        }
    }
    L2DPicZurashi();
    let _CkPicArr = L2D_GetPicNo(_MdlName);
    if(_CkPicArr.length > 0) {
        SpriteNoCng(_CkPicArr[0], PN_L2DView + L2DLayerPicCnt);
    }
    N_Sprite_DLayer = _bef_DLayerId;
    if (_arrL2dTopKeep.indexOf(_MdlName) == -1) {
        for (let i = 0; i < _arrL2dTopKeep.length; i++) {
            L2dTop(_arrL2dTopKeep[i]);
        }
    }
    return true;
}
let _arrL2dTopKeep = [];
let L2dTopKeep = function (_MdlName) {
    _arrL2dTopKeep.push(_MdlName);
}
let ResetL2dTopKeep = function () {
    _arrL2dTopKeep = [];
}
let L2dTalkTop = function(_MdlName) {
    DbgLive2D_Check(_MdlName , "L2dTalkTop");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    if (!L2dTop(_MdlName)) {
        return; 
    }
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    if(L2D_GetGameLive2D_PicNo(PN_L2DView + L2DLayerPicCnt) == undefined){
        N_Sprite_DLayer = _bef_DLayerId;
        return;
    }
    L2D_GetGameLive2D_PicNo(PN_L2DView + L2DLayerPicCnt).setColor(1, 1, 1);
    if (_arrL2dTopKeep.length <= 0) {
        if (_arrL2dTopKeep.indexOf(_MdlName) == -1) {
            for (let i = 0; i <= L2DLayerPicCnt - 1; i++) {
                if (GetSpriteData(PN_L2DView + i) == undefined) continue;
                if (!GetSpriteData(PN_L2DView + i)._live2d) continue;
                let live2DData = L2D_GetGameLive2D_PicNo(PN_L2DView + i);
                if (live2DData._name != _MdlName) live2DData.setColor(0.5, 0.5, 0.5);
            }
        }
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2dTalkTopAll = function() {
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    for (let i = 0; i <= L2DLayerPicCnt; i++) {
        if (GetSpriteData(PN_L2DView + i) == undefined) continue;
        if (!GetSpriteData(PN_L2DView + i)._live2d) continue;
        L2D_GetGameLive2D_PicNo(PN_L2DView + i).setColor(1, 1, 1);
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2dAllDark = function () {
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    for (let i = 0; i <= L2DLayerPicCnt; i++) {
        if (GetSpriteData(PN_L2DView + i) == undefined) continue;
        if (!GetSpriteData(PN_L2DView + i)._live2d) continue;
        L2D_GetGameLive2D_PicNo(PN_L2DView + i).setColor(0.5, 0.5, 0.5);
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2dAutoFadeOut = function (_MdlName, SetTime) {
    DbgLive2D_Check(_MdlName , "L2dAutoFadeOut");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _ZureX = 0;
    let _CkPicArr = L2D_GetPicNo(_MdlName);
    if(_CkPicArr.length > 0) {
        if (_MdlName == "romasha_talk") {
            notRomaFadeOutCount = 0;
        } else {
            let ckArr = L2D_GetPicNo("romasha_talk")
            if (ckArr.length > 0) {
                notRomaFadeOutCount++;
            }
        }
        if (_MdlName == "spina_talk") {
            notSpinaFadeOutCount = 0;
        } else {
            let ckArr = L2D_GetPicNo("spina_talk")
            if (ckArr.length > 0) {
                notSpinaFadeOutCount++;
            }
        }
        let _CkSPs = "center";
        if (L2D_GetGameLive2D_PicNo(_CkPicArr[0])._FadePs != undefined){
            _CkSPs = L2D_GetGameLive2D_PicNo(_CkPicArr[0])._FadePs;
        }
        switch(_CkSPs) {
            case "left":
                _ZureX = -30;
                break;
            case "right":
                _ZureX = +30;
                break;
            case "center":
                break;
        }
        L2D_GetGameLive2D_PicNo(_CkPicArr[0])._TgtAlpha = 0;
        L2D_GetGameLive2D_PicNo(_CkPicArr[0])._TgtAlphaCng = -1 / SetTime;
        L2D_GetGameLive2D_PicNo(_CkPicArr[0])._TgtAlphaFlame = SetTime;
        let PicNo = _CkPicArr[0];
        if(GetSpriteData(PicNo) != null){
            if(SetTime != 0) {
                let setx = GetSpriteData(PicNo)._x + _ZureX;
                let sety = GetSpriteData(PicNo)._y;
                let opi = GetSpriteData(PicNo)._opacity;
                let mx = GetSpriteData(PicNo)._scaleX;
                let my = GetSpriteData(PicNo)._scaleY;
                MoveSprite(PicNo, setx, sety, SetTime, opi, mx, my);
            } else {
                DelSprite(PicNo);
            }
        }
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2dAllFadeOut = function(SetTime) {
    SetTime = typeof SetTime !== 'undefined' ? SetTime : 15;
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    for (let i = 0; i <= L2DLayerPicCnt; i++) {
        if (GetSpriteData(PN_L2DView + i) == undefined) continue;
        if (!GetSpriteData(PN_L2DView + i)._live2d) continue;
        let _cklive2D = GetSpriteData(PN_L2DView + i)._live2d;
        L2dAutoFadeOut(_cklive2D._name , SetTime);
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2dAllDelSprite = function () {
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    DelSpriteSpan(PN_L2DView , PN_L2DView + L2DLayerPicCnt);
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2dSetExp_KeepFlg = false; 
let L2dSetExp = function(_MdlName , _ExpName , _trackNo){
    DbgLive2D_Check(_MdlName , "L2dSetExp");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    _trackNo = typeof _trackNo !== 'undefined' ? _trackNo : 0;
    let _CkMdl = L2D_GetGameLive2D(_MdlName);
    if (_CkMdl.length > 0) {
        _CkMdl[0].setExpression(_trackNo, _ExpName); 
    } else {
        if(!L2dSetExp_KeepFlg) {
            L2DExp_KeepArr.push(new L2DExp_Keep(_MdlName , _ExpName , _trackNo));
        }
        return false; 
    }
    N_Sprite_DLayer = _bef_DLayerId;
    return true; 
}
let L2dSetExp2 = function(_MdlName , _ExpName , _trackNo) { L2dSetExp(_MdlName , _ExpName , _trackNo); }
let L2dDelExp = function(_MdlName , _trackNo) {
    DbgLive2D_Check(_MdlName , "L2dDelExp");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    _trackNo = typeof _trackNo !== 'undefined' ? _trackNo : 0;
    let _CkMdl = L2D_GetLive2D(_MdlName);
    if (_CkMdl.length > 0) {
        _CkMdl[0]._animation.delExpression(_trackNo);
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2dSetMotion = function(_MdlName , _MtnName , _trackNo){
    _MdlName = L2D_MdlNameCus(_MdlName); 
    _trackNo = typeof _trackNo !== 'undefined' ? _trackNo : 0;
    DbgLive2D_Check(_MdlName , "L2dSetMotion:" + _MtnName);
    if(L2D_MotionIndexGet(_MdlName , _MtnName) == -1) return;
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _CkMdl = L2D_GetGameLive2D(_MdlName);
    if (_CkMdl.length > 0) {
        let _IdlMtnStr = "";
        let _cktrack = _CkMdl[0].track[_trackNo];
        if(_cktrack != undefined){
            if(_cktrack.continuance == "continue"){
                _IdlMtnStr = _cktrack.list[_cktrack.list.length - 1].name;
            }
        }
        Add_L2D_MotionKpArr(_MdlName , _MtnName , _trackNo); 
        if(_IdlMtnStr == "") {
            _CkMdl[0].setAnimation(_trackNo, [_MtnName],'sequential','none' , true);
        } else {
            _CkMdl[0].setAnimation(_trackNo, [_MtnName , _IdlMtnStr],'sequential','continue' , true);
        }
    } else {
        N_Sprite_DLayer = _bef_DLayerId;
        return; 
    }
    Motion_EyeSetting(_MdlName , _MtnName);
    N_Sprite_DLayer = _bef_DLayerId;
}
let Motion_EyeSetting = function (_MdlName , _MtnName) {
    _MdlName = L2D_MdlNameCus(_MdlName); 
    DbgLive2D_Check(_MdlName , "Motion_EyeSetting:" + _MtnName);
    let _MtnMdlArr = L2D_SpriteLive2D(_MdlName);
    if(_MtnMdlArr.length > 0) {
        _MtnMdlArr[0].blnk_flame = 100 + Math.floor(Math.random() * 300);
        _MtnMdlArr[0].blnk_LOpenPrm = _MtnMdlArr[0].blnk_LBaseOpenPrm;
        _MtnMdlArr[0].blnk_ROpenPrm = _MtnMdlArr[0].blnk_RBaseOpenPrm;
        for (let i = 0; i <= expSetting.length - 1; i++) {
            if(expSetting[i]._MdlName == _MdlName && expSetting[i]._MtnName == _MtnName) {
                _MtnMdlArr[0].blnk_LOpenPrm = expSetting[i]._LEyeMax;
                _MtnMdlArr[0].blnk_ROpenPrm = expSetting[i]._REyeMax;
            }
        }
    }
}
let L2D_ContinueIdleMotion = [];
let L2dIdle即CngFlg = false;
let L2dIdleStartSoku = function (_MdlName , _MtnName , _trackNo) {
    L2dIdle即CngFlg = true;
    L2dIdleStart(_MdlName , _MtnName , _trackNo)
}
let L2dIdleStart = function(_MdlName , _MtnName , _trackNo) {
    _trackNo = typeof _trackNo !== 'undefined' ? _trackNo : 0;
    _MdlName = L2D_MdlNameCus(_MdlName); 
    if (_MtnName == "") return;
    DbgLive2D_Check(_MdlName , "L2dIdleStart:" + _MtnName);
    if (L2D_MotionIndexGet(_MdlName , _MtnName) == -1) {
        return;
    }
    let _befLayer = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    try {
        let _CkMdl = L2D_GetGameLive2D(_MdlName);
        if (_CkMdl.length > 0) {
            if (!L2dIdle即CngFlg) {
                for (let i = 0; i <= L2D_MotionKpArr.length - 1; i++) {
                    if(L2D_MotionKpArr[i]._MdlName == _MdlName && L2D_MotionKpArr[i]._Track == _trackNo) {
                        L2D_ContinueIdleMotion.push(new L2D_MotionKp(_MdlName , _MtnName , _trackNo));
                        return; 
                    }
                }
            }
            _CkMdl[0].skip_IdleMtn = _CkMdl[0]._IdleMtn;
            if (L2dIdle即CngFlg) {
                _CkMdl[0].setAnimation(_trackNo, [_MtnName],'sequential','continue' , true);
            }
            else {
                _CkMdl[0].setAnimation(_trackNo, [_MtnName] , 'sequential', 'continue');
            }
            _CkMdl[0]._IdleMtn = _MtnName;
        }
    } catch (e) {
        console.log("L2dIdleStart：エラー");
    }
    finally {
        N_Sprite_DLayer = _befLayer; 
        L2dIdle即CngFlg = false;
    }
}
let L2dSetSpeed = function(_MdlName , _TimeScale) {
    _MdlName = L2D_MdlNameCus(_MdlName); 
    DbgLive2D_Check(_MdlName , "L2dSetSpeed");
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _CkMdl = L2D_GetGameLive2D(_MdlName);
    if (_CkMdl.length > 0) {
        _CkMdl[0].setTimeScale(_TimeScale);
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
class L2D_MotionKp {
    constructor(_MdlName , _Motion , _Track , _Cnt) {
        _Cnt = typeof _Cnt !== 'undefined' ? _Cnt : 2;
        this._MdlName = _MdlName;
        this._Motion = _Motion;
        this._Track = _Track;
        this._Cnt = _Cnt;
    }
}
let L2D_MotionKpArr = [];
let L2D_MotionUpdate = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    L2D_MotionUpdate.call(this);
    L2D_MotionKpArr = []; 
    for (let i = 0; i <= L2D_ContinueIdleMotion.length - 1; i++) {
        L2D_ContinueIdleMotion[i]._Cnt--;
        if(L2D_ContinueIdleMotion[i]._Cnt <= 0) {
            L2dIdleStart(L2D_ContinueIdleMotion[i]._MdlName ,
                L2D_ContinueIdleMotion[i]._Motion ,
                L2D_ContinueIdleMotion[i]._Track);
            L2D_ContinueIdleMotion.splice(i , 1);
        }
    }
}
let Add_L2D_MotionKpArr = function (_MdlName , _Motion , _Track) {
    _MdlName = L2D_MdlNameCus(_MdlName); 
    DbgLive2D_Check(_MdlName , "Add_L2D_MotionKpArr:" + _Motion);
    let _AddFlg = true;
    for (let i = 0; i <= L2D_MotionKpArr.length - 1; i++) {
        if(L2D_MotionKpArr[i]._MdlName == _MdlName) {
            L2D_MotionKpArr[i]._Motion = _Motion;
            _AddFlg = false;
        }
    }
    if (_AddFlg) {
        L2D_MotionKpArr.push(new L2D_MotionKp(_MdlName , _Motion , _Track));
    }
}
let PlayL2DAnimeName = function(_MdlName , _trackNo) {
    let l2dModel = L2D_GetLive2D(_MdlName);
    if (l2dModel.length > 0) {
        if (l2dModel[0] != undefined) {
            if (l2dModel[0]._animation._tracks.length > 0) {
                if (l2dModel[0]._animation._tracks[0]._entries.length > 0){
                    return l2dModel[0]._animation._tracks[0]._entries[0]._motion._name;
                }
            }
        }
    }
    return "";
}
let L2D_SaveSetting = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () { 
    L2D_SaveSetting.call(this);
    this.L2D_SetParams_Arr = []; 
};
let L2D_ParamSet = function(_MdlName , _paramId , _value) {
    _MdlName = L2D_MdlNameCus(_MdlName); 
    DbgLive2D_Check(_MdlName , "L2D_ParamSet");
    let _AddFlg = true;
    for (let i = 0; i <= $gameSystem.L2D_SetParams_Arr.length - 1; i++) {
        if($gameSystem.L2D_SetParams_Arr[i]._modelId == _MdlName) {
            _AddFlg = false;
            let _SetNo = $gameSystem.L2D_SetParams_Arr[i]._ids.indexOf(_paramId);
            if(_SetNo != -1){
                $gameSystem.L2D_SetParams_Arr[i]._values[_SetNo] = _value;
            } else {
                $gameSystem.L2D_SetParams_Arr[i]._ids.push(_paramId);
                $gameSystem.L2D_SetParams_Arr[i]._values.push(_value);
            }
        }
    }
    if(_AddFlg) {
        let _SetData = new L2D_SetParams();
        _SetData._modelId = _MdlName;
        _SetData._ids.push(_paramId);
        _SetData._values.push(_value);
        $gameSystem.L2D_SetParams_Arr.push(_SetData)
    }
}
let L2D_ParamSetVN = function(_MdlName , _paramId , _VNNo){
    _MdlName = L2D_MdlNameCus(_MdlName); 
    DbgLive2D_Check(_MdlName , "L2D_ParamSetVN");
    L2D_ParamSet(_MdlName , _paramId , $gameVariables.value(_VNNo));
}
let L2D_ParamSetSW = function(_MdlName , _paramId , _SWNo , _ONVar , _OFFVar){
    _MdlName = L2D_MdlNameCus(_MdlName); 
    DbgLive2D_Check(_MdlName , "L2D_ParamSetSW");
    if($gameSwitches.value(_SWNo)) {
        L2D_ParamSet(_MdlName , _paramId , _ONVar);
    } else {
        L2D_ParamSet(_MdlName , _paramId , _OFFVar);
    }
}
let L2D_SetParamsOne_Arr = [];
let L2D_ParamSetOne = function (_MdlName , _paramId , _value) {
    _MdlName = L2D_MdlNameCus(_MdlName); 
    DbgLive2D_Check(_MdlName , "L2D_ParamSetOne");
    let _AddFlg = true;
    for (let i = 0; i <= L2D_SetParamsOne_Arr.length - 1; i++) {
        if(L2D_SetParamsOne_Arr[i]._modelId == _MdlName) {
            _AddFlg = false;
            let _SetNo = L2D_SetParamsOne_Arr[i]._ids.indexOf(_paramId);
            if(_SetNo != -1){
                L2D_SetParamsOne_Arr[i]._values[_SetNo] = _value;
            } else {
                L2D_SetParamsOne_Arr[i]._ids.push(_paramId);
                L2D_SetParamsOne_Arr[i]._values.push(_value);
            }
        }
    }
    if(_AddFlg) {
        let _SetData = new L2D_SetParams();
        _SetData._modelId = _MdlName;
        _SetData._ids.push(_paramId);
        _SetData._values.push(_value);
        L2D_SetParamsOne_Arr.push(_SetData)
    }
}
let L2D_ParamSetOneVN = function(_MdlName , _paramId , _VNNo){
    DbgLive2D_Check(_MdlName , "L2D_ParamSetOneVN");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    L2D_ParamSetOne(_MdlName , _paramId , $gameVariables.value(_VNNo));
}
let L2D_ParamSetOneSW = function(_MdlName , _paramId , _SWNo , _ONVar , _OFFVar){
    _MdlName = L2D_MdlNameCus(_MdlName); 
    DbgLive2D_Check(_MdlName , "L2D_ParamSetOneSW");
    if($gameSwitches.value(_SWNo)) {
        L2D_ParamSetOne(_MdlName , _paramId , _ONVar);
    } else {
        L2D_ParamSetOne(_MdlName , _paramId , _OFFVar);
    }
}
let L2D_ParamDel = function(_MdlName , _paramId) {
    _MdlName = L2D_MdlNameCus(_MdlName); 
    for (let i = 0; i <= $gameSystem.L2D_SetParams_Arr.length - 1; i++) {
        if($gameSystem.L2D_SetParams_Arr[i]._modelId == _MdlName) {
            let _SetNo = $gameSystem.L2D_SetParams_Arr[i]._ids.indexOf(_paramId);
            if(_SetNo != -1) {
                $gameSystem.L2D_SetParams_Arr[i]._ids.splice(_SetNo , 1);
                $gameSystem.L2D_SetParams_Arr[i]._values.splice(_SetNo , 1);
            }
        }
    }
}
let Re_L2DPrmDell = function(_MdlName , _paramId) {
    DbgLive2D_Check(_MdlName , "Re_L2DPrmDell");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    L2D_ParamDel(_MdlName , _paramId);
}
let L2D_PrmDellAll = function(_MdlName) {
    DbgLive2D_Check(_MdlName , "L2D_PrmDellAll");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    for (let i = 0; i <= $gameSystem.L2D_SetParams_Arr.length - 1; i++) {
        if($gameSystem.L2D_SetParams_Arr[i]._modelId == _MdlName) {
            $gameSystem.L2D_SetParams_Arr[i]._ids = [];
            $gameSystem.L2D_SetParams_Arr[i]._values = [];
        }
    }
}
let Re_L2DPrmDellAll = function(_MdlName) {
    DbgLive2D_Check(_MdlName , "Re_L2DPrmDellAll");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    L2D_PrmDellAll(_MdlName);
}
let toString = Object.prototype.toString
let L2D_ParamGet = function(_MdlName , _paramId){
    DbgLive2D_Check(_MdlName , "L2D_ParamGet");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    try {
        let _CkMdl = L2D_GetLive2D(_MdlName);
        if(_CkMdl.length == 0) return null; 
        let _CkNo = _CkMdl[0]._animation._parameters.ids.indexOf(_paramId);
        if(_CkNo != -1) {
            return _CkMdl[0]._animation._parameters.values[_CkNo];
        } else {
            return null;
        }
    } catch (e) {
        return  null;
    }
}
class MosaicData {
    constructor(_Model , _ArtMesh , _MosPx , _ZureX , _ZureY){
        this._Model = _Model;
        this._ArtMesh = _ArtMesh;
        this._MosPx = _MosPx;
        this._ZureX = _ZureX;
        this._ZureY = _ZureY;
    }
}
let L2D_MosaicDataArr = [];
let L2D_MosaicMdlAdd = function (_MdlName , _ArtMeshId , _MosPx , _ZureX , _ZureY){
    DbgLive2D_Check(_MdlName , "L2D_MosaicMdlAdd:" + _ArtMeshId);
    _MdlName = L2D_MdlNameCus(_MdlName); 
    _MosPx = typeof _MosPx !== 'undefined' ? _MosPx : 15;
    _ZureX = typeof _ZureX !== 'undefined' ? _ZureX : 0;
    _ZureY = typeof _ZureY !== 'undefined' ? _ZureY : 0;
    let _AddFlg = true;
    for (let i = 0; i <= L2D_MosaicDataArr.length - 1; i++) {
        if(L2D_MosaicDataArr[i]._Model == _MdlName && L2D_MosaicDataArr[i]._ArtMesh == _ArtMeshId) {
            L2D_MosaicDataArr[i]._MosPx = _MosPx;
        }
    }
    if(_AddFlg) {
        let _SetMos = new MosaicData(_MdlName , _ArtMeshId , _MosPx , _ZureX , _ZureY);
        L2D_MosaicDataArr.push(_SetMos);
    }
}
let L2D_MosaicSet = function(_MdlName , _ArtMeshId , _MosPx) {
    _MosPx = typeof _MosPx !== 'undefined' ? _MosPx : 15;
    DbgLive2D_Check(_MdlName , "L2D_MosaicSet:" + _ArtMeshId);
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _CkArr = L2D_GetGameLive2D(_MdlName);
    if(_CkArr.length > 0){
        if(_ArtMeshId == ""){
            _CkArr[0].setMosaic(_MosPx);
        } else {
            _CkArr[0].setMosaic(_ArtMeshId, _MosPx);
        }
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2D_MosaicGet = function (_MdlName , _ArtMeshId) {
    DbgLive2D_Check(_MdlName , "L2D_MosaicGet:" + _ArtMeshId);
    for (let i = 0; i <= L2D_MosaicDataArr.length - 1; i++) {
        if(L2D_MosaicDataArr[i]._Model == _MdlName && 
           L2D_MosaicDataArr[i]._ArtMesh == _ArtMeshId) {
            return L2D_MosaicDataArr[i];
        }
    }
}
let L2D_SePlayEvArr = [];    
let L2D_SePlayEvArr_Update = Game_Interpreter.prototype.NUpdateSc;
let L2DSePath = "live2D/"
Game_Interpreter.prototype.NUpdateSc = function () {
    L2D_SePlayEvArr_Update.call(this);
    for (let i = 0; i <= L2D_SePlayEvArr.length - 1; i++) {
        StopVoiceFlg = false; 
        this.PlaySe(L2DSePath + L2D_SePlayEvArr[i]);
    }
    L2D_SePlayEvArr = [];
}
let L2D_VoicePlayEvArr = [];    
let L2D_VoicePlayEvArr_Update = Game_Interpreter.prototype.NUpdateSc;
let L2DVoiceSeFlg = false;
/** @type {Live2DOffVoiceData[]} */
let Live2DOffVoices = [];
class Live2DOffVoiceData {
    constructor(voiceName , removeFrame) {
        this.voiceName = voiceName;
        this.removeFrame = removeFrame;
    }
}
Game_Interpreter.prototype.SetLive2DOffVoice = function ( voiceName , removeFrame) {
    Live2DOffVoices.push(new Live2DOffVoiceData(voiceName , removeFrame));
}
Game_Interpreter.prototype.ResetLive2DOffVoice = function () {
    Live2DOffVoices = [];
}
let skipL2DVoiceFrame = 0;
Game_Interpreter.prototype.NUpdateSc = function () {
    L2D_VoicePlayEvArr_Update.call(this);
    if (skipL2DVoiceFrame > 0) skipL2DVoiceFrame--;
    for (let i = 0; i < Live2DOffVoices.length; i++) {
        Live2DOffVoices[i].removeFrame--;
        if (Live2DOffVoices[i].removeFrame <= 0) {
            Live2DOffVoices.splice(i, 1);
            i--;
        }
    }
    for (let i = 0; i <= L2D_VoicePlayEvArr.length - 1; i++) {
        let _VoiceChara = L2D_VoicePlayEvArr[i][0];
        let _VoiceSe = L2D_VoicePlayEvArr[i][1];
        let _OffVoiceFlg = false;
        for (let j = 0; j < Live2DOffVoices.length; j++) {
            if (Live2DOffVoices[j].voiceName === _VoiceSe) {
                _OffVoiceFlg = true;
                break;
            }
        }
        if (_OffVoiceFlg) continue; 
        if (skipL2DVoiceFrame > 0) continue; 
        this.PVoice(_VoiceChara , _VoiceSe);
    }
    L2D_VoicePlayEvArr = [];
}
let L2D_CmnEvArr = [];    
let L2D_CmnEvMdl = "";    
let L2D_CmnEvArr_Update = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    L2D_CmnEvArr_Update.call(this);
    for (let i = 0; i <= L2D_CmnEvArr.length - 1; i++) {
        this.SetCmnEventSc(L2D_CmnEvArr[i][0]); 
        L2D_CmnEvMdl = L2D_CmnEvArr[i][1];      
    }
    L2D_CmnEvArr = [];
}
let N_L2D_reserveTransfer = Game_Player.prototype.reserveTransfer;
Game_Player.prototype.reserveTransfer = function (mapId, x, y, d, fadeType) {
    N_L2D_Sprite_Keep();
    if ($gameMap.mapId() != mapId) {
    }
    N_L2D_reserveTransfer.call(this, mapId, x, y, d, fadeType);
}
let _L2D_Sprite_Keep = [];
let N_L2D_Sprite_Keep = function(){
    _L2D_Sprite_Keep = [];
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    for (let i = 0; i <= L2DLayerPicCnt; i++) {
        let _SetSprite = GetSpriteData(PN_L2DView + i);
        if(_SetSprite == null) continue;
        _L2D_Sprite_Keep.push(_SetSprite);
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let N_L2D_Sprite_ArrSetting = function(_SetArr) {
    for (let i = 0; i <= _SetArr.length - 1; i++) {
        let _N_Sprite = SelSprite(_SetArr[i].No);   
        let _INNo = _GetSpriteInNo(_SetArr[i].No);
        _N_Sprite.addChildAt(_SetArr[i] , _INNo);
    }
}
let N_L2D_Scene_Map_start = Scene_Map.prototype.start;
Scene_Map.prototype.start = function() {
    N_L2D_Scene_Map_start.call(this);
    if(_L2D_Sprite_Keep.length > 0) {
        N_L2D_Sprite_ArrSetting(_L2D_Sprite_Keep);
    }
}
class L2DFlashTimer {
    constructor(mdlName , FlashTimer , FlashOnTimer , FlashOffTimer , FlashWhite){
        this.MdlName = mdlName; 
        this.FlashTimer = FlashTimer;       
        this.FlashOnTimer = FlashOnTimer;   
        this.FlashOffTimer = FlashOffTimer; 
        this.FlashWhite = FlashWhite;       //光の光量(0~255)
        this.Flame = 0;     
        this.OnFlame = 0;   
        this.OffFlame = 0;  
    }
}
let L2DFlashTimerArr = [];
let L2DFlashSet = function (_MdlName , FlashTimer , FlashOnTimer , FlashOffTimer , FlashWhite) {
    FlashTimer = typeof FlashTimer !== 'undefined' ? FlashTimer : 15;
    FlashOnTimer = typeof FlashOnTimer !== 'undefined' ? FlashOnTimer : 15;
    FlashOffTimer = typeof FlashOffTimer !== 'undefined' ? FlashOffTimer : 15;
    FlashWhite = typeof FlashWhite !== 'undefined' ? FlashWhite : 3;
    _MdlName = L2D_MdlNameCus(_MdlName); 
    DbgLive2D_Check(_MdlName , "L2DFlashSet");
    let _CkMArr = L2D_GetGameLive2D(_MdlName);
    if (_CkMArr.length == 0) return; 
    let _addFlg = true;
    for (let i = 0; i <= L2DFlashTimerArr.length - 1; i++) {
        if (_MdlName == L2DFlashTimerArr[i].MdlName) {
            _addFlg = false;
            L2DFlashTimerArr[i].MdlName = _MdlName;
            L2DFlashTimerArr[i].FlashTimer = FlashTimer;
            L2DFlashTimerArr[i].FlashOnTimer = FlashOnTimer;
            L2DFlashTimerArr[i].FlashOffTimer = FlashOffTimer;
            L2DFlashTimerArr[i].FlashWhite = FlashWhite;
        }
    }
    if (_addFlg) {
        let _SetL2DFlashTimer = new L2DFlashTimer(_MdlName , FlashTimer , FlashOnTimer , FlashOffTimer , FlashWhite)
        L2DFlashTimerArr.push(_SetL2DFlashTimer);
    }
}
let NLv2SUpdateBtl2 = Scene_Map.prototype.update;
Scene_Map.prototype.update = function () {
    NLv2SUpdateBtl2.call(this);
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    for (let i = 0; i <= L2DFlashTimerArr.length - 1; i++) {
        let _CkMArr = L2D_GetGameLive2D(L2DFlashTimerArr[i].MdlName);
        if(_CkMArr.length == 0) continue;
        if(L2DFlashTimerArr[i].OnFlame < L2DFlashTimerArr[i].FlashOnTimer){
            let _SetVar = (L2DFlashTimerArr[i].FlashWhite - 1) * (L2DFlashTimerArr[i].OnFlame / L2DFlashTimerArr[i].FlashOnTimer)
            _SetVar += 1;
            _CkMArr[0].setColor(_SetVar, _SetVar, _SetVar, 1);
            L2DFlashTimerArr[i].OnFlame++;
        }
        else if(L2DFlashTimerArr[i].Flame < L2DFlashTimerArr[i].FlashTimer) {
            let _SetVar = L2DFlashTimerArr[i].FlashWhite;
            _CkMArr[0].setColor(_SetVar, _SetVar, _SetVar, 1);
            L2DFlashTimerArr[i].Flame++;
        }
        else if(L2DFlashTimerArr[i].OffFlame < L2DFlashTimerArr[i].FlashOffTimer){
            let _SetVar = (L2DFlashTimerArr[i].FlashWhite - 1) * (L2DFlashTimerArr[i].OffFlame / L2DFlashTimerArr[i].FlashOffTimer)
            _SetVar = L2DFlashTimerArr[i].FlashWhite - _SetVar;
            _CkMArr[0].setColor(_SetVar, _SetVar, _SetVar, 1);
            L2DFlashTimerArr[i].OffFlame++;
        } else {
            _CkMArr[0].setColor(1, 1, 1, 1);
        }
    }
    N_Sprite_DLayer = _bef_DLayerId;
}
let L2D_GetPicNo = function(_MdlName) {
    DbgLive2D_Check(_MdlName , "L2D_GetPicNo");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _RtnPicArr = [];
    N_Sprite_All.some( _CkSprite => {
        for (let i = 0; i <= _CkSprite.children.length - 1; i++) {
            if(_CkSprite.children[i]._live2d == null) continue;
            if(_CkSprite.children[i]._live2d._name == _MdlName){
                _RtnPicArr.push(_CkSprite.children[i].No);
            }
        }
    });
    N_Sprite_DLayer = _bef_DLayerId;
    return _RtnPicArr;
}
let L2D_GetGameLive2D = function(_MdlName) {
    DbgLive2D_Check(_MdlName , "L2D_GetGameLive2D");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _RtnMdlArr = [];
    N_Sprite_All.some( _CkSprite => {
        for (let i = 0; i <= _CkSprite.children.length - 1; i++) {
            if(_CkSprite.children[i].dLayer != N_Sprite_DLayer) continue;
            if(_CkSprite.children[i]._live2d == null) continue;
            if(_CkSprite.children[i]._live2d._name == _MdlName){
                _RtnMdlArr.push(_CkSprite.children[i]._live2d);
            }
        }
    });
    N_Sprite_DLayer = _bef_DLayerId;
    return _RtnMdlArr;
}
let L2D_GetGameLive2D_PicNo = function(_PicNo) {
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _Value = null;
    N_Sprite_All.some( _CkSprite => {
        for (let i = 0; i <= _CkSprite.children.length - 1; i++) {
            if(_CkSprite.children[i].dLayer != N_Sprite_DLayer) continue;
            if(_CkSprite.children[i]._live2d == null) continue;
            if(_CkSprite.children[i].No == _PicNo){
                _Value = _CkSprite.children[i]._live2d; 
                return true;
            }
        }
        if(_Value != null) return true; 
    });
    N_Sprite_DLayer = _bef_DLayerId;
    return _Value;
}
let L2D_SpriteLive2D = function(_MdlName) {
    DbgLive2D_Check(_MdlName , "L2D_SpriteLive2D");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _RtnMdlArr = [];
    N_Sprite_All.some( _CkSprite => {
        for (let i = 0; i <= _CkSprite.children.length - 1; i++) {
            if(_CkSprite.children[i].dLayer != N_Sprite_DLayer) continue;
            if(_CkSprite.children[i]._live2d == null) continue;
            if(_CkSprite.children[i]._live2d._name == _MdlName){
                _RtnMdlArr.push(_CkSprite.children[i].children[0]);
            }
        }
    });
    N_Sprite_DLayer = _bef_DLayerId;
    return _RtnMdlArr;
}
let L2D_SpriteLive2D_PicNo = function(_PicNo) {
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _RtnMdlArr = [];
    N_Sprite_All.some( _CkSprite => {
        for (let i = 0; i <= _CkSprite.children.length - 1; i++) {
            if(_CkSprite.children[i].dLayer != N_Sprite_DLayer) continue;
            if(_CkSprite.children[i]._live2d == null) continue;
            if(_CkSprite.children[i].No == _PicNo) {
                if(_CkSprite.children[i].children[0].children.length > 0){
                    _RtnMdlArr.push(_CkSprite.children[i].children[0]);
                }
            }
        }
    });
    N_Sprite_DLayer = _bef_DLayerId;
    return _RtnMdlArr;
}
let L2D_GetLive2D = function(_MdlName) {
    DbgLive2D_Check(_MdlName , "L2D_GetLive2D");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _RtnMdlArr = [];
    N_Sprite_All.some( _CkSprite => {
        for (let i = 0; i <= _CkSprite.children.length - 1; i++) {
            if(_CkSprite.children[i].dLayer != N_Sprite_DLayer) continue;
            if(_CkSprite.children[i]._live2d == null) continue;
            if(_CkSprite.children[i]._live2d._name == _MdlName){
                _RtnMdlArr.push(_CkSprite.children[i].children[0].children[0]);
            }
        }
    });
    N_Sprite_DLayer = _bef_DLayerId;
    return _RtnMdlArr;
}
let L2D_GetLive2D_PicNo = function(_PicNo) {
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _RtnMdlArr = [];
    N_Sprite_All.some( _CkSprite => {
        for (let i = 0; i <= _CkSprite.children.length - 1; i++) {
            if(_CkSprite.children[i].dLayer != N_Sprite_DLayer) continue;
            if(_CkSprite.children[i]._live2d == null) continue;
            if(_CkSprite.children[i].No == _PicNo){
                if(_CkSprite.children[i].children[0].children.length > 0){
                    _RtnMdlArr.push(_CkSprite.children[i].children[0].children[0]);
                }
            }
        }
    });
    N_Sprite_DLayer = _bef_DLayerId;
    return _RtnMdlArr;
}
let L2D_GetPicNo_MdlName = function(_MdlName) {
    DbgLive2D_Check(_MdlName , "L2D_GetPicNo_MdlName");
    _MdlName = L2D_MdlNameCus(_MdlName); 
    let _bef_DLayerId = N_Sprite_DLayer;
    N_Sprite_DLayer = L2D_Dlayer;
    let _RtnNo = -1;
    N_Sprite_All.some( _CkSprite => {
        for (let i = 0; i <= _CkSprite.children.length - 1; i++) {
            if(_CkSprite.children[i].dLayer != N_Sprite_DLayer) continue;
            if(_CkSprite.children[i]._live2d == null) continue;
            if(_CkSprite.children[i]._live2d._name == _MdlName){
                _RtnNo = _CkSprite.children[i].No; 
            }
        }
    });
    N_Sprite_DLayer = _bef_DLayerId;
    return _RtnNo; 
}
