#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 60;
life = 3000;

@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-90,20,5);
	EnemyWave = true;
	loop(60){yield;}
	TLife(120,20,32,57,57,false);
	
	let spread = [[9,10,11][Dif],[2,3,3][Dif],[64,96,96][Dif]];
	let time = [13,11,9][Dif];
	let accel = [[1/60,1/38,1/24][Dif],1/150];
	let timeA = 75;
	let c = -1;
	let a = 90+180/spread[0];
	let timeM = [160,135,110][Dif];
	loop(timeM-time*2)
		{
		c+=1;
		if(c%time == 0)
			{
			ascent(i in 0..spread[0])
				{
				TShot1(timeM-c,a+i*360/spread[0]);
				}
			a+=180/spread[0]+4;	
			loop(3){yield;}
			}
		}
	a-=90;
	loop(8+timeA+15){yield;}
	let cA = -1;
	loop
		{
		cA+=1;
		c+=1;
		if(c%time == 0)
			{
			ascent(i in 0..spread[0])
				{
				TShot2(a+i*360/spread[0]);
				}
			a+=180/spread[0]+4;
			}
		if(cA >= 150 && (cA-150)%105 == 0)
			{
			ObjEnemy_Move(objEnemy,40,4,rand(60,80),rand(75,105),100,2);
			}
		if(cA >= 270 && (cA-270)%180 == 0)
			{
			let a = rand(0,360);
			ascent(i in 0..spread[2])
				{
				let shot = CreateBulletA1(enmx,enmy,0.75,i*360/spread[2]+a,FIRE,8,1,SHOT_BLUE);
				ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
				ObjRender_SetColorHSV(shot,240,(i%2)*100,255);
				ObjMove_AddPatternA2(shot,150,NO_CHANGE,NO_CHANGE,0,((i%2)*2-1)*90/900,0);
				}
			}
		yield;
		}
	task TShot1(t,a)
		{
		let d = 0.5*accel[0]*t^2;
		let x = enmx;
		let y = enmy;
		let shot2 = CreateBulletA1(x,y,0,0,ORB,8,0.75,SHOT_BLUE);
		ObjShot_SetSpellResist(shot2,true);
		ObjRender_SetBlendType(shot2,BLEND_ADD_ARGB);
		TFlicker(shot2);
		let shot = CreateBulletA1(x,y,0,0,SMALL,8,1,SHOT_GREEN);
		ObjShot_SetSpellResist(shot,true);
		if(t <= time)
			{
			Obj_Delete(shot2);
			}
		loop(8){yield;}
		descent(n in 0..timeA)
			{
			n = (n^2)/(timeA^2);
			let aA = a-90*n;
			let dA = d*(1-n);
			ObjMove_SetDestAtFrame(shot,x+dA*cos(aA),y+dA*sin(aA),1);
			ObjMove_SetDestAtFrame(shot2,x+dA*cos(aA+0.25),y+dA*sin(aA+0.25),1);
			yield;
			}
		loop(15+(timeM-t)*17/timeM){yield;}
		if(!Obj_IsDeleted(shot))
			{
			ascent(i in 0..spread[1])
				{
				i = i-spread[1]*0.5+0.5;
				let shotA = CreateBulletA2(ObjMove_GetX(shot),ObjMove_GetY(shot),0,a+i*10/3,accel[1],0,1.25,SMALL,0,1,SHOT_GREEN);
				}
			Obj_Delete(shot);
			}
		ObjMove_SetAcceleration(shot2,accel[0]);
		ObjMove_SetMaxSpeed(shot2,10);
		ObjMove_SetAngle(shot2,a);
		}
	task TShot2(a)
		{
		let shot = CreateBulletA2(enmx,enmy,0,a,accel[0],0,10,ORB,8,0.75,SHOT_BLUE);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);	
		ObjShot_SetSpellResist(shot,true);
		TFlicker(shot);
		loop(8){yield;}
		let t = rand(0,180*[3,2.5,2][Dif]);
		if(t > 180){return;}
		t = (2*t/accel[0])^0.5;
		loop(t){yield;if(Obj_IsDeleted(shot)){return;}}
		let s = ObjMove_GetSpeed(shot);
		ascent(i in 0..spread[1])
			{
			i = i-spread[1]*0.5+0.5;
			let shot2 = CreateBulletA2(ObjMove_GetX(shot),ObjMove_GetY(shot),s,a,-s/90,0,0,SMALL,0,1,SHOT_GREEN);
			ObjMove_AddPatternA2(shot2,100,NO_CHANGE,a+i*10/3,accel[1],0,1.5);
			}
		ObjShot_SetDelay(shot,9);
		loop(8){yield;}
		Obj_Delete(shot);
		}

	task TFlicker(shot)
		{
		let c = trunc(rand(0,9));
		ObjRender_SetColorHSV(shot,240,rand(0,100),255);
		while(!Obj_IsDeleted(shot))
			{
			c+=1;
			ObjShot_SetScale(shot,0.65+(floor(c/4)%2)*0.05);
			ObjRender_SetAlpha(shot,rand(200,255));
			yield;
			}
		}
}            