#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 60;
life = 12000;

@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-125,20,5);
	loop(90){yield;}
	TLife(0,20,32,50,50,false);
	let spread = [9,1,6,7,46,3];
	let graph = [SHOT_PURPLE,SHOT_RED];
	let aA = 90;
	let c = 0;
	let aP = 90;
	TSpamA;
	loop
		{
		ascent(dir in 0..2)
			{
			let aM = aP;
			ascent(i in 0..spread[1])
				{
				let g = (dir+i)%2;
				i = i-spread[1]*0.5+0.5;
				TShot(aP+(dir*2-1)*i*aA/spread[1],g,aM,aP);
				}		
			if(spread[1] < 5)
				{
				spread[1]=spread[1]+2;
				aA+=90;
				}
			loop(47*5-7){yield;}		
			aP = 90+(dir*2-1)*10+rand(-1,1)*2.5;
			}
		}
	task TSpamA
		{
		loop((47*5-7)*3-60){yield;}
		loop(8)
			{
			PlaySound("se_Shot1",60);
			ascent(i in 0..spread[4])
				{
				let shot = CreateBulletA2(enmx,enmy,1,i*360/spread[4]+GetAngleToPlayer(objEnemy),1/30,0,10,BL_SMALL,6,1,SHOT_BLUE);
				Obj_SetRenderPriority(shot,0.49);
				}
			loop(95){yield;}
			}
		let a = 90-180/spread[5];
		loop
			{
			PlaySound("se_Shot1",60);
			ascent(i in 0..spread[5])
				{
				let shot = CreateBulletA2(enmx,enmy,1,i*360/spread[5]+a,1/30,0,10,BL_SMALL,6,1,SHOT_BLUE);
				Obj_SetRenderPriority(shot,0.49);
				}
			loop(3){yield;}
			a+=13;
			}
		}
	task TShot(aM,g,aA,aP)
		{
		let xM = enmx;
		let yM = enmy;
		let dM = 115;
		let x = xM+dM*cos(aM);
		let y = yM+dM*sin(aM);
		let shot = CreateBulletA1(enmx,enmy,0,aM,ORB,0,1.125,SHOT_BLUE);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
		let time = [15,60];
		ascent(t in 0..time[1])
			{
			if(t <= time[0])
				{
				ObjRender_SetScaleXYZ(shot,(1-t/time[0])+1.125,(1-t/time[0])+1.125,0);
				}
			let d = dM-dM*((1-t/time[1])^2);
			ObjMove_SetDestAtFrame(shot,xM+d*cos(aM),yM+d*sin(aM),1);		
			ObjRender_SetAlpha(shot,min(t*255/time[0],205)*(1+rand(-1,1)*0.25));
			let sc = 1+rand(-1,1)*0.01;
			ObjRender_SetScaleXYZ(shot,ObjRender_GetScaleX(shot)*sc,ObjRender_GetScaleY(shot)*sc,0);
			yield;
			}
		ObjMove_SetAngle(shot,aM);
		loop(30)
			{
			ObjRender_SetAlpha(shot,205*(1+rand(-1,1)*0.25));
			let sc = 1+rand(-1,1)*0.01;
			ObjRender_SetScaleXYZ(shot,ObjRender_GetScaleX(shot)*sc,ObjRender_GetScaleY(shot)*sc,0);
			yield;
			}
		if(Obj_IsDeleted(shot)){return;}
		//aM = rand(0,360);
		//ascent(i in 0..spread[0]*2)
			//{
			//i = trunc(i*0.5);
			//let a = i*360/spread[0]+aM;
			//CreateBulletA2(x+18*cos(a),y+18*sin(a),4,a+dir*120,-2.5/30,0,1.5,SMALL,4,1,graph[1-g]);
			//}
		ascent(i in 0..spread[2])
			{
			TSpam(x,y,i*360/spread[2]+aP,aA);
			}
		ascent(t in 0..time[0])
			{
			ObjRender_SetScaleXYZ(shot,t*1/time[0]+1,t*1/time[0]+1,0);
			ObjRender_SetAlpha(shot,(1-t/time[0])*255);
			yield;
			}
		Obj_Delete(shot);
		}
	task TSpam(x,y,a,aA)
		{
		let t = 56;
		while(x > 0 && x < GetStgFrameWidth && y > 0 && y < GetStgFrameHeight)
			{			
			t+=1;
			x+=1*cos(a);
			y+=1*sin(a);
			if(t == 57)
				{
				ascent(xA in 0..spread[3])
					{
					xA = xA-spread[3]*0.5+0.5;
					ascent(yA in 0..spread[3])
						{
						yA = yA-spread[3]*0.5+0.5;
						TSpamA(xA,yA,x+xA*14,y+yA*14,a);
						}
					}
				t = 0;	
				}
			yield;
			}
		task TSpamA(nx,ny,x,y,a)
			{
			let nA = rand(0.5,spread[3]*0.5-1);
			if(nA < absolute(nx) || nA < absolute(ny)){return;}
			let shot = CreateBulletA1(x,y,0.33,aA,SMALL,6,1,SHOT_BLUE);
			ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
			loop(47){yield;}
			ObjShot_FadeDelete(shot);
			}
		}
}            