#e
#Title[ugfrv]
#Text[]
#Player[FREE]
#ScriptVersion[2]
#BackGround[User()]
script_enemy_main
{
    let count=0;
    let count2=0;
    let count3=0;
    let R2=250;
    let R4=300;
    let get;
    let try;
    let N15=0;
    let o=0;

    let pass=GetCurrentScriptDirectory~"Spell.dat";

    let bombalpha=255;

    let cx=GetCenterX();
    let cy=GetCenterY();
    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 = "";//߂l
        let numStr = ToString(num);

        let i = 0;
        while (i < length(numStr)) {
            let tmp = numStr[i];
            if (tmp == '.') {	//_ŏI
                break;
            }
            retStr = retStr ~ [tmp];
            i++;
        }
        return retStr;
    }

    task Blood(let x, let y, let speed, let angle, let grf, let delay, let Aadd,)
    {
        //e^XN
        let obj = Obj_Create(OBJ_SHOT);//eIuWFNg쐬
        Obj_SetX(obj, x);//xWݒ
        Obj_SetY(obj, y);//yWݒ;
        Obj_SetSpeed(obj, speed);//xݒ
        Obj_SetAngle(obj, angle);//ړpxݒ
        ObjShot_SetGraphic(obj, grf);//摜ݒ
        ObjShot_SetDelay(obj, delay);//xԐݒ
        ObjShot_SetBombResist(obj, true);

        while ( !Obj_BeDeleted(obj) )
        {
            angle+=Aadd;
            Obj_SetAngle(obj, angle);
            yield;
        }
    }

    task gl() {
        let R=300;
        let R3=0;

        loop(270) {

            R3++;

            if (R3%2==0&&R3%R<100)
            {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot00.wav");
                ascent(let t in 0..5) {
                    CreateShotA(N15,GetClipMinX+(R3%R)*5,GetClipMinY+t*10,5);
                    SetShotDataA(N15,0,0,0,0,0,0,23);
                    SetShotKillTime(N15,rand(20,50));
                    FireShot(N15);
                }
                ascent(let t in 0..4) {
                    CreateShotA(N15,GetClipMinX+(R3%R)*5,GetClipMinY+t*10+60,5);
                    SetShotDataA(N15,0,0,0,0,0,0,17);
                    SetShotKillTime(N15,rand(10,40));
                    FireShot(N15);
                }
            }

            if (R3%2==0&&R3%R>50&&R3%R<150)
            {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot00.wav");
                ascent(let t in 0..5) {
                    CreateShotA(N15,GetClipMaxX-t*10,GetClipMinY+(R3%R-50)*5-10,5);
                    SetShotDataA(N15,0,0,0,0,0,0,23);
                    SetShotKillTime(N15,rand(20,50));
                    FireShot(N15);
                }
                ascent(let t in 0..4) {
                    CreateShotA(N15,GetClipMaxX-t*10-60,GetClipMinY+(R3%R-50)*5-10,5);
                    SetShotDataA(N15,0,0,0,0,0,0,17);
                    SetShotKillTime(N15,rand(10,40));
                    FireShot(N15);
                }
            }

            if (R3%2==0&&R3%R>110&&R3%R<210)
            {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot00.wav");
                ascent(let t in 0..5) {
                    CreateShotA(N15,GetClipMaxX-(R3%R-110)*5,GetClipMaxY-t*10-2,5);
                    SetShotDataA(N15,0,0,0,0,0,0,23);
                    SetShotKillTime(N15,rand(20,50));
                    FireShot(N15);
                }
                ascent(let t in 0..4) {
                    CreateShotA(N15,GetClipMaxX-(R3%R-110)*5,GetClipMaxY-t*10-60,5);
                    SetShotDataA(N15,0,0,0,0,0,0,17);
                    SetShotKillTime(N15,rand(10,40));
                    FireShot(N15);
                }
            }

            if (R3%2==0&&R3%R>160&&R3%R<300)
            {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot00.wav");
                ascent(let t in 0..5) {
                    CreateShotA(N15,GetClipMinX+t*10,GetClipMaxY-(R3%R-160)*5+10,5);
                    SetShotDataA(N15,0,0,0,0,0,0,23);
                    SetShotKillTime(N15,rand(20,50));
                    FireShot(N15);
                }
                ascent(let t in 0..4) {
                    CreateShotA(N15,GetClipMinX+t*10+60,GetClipMaxY-(R3%R-160)*5+10,5);
                    SetShotDataA(N15,0,0,0,0,0,0,17);
                    SetShotKillTime(N15,rand(10,40));
                    FireShot(N15);
                }
            }

            yield;
        }
    }

    task Laser(x,y,s,a,l,w,g,d,rm)
    {
        let obj = Obj_Create(OBJ_LASER);
        let c = 0;
        Obj_SetX(obj,x);
        Obj_SetY(obj,y);
        Obj_SetAngle(obj,a);
        ObjShot_SetGraphic(obj,g);
        ObjShot_SetDelay(obj,d);
        ObjLaser_SetLength(obj,l);
        ObjLaser_SetWidth(obj,w);
        ObjShot_SetBombResist(obj,true);
        ObjShot_ToItem(obj,false);
        ObjLaser_SetSource(obj,false);

        while (c<rm)
        {
            ObjLaser_SetLength(obj,l);
            l+=s;
            c++;
            if (d>=c) {
                ObjLaser_SetWidth(obj,20);
            }else {
                ObjLaser_SetWidth(obj,w);
            }
            if (c>=d+20) {
                Obj_SetCollisionToPlayer(obj,false);
            }
            if (c>=rm-20) {
                w=w/1.2;
                ObjLaser_SetWidth(obj,w);
            }
            if (c>=rm) {
                Obj_Delete(obj);
            }
            yield;
        }

    }

    @Initialize()
    {
        MagicCircle(false);
        SetEnemyMarker(true);
        if (IsCommonDataAreaExists("Spell")==false) {
            CreateCommonDataArea("Spell");
        }
        load();
        SetLife(1800);
        SetDamageRate(40,0);
        SetTimer(55);
        SetInvincibility(150);
        SetNormPoint(-1);
        LoadGraphic(img);
        SetShotAutoDeleteClip(8,8,8,8);
        SetMovePositionRandom01(0,200,3,cx,138,cx,138);
        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","2no2get",0);
        try=GetCommonDataDefaultEx("Spell","2no2try",0);}
        else {
        get=GetCommonDataDefaultEx("Spell","2no2get2",0);
        try=GetCommonDataDefaultEx("Spell","2no2try2",0);}
        try++;
        if(GetCommonDataDefault("ii1",0)<2000) {
            SetCommonDataEx("Spell","2no2try",try);
	}
        else {
            SetCommonDataEx("Spell","2no2try2",try);
	}
        save();

    }

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

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

    @MainLoop()
    {
        if (count2<=240) {
            SetCollisionA(GetX(),GetY()-16,32);
            SetCollisionB(GetX(),GetY(),14);
        }
        if (count2>=240) {
            SetCollisionA(GetX(),GetY()-16,32);
        }

        if (count==25)
        {
            PlaySE(GetCurrentScriptDirectory~"\se\Laser2.wav");
        }

        if (count==52)
        {
            PlaySE(GetCurrentScriptDirectory~"\se\Baaaaaaaan.wav");
            ascent( let t in 0..75 ) {
                CreateShot02(GetX(),GetY(),rand(1.6,6.5),rand(-35,35),-0.04,rand(0.3,1.5),17,10);
            }
            ascent( let t in 0..15 ) {
                CreateShot02(GetX(),GetY(),rand(3.6,8.5),rand(-10,10),-0.04,rand(0.3,1.5),17,2);
            }
        }

        if (count==17) {
            Laser(25,GetY,15,0,12,34,41,38,140);
            Laser(25,GetY,12,-12,12,8,41,38,110);
            Laser(25,GetY,12,12,12,8,41,38,110);
            Laser(25,GetY,8,-7,12,13,41,38,120);
            Laser(25,GetY,8,7,12,13,41,38,120);
            Laser(25,GetY,10,-2,12,14,41,38,130);
            Laser(25,GetY,10,2,12,14,41,38,130);
        }

        /*if(count>=25&&count<=130)
        {CreateLaser01(GetX()-190,GetY(),8.7+(count-25)/15,o+(count-25)/2,811-(count-25)*25,8,41,0);
         CreateLaser01(GetX()-190,GetY(),8.7+(count-25)/15,o-(count-25)/2,811-(count-25)*25,8,41,0);}*/

        if (count2==340)
        {
            SetMovePositionRandom01(0,200,2,cx,cy-44,cx,cy-44);
            d=80;
        }

        if (count2>=300)
        {
            tame=-1;
            if (count2%100==0&&count2>=360) {
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy02.txt",cx+rand(-100,-40),cy+rand(-65,15),0,0,0);
            }
            if (count2%100==50&&count2>=360) {
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Enemy02.txt",cx+rand(100,40),cy+rand(-65,15),0,0,0);
            }
        }

        if (count%4==0&&count>=50&&count2<=285) {
            PlaySE(GetCurrentScriptDirectory~"\se\Shot00.wav");
            if (R2>0) {
                SetShotColor(rand(R2,255),R2,R2);
                R2-=20;
            }
            if (R2<=0) {
                SetShotColor(rand(0,255),0,0);
                R2=0;
            }
            ascent(let t in 0..1) {
                Blood(GetX()+rand(0,(255-R2)/2.5)*cos(rand(0,360)),GetY()+rand(0,(255-R2)/2.5)*sin(rand(0,360)), 1.4, rand(220,330)+t, RED04, 30, rand(1.6,3.8));
                Blood(GetX()+rand(0,(255-R2)/2.5)*cos(rand(0,360)),GetY()+rand(0,(255-R2)/2.5)*sin(rand(0,360)), 1.4, rand(220,330)+t, RED04, 30, rand(-1.6,-3.8));
            }
        }
        SetShotColor(255,255,255);

        if (count2>=300&&count2%230==120)
        {
            gl;
        }

        if (count2>=300&&count2%230==5)
        {
            gl;
        }


        if (count2>=80) {
            count++;
        }count2++;
        if (count2>=70) {
            count3++;
            tame++;
        }if (count2>=160) {
            tame2++;
        }
        if (count2==40) {
            SetColor(255,0,0);
            Concentration01(75);
            PlaySE(GetCurrentScriptDirectory~"\se\eco00.wav");
        }
        if (count==10) {
            PlaySE(GetCurrentScriptDirectory~"\se\warp02.wav");
            SetScore(12000000);
            CutIn(YOUMU,"ugfrv",GetCurrentScriptDirectory~"img\remiria2.png",0,0,512,512);
        }

        if (count2>184&&bombalpha>0) {
            bombalpha=0;
        }
        c+=5-2*sin(count2*2);
        yield;

    }

    @Finalize()
    {
        if (GotSpellCardBonus()==true) {
            get++;
            if(GetCommonDataDefault("ii1",0)<2000) {
                SetCommonDataEx("Spell","2no2get",get);
            }
            else {
                SetCommonDataEx("Spell","2no2get2",get);
            }
        }
        save();
        SetShotColor(255,255,255);
        SetShotAutoDeleteClip(32,32,32,32);
        DeleteAllEnemyWithoutBoss();
        loop(120) {
            CreateItem(ITEM_SCORE,GetX()+rand(-160,160),GetY()+rand(-90,140));
        }

    }

    @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 (count<=1) {
                SetGraphicScale(0,0);
            }
            if (count>1) {
                SetGraphicScale(0.02*count+0.1*sin(count2*2),0.02*count+0.1*sin(count2*2));
            }
            if (count>=50) {
                SetGraphicScale(1.0+0.1*sin(count2*2),1.0+0.1*sin(count2*2));
            }
            DrawGraphic(GetX(),GetY());
        }

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

        if (count2<=184) {
            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) {
                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&&tame2<=0) {
                SetGraphicRect(128*3,0,128*4,128);
            }
            if (tame>32&&tame2<=0) {
                SetGraphicRect(128*3,128,128*4,256);
            }
            if (tame>42&&tame2<=0) {
                SetGraphicRect(128*3,256,128*4,384);
            }
            DrawGraphic(GetX(),GetY());
        }

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

        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);
    }

}

