#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 55;
life = 5750;

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

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	SetSpellBack(SHOT_X);
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-120,20,5);
	loop(60){yield;}
	EnemyWave = true;
	TLife(120,20,32,100,100,false);
	
	let spread = [4,22+Dif*16];
	let aM = 0;
	let aMA = 0;
	let eW = 0;
	let eA = 0;
	let wM = 72;
	let c = -1;
	let dir = trunc(rand(0,2))*2-1;
	let sM = 180;
	
	loop
		{
		let w = wM*0.5*(1+(0.5+cos(eW)*0.5));
		let h = wM*0.5*(1+(0.5+cos(eW+180)*0.5));
		ascent(i in 0..spread[0])
			{
			ascent(n in 0..2)
				{
				let a = (i+n*0.5)*360/spread[0];
				let x = w*cos(a+aMA);
				let y = h*sin(a+aMA);
				local
					{
					let d = GetDistance(0,0,x,y);
					let aA = atan2(-y,-x)+eA;
					x = d*cos(aA);
					y = d*sin(aA);
					}	
				let dir = (n%2)*2-1;
				let spreadA = 1;
				if(Dif == 2 && n == 0)
					{
					spreadA = 2;
					}
				ascent(n1 in 0..spreadA)
					{
					let s = 2.25*(0.75+n*0.125+(0.125+(1-n)*0.125)*cos(sM))+n1*0.25+n*(1+Dif*0.45-n1*0.25);
					let shot = CreateBulletA2(enmx+x,enmy+y,0,a+dir*(1+n*0.375)*aM,s/60,0,s,[BALL_M,SCALE][n],8,1,[SHOT_PURPLE,SHOT_YELLOW][n]);
					Obj_SetRenderPriority(shot,0.5+n*0.01);
					}
				}
			}
		PlaySound("se_Shot4",45);
		loop(6-ceil(Dif/2)*2)
			{
			sM+=1.5;
			aMA+=5.5;
			eW+=2.5;
			eA+=2.5;
			aM-=6.5;
			c+=1;
			if(c%120 == 60 && c > 60)
				{
				ObjEnemy_Move(objEnemy,20,5,rand(15,25),rand(110,130),100,2);
				}
			if(c >= 240 && (c-240)%70 == 0)
				{
				PlaySound("se_Shot1",80);
				let x = enmx+dir*rand(0,1)*50;
				let y = enmy+rand(-1,1)*50;
				ascent(i in 0..spread[1])
					{
					CreateBulletA2(x,y,0,i*360/spread[1]+atan2(GetPlayerY-y,GetPlayerX-x),1/25,0,10,BALL_M,8,1,SHOT_YELLOW);
					}
				dir*=-1;
				}
			yield;
			}
		}
}