#TouhouDanmakufu
#Title[SatoriApollo H]
#Text[Nonspell]
#BackGround[User(.\img\stage.png,0,-0.5)]
#PlayLevel[Extra]
#Player[FREE]
#BGM[.\Heartfelt Fancy.mp3]
#ScriptVersion[2]

script_enemy_main
{
#include_function ".\Library.dnh"

let SatoriSprite = csd~"img/Satori/SatoriSprite.png";

@Initialize
	{
	LoadUserShotData(csd~"Shot.txt");
	LoadGraphic(SatoriSprite);
	SetLife(8000);
	SetEnemyMarker(true);
	SetDamageRate(100,10);
	SetTimer(141);
	//let a=0;
	SetEffectForZeroLife(60,0,0);	
	SetDurableSpellCard;
	

MainTask;
Vulnerable;
Timer;
IdleSatori;
//MoveAbout;
SetMovePosition03(GetCenterX, GetCenterY - 30, 30,10);
}

task Vulnerable
{
Wait(1200);
SetDamageRate(50,50);
}


task MainTask
{
Wait(120);

loop
	{
	let angle = rand(0,360);
	
	PlaySE(Shot2);

	Wait(20);
	let a=0;
	let t=-1;
	let u=1;
	loop(8000)
		{
		
		
		if(t%360==0){
//ascent(j in 0..2){ascent(i in 0..16){CreateShot02(GetX,GetY,3,22.5*i,-0.2,2.3,229+j,180*j+30-10*(-1)^i)}};
ascent(i in 0..12)
		{
		//Wait(3);
		}}
		u++;
		let n=a+2;
		let r=25;
		let R=-80;
		let j=1;
		let b=1+floor((8000-GetEnemyLife)/1000);
		if(GetTimer<40){b=15}
		let s=2.324;
		let S=26.189;
		let v=2.5;
		//if(t%20==10){ascent(i in 0..36){CreateShot02(GetX,GetY,5,a+2*360/72+360/36*i,-0.1,-2,95+2*(t%7),0)}}
		if(t%30==15){a=rand(0,360);u=-u;ascent(i in 0..30){
		//Bouncer(GetX,GetY,2,12*i,140,25,229+(t%b),0,t)
		}
		ascent(i in 0..3*b){Hopper(GetX,GetY,3+1.5*b,a+360/(3*b)*i,360*10,55+b,10)}
};
		t++;
		
		
		
	
		angle += rand(0,360);
		yield;
		}
	Wait(60);
	}
}
task Hopper(x,y,s,a,h,t,d){
		let obj= Obj_Create(OBJ_SHOT);
		ObjShot_SetGraphic(obj, t);
		ObjShot_SetDelay(obj,d);
		Obj_SetX(obj, x);
		Obj_SetY(obj, y);
		Obj_SetAngle(obj,a+90);
		Obj_SetSpeed(obj,s);
		let c=h;
		let v=s;
		let do=0; 

		while( !Obj_BeDeleted(obj)){
			do++;
			c=0.992*c;
			v=0.995*v;
			Obj_SetAngle(obj,a+c);
			Obj_SetSpeed(obj,v);
			if(do>800){Obj_SetSpeed(obj,0.5);}
			yield;
		}
}
task Bouncer(x,y,s,a,R,n,t,d,time){
		let obj= Obj_Create(OBJ_SHOT);
		ObjShot_SetGraphic(obj, t);
		ObjShot_SetDelay(obj,d);
		Obj_SetX(obj, x);
		Obj_SetY(obj, y);
		Obj_SetAngle(obj,a);
		Obj_SetSpeed(obj,s);
		let c=0;
		let done=0; 

		while( !Obj_BeDeleted(obj)){
			
			if((done==0) && (((Obj_GetX(obj)-x)^2+(Obj_GetY(obj)-y)^2)^(1/2)>=R)){ 
				if((5+(time+c*R+Obj_GetAngle(obj))*n/360-floor((time+c*R+Obj_GetAngle(obj))*n/360))%1<0.6){
						Obj_SetAngle(obj,Obj_GetAngle(obj)+180);c=c+1/s;}
						else{done=1;Obj_SetSpeed(obj, s/(cos((time%7-3)*20))^(1/2));Obj_SetAngle(obj, Obj_GetAngle(obj)+(time%7-3)*10)}
				}
			yield;
		}
}

task Waiter(x,y,s,a,t,d,w){
		let obj= Obj_Create(OBJ_SHOT);
		let hit=w;
		ObjShot_SetGraphic(obj, t);
		ObjShot_SetDelay(obj,d);
		Obj_SetX(obj, x);
		Obj_SetY(obj, y);
		Obj_SetAngle(obj,a);
		Obj_SetSpeed(obj,0);
		
		while( !Obj_BeDeleted(obj)){
			w--;
			if(w<0){Obj_SetSpeed(obj,s);}
			
			yield;
		}
}

task MoveAbout
{
SetMovePosition02(GetCenterX,GetCenterY-120,60);
MoveSatori;
loop
	{
	Wait(250);
	SetMovePositionRandom01(64,64,1,GetClipMinX+64,GetClipMinY+64,GetClipMaxX-64,GetClipMinY+128);
	MoveSatori;
	Wait(150);
	}
}

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



@DrawLoop
	{
	SetAlpha(255);
	if (bossflip==0){SetGraphicScale(1,1);}
	if (bossflip==1){SetGraphicScale(-1,1);}
	SetColor(255,255,255);
	SetGraphicAngle(0,0,0);
	SetTexture(SatoriSprite);
	SetGraphicRect(BR[0],BR[1],BR[2],BR[3]);
	DrawGraphic(GetX,GetY);
	}

@Finalize
	{
	DeleteGraphic(SatoriSprite);
	}

}