#TouhouDanmakufu
#Title[FdRstar : "Stream flowers under the shooting stars"]
#Text[La derniere SC de FdRstar necessite des mouvements rapides mais precis. Eviter ces fleurs seraient plus aise si seulement J64 n'etait pas venu fourer son nez la-dedans ! Qu'il est agacant...]
#Image[]
#BGM[script\DanMUku\Stage4\FdRstar.mp3]
#Player[script\DanMUku\Joueur\KorA\KorHosik.txt, script\DanMUku\Joueur\KorB\KorB.txt, script\DanMUku\Joueur\KorC\KorC.txt]
#ScriptVersion[2]

script_enemy_main
{    
    let imgEnemy ="script\DanMUku\Stage4\Image\fdrstar.png";
    let count = -60;
    let tir ="script\DanMUku\Stage1\Son\tir.wav";
    let munch ="script\DanMUku\Stage1\Son\muncher.wav";
    let a=0;
    let b=55;
    let x=GetX;
    let c=0;
    let dif=3;
    let dir=0;
    let d=0;
    let angle=0;
    let q=0;
    let arbre = "script\DanMUku\Stage1\Image\arbre.png";
    let bg = "script\DanMUku\Stage1\Image\sol.png";


function ShootShape(spawnX, spawnY, v, angle, angle2, num, bside, graphic, delay, size) {
	ascent(i in 0..num) {
		let sx = spawnX+cos(angle+i*360/num)*size; let sy = spawnY+sin(angle+i*360/num)*size; let sxN = spawnX+cos(angle+(i+1)*360/num)*size; let syN = spawnY+sin(angle+(i+1)*360/num)*size;
		Tir(spawnX, spawnY, v*(((sx-spawnX)^2+(sy-spawnY)^2)^0.5), v, atan2(sy-spawnY,sx-spawnX), angle2, graphic, delay);
		ascent(j in 0..bside) {
			let toAngle = atan2(syN-sy,sxN-sx); let toDist = (((sxN-sx)^2+(syN-sy)^2)^0.5); let sx2 = sx+toDist/bside*j*cos(toAngle); let sy2 = sy+toDist/bside*j*sin(toAngle);
			Tir(spawnX, spawnY, v*(((sx2-spawnX)^2+(sy2-spawnY)^2)^0.5), v, atan2(sy2-spawnY,sx2-spawnX), angle2, graphic, delay);
		}
	}
}


 function Wait(let frames){
   loop(frames){yield;}
 }

   @Initialize
   {
            LoadUserShotData("script\DanMUku\MUtir\MUtir.txt");
            LoadGraphic(imgEnemy);
	    LoadGraphic(bg);
	    LoadGraphic(arbre);
            SetLife(2000);
            SetTimer(50);
            SetInvincibility(120);
            SetEnemyMarker(true);
            SetScore(3000000);
            CutIn(YOUMU, "Stream flowers under the shooting stars", "script\DanMUku\Stage4\Image\FdRcutin.png", 0,0,121,214);
	    SetEffectForZeroLife(60,200,2);
            SetMovePosition02(GetCenterX, GetCenterY-100, 50);   
           
   }
   
   @MainLoop
   {
        SetCollisionA(GetX, GetY, 16);
        SetCollisionB(GetX, GetY, 16);
if(count%10==0){
	if(a==0){a=55;b=112;c=0;}
	else if(a==55&&c==0){a=112;b=168;}
	else if(a==55&&c==1){a=0;b=55;}
	else{a=55;b=112;c=1;}
	}

	if(count%180==0&&count>=0)
	{
	angle=GetAngleToPlayer;
	SetMovePositionRandom01(100, 100, 3, GetClipMinX+10, GetClipMinY+10, GetClipMaxX-10, GetCenterY-60);
	ShootShape(GetX, GetY, 3, 0, angle, 4, 7, 67, 45, 0.8);
	ShootShape(GetX, GetY, 3, 45, angle, 4, 7, 67, 45, 0.8);
		loop(5){
			Butterfly(GetX, GetY, 1, 0.5, angle, 0, 0, 60, 2, 45);
			angle+=12;
			}
	}
	if(count%180==10&&count>0)
	{
	ShootShape(GetX, GetY, 3, 0, angle,4, 7, 72, 45, 0.8);
	ShootShape(GetX, GetY, 3, 45, angle, 4, 7, 72, 45, 0.8);
		loop(5){
			Butterfly(GetX, GetY, 1, 0.5, angle, 0, 0, 65, 2, 45);
			angle+=12;
			}
	}
	if(count%180==20&&count>0)
	{
	ShootShape(GetX, GetY, 3, 0, angle,4, 7, 71, 45, 0.8);
	ShootShape(GetX, GetY, 3, 45, angle, 4, 7, 71, 45, 0.8);
		loop(5){
			Butterfly(GetX, GetY, 1, 0.5, angle, 0, 0, 64, 2, 45);
			angle+=12;
			}
	}
	if(count%180==30&&count>0)
	{
	ShootShape(GetX, GetY, 3, 0, angle,4, 7, 70, 45, 0.8);
	ShootShape(GetX, GetY, 3, 45, angle, 4, 7, 70, 45, 0.8);
		loop(5){
			Butterfly(GetX, GetY, 1, 0.5, angle, 0, 0, 63, 2, 45);
			angle+=12;
			}
	}
	if(count%180==40&&count>0)
	{
	ShootShape(GetX, GetY, 3, 0, angle,4, 7, 69, 45, 0.8);
	ShootShape(GetX, GetY, 3, 45, angle, 4, 7, 69, 45, 0.8);
		loop(5){
			Butterfly(GetX, GetY, 1, 0.5, angle, 0, 0, 62, 2, 45);
			angle+=12;
			}
	}
	if(count%180==50&&count>0)
	{
	ShootShape(GetX, GetY, 3, 0, angle,4, 7, 68, 45, 0.8);
	ShootShape(GetX, GetY, 3, 45, angle, 4, 7, 68, 45, 0.8);
		loop(5){
			Butterfly(GetX, GetY, 1, 0.5, angle, 0, 0, 61, 2, 45);
			angle+=12;
			}
	}


	if(count%480==0&&count>0){
	CreateEnemyFromScript("j64", GetClipMinX, rand(50,150), 5, rand(-10,10), 0);
	}


	if(count%480==240&&count>0){
	CreateEnemyFromScript("j64", GetClipMaxX, rand(50,150), 5, rand(170,190), 0);
	}

count++;
yield;
   }
   @Finalize
   {
      DeleteGraphic(imgEnemy);
   }
   
   @DrawLoop
   {
            SetTexture(bg);
            SetRenderState(ALPHA);
            SetAlpha(255);
            SetGraphicRect(0, 0, 50000+q, 50000+q);
            SetGraphicScale(1,1);
            SetGraphicAngle(0, 0, 0);
            DrawGraphic(GetCenterX+q, GetCenterY+q);

            SetTexture(arbre);
            SetRenderState(ALPHA);
            SetAlpha(255);
            SetGraphicRect(0, 0, 50000+q, 50000+q);
            SetGraphicScale(1,1);
            SetGraphicAngle(0, 0, 0);
            DrawGraphic(GetCenterX+q, GetCenterY+q);

	if(x>GetX){
		SetGraphicAngle(180,0,0);
	}
	if(x<GetX){
		SetGraphicAngle(0,0,0);
	}
      x=GetX;
            SetColor(255,255,255);
            SetRenderState(ALPHA);
            SetTexture(imgEnemy);
            SetGraphicRect(a, 0, b, 64);
            DrawGraphic(GetX,GetY);

	DrawText("FdRstar", GetClipMinX+10, GetClipMinY+15, 12, 255);
	q-=1;
   }

task Tir(x, y, v, vv, angle, angle2, graphic, delay){

            let obj=Obj_Create(OBJ_SHOT);
            Obj_SetPosition(obj, x, y);
            Obj_SetAngle(obj, angle);
            Obj_SetSpeed(obj, v);
            ObjShot_SetGraphic(obj, graphic);
	    ObjShot_SetBombResist(obj, true);
	    Wait(delay);
	    Obj_SetAngle(obj, angle2);
	    Obj_SetSpeed(obj, vv);

	}
    task Butterfly(x, y, v, acc, angle, angv, delay, bullet, type, time)
   {
            let obj=Obj_Create(OBJ_SHOT);

            Obj_SetPosition(obj, x, y);
            Obj_SetAngle(obj, angle);
            Obj_SetSpeed(obj, 0);
            ObjShot_SetGraphic(obj, bullet);
            ObjShot_SetDelay  (obj, delay);
            ObjShot_SetBombResist(obj, true);

		Papillon(obj, 0.1, 5, type, time, bullet);
            while(Obj_BeDeleted(obj)==false)
      {
	Obj_SetAngle(obj, Obj_GetAngle(obj)+angv);
	if(Obj_GetSpeed(obj)<v){
		Obj_SetSpeed(obj, Obj_GetSpeed(obj)+acc);
	}
         yield;
            }
   }

	task Papillon(obj, acc, vm, type, time, bullet)
     {
	let d=vm;
	Wait(time);
	if(type==2){
		PlaySE(munch);
		CreateShot02(Obj_GetX(obj), Obj_GetY(obj), 0, atan2(-Obj_GetY(obj)+GetPlayerY, -Obj_GetX(obj)+GetPlayerX)+20, acc, d*1.5, bullet, 10);
		CreateShot02(Obj_GetX(obj), Obj_GetY(obj), 0, atan2(-Obj_GetY(obj)+GetPlayerY, -Obj_GetX(obj)+GetPlayerX)-20, acc, d*1.5, bullet, 10);
		}
	Obj_Delete(obj);
}

}


script_enemy j64
{    
   let imgEnemy="script\DanMUku\Stage4\Image\j64.png";
   let count=0;
   let d=0;
   let x=GetX;
   let colour=0;
   let couleur=[38, 43, 40, 45, 47, 50];
   @Initialize
   {            
		LoadUserShotData("lib\MUtir\MUtir.txt");
            LoadGraphic(imgEnemy);
            SetLife(15);    
   }
   
   @MainLoop
   {
	if(count%12==0)
	{
		colour=rand_int(53,58);
		d=rand(80,100);
		CreateShot01(GetX, GetY, 5, d, colour, 0);
		CreateShot02(GetX, GetY, 1, d, 0.5, 8, couleur[colour-53], 0);

	}
      count++;
   }
   
   @Finalize
   {
      DeleteGraphic(imgEnemy);
   }
   
   @DrawLoop
   {
	if(x>GetX){
		SetGraphicAngle(180,0,0);
	}
	if(x<GetX){
		SetGraphicAngle(0,0,0);
	}
      x=GetX;
      SetColor(255,255,255);
            SetRenderState(ALPHA);
            SetTexture(imgEnemy);
            SetGraphicRect(0,0,36,43);
            DrawGraphic(GetX,GetY);
   }
}
