#TouhouDanmakufu
#Title[Eirin tira planetas con Kaguya]
#Text[Y kaguya tira aliens]
#Player[FREE]
#ScriptVersion[2]
#BGM[.\ei.mp3]

script_enemy_main {
    let cn = ("script\Scarly\SE\se_charge01.wav");
    let shot = ("script\Scarly\PMex\PMShot.txt"); 	
    let CSD = GetCurrentScriptDirectory;
    let imgBoss = CSD ~ "img\bossei.png";
    let bg = CSD ~ "img\mw.jpg";
    let cut = CSD ~ "img\eiri.png";
    #include_function "script\Scarly\MCircle.txt";
    #include_function "script\Scarly\CutIn\function_cutin.txt"

    // initial position
    let XIni    = GetCenterX;
    let YIni    = GetClipMinY + 128;

    @Initialize {
        LoadUserShotData(shot);
        SetLife(4800);
        SetTimer(90);
        SetScore(2000000);
        SetInvincibility(150);
        SetEnemyMarker(true);
        LoadGraphic(imgBoss);
        LoadGraphic(ImgFam);
        SetTexture(imgBoss);
        setGraphicStop;
        LoadGraphic(bg);
        LoadGraphic(cut);
        PlaySE(cn);

        cutin("BYAKUREN","Trow sign -Venus ~ Aliens-",cut, 0, 0, 620, 600);

        TMain;
    }

    @MainLoop {
        SetCollisionA(GetX, GetY, 32);
        SetCollisionB(GetX, GetY, 16);
        yield;
    }

   @DrawLoop {
       Circle;
       SetTexture(imgBoss);
       SetRenderState(ALPHA);
       SetAlpha(255);
       SetGraphicRect(0, 0, 128, 128);
       SetGraphicScale(0.5, 0.5);
       SetGraphicAngle(0, 0, 0);
       DrawGraphic(GetX,GetY);
       let BossName="Eirin Yagokoro";
       DrawText(BossName, 36, 39, 12, 225);
       }

    @BackGround {
        SetTexture(bg);
        SetRenderState(ALPHA);
        SetAlpha(200);
        SetGraphicRect(0, 0, 512, 512);
        SetGraphicScale(1, 1);
        SetGraphicAngle(0, 0, 0);
        DrawGraphic(GetCenterX,GetCenterY);
        SetGraphicAngle(0, 0, 30);
         }


    @Finalize {
        DeleteGraphic(imgBoss);
        DeleteGraphic(ImgFam);
        DeleteGraphic(cut);
        DeleteGraphic(bg);

        PlaySE(CSD ~ "SE\death.wav");

        }

    // main task
    task TMain {
        yield;

        ready;
        TRate;

        arrangeFam;

        TLevitate;
        TFire;
    }

    // change the damage rates
    task TRate {
        wait(120);
        SetDamageRate(60, 60);
    }

    // go to the initial position and ready to attack
    sub ready {
        let wIni = 120 - WArrFam;

        SetMovePosition02(XIni, YIni, wIni);
        setGraphicMove;

        SetInvincibility(wIni);
        wait(wIni);

        setGraphicPose;
    }

    // arrange the familiars
    sub arrangeFam {
        // the center of the familiar triangle
        // The x-coordinate is measured from the center line.
        let xFamCenter = (GetCenterX - GetClipMinX) / 2;
        let yFamCenter = YIni;

        // direction angle of the familiars from the center
        let angleFam   = [0, 120, -120];

        // summon the left familiars
        ascent(i in 0..3) {
            CreateEnemyFromScript("Familiar", GetCenterX - xFamCenter, yFamCenter,
                                  0, angleFam[i], 1);
        }

        // summon the right familiars
        ascent(i in 0..3) {
            CreateEnemyFromScript("Familiar", GetCenterX + xFamCenter, yFamCenter,
                                  0, angleFam[i] + 180, -1);
        }

        wait(WArrFam);
    }

    // levitate little bit
    task TLevitate {
        let angle = 0;

        loop {
            SetY(YIni + 4 * sin(angle));
            yield;

            angle += 3;
        }
    }

    // fire the aiming shots
    task TFire {
        loop {
            wait(120);
            CreateShot01(GetX, GetY, 1.5, GetAngleToPlayer-90, 59, 60);
            CreateShot01(GetX, GetY, 1.5, GetAngleToPlayer, 59, 60);
            CreateShot01(GetX, GetY, 1.5, GetAngleToPlayer+90, 59, 60);
            PlaySE(CSD ~ "SE\elec.wav");
        }
    }

    // set the graphic region
    sub setGraphicStop  { SetGraphicRect(  0,   0,  64,  64); }    // stopping
    sub setGraphicPose  { SetGraphicRect( 64,   0, 128,  64); }    // special pose
    sub setGraphicLeft  { SetGraphicRect(128,   0, 192,  64); }    // move left
    sub setGraphicRight { SetGraphicRect(192,   0, 256,  64); }    // move right

    // set the graphic region according to the moving direction
    sub setGraphicMove {
        if(GetSpeedX < 0) {
            setGraphicLeft;
        } else {
            setGraphicRight;
        }
    }

    #include_function "script\Scarly\ChibiKaguyas.txt";
}

script_enemy Familiar {
    // center of the familiars
    let XCenter = GetX;
    let YCenter = GetY;

    // distance and direction angle from the center
    // The actual direction is dirBase + dir.
    let r       = 0;
    let dir     = 0;
    let DirBase = GetAngle;

    // moving direction (+-1)
    let DirMove = GetArgument;

    @Initialize {
        SetLife(2000);
        SetScore(10000);
        SetDamageRateEx(50, 50, 25, 25);

        SetTexture(ImgFam);
        setGraphicFast;

        TMain;
    }

    @MainLoop {
        // change the appearance according to the moving mode
        if(GetKeyState(VK_SLOWMOVE) == KEY_HOLD) {
            setGraphicSlow;
        } else {
            SetCollisionA(GetX, GetY, 32);
            setGraphicFast;
        }

        yield;
    }

    @DrawLoop {
        DrawGraphic(GetX, GetY);
    }

    // main task
    task TMain {
        yield;

        ready;

        TRotate;
        TFire;
    }

    // go to the initial position and ready to attack
    sub ready {
        let vr =  32 / WArrFam;    // radial velocity
        let w  = 210 / WArrFam;    // anglular velocity

        loop(WArrFam) {
            r += vr;
            move(w);
            yield;
        }
    }

    // rotate around the (XCenter, YCenter)
    task TRotate {
        wait(45);

        loop {
            move(0.77);
            yield;
        }
    }

    // fire to the center of the familiar triangle
    task TFire {
        loop {
            CreateShot01(GetX, GetY, 1.7, DirBase + dir + 180, 47, 0);
            wait(9);
            PlaySE(CSD ~ "SE\Shot1.wav");
        }
    }

    // move to the next position
    //     w: angular velocity
    function move(w) {
        dir += w * DirMove;

        SetX(XCenter + r * cos(DirBase + dir));
        SetY(YCenter + r * sin(DirBase + dir));
        SetGraphicAngle(0, 0, dir);    // tilt the graphic
    }

    // set the graphic region
    sub setGraphicFast { SetGraphicRect( 0,  0, 48, 48); }    // on fast move
    sub setGraphicSlow { SetGraphicRect( 0, 48, 48, 96); }    // on slow move

    #include_function "script\Scarly\ChibiKaguyas.txt";
}