#東方弾幕風
#Title[幻術「グングニル異説」（強）]
#Text[]
#Player[FREE]
#ScriptVersion[2]
#BackGround[User()]
script_enemy_main
{
    let count=0;
    let count2=0;
    let count3=0;
    let fl=0;
    let R=0;
    let R2=0;
    let R3=0;
    let R4=0;
    let ten=0;
    let life=0;
    let o=0;
    let move=0;
    let get;
    let try;

    let pass=GetCurrentScriptDirectory~"Spell.dat";

    let bombalpha=255;

    let cx=GetCenterX();
    let img=GetCurrentScriptDirectory~"img\dot_remilia.png";
    let bg=GetCurrentScriptDirectory~"img\st00.png";
    let bg2=GetCurrentScriptDirectory~"img\moon3-1.png";
    let bg3=GetCurrentScriptDirectory~"img\cloud.png";
    let sh00=GetCurrentScriptDirectory~"\se\Shot00.wav";
    let mm=0;
    let shot=0;
    let tame=0;
    let tame2=0;
    let c=0;
    let d=60;
    let ang=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;
    }

    task laser(x,s) {
        let R=0;
        let R2=0;
        let R3=180;
        let o=0;
        R=x;
        R2=GetPlayerY;
        PlaySE(GetCurrentScriptDirectory~"\se\eco00.wav");

        loop(180) {

            if (R3>0) {
                R3--;
            }

            if (R3>30&&R3<=120) {
                if (OnBomb==false) {
                    CreateLaser01(R+o,GetClipMinY+1,24.7-(o)/15-s,90,1211-(o)*35,5,65,0);
                    CreateLaser01(R-o,GetClipMinY+1,24.7-(o)/15-s,90,1211-(o)*35,5,65,0);
                    o++;
                    R3--;
                }
            }

            yield;
        }

    }

    task laser2(x) {
        let R=0;
        let R2=0;
        let R3=180;
        let o=0;
        R=x;
        R2=GetPlayerY;

        loop(180) {

            if (R3>0) {
                R3--;
            }

            if (R3>30&&R3<=120) {
                if (OnBomb==false) {
                    CreateLaser01(R+o*2,GetClipMinY+1,19.7-(o)/15,90,1211-(o)*35,5,65,0);
                    CreateLaser01(R-o*2,GetClipMinY+1,19.7-(o)/15,90,1211-(o)*35,5,65,0);
                    o++;
                    R3--;
                }
            }

            yield;
        }

    }

    task Shot_R() {
        let R=GetPlayerX+15*(GetCommonDataDefault("ii1",0)<2000);
        let R2=0;
        let R3=0;
        let o=0;

        R2+=R;

        loop(120) {

            if (R3>58&&R3<=120&&R3%3==0) {
                if (OnBomb==false) {
                    CreateShot02(R2+130*(1-cos(R3)),9,0,90,0.2-o*0.001,10-o*0.01,170,13);
                    CreateShot02(R2+135*(1-cos(R3)),9,0,90,0.2-o*0.0015,10.2-o*0.01,170,15);
                    CreateShot02(R2+140*(1-cos(R3)),9,0,90,0.2-o*0.002,10.4-o*0.01,170,17);
                    CreateShot02(R2+145*(1-cos(R3)),9,0,90,0.2-o*0.0025,10.6-o*0.01,170,17);
                }
            }

            R3+=2;
            o++;

            yield;
        }

    }

    task Shot_L() {
        let R=GetPlayerX-15*(GetCommonDataDefault("ii1",0)<2000);
        let R2=0;
        let R3=0;
        let o=0;

        R2+=R;

        loop(120) {

            if (R3>58&&R3<=120&&R3%3==0) {
                if (OnBomb==false) {
                    PlaySE(GetCurrentScriptDirectory~"\se\Shot02.wav");
                    CreateShot02(R2-130*(1-cos(R3)),9,0,90,0.2-o*0.001,10-o*0.01,170,13);
                    CreateShot02(R2-135*(1-cos(R3)),9,0,90,0.2-o*0.0015,10.2-o*0.01,170,15);
                    CreateShot02(R2-140*(1-cos(R3)),9,0,90,0.2-o*0.002,10.4-o*0.01,170,17);
                    CreateShot02(R2-145*(1-cos(R3)),9,0,90,0.2-o*0.0025,10.6-o*0.01,170,17);
                }
            }

            R3+=2;
            o++;

            yield;
        }

    }

    task Laser(x,y,s,a,l,w,g,d)
    {
        let obj = Obj_Create(OBJ_LASER);
        Obj_SetX(obj,x);
        Obj_SetY(obj,y);
        Obj_SetAngle(obj,a);
        ObjShot_SetGraphic(obj,g);
        ObjShot_SetDelay(obj,d);
        //Obj_SetSpeed(obj,s);
        ObjLaser_SetLength(obj,l);
        ObjLaser_SetWidth(obj,w);
        ObjShot_SetBombResist(obj,true);
        ObjLaser_SetSource(obj,false);
        while ( !Obj_BeDeleted(obj) )
        {
            ObjLaser_SetLength(obj,l);
            yield;
        }
    }

    @Initialize()
    {
        SetCommonData("ii1",2500);
        MagicCircle(false);
        SetEnemyMarker(true);
        if (IsCommonDataAreaExists("Spell")==false) {
            CreateCommonDataArea("Spell");
        }
        load();
        SetLife(1000);
        SetDamageRate(28,0);
        SetTimer(55);
        SetInvincibility(150);
        SetNormPoint(-1);
        LoadGraphic(img);
        SetShotAutoDeleteClip(128,128,128,128);
        SetMovePositionRandom01(0,200,5,cx,138,cx,138);
        move=90;
        LoadGraphic(bg);
        LoadGraphic(bg2);
        LoadGraphic(bg3);
        LoadGraphic(GetCurrentScriptDirectory~"\img\circle_l.png");
        LoadUserShotData(GetCurrentScriptDirectory~".\shot_All.txt");

        if(GetCommonDataDefault("ii1",0)<2000) {
        get=GetCommonDataDefaultEx("Spell","2no3get",0);
        try=GetCommonDataDefaultEx("Spell","2no3try",0);}
        else {
        get=GetCommonDataDefaultEx("Spell","2no3get2",0);
        try=GetCommonDataDefaultEx("Spell","2no3try2",0);}
        try++;
        if(GetCommonDataDefault("ii1",0)<2000) {
            SetCommonDataEx("Spell","2no3try",try);
	}
        else {
            SetCommonDataEx("Spell","2no3try2",try);
	}
        save();

    }

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

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

    @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;
        }
        SetCollisionA(GetX(),GetY()-16,30);
        SetCollisionB(GetX(),GetY(),12);

        if (OnBomb==true) {
            DeleteEnemyShotImmediatelyInCircle(SHOT,GetX,GetY,2000);
            fl++;
        }

        if (fl==1) {
            SetCommonData("flash2",145);
            PlaySE(GetCurrentScriptDirectory~"\se\Shot01.wav");
        }

        if (OnBomb==false) {
            fl=0;
        }

        if (OnBomb==false&&count>=45&&count%45==10) {
            laser2(cx+200);
            laser2(cx-200);
        }

        if (count2>=25&&count2%25==20&&count2<=155) {
            laser(cx+(count2-95)*4,(count2-25)*0.1);
            laser(cx-(count2-95)*4,(count2-25)*0.1);
        }

        if (count2==82) {
            PlaySE(GetCurrentScriptDirectory~"\se\Shot04.wav");
            ascent( let t in 0..7 ) {
                ascent( let tt in 0..60 ) {
                    CreateShot01(GetX(),GetY(),4+t,120+3+tt*5,170,0)
                }
            }
        }

        if (count2==85) {
            SetMovePositionRandom01(85,rand(50,150),12,cx,-126,cx,-126);
            move=42;
        }

        if (count>=35&&count%2==1) {
            PlaySE(GetCurrentScriptDirectory~"\se\Shot00.wav");
        }

        if (count>=115&&count%42==40&&rand(0,100)<=70) {
            SetMovePositionRandom01(85,rand(10,40),7,GetPlayerX()+rand(-42,42),90,GetPlayerX()+rand(-42,42),150);
            move=43;
        }

        if (count>=115&&count<=145&&count%42==40) {
            SetMovePositionRandom01(85,rand(10,40),7,GetPlayerX()+rand(-50,50),120,GetPlayerX()+rand(-50,50),150);
            move=43;
        }

        if (count>=145&&count%42==20) {
            Shot_R;
        }
        if (count>=145&&count%42==40) {
            Shot_L;
        }

        if (count>=165&&count%18==10&&OnBomb==false) {
            PlaySE(GetCurrentScriptDirectory~"\se\Laser2.wav");
            CreateShot12(GetX,GetY,rand(-1.9,1.9),rand(-0.4,-2.8),0,0.04,0,5.2+rand(0.4,-0.8),49,18);
            ascent( let t in 0..6-2*(GetCommonDataDefault("ii1",0)<2000) ) {
                CreateShot12(GetX,GetY,rand(-1.9,1.9),rand(-0.4,-2.6),0,0.03,0,4.4,196,12);
            }
            ascent( let t in 0..6-2*(GetCommonDataDefault("ii1",0)<2000) ) {
                CreateShot12(GetX,GetY,rand(-1.7,1.7),rand(-0.4,-2.2),0,0.04,0,4.4,196,11);
            }
        }

        if (count2>=150) {
            count++;
        }count2++;
        if (count2>=42) {
            count3++;
            tame++;
        }
        if (count2==10) {
            SetColor(255,0,0);
            Concentration01(85);
        }
        if (count2==5) {
            PlaySE(GetCurrentScriptDirectory~"\se\puu81.wav");
        }
        if (count2==240) {
            PlaySE(GetCurrentScriptDirectory~"\se\Shot02.wav");
            SetScore(8000000);
            CutIn(YOUMU,"幻術「グングニル異説」",GetCurrentScriptDirectory~"img\remiria2.png",0,0,512,512);
        }

        c+=5;
        move--;
        yield;

    }

    @Finalize()
    {
        if (GotSpellCardBonus()==true) {
            get++;
            if(GetCommonDataDefault("ii1",0)<2000) {
            SetCommonDataEx("Spell","2no3get",get);
            }
            else {
            SetCommonDataEx("Spell","2no3get2",get);
            }
        }
        save();

        SetShotAutoDeleteClip(32,32,32,32);
        loop(80) {
            CreateItem(ITEM_SCORE,GetX()+rand(-150,150),GetY()+rand(-90,135));
        }CreateItem(ITEM_1UP,GetX(),GetY()+20);

    }

    @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.1*(count2-88)+0.1*sin(count*3),0.1*(count2-88)+0.1*sin(count*3));
            }
            if (count2>=98) {
                SetGraphicScale(1.0+0.1*sin((count-120)*3),1.0+0.1*sin((count-120)*3));
            }
            DrawGraphic(GetX(),GetY());
        }

        if (OnEnemySpell==true&&OnBomb==true&&bombalpha>0) {
            bombalpha-=5;
        }
        if (OnEnemySpell==true&&OnBomb==false&&bombalpha<255) {
            bombalpha+=5;
        }

        SetTexture(img);
        SetAlpha(255);
        SetGraphicAngle(0,0,0);
        SetColor(bombalpha,bombalpha,bombalpha);
        SetRenderState(ALPHA);
        SetGraphicScale(1,1);
        SetGraphicRect(0,0,128,128);
        if (int(GetSpeedX())==0&&count2/12%4>=0) {
            SetGraphicRect(0,0,128,128);
        }
        if (int(GetSpeedX())==0&&count2/12%4>=1) {
            SetGraphicRect(0,128,128,256);
        }
        if (int(GetSpeedX())==0&&count2/12%4>=2) {
            SetGraphicRect(0,256,128,384);
        }
        if (int(GetSpeedX())==0&&count2/12%4>=3) {
            SetGraphicRect(0,384,128,512);
        }
        if (int(GetSpeedX())==0&&tame>0&&count<=115) {
            SetGraphicRect(128*4,0,128*5,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 (tame>0&&count<=115) {
            SetGraphicRect(128*3,0,128*4,128);
        }
        if (GetSpeedX()<0&&move>0) {
            SetGraphicRect(256,0,384,128);
        }
        if (GetSpeedX()<0&&move>9) {
            SetGraphicRect(256,128,384,256);
        }
        if (GetSpeedX()>=0&&move>0) {
            SetGraphicRect(256,256,384,384);
        }
        if (GetSpeedX()>=0&&move>9) {
            SetGraphicRect(256,384,384,512);
        }
        if (ten==1&&OnBomb==false) {
            SetColor(160,160,180);
        }
        DrawGraphic(GetX(),GetY());

        if (bombalpha<255) {
            SetTexture(img);
            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) {
            if(GetCommonDataDefault("ii1",0)<2000) {
                SetFontColor(255,255,255,64,64,255);
            }
            else {
                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);
        }
    }

    @BackGround()
    {
        SetTexture(bg);
        SetGraphicRect(0,0,384,448);
        SetRenderState(ALPHA);
        SetGraphicScale(1,1);
        SetGraphicAngle(0,0,0);
        SetColor(255,255,255);
        if (count3<=1) {
            SetAlpha(0);
        }
        if (count3>1) {
            SetAlpha(count3*5);
        }
        if (count3>=51) {
            SetAlpha(255);
        }
        DrawGraphic(GetCenterX(),GetCenterY());

        SetTexture(bg2);
        SetRenderState(ADD);
        SetGraphicAngle(0,0,-count2);
        if (count2<=1) {
            SetColor(0,0,0);
        }
        if (count2>1) {
            SetColor(count2,count2,count2);
        }
        if (count2>=128) {
            SetColor(128,128,128);
        }
        DrawGraphic(GetCenterX(),GetCenterY()-30);
        SetGraphicScale(1.9,1.9);
        SetGraphicAngle(0,0,count2);
        DrawGraphic(GetCenterX(),GetCenterY()-30);

        SetTexture(bg3);
        SetGraphicAngle(0,0,0);
        SetGraphicRect(-count2,512,1028-count2,768);
        SetGraphicScale(1,1);
        SetAlpha(255);
        if (count2<=1) {
            SetColor(0,0,0);
        }
        if (count2>1) {
            SetColor(count2*2,count2*2,count2*2);
        }
        if (count2>=128) {
            SetColor(255,255,255);
        }
        SetRenderState(SUBTRACT);
        DrawGraphic(GetCenterX(),GetCenterY()+100);
        DrawGraphic(GetCenterX(),GetCenterY()+100);
    }

}

