#東方弾幕風
#Title[禁弾「カタディオプトリック」]
#Text[]
#Player[FREE]
#ScriptVersion[2]
#BackGround[User()]
script_enemy_main
{
    let count=0;
    let count2=0;
    let count3=0;
    let ten=0;
    let life=0;
    let R=0;
    let R2=0;
    let R3=0;
    let ac=0;
    let N15=0;
    let move=0;
    let step=0;
    let MT=0;
    let cx=GetCenterX();
    let cy=GetCenterY();
    let get;
    let try;

    let pass=GetCurrentScriptDirectory~"Spell.dat";

    let bombalpha=255;

    let cx=GetCenterX();
    let mm=0;
    let shot=0;
    let tame=0;
    let tame2=0;
    let c=0;
    let d=60;
    let ang=0;
    let angW=0;
    let MT=0;

    function IntToString(num) {
        let retStr = "";//戻り値
        let numStr = ToString(num);

        let i = 0;
        while (i < length(numStr)) {
            let tmp = numStr[i];
            if (tmp == '.') {	//小数点で終了
                break;
            }
            retStr = retStr ~ [tmp];
            i++;
        }
        return retStr;
    }

    function load() {
        LoadCommonDataEx("Spell",pass);
    }

    function save() {
        SaveCommonDataEx("Spell",pass);
    }

#include_function ".\BGfx.txt"

    @Initialize()
    {
        MagicCircle(false);
        SetEnemyMarker(true);
        if (IsCommonDataAreaExists("Spell")==false) {
            CreateCommonDataArea("Spell");
        }
        load();
        SetLife(500);
        SetDamageRate(12,10);
        SetTimer(42);
        SetInvincibility(200);
        SetNormPoint(-1);
        SetShotAutoDeleteClip(32,32,32,32);
        SetMovePositionRandom01(0,200,3,cx,128,cx,128);
        LoadGraphic(GetCurrentScriptDirectory~"\img\dot_flandre.png");
        LoadGraphic(GetCurrentScriptDirectory~"\img\circle_l.png");
        LoadUserShotData(GetCurrentScriptDirectory~".\shot_All.txt");

        get=GetCommonDataDefaultEx("Spell","4no5get",0);
        try=GetCommonDataDefaultEx("Spell","4no5try",0);
        try++;
        SetCommonDataEx("Spell","4no5try",try);
        save();

    }

    @MainLoop() {

        if (count2%4==0) {
            life=GetLife();
        }if (ten==1&&count2%4==1) {
            ten=0;
        }
        if (count2%4==3&&GetLife()<life&&GetLife()>0) {
            ten=1;
        }
        SetCommonData("Black2",228);

        if (count==1) {
            PlaySE(GetCurrentScriptDirectory~"\se\puu811.wav");
        }
        //SetColor(255,0,0);Concentration01(113);}

        SetCollisionA(GetX(),GetY()-12,32);
        SetCollisionB(GetX(),GetY(),12);

        if (GetEnemyLife<=0&&MT==0) {
            SetPlayerInvincibility(140);
            MT++;
            SetCommonData("last",true);
            return;
        }

        if (count%1100>=95&&count%1100<110&&count%37==35) {
            PlaySE(GetCurrentScriptDirectory~"\se\Shot06.wav");
            ascent( let tt in 0..2 ) {
                ascent( let t in 0..4 ) {
                    CreateReflectShot02(GetX(),GetY()+2,rand(4.2,6.2),90+51-102*tt+rand(-3,3),0,0,38,10,6,1,440);
                }
                ascent( let t in 0..8 ) {
                    CreateReflectShot02(GetX(),GetY()+2,rand(2.2,5.2),90+51-102*tt+rand(-2,2),0,0,195,10,6,1,440);
                }
                CreateReflectShot02(GetX(),GetY()+2,9,90+51-102*tt,-0.2,5.5,54,10,6,1,440);
            }
        }

        if (count%1100>=110&&count%1100<360&&count%37==35) {
            PlaySE(GetCurrentScriptDirectory~"\se\Shot06.wav");
            ascent( let t in 0..5 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(4.2,6.2),GetAngleToPlayer()+rand(-3,3),0,0,38,10,6,1,440);
            }
            ascent( let t in 0..10 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(2.2,5.2),GetAngleToPlayer()+rand(-2,2),0,0,195,10,6,1,440);
            }
            CreateReflectShot02(GetX(),GetY()+2,9,GetAngleToPlayer(),-0.2,5.5,54,10,6,1,440);
            ascent( let tt in 0..3 ) {
                ascent( let t in 0..4 ) {
                    CreateReflectShot02(GetX(),GetY()+2,rand(4.2,6.2),-23+23*tt+rand(-3,3),0,0,38,10,6,1,440);
                }
                ascent( let t in 0..8 ) {
                    CreateReflectShot02(GetX(),GetY()+2,rand(2.2,5.2),-23+23*tt+rand(-2,2),0,0,195,10,6,1,440);
                }
                CreateReflectShot02(GetX(),GetY()+2,9,-23+23*tt,-0.2,5.5,54,10,6,1,440);
            }
            ascent( let tt in 0..3 ) {
                ascent( let t in 0..4 ) {
                    CreateReflectShot02(GetX(),GetY()+2,rand(4.2,6.2),157+23*tt+rand(-3,3),0,0,38,10,6,1,440);
                }
                ascent( let t in 0..8 ) {
                    CreateReflectShot02(GetX(),GetY()+2,rand(2.2,5.2),157+23*tt+rand(-2,2),0,0,195,10,6,1,440);
                }
                CreateReflectShot02(GetX(),GetY()+2,9,157+23*tt,-0.2,5.5,54,10,6,1,440);
            }
        }

        if (count%1100>=450&&count%1100<=500&&count%10==0) {
            PlaySE(GetCurrentScriptDirectory~"\se\Shot06.wav");
            ascent( let t in 0..4 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(4.6,7.2),GetAngleToPlayer()+rand(-3,3),0,0,38,10,15,1,440);
            }
            ascent( let t in 0..8 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(2.6,6.2),GetAngleToPlayer()+rand(-2,2),0,0,195,10,15,1,440);
            }
            CreateReflectShot02(GetX(),GetY()+2,9,GetAngleToPlayer(),-0.2,7,54,15,6,1,440);
            ascent( let t in 0..3 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(3.2,5.2),270+rand(-4,4),0,0,38,10,14,1,440);
            }
            ascent( let t in 0..8 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(2.2,5.2),270+rand(-9,9),0,0,195,10,14,1,440);
            }
            CreateReflectShot02(GetX(),GetY()+2,9,270,-0.2,5.5,54,10,14,1,440);
        }

        if (count%1100>=620&&count%1100<=900&&count%15==0) {
            PlaySE(GetCurrentScriptDirectory~"\se\Shot06.wav");
            ascent( let t in 0..4 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(2.6,4.2),count+rand(-2,2),0,0,38,10,6,1,440);
            }
            ascent( let t in 0..8 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(1.6,4.2),count+rand(-3,3),0,0,195,10,6,1,440);
            }
            CreateReflectShot02(GetX(),GetY()+2,9,count,-0.2,3.5,54,6,6,1,440);
            ascent( let t in 0..4 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(2.6,4.2),count+180+rand(-2,2),0,0,38,10,6,1,440);
            }
            ascent( let t in 0..8 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(1.6,4.2),count+180+rand(-3,3),0,0,195,10,6,1,440);
            }
            CreateReflectShot02(GetX(),GetY()+2,9,count+180,-0.2,3.5,54,6,6,1,440);
        }

        if (count%1100>=1040&&count%1100<=1068&&count%2==0) {
            PlaySE(GetCurrentScriptDirectory~"\se\Shot06.wav");
            ascent( let t in 0..1 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(2.6,3.2),rand(-6,6),0,0,38,10,6,1,440);
            }
            ascent( let t in 0..3 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(1.6,3.2),rand(-7,7),0,0,195,10,6,1,440);
            }
            CreateReflectShot02(GetX(),GetY()+2,9,0,-0.2,2.5,54,10,0,0,440);
            ascent( let t in 0..1 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(2.6,3.2),180+rand(-6,6),0,0,38,10,6,1,440);
            }
            ascent( let t in 0..3 ) {
                CreateReflectShot02(GetX(),GetY()+2,rand(1.6,3.2),180+rand(-7,7),0,0,195,10,6,1,440);
            }
            CreateReflectShot02(GetX(),GetY()+2,9,180,-0.2,2.5,54,10,0,0,440);
        }

        if (count%1100>=115&&count%1100<360&&count%42==40) {
            SetMovePositionRandom01(90,90,9,GetPlayerX()+rand(-30,30),80,GetPlayerX()+rand(-30,30),160);
            move=53;
        }
        if (count%1100>=655&&count%1100<910&&count%42==40&&rand(0,100)<=40) {
            SetMovePositionRandom01(rand(15,80),rand(10,50),9,cx-100,80,cx+100,160);
            move=53;
        }
        if (count==115) {
            SetMovePositionRandom01(rand(10,80),rand(0,40),7,GetX,80,GetX,160);
            move=53;
        }
        if (count%1100==360) {
            SetMovePositionRandom01(50,50,7,cx-150,190,cx-150,190);
            move=53;
        }
        if (count%1100==450) {
            SetMovePositionRandom01(50,50,7,cx+150,190,cx+150,190);
            move=53;
        }
        if (count%1100==520) {
            SetMovePositionRandom01(50,50,7,cx,120,cx,120);
            move=53;
        }
        if (count%1100==910) {
            SetMovePositionRandom01(50,50,7,cx,20,cx,20);
            move=158;
            SetDamageRate(0,0);
        }
        if (count%1100==1040) {
            SetMovePositionRandom01(50,50,25,cx,480,cx,480);
            move=53;
            SetDamageRate(120,0);
        }
        if (count%1100==1090) {
            SetMovePositionRandom01(50,50,7,cx,120,cx,120);
            move=53;
            SetDamageRate(12,10);
        }

        if (count2>=70) {
            count++;
            move--;
        }count2++;
        if (count2>=40&&count<115) {
            tame++;
        }if (count>=10) {
            count3++;
        }
        if (count==10) {
            PlaySE(GetCurrentScriptDirectory~"\se\warp02.wav");
            SetScore(5000000);
            CutIn(YOUMU,"禁弾「カタディオプトリック」",0,0,0,0,0);
        }

        c+=4;
        yield;
    }

    @Finalize()
    {
        if (GotSpellCardBonus()==true) {
            get++;
            SetCommonDataEx("Spell","4no5get",get);
        }
        save();

        SetShotAutoDeleteClip(32,32,32,32);
        //loop(60){CreateItem(ITEM_SCORE,GetX()+rand(-150,150),GetY()+rand(-80,120));}
        //CreateItem(ITEM_1UP,GetX(),GetY()+20);
        SetEnemyMarker(false);

    }

    @DrawLoop()
    {
        if (GetEnemyLife>0) {
            SetTexture(GetCurrentScriptDirectory~"\img\circle_l.png");
            SetGraphicAngle(0,0,-c);
            SetGraphicRect(0,0,256,256);
            SetAlpha(255);
            SetRenderState(ADD);
            SetColor(160+20*sin(c),160+20*sin(c),160+20*sin(c));
            if (count2<=88) {
                SetGraphicScale(0,0);
            }
            if (count2>88) {
                SetGraphicScale(0.02*(count2-88),0.02*(count2-88));
            }
            if (count2>=138) {
                SetGraphicScale(1.0+0.1*sin((count2-138)*3),1.0+0.1*sin((count2-138)*3));
            }
            DrawGraphic(GetX(),GetY());
        }

        SetTexture(GetCurrentScriptDirectory~"\img\dot_flandre.png");
        SetAlpha(255);
        SetGraphicAngle(0,0,0);
        SetColor(bombalpha,bombalpha,bombalpha);
        SetRenderState(ALPHA);
        SetGraphicScale(1,1);
        SetGraphicRect(0,0,128,128);
        if (GetSpeedX()<0&&count2<=d) {
            SetGraphicRect(256,0,384,128);
        }
        if (GetSpeedX()<0&&count2<=d-8) {
            SetGraphicRect(256,128,384,256);
        }
        if (GetSpeedX()>=0&&count2<=d) {
            SetGraphicRect(256,256,384,384);
        }
        if (GetSpeedX()>=0&&count2<=d-8) {
            SetGraphicRect(256,384,384,512);
        }
        if (count2/12%4>=0&&move<=0) {
            SetGraphicRect(0,0,128,128);
        }
        if (count2/12%4>=1&&move<=0) {
            SetGraphicRect(0,128,128,256);
        }
        if (count2/12%4>=2&&move<=0) {
            SetGraphicRect(0,256,128,384);
        }
        if (count2/12%4>=3&&move<=0) {
            SetGraphicRect(0,384,128,512);
        }
        if (tame>0) {
            SetGraphicRect(128*3,0,128*4,128);
        }
        if (tame>32) {
            SetGraphicRect(128*3,128,128*4,256);
        }
        if (tame>42) {
            SetGraphicRect(128*3,256,128*4,384);
        }
        if (GetSpeedX()<0&&move>0) {
            SetGraphicRect(256,0,384,128);
            tame=0;
        }
        if (GetSpeedX()<0&&move>8) {
            SetGraphicRect(256,128,384,256);
            tame=0;
        }
        if (GetSpeedX()>=0&&move>0) {
            SetGraphicRect(256,256,384,384);
            tame=0;
        }
        if (GetSpeedX()>=0&&move>8) {
            SetGraphicRect(256,384,384,512);
            tame=0;
        }
        if (ten==1) {
            SetColor(160,160,180);
        }
        DrawGraphic(GetX(),GetY());

        if (bombalpha<255) {
            SetAlpha(255-bombalpha);
            SetGraphicRect(128*6,0,128*7,128);
            SetColor(255,200,200);
            SetGraphicScale(1.1,1.1);
            SetRenderState(ALPHA);
            if (count2/4%4>=0) {
                SetGraphicRect(128*6,0,128*7,128);
            }
            if (count2/4%4>=1) {
                SetGraphicRect(128*6,128,128*7,256);
            }
            if (count2/4%4>=2) {
                SetGraphicRect(128*6,256,128*7,384);
            }
            if (count2/4%4>=3) {
                SetGraphicRect(128*6,384,128*7,512);
            }
            DrawGraphic(GetX()+15*cos(count2*28),GetY()+30*cos(count2*18));
            if (count2/4%4>=1) {
                SetGraphicRect(128*6,0,128*7,128);
            }
            if (count2/4%4>=2) {
                SetGraphicRect(128*6,128,128*7,256);
            }
            if (count2/4%4>=3) {
                SetGraphicRect(128*6,256,128*7,384);
            }
            if (count2/4%4>=0) {
                SetGraphicRect(128*6,384,128*7,512);
            }
            DrawGraphic(GetX()-30*cos(count2*18),GetY()-15*cos(count2*28));
            if (count2/4%4>=2) {
                SetGraphicRect(128*6,0,128*7,128);
            }
            if (count2/4%4>=3) {
                SetGraphicRect(128*6,128,128*7,256);
            }
            if (count2/4%4>=0) {
                SetGraphicRect(128*6,256,128*7,384);
            }
            if (count2/4%4>=1) {
                SetGraphicRect(128*6,384,128*7,512);
            }
            DrawGraphic(GetX()+24*cos(count2*46),GetY()-40*cos(count2*58));
            if (count2/4%4>=3) {
                SetGraphicRect(128*6,0,128*7,128);
            }
            if (count2/4%4>=0) {
                SetGraphicRect(128*6,128,128*7,256);
            }
            if (count2/4%4>=1) {
                SetGraphicRect(128*6,256,128*7,384);
            }
            if (count2/4%4>=2) {
                SetGraphicRect(128*6,384,128*7,512);
            }
            DrawGraphic(GetX()-44*cos(count2*46),GetY()+20*cos(count2*58));
        }

        if (count>=10) {
            SetFontColor(255,196,196,255,0,0);
            let posX = 80;
            if (try>=1000) {
                posX+=21;
            }if (try>=100) {
                posX+=14;
            }else if (try>=10) {
                posX+=7;
            }
            if (get>=1000) {
                posX+=15;
            }if (get>=100) {
                posX+=10;
            }else if (get>=10) {
                posX+=5;
            }
            DrawText("Score "~IntToString(get)~" / "~IntToString(try)~"",GetClipMaxX()-posX,GetClipMinY()+44,10,196);
        }
    }


}

