/*:
 * @plugindesc 
 * @author ぬぷ竜
 */
const StartCmnEv = 8;
const TitleUIPath = "pictures/UI_Title/";
let TitleFlame = 0;
let TitleSelectBtnNo = 0;
let noSaveFlg = true; 
let GaroFlg = false; 
let _SetupFlg = false; 
let _BtnX = 0; 
let _BtnY = 527 - 22; 
let SSkipFlg = false; 
let TitleSplashFrame = 0; 
let TitleSplashFlg = false; 
Game_Interpreter.prototype.NTitleUpdate = function () {
    let ImgTitleBack = "title_Back";
    switch ($gameVariables.value(VN_TitleType)) {
        case 10: 
            ImgTitleBack = "v10_Back";
            break;
    }
    if (TitleSplashFrame < 120) {
        if (TitleSplashFrame == 0) {
            this.SetPict(PN_TitleBack, TitleUIPath + "PN_TitleSplash", 0, 0 , 0);
            this.MovePict(PN_TitleBack, 0, 0, 30);
        }
        if (TitleSplashFrame == 90) {
            this.MovePict(PN_TitleBack, 0, 0, 25 , 0);
            this.SetPict(PN_TitleBack + 1, TitleUIPath + ImgTitleBack, 0, 0 , 0);
            this.MovePict(PN_TitleBack + 1, 0, 0, 30);
        }
        TitleSplashFrame++;
        return;
    }
    if (!TitleSplashFlg) {
        TitleSplashFlg = true;
        this.DelPict(PN_TitleBack);
    }
    if (!Live2DFirstLoadCompFlg) {
        this.SetPict(PN_TitleBack, TitleUIPath + ImgTitleBack, 0, 0);
        return;
    }
    ViewPicReset();
    _AutoAutoSaveFlame = SetAutoAutoSaveFlame;
    if (TitleFlame == 0) { 
        if($gameSwitches.value(SW_DbgGameCng)){
            $gamePlayer.reserveTransfer(168, 14, 14, 8, 2);
            return;
        }
        let SSkp_LoadNo = $gameVariables.value(VN_SSkpSaveNo);
        if (SSkp_LoadNo != 0) {
            _LoadSelectNo = SSkp_LoadNo;
            $gameVariables.setValue(VN_SSkpSaveNo ,0);
            this.UTA_Save();
            SaveFolCngName = "save_skip";
            this.LoadStarting();
            SSkipFlg = true;
            return;
        }
        L2ModelLoadMdl("romasha_talk")
        N_Sprite_TNo = -1; 
        this.SaveVarGet(); 
        _LoadStartingFlame = 0;
        var btnAddTxt = ""; 
        this.SetPict(PN_TitleBack, TitleUIPath + ImgTitleBack, 0, 0);
        switch ($gameVariables.value(VN_TitleType)) {
            case 0: 
                this.SetPict(PN_StartWhite, "pictures/" + "Black", 0, 0, 0); 
                this.PicAnimeCell(PN_TitleLogo, TitleUIPath + "Title_Logo_5", 5, 4, -80, -50, 255);
                this.SetPict(PN_TitleChara, TitleUIPath + "Chara_Set", -30, 0, 0);
                this.MovePict(PN_TitleChara,0,0,60);
                break;
            case 10: 
                this.SetPict(PN_TitleBack, TitleUIPath + ImgTitleBack, 0, 0);
                this.SetPict(PN_TitleChara, TitleUIPath + "v10_Top", 0, 0, 0);
                this.MovePict(PN_TitleChara,0,0,120);
                this.PlayPicAnime(PN_TitleChara + 1, TitleUIPath + "v10_PenAnime", 91, 4, 1290, 155, 0);
                this.MovePict(PN_TitleChara + 1,1290, 155,120);
                this.PicAnimeCell(PN_TitleLogo, TitleUIPath + "Title_Logo_5", 5, 4, -80, -50, 255);
                btnAddTxt = "_10";
                break;
        }
        this.SetPict(PN_StartBtn, TitleUIPath + "tBtn1a" + btnAddTxt, _BtnX + 0, _BtnY, 0);
        this.SetPict(PN_LoadBtn, TitleUIPath + "tBtn2a" + btnAddTxt, _BtnX + 0, _BtnY + (72 * 1), 0);
        noSaveFlg = true;
        for (let i = 0; i < 50; i++) {
            if (StorageManager.exists(i)) {
                noSaveFlg = false; 
                break;
            }
        }
        if (noSaveFlg) {
            this.SetPict(PN_LoadBtn, TitleUIPath + "tBtn2c", _BtnX + 0, _BtnY + (72 * 1), 0);
        }
        this.SetPict(PN_OptiBtn, TitleUIPath + "tBtn3a" + btnAddTxt, _BtnX + 0, _BtnY + (72 * 2), 0);
        this.MovePict(PN_StartBtn, true, true, 30);
        this.MovePict(PN_LoadBtn, true, true, 30);
        this.MovePict(PN_OptiBtn, true, true, 30);
        this.SetPict(PN_OpenPoint, "PN_OpenPoint", 0, 0 , 10);
        D_Text_Cng_font = "Makinas";
        this.SetPicStr(PN_T_Setu, "　　　　　　", 15, -50, -50);
        D_Text_Cng_font = "";
    }
    if (TitleFlame == 1) { 
        // CienVerStr = $dataSystem.gameTitle.replace("プリンセスシナジー", "Ci-en ");
        // this.SetPict(PN_Cien_Win, TitleUIPath + "PN_Cien_Win", 0, 0, 0);
        // this.SetPict(PN_Cien_Btn + 0, TitleUIPath + "PN_Cien_Btn1", 652, 727, 0);
        // this.SetPict(PN_Cien_Btn + 1, TitleUIPath + "PN_Cien_Btn2", 652, 727 + 39, 0);
        // this.MovePict(PN_Cien_Win, true, true, 30);
        // this.MovePict(PN_Cien_Btn + 0, true, true, 30);
        // this.MovePict(PN_Cien_Btn + 1, true, true, 30);
        // this.SetPict(PN_Cien_BgBtn, TitleUIPath + "PN_Cien_BgBtn", 1000, 745, 0);
        // this.MovePict(PN_Cien_BgBtn, true, true, 30);
        // this.SetPicStr(PN_Cien_VerTxt, CienVerStr, 25, 14, 666, 0);
        // this.MovePict(PN_Cien_VerTxt, true, true, 30);
    } 
    this.TitleEffectUpdate(); 
    if (TitleFlame > 30) { 
        this.TitleKeyUpdate(); 
    }
    TitleFlame++;
}
Game_Interpreter.prototype.TitleView = function (_SetupFlg) {
}
let _HanabiraSetter = [
];
let _Pcount = 5; 
let HanaSetFlame = 0;
let kiraCnt = 1;
let MkiraCnt = 1; let MkiraFlm = 0;
let NizTimer = 0;
let NizStart = 80;
Game_Interpreter.prototype.TitleEffectUpdate = function () {
    if (NizStart >= 0){
        if (NizStart % 4 == 0) {
            this.PicAnimeCellCng(PN_TitleLogo, Math.floor(Math.random() * 4));
        }
        NizStart--;
    } else {
        if (NizTimer <= 0) {
            NizTimer = 300; 
            this.PicAnimeCellCng(PN_TitleLogo, 4);
        } else if (NizTimer == 3) {
            this.PicAnimeCellCng(PN_TitleLogo, Math.floor(Math.random() * 4));
            if (Math.random() < 0.5) {
                NizTimer += 2;
            }
            NizTimer--;
        } else {
            NizTimer--;
        }
    }
    if (MkiraFlm <= 0){
        MkiraFlm = 80;
        this.SetPict(PN_BtnKira + (MkiraCnt - 1), TitleUIPath + "T_kirabtm" + MkiraCnt ,
         0, 80, 255);
        this.MovePict(PN_BtnKira + (MkiraCnt - 1) , 0 , 0 , 150 , 0);
        MkiraCnt++;
        if (MkiraCnt >= 4) MkiraCnt = 1;
    } else {
        MkiraFlm--;
    }
    HanaSetFlame--;
    if (HanaSetFlame < 0) {
        HanaSetFlame = 150 + Math.random() * 100;
        let _setstr = "T_kira" + kiraCnt;
        if (_HanabiraSetter.length < 9) {
            _HanabiraSetter.push([_setstr, 0,
                0.3 + Math.random() * 0.7,
                0.3 + Math.random() * 0.7,
                -(1 + Math.random()), _Pcount]);
            _Pcount++;
            if (_Pcount > 9) {
                _Pcount = 0;
            }
        }
        kiraCnt++;
        if(kiraCnt >= 4) kiraCnt = 1;
    }
    for (let _i = 0; _i <= _HanabiraSetter.length - 1; _i++) {
        let _HFlame = _HanabiraSetter[_i][1];
        _HanabiraSetter[_i][1]++; 
        let _Rad = _HFlame * (Math.PI / 180) * _HanabiraSetter[_i][3];
        this.SetPictC(PN_Hanabira + _HanabiraSetter[_i][5],
            TitleUIPath + _HanabiraSetter[_i][0],
            600 + 300 * Math.sin(_Rad) * _HanabiraSetter[_i][2]
            , 945 + (_HFlame * _HanabiraSetter[_i][4]));
    }
    for (let _i = 0; _i <= _HanabiraSetter.length - 1; _i++) {
        let _HFlame = _HanabiraSetter[_i][1];
        if (_HFlame * _HanabiraSetter[_i][4] < -1330) {
            _HanabiraSetter.splice(_i, 1);
        }
    }
}
let TitleMode = 0;
Game_Interpreter.prototype.TitleKeyUpdate = function () {
    switch (TitleMode) {
        case 0: 
            this.TitleKeyTitle();
            break;
        case 1: 
            this.TitleOptionUpdate();
            break;
        case 2: 
            this.TitleLoadUpdate();
            break;
        case 3: 
            break;
        case 4: 
            LoadModeFlg = false; 
            this.TitleStartUpdate();
            break;
    }
}
let _gstartFlame = 0;
Game_Interpreter.prototype.TitleStartUpdate = function () {
    if (_gstartFlame == 1) {
        this.First_SeedSet(); 
        AudioManager.fadeOutBgm(1); 
        this.MovePict(PN_StartEn, -1500, 0, 60);
        this.MovePict(PN_StartWhite, 0, 0, 60);
    }
    if (_gstartFlame == 30) {
        this.MovePict(PN_StartWhite, 0, 0, 30);
    }
    if (_gstartFlame == 60) {
        N_Sprite_TNo = 130; 
        $gameTemp.reserveCommonEvent(StartCmnEv);
    }
    _gstartFlame++;
}
Game_Interpreter.prototype.TitleLoadUpdate = function () {
    if (_LoadStartingFlame > 0) { 
        this.LoadStarting();
        return;
    }
    this.MenuSaveUpdate(); 
    if (Input.isTriggered('menu') || TouchInput.isCancelled()) {
        SoundManager.playCancel();
        TitleMode = 0; 
        this.Layer1_DelPicture();
        this.DelPict(PN_BlackBk);
        this.SetPict(PN_StartWhite, "pictures/" + "Black", 0, 0, 0);
    }
}
let TitleLoadClickFlg = false;
let _Title_LoadBtnClick = Game_Interpreter.prototype.LoadBtnClick;
Game_Interpreter.prototype.LoadBtnClick = function () {
    if (_LoadOKFlg) {
        TitleLoadClickFlg = true;
        SoundManager.playLoad();
        _Title_LoadBtnClick.call(this);
        this.LoadStarting(); 
    } else {
        SoundManager.playCancel();
    }
}
let _LoadAfterFrame = 0;
let _LoadStartingFlame = 0;
Game_Interpreter.prototype.LoadStarting = function () {
    Save_ZureX = 0; 
    Save_ZureY = -20; 
    _LoadAfterFrame = 60; 
    LoadModeFlg = false; 
    if (_LoadStartingFlame == 0) {
        N_Sprite_TNo = 130; 
        this.NupuGameStart(); 
        if (_LoadSelectNo == 0) {
            _LoadSelectNo = SaveSelectNo + (SaveSelPage * 12) + 1;
            if (AutoSaveViewNo != -1) _LoadSelectNo = SaveCount + 1 + AutoSaveViewNo;
        }
        SceneManager.push(Scene_Load);
    }
    _LoadStartingFlame++;
}
let LoadStarting_NUpdateSc = Game_Interpreter.prototype.NUpdateSc;
Game_Interpreter.prototype.NUpdateSc = function () {
    LoadStarting_NUpdateSc.call(this);
    if (_LoadAfterFrame > 0) _LoadAfterFrame--;
}
let _LoadSelectNo = 0; 
Scene_Load.prototype.update = function () {
    Scene_File.prototype.update.call(this);
    if (_LoadSelectNo != 0) {
        this.onSavefileOk();
        _LoadSelectNo = 0
    }
}
Scene_File.prototype.savefileId = function () {
    return _LoadSelectNo; 
};
Scene_Load.prototype.createWindowLayer = function () {
    let width = 0;
    let height = 0;
    let x = (Graphics.width - width) / 2;
    let y = (Graphics.height - height) / 2;
    this._windowLayer = new WindowLayer();
    this._windowLayer.move(x, y, width, height);
    this.addChild(this._windowLayer);
}
Scene_Load.prototype.onLoadSuccess = function () {
    this.fadeOutAll();
    this.reloadMapIfUpdated();
    SceneManager.goto(Scene_Map);
    SetSprite(PN_LoadSucTop , "PN_LoadSucTop" , 0 , 0 , 0);
    this._loadSuccess = true;
    NowLoadNo = this.savefileId(); 
    N_LoadSuccessFlg = true;
    SaveFolCngName = ""; 
    if (SkipDeckKeep.length > 0) {
        $gameSwitches.setValue(SW_ScSkipData , true);
        $gameSystem.DeckArr = JSON.parse(JSON.stringify(SkipDeckKeep));
        $gameParty._weapons = JsonEx.makeDeepCopy(savedWeapons);
        for (let i = 0; i < SkipVNKeep.length; i++) {
            $gameVariables.setValue(SkipVNKeep[i][0] ,SkipVNKeep[i][1]);
        }
        for (let i = 0; i < SkipSWKeep.length; i++) {
            $gameSwitches.setValue(SkipSWKeep[i][0] , SkipSWKeep[i][1])
        }
        //$gameParty._armors = JsonEx.makeDeepCopy(SavedSyogoed);
        SkipDeckKeep = [];
        savedWeapons = null;
        SavedSyogoed = null;
        if (Skp_3SyoFlg) AcCard削除_Void();
        Skp_3SyoFlg = false;
        if (! $gameParty.hasItem($dataItems[2])) {
            $gameParty.gainItem($dataItems[2], 1);
        }
    }
    if (!$gameSwitches.value(752)) {
        $gamePlayer.setMoveSpeed(4);
    }
    Reset_慣性(); 
};
let Reset_慣性 = function () {
    _Reset_AnalogMove();
    $gamePlayer._mover._tarPosVec = null;
    $gamePlayer._mover._velVec._len = 0;
}
Game_Interpreter.prototype.TitleOptionUpdate = function () {
    this.MenuOptionUpdate();
    if (Input.isTriggered('menu') || TouchInput.isCancelled()) {
        TitleMode = 0; 
        this.Layer1_DelPicture();
        this.DelPict(PN_BlackBk);
        this.SetPict(PN_StartWhite, "pictures/" + "Black", 0, 0, 0);
    }
}
let BefTitleSelectNo = -1;
Game_Interpreter.prototype.TitleKeyTitle = function () {
    if (Input.isTriggered('up') || Input.isTriggered('left')) {
        TitleSelectBtnNo--;
        if (noSaveFlg && TitleSelectBtnNo == 1) TitleSelectBtnNo--;
        if (GaroFlg != true && TitleSelectBtnNo == -1) TitleSelectBtnNo = 2;
    } else if (Input.isTriggered('down') || Input.isTriggered('right')) {
        TitleSelectBtnNo++;
        if (noSaveFlg && TitleSelectBtnNo == 1) TitleSelectBtnNo++;
        if (GaroFlg != true && TitleSelectBtnNo == 3) TitleSelectBtnNo = 0;
    }
    if (TitleSelectBtnNo < 0) TitleSelectBtnNo = 3;
    if (TitleSelectBtnNo > 3) TitleSelectBtnNo = 0;
    let _ClickFlg = false;
    let _OverPicNo = overPointerCkArr([
        PN_StartBtn, PN_LoadBtn, PN_OptiBtn, PN_GallalyBtn,
        PN_Cien_Btn + 0, PN_Cien_Btn + 1, PN_Cien_BgBtn
    ]);
    this.PicCngColor(PN_Cien_Btn + 0);
    this.CngPict(PN_Cien_Btn + 0, TitleUIPath + "PN_Cien_Btn1");
    this.PicCngColor(PN_Cien_Btn + 1);
    this.CngPict(PN_Cien_Btn + 1, TitleUIPath + "PN_Cien_Btn2");
    this.PicCngColor(PN_Cien_BgBtn);
    if (_OverPicNo != -1) {
        if (_OverPicNo == PN_Cien_Btn + 0 || _OverPicNo == PN_Cien_Btn + 1 ||
            _OverPicNo == PN_Cien_BgBtn) {
            let _ckStr = _OverPicNo - PN_Cien_Btn + 1;
            this.PicCngColor(_OverPicNo, [80, 80, 80, 0]);
            if (TouchInput.isTriggered()) {
                switch (_ckStr){
                    case 1:
                        this.CngPict(_OverPicNo, TitleUIPath + "PN_Cien_Btn" + _ckStr + "b");
                        WebPageOpen("https://ci-en.dlsite.com/creator/976");
                        break;
                    case 2:
                        this.CngPict(_OverPicNo, TitleUIPath + "PN_Cien_Btn" + _ckStr + "b");
                        WebPageOpen("https://ci-en.dlsite.com/creator/718");
                        break;
                    case 3: 
                        WebPageOpen("https://docs.google.com/forms/d/e/1FAIpQLSekEchlgXXX7j20iuGh8lDBZjZdrmOC4v9IiO-3WWhT3vKN_w/viewform?usp=sf_link");
                        break;
                }
            }
            return;
        }
        if (noSaveFlg && _OverPicNo - PN_StartBtn == 1) return; 
        TitleSelectBtnNo = _OverPicNo - PN_StartBtn;
        if (BefTitleSelectNo != TitleSelectBtnNo){
            BefTitleSelectNo = TitleSelectBtnNo;
            let _MovingX = 0;
            switch (BefTitleSelectNo) {
                case 0:
                    _MovingX = 409;
                    this.SetPict(PN_SelectH, TitleUIPath + "T_SelectLine",
                        _BtnX - 584 + _MovingX, _BtnY + (72 * 0), 0);
                    break;
                case 1:
                    _MovingX = 507;
                    this.SetPict(PN_SelectH, TitleUIPath + "T_SelectLine",
                        _BtnX - 584 + _MovingX, _BtnY + (72 * 1), 0);
                    break;
                case 2:
                    _MovingX = 584;
                    this.SetPict(PN_SelectH, TitleUIPath + "T_SelectLine",
                        _BtnX - 584 + _MovingX, _BtnY + (72 * 2), 0);
                    break;
                case 3:
                    break;
            }
            this.MovePict(PN_SelectH, _BtnX - 584 + _MovingX , true,10,255);
        }
        if (TouchInput.isTriggered()) {
            _ClickFlg = true; 
        }
    }
    if (Input.isTriggered('ok') || _ClickFlg) { 
        switch (TitleSelectBtnNo) {
            case 0: 
                Save_ZureX = 0; 
                Save_ZureY = -20; 
                SoundManager.playOk();
                TitleMode = 4; 
                break;
            case 1: 
                this.TitleLoadBtnClick();
                break
            case 2: 
                SoundManager.playOk();
                TitleMode = 1;
                Save_ZureX = 129; 
                Save_ZureY = -39; 
                this.SetPict(PN_BlackBk, TitleUIPath + "title_save_back", 0, 0);
                this.MenuOptionView(true); 
                break
            case 3: 
                break
        }
    }
    if (overPointerCkTm(PN_OpenPoint) && TouchInput.isTriggered()) {
        TitleOpenPoint_Click();
    }
}
let TitleOpenPoint_Click = function () {
    console.log("PN_OpenPoint_Click");
}
Game_Interpreter.prototype.TitleLoadBtnClick = function () {
    SoundManager.playOk();
    TitleMode = 2;
    Save_ZureX = 129; 
    Save_ZureY = -39; 
    LoadModeFlg = true;
    NowLoadNo = $gameVariables.value(VN_BefSaveNo); 
    if (NowLoadNo == 0) NowLoadNo = -1;
    this.SetPict(PN_BlackBk, TitleUIPath + "title_save_back", 0, 0);
    if (NowLoadNo != -1) {
        if (NowLoadNo > 12) {
            SaveSelectNo = (NowLoadNo - 1) % 12;
            SaveSelPage = Math.floor((NowLoadNo - 1) / 12);
        } else {
            SaveSelectNo = NowLoadNo - 1;
        }
    }
    _File_CngFlg = true;
    this.MenuSaveView(true); 
}
Game_Interpreter.prototype.Fst_VolCk_View = function () {
    let _MasVol = $gameVariables.value(MasterVolVarNo);
    this.SetPict(PN_OnBase, TitleUIPath + "PN_OnBase", 0, 0);
    this.SetPict(PN_OnBar_Hit, TitleUIPath + "M_OBar1", 587, 356 , 1);
    this.SetPict(PN_OnBar, TitleUIPath + "M_OBar1", 587, 356 , 255 , _MasVol);
    let SxPos = 0; 
    let BarWidth = 414; 
    SxPos = Math.floor(BarWidth * (_MasVol / 100));
    this.SetPictC(PN_OnBar_Rct , TitleUIPath + "M_OPoint", 587 + SxPos , 356 + 8);
    this.SetPict(PN_OnTestBtn_1, TitleUIPath + "PN_OnTestBtn_1", 541, 426);
    this.SetPict(PN_OnTestBtn_2, TitleUIPath + "PN_OnTestBtn_2", 728, 426);
}
Game_Interpreter.prototype.Fst_VolCk = function () {
    if (overPointerCk(PN_OnBar_Hit)){
        if (TouchInput.isTriggered() || TouchInput.isRepeated()) { 
            let _BarPosX = TouchInput.mouseX - 587;
            let _SetVar = Math.floor((_BarPosX / 414) * 100);
            this.MasterVolSet(_SetVar);
            this.SetPict(PN_OnBar, TitleUIPath + "M_OBar1", 587, 356 , 255 , _SetVar);
            let SxPos = 0; 
            let BarWidth = 414; 
            SxPos = Math.floor(BarWidth * (_SetVar / 100));
            this.SetPictC(PN_OnBar_Rct , TitleUIPath + "M_OPoint", 587 + SxPos , 356 + 8);
        }
    }
    this.PicCngColor(PN_OnTestBtn_1, [0, 0, 0, 0]);
    if(overPointerCk(PN_OnTestBtn_1)){ 
        this.PicCngColor(PN_OnTestBtn_1, [100, 100, 100, 0]);
        if(TouchInput.isTriggered()){ 
            this.PlaySe("zb_GetSup");
        }
    }
    this.PicCngColor(PN_OnTestBtn_2, [0, 0, 0, 0]);
    if(overPointerCk(PN_OnTestBtn_2)){
        this.PicCngColor(PN_OnTestBtn_2, [100, 100, 100, 0]);
        if(TouchInput.isTriggered()){ 
            this.DelPictSpan(PN_OnBase , PN_OnTestBtn_2);
            return false;
        }
    }
    return true;
}
Game_Interpreter.prototype.NupuGameStart = function () {
    this.PreLoadArr(MenuPicArr); 
};
