#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
{
	SetSpellBack(SHOT_X);
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-135,20,5);
	EnemyWave = true;
	loop(60){yield;}
	TLife(120,20,32,57,57,false);
	
	let spreadM = 4+Dif*4;
	let spread = [16+Dif*4,54,2+Dif];
	let dir = trunc(rand(0,2))*2-1;
	let time = [360];
	loop
		{
		let cn = 0;
		local
			{
			let aP = GetAngleToPlayer(objEnemy);
			let dirA = dir;
			ascent(t in 0..time[0])
				{
				let dir = ((t%2)*2-1);
				if(t%floor(time[0]/4) == 0)
					{
					PlaySound("se_Shot1",80);
					if(cn >= 1)
						{
						TWave;
						}
					cn+=1;
					dirA*=-1;
					}
				let d = min(t,time[0])*120/time[0];		
				let spreadA = 2-Dif;
				if(dirA*dir == 1)
					{
					spreadA = 2-floor(Dif/2);
					}
				ascent(i in spreadA..5)
					{
					let a = aP+180+rand(0.975,1)*dir*(t)*1440/time[0];
					let s = rand(0.25,0.425)+Dif*0.125+i*(2-Dif*0.125-t*1.5/time[0])/4+t*(0.75+i*2.5/4)/time[0];
					let shot = CreateBulletA2(enmx+d*cos(a),enmy+d*sin(a),s,a+dir*(45+min(t,time[0])*90/time[0]),1/(40+i*20/4),0,4-(dirA*dir+1)*0.5*(2.5-t*1/time[0]),[FIRE,BALL_S][(dirA*dir+1)*0.5],8,1,SHOT_RED);
					if(Dif >= 1)
						{
						ObjMove_SetAngularVelocity(shot,dir*(50+Dif*20)/(290-Dif*20+(dirA*dir+1)*0.5*90));
						}
					}
				yield;
				}
			}
		loop(60){yield;}
		EnemyWave = true;
		ascent(t in 0..3)
			{
			if(t == 2)
				{
				TWave;
				}
			ascent(n in 0..spread[2])
				{
				PlaySound("se_Shot1",80);
				let aM = rand(0,360);
				let spreadM = spread[1]-floor(t/2)*(16-Dif*4);
				ascent(i in 0..spreadM)
					{
					let a = i*360/spreadM+aM;
					let s = 8+rand(0,0.25);
					let sM = 1.25-n*0.5/spread[2]+floor(t/2)*1;
					CreateBulletA2(enmx+32*cos(a),enmy+32*sin(a),s,a,(sM-s)/20,dir*((n%2)*2-1)*90/(GetStgFrameWidth/sM),sM,BALL_M,8,1,SHOT_BLUE);
					}
				loop(20/spread[2]){yield;}
				}
			dir *= -1;
			loop(30){yield;}
			if(t < 2)
				{	
				ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*(0.5+dir*rand(0.375,0.4)),GetStgFrameHeight*0.5-rand(105,120)+t*20,20,4+t*1.5);
				let c = floor(rand(0,7));
				while(!Obj_IsDeleted(objEnemy) && ObjMove_GetSpeed(objEnemy) > 0.5)
					{
					c+=1;
					if(c%7 == 0)
						{
						PlaySound("se_Shot4",40);
						let x = enmx+rand(-1,1)*50;
						let y = enmy+rand(-1,1)*50;
						ascent(i in 0..spread[0])
							{
							let a = i*360/spread[0]+atan2(GetPlayerY-y,GetPlayerX-x);
							CreateBulletA2(x+8*cos(a),y+8*sin(a),1+rand(0,0.125),a,1/10,0,10,BALL_M,8,1,SHOT_RED);
							}
						}
					yield;
					}
				while(!Obj_IsDeleted(objEnemy) && ObjMove_GetSpeed(objEnemy) > 0){yield;}
				}
			}
		ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-135,20,5);	
		EnemyWave = true;
		loop(90){yield;}	
		}
	task TWave
		{
		EnemyWave = true;
		loop(22){yield;}
		EnemyWave = false;
		}
}            