
script_enemy_main
{
    let count=0;
    let count2=0;
    let count3=0;
    let count4=0;
    let huttobi=0;
    let huttobi2=0;
    let huttobi3=0;
    let huttobi4=0;
    let ang=270;
    let alp=250;
    let sAng=0;
    let ye=0;
    let ye2=0;
    let get;
    let try;
    let life=0;
    let item=0;
    let c=0;

    let move=0;

    let pass=GetCurrentScriptDirectory~"Spell.dat";

    let cx=GetCenterX();
    let mm=0;
    let shot=0;
    let tame=0;
    let tame2=0;
    let pat=0;
    let d=70;
    let ten=0;
    let damage=0;

    task CreateShot021(let x, let y, let speed, let angle, let Accelerate, let MaxSpeed, let grf, let delay)
    {
        let obj = Obj_Create(OBJ_SHOT);
        let N15 = 0;
        Obj_SetX(obj, x);
        Obj_SetY(obj, y);
        Obj_SetSpeed(obj, speed);
        Obj_SetAngle(obj, angle);
        ObjShot_SetGraphic(obj, grf);
        ObjShot_SetDelay(obj, delay);
        ObjShot_SetBombResist(obj,true);
        if (Accelerate<0) {
            N15=1;
        }
        if (Accelerate>0) {
            N15=2;
        }
        while ( !Obj_BeDeleted(obj) )
        {
            if (speed>MaxSpeed&&N15==1) {
                Obj_SetSpeed(obj, speed+Accelerate);
            }
            if (speed<MaxSpeed&&N15==1) {
                Obj_SetSpeed(obj, MaxSpeed);
            }
            if (speed<MaxSpeed&&N15==2) {
                Obj_SetSpeed(obj, speed+Accelerate);
            }
            if (speed>MaxSpeed&&N15==2) {
                Obj_SetSpeed(obj, MaxSpeed);
            }
            yield;
        }
    }

    task Homing(let x, let y, let speed, let angle, let delay)
    {
        //e^XN
        let count = 0;
        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, 255);//摜ݒ
        ObjShot_SetDelay(obj, delay);//xԐݒ
        let dAngle=2;

        while ( !Obj_BeDeleted(obj) )
        {
            //z[~O
            let toAngle=atan2(GetPlayerY()-Obj_GetY(obj), GetPlayerX()-Obj_GetX(obj));
            if (toAngle>=360) {
                toAngle-=360;
            }
            if (toAngle<0) {
                toAngle+=360;
            }
            let ret=toAngle-angle;
            if ((ret>0&&ret<=180)||(ret<=0&&ret<=-180)) {
                angle+=dAngle;
            }
            if ((ret>0&&ret>180)||(ret<=0&&ret>-180)) {
                angle-=dAngle;
            }
            if (angle>=360) {
                angle-=360;    //360ȏȂ0`360ɕ␳
            }
            if (angle<0) {
                angle+=360;    //0Ȃ0`360ɕ␳
            }
            Obj_SetAngle(obj, angle);
            Obj_SetSpeed(obj, speed);
            ObjShot_SetBombResist(obj,true);
            if (count>=9&&dAngle>0.2) {
                dAngle-=0.1;
                speed-=0.2;
            }
            if (count>=9&&dAngle<=0.2) {
                dAngle=0.2;
            }
            if (count>=120) {
                speed+=0.005;
            }
            count++;
            yield;
        }
    }


    function CreateWayShot02(Way,Gap,PosX,PosY,Dist,Speed,Angle,Accelerate,MaxSpeed,Graphic,Delay) {
        if (Graphic==100) {
            ascent(let i in 0..Way) {
                CreateShot021(PosX+Dist*cos(Angle+Gap*(i+0.5-(Way * 0.5))),PosY+Dist*sin(Angle+Gap*(i+0.5-(Way * 0.5))),Speed,Angle+Gap*(i+0.5-(Way*0.5)),Accelerate,MaxSpeed,Graphic,Delay);
            }
        }
        else {
            ascent(let i in 0..Way) {
                CreateShot02(PosX+Dist*cos(Angle+Gap*(i+0.5-(Way * 0.5))),PosY+Dist*sin(Angle+Gap*(i+0.5-(Way * 0.5))),Speed,Angle+Gap*(i+0.5-(Way*0.5)),Accelerate,MaxSpeed,Graphic,Delay);
            }
        }
    }

#include_function ".\BGfx.txt"

    @Initialize()
    {
        if (GetY>=-300) {
            MagicCircle(false);
            SetEnemyMarker(true);
            SetCommonData("P01",0);
            SetCommonData("P02",0);
            SetCommonData("P03",0);
            SetCommonData("P04",0);
            SetCommonData("P05",0);
            SetCommonData("Go",0);
        }
        SetLife(19000);
        SetDamageRate(900,100);
        //SetTimer(20000);
        SetInvincibility(150);
        SetNormPoint(-1);
        SetShotAutoDeleteClip(32,32,32,32);
        SetMovePositionRandom01(0,200,5,cx,150,cx,150);
        LoadUserShotData(GetCurrentScriptDirectory~".\shot_All.txt");

    }

    @MainLoop()
    {
        if (count%4==0) {
            life=GetLife();
        }c+=7;
        if (GetLife()>=200&&GetLife()<=2550&&count%4==3&&GetLife()<life) {
            SetDamageRate(0.1,0.1);
            AddLife(-80);
        }
        if (count%4==3&&GetLife()<life) {
            ten=1;
        }

        if (ten==1&&count%4==1) {
            ten=0;
        }



        if (pat==0)
        {

            if (count4==0&&move<=0)
            {
                SetSpeed(0);
                SetMovePositionRandom01(85,55,3,GetCenterX(),160,GetCenterX(),160);
                move=80;
            }

            if (count4>120&&count4<140&&ang>90) {
                ang-=10;
            }

            if (count4>90&&count4<240&&alp>0) {
                alp-=5;
            }

            if (count4==140&&move<=0)
            {
                SetSpeed(0);
                SetMovePositionRandom01(85,55,3,GetCenterX(),140,GetCenterX(),140);
                move=80;
            }

            if (count4>140&&count4<=156) {
                ye2+=5;
            }

            if (count4==150)
            {
                tame=0;
                tame+=GetAngleToPlayer();
            }

            if (count4>150&&count4<420&&count%13==7) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot06.wav");
                CreateWayShot02(2,44,GetX,GetY-8,0,9,90,-1,5,175,0);
                CreateWayShot02(1,0,GetX,GetY-8,0,9,90,-1,5,175,0);
            }

            if (count4>150&&count4<420&&count%13==4) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot06.wav");
                CreateWayShot02(2,22,GetX,GetY-8,0,9,90,-1,5,175,0);
            }

            if (count4>150&&count4<420&&count%12==5) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot04.wav");
                    CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX(),GetY()-55,-4,90+rand(-25,25),0.5);
               
                    CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX(),GetY()-55,-4,90+rand(-25,25),0.5);
                
            }

            if (count4>150&&count4<420&&count%12==10) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot04.wav");
                    CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX(),GetY()-20,-4,90+rand(15,55),0.5);
                
                    CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX(),GetY()-20,-4,90+rand(-15,-55),0.5);
                
            }

            if (count4>=180&&count4<420&&count%35==1)
            {
                SetSpeed(0);
                SetMovePositionRandom01(rand(22,155),rand(22,155),5,GetCenterX()-200,20,GetCenterX()+200,140);
                move=20;
            }

            if (count4>=70&&count<410) {
                tame2++;
            }
            if (count4>420) {
                pat=1;
                count4=0;
                tame2=0;
            }
            if (GetLife()<20) {
                pat=5;
                count4=0;
                tame2=0;
            }
        }








        if (pat==1)
        {

            if (count4<120&&ye2>0) {
                ye2-=5;
            }

            if (count4==120||count4==360||count4==450)
            {
                tame=0;
                tame+=GetAngleToPlayer();
            }

            if (count4>0&&count4<50&&count4%9==4) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot04.wav");
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()-1,GetY()+65,-2,90+rand(-11,11),0.5);
            }

            ascent( let t in 0..12 ) {
                if (tame>(t*30)-15&&tame<(t*30)+15) {
                    sAng=(t*30);    //΍
                }
            }

            if (count4>=130&&count4<=180&&count3%4==0) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot04.wav");
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()-26,GetY()-15,-5,GetAngleToPlayer(),0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()+26,GetY()-15,-5,GetAngleToPlayer(),0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()-26,GetY()-15,-7,90,0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()+26,GetY()-15,-7,90,0.5);
            }

            if (count4==50)
            {
                SetSpeed(0);
                SetMovePositionRandom01(255,255,15,GetCenterX()+188,110,GetCenterX()+188,150);
                move=40;
            }

            if (count4==130)
            {
                SetSpeed(0);
                SetMovePositionRandom01(255,255,15,GetCenterX()-188,110,GetCenterX()-188,150);
                move=40;
            }

            if (count4==220)
            {
                SetSpeed(0);
                SetMovePositionRandom01(255,255,15,GetCenterX()+188,210,GetCenterX()+188,250);
                move=40;
            }

            if (count4>=220&&count4<=270&&count3%4==0) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot04.wav");
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()-26,GetY()-15,-5,GetAngleToPlayer(),0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()+26,GetY()-15,-5,GetAngleToPlayer(),0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()-26,GetY()-15,-7,90,0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()+26,GetY()-15,-7,90,0.5);
            }

            if (count4==310)
            {
                SetSpeed(0);
                SetMovePositionRandom01(255,255,15,GetCenterX()-188,110,GetCenterX()-188,150);
                move=40;
            }

            if (count4>=310&&count4<=350&&count3%4==0) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot04.wav");
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()-26,GetY()-15,-5,GetAngleToPlayer(),0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()+26,GetY()-15,-5,GetAngleToPlayer(),0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()-26,GetY()-15,-7,90,0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()+26,GetY()-15,-7,90,0.5);
            }

            if (count4==390)
            {
                SetSpeed(0);
                SetMovePositionRandom01(255,255,15,GetCenterX()+188,210,GetCenterX()+188,250);
                move=40;
            }

            if (count4>=390&&count4<=450&&count3%4==0) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot04.wav");
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()-26,GetY()-15,-5,GetAngleToPlayer(),0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()+26,GetY()-15,-5,GetAngleToPlayer(),0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()-26,GetY()-15,-7,90,0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()+26,GetY()-15,-7,90,0.5);
            }

            if (count4==450)
            {
                SetSpeed(0);
                SetMovePositionRandom01(255,255,15,GetCenterX()+188,450,GetCenterX()+188,450);
                move=40;
            }

            if (count4==550)
            {
                SetSpeed(0);
                SetMovePositionRandom01(255,255,5,GetCenterX(),90,GetCenterX(),90);
                move=80;
            }

            if (count4>=550&&count4<=620&&count3%5==0) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot04.wav");
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()-26,GetY()-15,-2,GetAngleToPlayer(),0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()+26,GetY()-15,-2,GetAngleToPlayer(),0.5);
            }

            if (count4>=650&&count4<=840&&count4%1==0) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot02.wav");
                CreateWayShot02(1,0,GetX+35,GetY-2,0,rand(4,8),rand(0,360),0,5,175,0);
                CreateWayShot02(1,0,GetX-35,GetY-2,0,rand(4,8),rand(0,360),0,5,175,0);
            }

            if (count4>=850&&count4<=1040&&count3%4==0) {
                SetSpeed(0);
                SetMovePositionRandom01(255,255,1.1,GetPlayerX(),GetPlayerY(),GetPlayerX(),GetPlayerY());
                move=40;
            }

            if (count4>=850&&count4<=1040&&count4%10==0) {
                PlaySE(GetCurrentScriptDirectory~"\se\Shot06.wav");
                CreateWayShot02(1,0,GetX+35,GetY-2,0,5,GetAngleToPlayer()-rand(-22,22),-1,5,175,0);
                CreateWayShot02(1,0,GetX-35,GetY-2,0,5,GetAngleToPlayer()+rand(-22,22),-1,5,175,0);
                CreateWayShot02(1,0,GetX,GetY-42,0,5,GetAngleToPlayer()-rand(-22,22),-1,5,175,0);
                CreateWayShot02(1,0,GetX,GetY+46,0,5,GetAngleToPlayer()+rand(-22,22),-1,5,175,0);
            }

            if (count4==1045) {
                SetSpeed(0);
                SetMovePositionRandom01(255,255,0.5,GetX(),GetY()+11,GetX(),GetY()+11);
                move=30;
            }

            if (count4>=1050) {
                count4=540;
            }

            count3++;

            if (GetLife()<200) {
                pat=5;
                count4=0;
            }
        }



        if (count2>=2&&GetY<-300) {
            VanishEnemy;
        }


        if (count2==2200&&pat!=5) {
            count2=9000;
            pat=6;
            count4=0;
        }


        if (pat==5)//
        {
            SetEnemyMarker(false);
            AddLife(5-GetLife);
            move=300;
            if (count4==120) {
                SetCommonData("Go",1);
            }
            if (count4>120) {
                count3-=2;
            }
            if (count4==20&&GetCommonDataDefault("P01",0)!=2) {
                SetCommonData("P01",3);
            }
            if (count4==40&&GetCommonDataDefault("P02",0)!=2) {
                SetCommonData("P02",3);
            }
            if (count4==0) {
                if (item!=1&&GetCommonDataDefault("P01",0)==2&&GetCommonDataDefault("P02",0)==2&&GetCommonDataDefault("P03",0)==2&&GetCommonDataDefault("P04",0)==2) {
                    item=2;
                }
                SetSpeed(0);
                SetMovePositionRandom01(45,0,8,GetX(),GetY-20,GetX(),GetY-20);
                DeleteEnemyShotToItemInCircle(CHILD,GetX(),GetY(),500);
                PlaySE(GetCurrentScriptDirectory~"\se\Baaaaaaaan.wav");
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Servant10.txt",GetX,GetY,0,0,4);
            }
            if (count4<=120&&count%15==0) {
                PlaySE(GetCurrentScriptDirectory~"\se\Baaaan2.wav");
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Servant10.txt",GetX+rand(-73,73),GetY+rand(-93,53),0,0,2);
            }
            if (count4>=240) {
                SetEnemyMarker(false);
                VanishEnemy;
            }
            if (count4==120) {
                PlaySE(GetCurrentScriptDirectory~"\se\bom13.wav");
                SetCommonData("flash2",255);
                if (count2<=8000) {
                    AddScore(10000000);
                    loop(100) {
                        CreateItem(ITEM_SCORE,GetX()+rand(-150,150),GetY()+rand(-90,135));
                    }
                    CreateItem(ITEM_1UP,GetX(),GetY()+35);
                }
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Servant10.txt",GetX,GetY,0,0,4);
            }
        }

        if (pat==6)//
        {
            SetDamageRate(0,0);
            if (count4==0&&GetCommonDataDefault("P01",0)!=2) {
                SetCommonData("P01",3);
            }
            if (count4==0&&GetCommonDataDefault("P02",0)!=2) {
                SetCommonData("P02",3);
            }
            if (count4<200&&count4>10&&count4%6==3) {
                MagicCircle(false);
                SetEnemyMarker(false);
                PlaySE(GetCurrentScriptDirectory~"\se\Shot04.wav");
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()-26,GetY()-15,-5,GetAngleToPlayer(),0.5);
                CreateEnemyFromFile(GetCurrentScriptDirectory~"Missiles.txt",GetX()+26,GetY()-15,-5,GetAngleToPlayer(),0.5);
            }
            if (count4>=50)
            {
                SetSpeed(0);
                SetMovePositionRandom01(0,0,1.5,GetPlayerX(),-200,GetPlayerX(),-200);
                move=80;
            }
            if (count4>=400) {
                SetEnemyMarker(false);
                SetCommonData("Go",1);
                VanishEnemy;
            }
        }




        if (count>=120&&GetLife()>200&&pat!=6) {
            SetCollisionA(GetX(),GetY()+26,36);
            SetCollisionA(GetX(),GetY()-11,22);
            SetCollisionB(GetX(),GetY(),22);
        }

        count++;
        count4++;
        count2++;
        move--;
        if (ye>0) {
            ye-=4;
        }
        if (ye<0) {
            ye=0;
        }
        if (ye2>0&&ye<244) {
            ye+=6;
        }

        SetCommonData("x",GetX);
        SetCommonData("y",GetY);
        yield;
    }

    @Finalize()
    {
        SetEnemyMarker(false);
    }

    @DrawLoop()
    {
        if (pat!=5) {
            SetTexture(GetCurrentScriptDirectory~"\img\familiar.png");
            if (count<100) {
                SetColor(0,0,0);
            }
            if (count>=100) {
                SetAlpha((count-100)*2.5);
                SetColor((count-100)*1.2,(count-100)*1.2,(count-100)*1.2);
            }
            if (count>=200) {
                SetAlpha(255);
                SetColor(255,255,255);
            }
            SetGraphicAngle(0,0,0);
            SetRenderState(ADD);
            SetGraphicRect(0,48,48,96);
            SetGraphicScale(1.3,1.3);
            if (ten==1) {
                SetColor(110,110,180);
            }
            if (count>100) {
                DrawGraphic(GetX(),GetY()+46);
                DrawGraphic(GetX(),GetY()-42);
                DrawGraphic(GetX()+35,GetY());
                DrawGraphic(GetX()-35,GetY());
            }
        }

        SetTexture(GetCurrentScriptDirectory~"\img\JUUJI.png");
        SetRenderState(ADD);
        SetGraphicScale(1.3,1.3);

        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.02*count);
            }
            if (count>=50) {
                SetGraphicScale(1.0+0.1*sin((count-50)*3),1.0+0.1*sin((count-50)*3));
            }
            if (pat==5&&count4>=120) {}else {
                DrawGraphic(GetX(),GetY());
            }
        }

        SetTexture(GetCurrentScriptDirectory~"\img\dot_sakuya.png");
        SetAlpha(255);
        SetGraphicAngle(0,0,0);
        SetColor(255,255,255);
        SetRenderState(ALPHA);
        SetGraphicScale(1,1);
        SetGraphicRect(0,0,128,128);
        if(int(GetSpeedX())==0&&count2/12%3>=0){SetGraphicRect(0,0,128,128);}
        if(int(GetSpeedX())==0&&count2/12%3>=1){SetGraphicRect(0,128,128,256);}
        if(int(GetSpeedX())==0&&count2/12%3>=2){SetGraphicRect(0,256,128,384);}
        //if(int(GetSpeedX())==0&&count2/12%4>=3){SetGraphicRect(0,384,128,512);}
        if(GetSpeedX()<0&&move>0){SetGraphicRect(256,0,384,128);}
        if(GetSpeedX()<0&&move>8){SetGraphicRect(256,128,384,256);}
        if(GetSpeedX()>=0&&move>0){SetGraphicRect(256,256,384,384);}
        if(GetSpeedX()>=0&&move>8){SetGraphicRect(256,384,384,512);}
        if(tame2>0){SetGraphicRect(128*4,0,128*5,128);}
        if(tame2>60){SetGraphicRect(128*4,128,128*5,256);}
        if(tame2>65){SetGraphicRect(128*5,0,128*6,128);}

        if (ten==1) {
            SetColor(60,60,180);
        }
        if (pat==5&&count4>=120) {}else {
            DrawGraphic(GetX(),GetY());
        }

    }


}

