
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 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 img=GetCurrentScriptDirectory~"\img\JUUJI.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 Beem=GetCurrentScriptDirectory~"\se\Laser.wav";

    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)
    {
        //弾タスク初期化
        let count = 0;
        let obj = Obj_Create(OBJ_SHOT);//弾オブジェクトを作成
        Obj_SetX(obj, x);//x座標設定
        Obj_SetY(obj, y);//y座標設定;
        Obj_SetSpeed(obj, speed);//速度設定
        Obj_SetAngle(obj, angle);//移動角度設定
        ObjShot_SetGraphic(obj, 255);//画像設定
        ObjShot_SetDelay(obj, delay);//遅延時間設定
        let dAngle=2;

        while ( !Obj_BeDeleted(obj) )
        {
            //ホーミング
            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(1800);
        SetDamageRate(100,20);
        //SetTimer(20000);
        SetInvincibility(150);
        SetNormPoint(-1);
        LoadGraphic(img);
        SetShotAutoDeleteClip(32,32,32,32);
        SetMovePositionRandom01(0,200,5,cx,150,cx,150);

    }

    @MainLoop()
    {

        SetLife(20);
        move=300;
        if (count4==120) {
            SetCommonData("Go",1);
        }
        if (count4>120) {
            count3-=2;
        }
        if (count4==0) {
            SetSpeed(0);
            SetMovePositionRandom01(45,0,8,GetCenterX(),GetY-40,GetCenterX(),GetY-40);
            DeleteEnemyShotToItemInCircle(CHILD,GetX(),GetY(),500);
            PlaySE(GetCurrentScriptDirectory~"\se\Baaaaaaaan.wav");
            CreateEnemyFromFile(GetCurrentScriptDirectory~"Servant10.txt",GetX,GetY,0,0,4);
        }
        if (count4<=190&&count%15==0) {
            PlaySE(GetCurrentScriptDirectory~"\se\Baaaan2.wav");
            CreateEnemyFromFile(GetCurrentScriptDirectory~"Servant10.txt",GetX+rand(-43,43),GetY+rand(-43,43),0,0,2);
        }
        if (count4>=80&&count4<=190&&count%2==0) {
            SetEnemyMarker(false);
            SetMovePositionRandom01(rand(12,22),rand(12,22),8,GetCenterX()-21,GetY-2,GetCenterX()+21,GetY+1);
        }
        if (count4==190&&count%3==0) {
            SetEnemyMarker(false);
            SetMovePositionRandom01(5,5,8,GetCenterX()-21,GetY-24,GetCenterX()+21,GetY-24);
        }
        if (count4>=380) {
            VanishEnemy;
        }
        if (count4==240) {
            PlaySE(GetCurrentScriptDirectory~"\se\bom13.wav");
            SetCommonData("flash2",255);
            if (count2<=8000) {
                AddScore(10000000);
                loop(80) {
                    CreateItem(ITEM_SCORE,GetX()+rand(-150,150),GetY()+rand(-80,120));
                }
                CreateItem(ITEM_1UP,GetX(),GetY()+35);
            }
            CreateEnemyFromFile(GetCurrentScriptDirectory~"Servant10.txt",GetX,GetY,0,0,4);
        }


        count++;
        count4++;
        count2++;
        move--;
        yield;
    }

    @Finalize()
    {
        SetEnemyMarker(false);
    }

    @DrawLoop()
    {
        if (count4<=240) {
            SetTexture(GetCurrentScriptDirectory~"\img\dot_flandre.png");
            SetAlpha(255);
            SetGraphicAngle(0,0,0);
            SetColor(255,120,120);
            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 (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*3,0,128*4,128);
            }
            if (tame2>32) {
                SetGraphicRect(128*3,128,128*4,256);
            }
            if (tame2>42) {
                SetGraphicRect(128*3,256,128*4,384);
            }
            if (ten==1) {
                SetColor(160,160,180);
            }
            if (pat==5&&count4>=120) {}else {
                DrawGraphic(GetX(),GetY());
            }
        }

    }


}

