#TouhouDanmakufu
#Title[Reimu Spellcard 2]
#Text[Dream Sign "Illusionary Rain"
Raining amullets, yay.]
#Player[FREE]
#ScriptVersion[2]


script_enemy_main
{
let CSD = GetCurrentScriptDirectory;
#include_function ".\newsfx.txt" // Non ZUN sfx
#include_function ".\function_cutin.txt"
#include_function ".\Obj_hpb.txt" // TLC Lifebar, no lags :)
#include_function ".\function_own.txt" // This script uses some Japanese functions
#include_function ".\function_value.txt"// Sadly they collide with some TheLastCorner Functions like the Cutin, so im using only function_cutin :(
let SpellBG1 = CSD~"system/1.png";
let SpellBG2 = CSD~"system/2.png";
let shot = GetCurrentScriptDirectory() ~ "kshotData.txt" ; // TLC shots
let frame = 0 ;
let cut = CSD ~ "system\Raymoo.png";
//let minx = GetClipMinX ;
//let maxx = GetClipMaxX ;
//let miny = GetClipMinY ;
//let maxy = GetClipMaxY ;
let bgalpha = 0;	//We create a new variable so that the background can fade in smoothly.
let bgangle = 0;	//Angle for the overlayed BG. 
let boss =  GetCurrentScriptDirectory ~ "system\raymoosprite.png" ;


@Initialize
{
LoadUserShotData( shot ) ;
cutin("NAZRIN", "Dream Sign "\""Illusionary Rain"\" ,cut,0,0,552,620);
SetScore( 2500000 ) ;
LoadGraphic(SpellBG1);
LoadGraphic(SpellBG2);
LoadGraphic(cut);
SetX( GetCenterX ) ;
SetY( GetClipMinY + 130 ) ;
SetLife( 400 ) ;
SetTimer( 40 ) ;
SetDamageRate( 16, 10 ) ;
SetInvincibility( 180 ) ;
let frame = 0 ;
hpb(1,0,0);
MainTask ;
}

@MainLoop
{
SetCollisionA( GetX, GetY, 24 ) ;
SetCollisionB( GetX, GetY, 24 ) ;
if ( frame % 360 == 0 )
  {
  moveToPlayer( rand( 30, 100 ), rand( -50, 50 ), 60, GetClipMinX + 32, GetClipMinY + 32, GetClipMaxX - 32, GetClipMinY + 160 ) ;
  }
yield ;
frame += 1 ;
}

@DrawLoop
{
DrawText("Reimu Hakurei", 40, 35, 12, 255);
LoadGraphic(boss) ;
SetTexture(boss) ;
SetGraphicRect( 0, 0, 63, 73 ) ;
DrawGraphic( GetX, GetY ) ;

}

@Finalize
{
DeleteGraphic(boss) ;

}

task MainTask
{
loop( 60 ) { yield ; }
PlaySE(charge);
SetColor(255,0,0);
Concentration01(60);
SetColor(255,255,255);
loop( 60 ) { yield ; }
Shot1 ;
Shot2 ;
}
task Shot1{
let x;
        let y;
        loop {
PlaySE(shot1);
            shotL(6, 1);
            wait(70);
PlaySE(kira);
wait(10);
PlaySE(shot1);
            shotR(6, -1);
            wait(70);
PlaySE(kira);
wait(10);
        }
}
task Shot2{
let x;
        let y;
        let angle;
        let dir;
        let radius;

        loop {
            SetShotDirectionType(ABSOLUTE);
            x = rand(GetClipMinX, GetClipMaxX);
            y = GetClipMinY + rand(0, 200);
            shotF(GetX, GetY, x, y, rand_int(60, 120), 10);
            wait(1);
        }
}
task shotF(x1, y1, x2, y2, delayA, delayB) {
        let angle = atan2(y2 - y1, x2 - x1);

        SetShotColor(255, 255, 255);

        let obj = Obj_Create(OBJ_SHOT);
        ObjShot_SetGraphic(obj, 93);
        Obj_SetAngle(obj, angle);
        Obj_SetSpeed(obj, 0);
        Obj_SetPosition(obj, x1, y1);

        SetShotColor(200, 255, 255);

        let obj2 = Obj_Create(OBJ_SHOT);
        ObjShot_SetGraphic(obj2, 93);
        Obj_SetAngle(obj2, angle);
        Obj_SetSpeed(obj2, 0);
        Obj_SetPosition(obj2, x1, y1);

        SetShotColor(255, 255, 255);

        let distance0 = ((x1 - x2)^2 + (y1 - y2)^2)^0.5;
        let p = 1;
        let distance = distance0 / (1 - 0.97^delayA);
        let frame = 0;

        loop(delayA) {
            Obj_SetPosition(obj, x1 + distance * (1 - p) * cos(angle),
                                 y1 + distance * (1 - p) * sin(angle));
            Obj_SetPosition(obj2, x1 + distance * (1 - p) * cos(angle),
                                  y1 + distance * (1 - p) * sin(angle));
            p *= 0.97;
            if(frame == 5) {
                ObjShot_SetDelay(obj2, 8);
                frame = 0;
            }
            frame ++;
            yield;
        }
        Obj_SetPosition(obj, x2, y2);
        Obj_SetPosition(obj2, x2, y2);

        ObjShot_SetGraphic(obj2, 13);
        let alpha = 0;
        loop(delayB) {
            Obj_SetPosition(obj, x1 + distance * (1 - p) * cos(angle),
                                 y1 + distance * (1 - p) * sin(angle));
            Obj_SetPosition(obj2, x1 + distance * (1 - p) * cos(angle),
                                  y1 + distance * (1 - p) * sin(angle));
            p *= 0.98;
            Obj_SetAlpha(obj, 255 - alpha);
            Obj_SetAlpha(obj2, alpha);
            alpha += 255 / delayB;
            yield;
        }

        Obj_Delete(obj);
        Obj_SetAlpha(obj2, 255);
        Obj_SetAngle(obj2, 90);

        while(Obj_BeDeleted(obj2) == false) {
            Obj_SetSpeed(obj2, Obj_GetSpeed(obj2) + 0.02);
            yield;
        }
    }
 task shotL(speed, SHIFT) {
        SetShotDirectionType(ABSOLUTE);

        let quantity = 5;
        let xDif = (GetClipMaxX - GetClipMinX) / quantity;
        let shift = 18 * (quantity - 1) / 2;

        let obj = Obj_Create(OBJ_SHOT);
        ObjShot_SetGraphic(obj, 131);
        ObjShot_SetBombResist(obj, true);
        Obj_SetAutoDelete(obj, false);
        Obj_SetAngle(obj, rand(-15, 5));
        Obj_SetSpeed(obj, speed);
        Obj_SetPosition(obj, GetClipMinX - xDif * rand(0.3, 0.7),
                             GetClipMinY + rand(160, 200));

        let delay = int(xDif / (speed * cos(Obj_GetAngle(obj)))) + 1;

        loop(quantity) {
            wait(delay);
            shotP(Obj_GetX(obj), Obj_GetY(obj), shift, SHIFT, 121, 93);
            shift += -19;
        }

        Obj_SetAutoDelete(obj, true);
    }

 
    task shotR(speed, SHIFT) {
        SetShotDirectionType(ABSOLUTE);

        let quantity = 5;
        let xDif = (GetClipMinX - GetClipMaxX) / quantity;
        let shift = -18 * (quantity - 1) / 2;

        let obj = Obj_Create(OBJ_SHOT);
        ObjShot_SetGraphic(obj, 136);
        ObjShot_SetBombResist(obj, true);
        Obj_SetAutoDelete(obj, false);
        Obj_SetAngle(obj, rand(175, 195));
        Obj_SetSpeed(obj, speed);
        Obj_SetPosition(obj, GetClipMaxX - xDif * rand(0.3, 0.7),
                             GetClipMinY + rand(160, 200));

        let delay = int(xDif / (speed * cos(Obj_GetAngle(obj)))) + 1;

        loop(quantity) {
            wait(delay);
            shotP(Obj_GetX(obj), Obj_GetY(obj), shift, SHIFT, 123, 94);
            shift += 19;
        }

        Obj_SetAutoDelete(obj, true);
    }

    task shotP(x, y, shiftA, shiftB, colorA, colorB) {
        let angle = shiftA;
        let speed = 0;
        let speed2 = 4;
        let delay = 5;
        let frame = 60;

        let quantityA = 12;
        let quantityB = 5;

        let dir = shiftA;

        loop(quantityA) {
            SetShotDirectionType(ABSOLUTE);
            CreateShotA(0, x, y, 5);
            SetShotDataA(0, 0, speed, 270, 0, -speed / 45, 0, colorA);

            SetShotDirectionType(PLAYER);
            angle = shiftA;
            speed2 = 4;
            delay = 15;
            loop(quantityB) {
                CreateShotA(1, 0, 0, delay);
                SetShotDataA(1, 0, speed2, angle, 0, 0, 0, colorB);
                AddShot(frame, 0, 1, 0);
                dir += 360 / quantityB;
                angle += shiftB;
                speed2 += 0.22;
                delay += -1;
            }
            SetShotDirectionType(ABSOLUTE);

            SetShotKillTime(0, frame + 1);
            FireShot(0);

            speed += 0.5;
            frame += 2;
        }
    }
@BackGround{

		if (bgalpha < 255){bgalpha += 5;}


SetAlpha(bgalpha);
SetGraphicScale(1.2,1.2);	//The background is scaled up so it can fill the whole screen.
SetColor(255,255,255);
SetGraphicRect(0,0,512,512);

SetTexture(SpellBG1);
DrawGraphic(GetCenterX,GetCenterY);

SetTexture(SpellBG2);
SetGraphicAngle(0,0,bgangle);
DrawGraphic(GetCenterX,GetCenterY);
	}

}