#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 38.5;
life = 12000;

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

	TEnemyWork;
	ResetSTime;
	TTime;
	ObjEnemy_Render(objEnemy);
}
task TEnemyWork
{
	loop(60){yield;}
	SetBossAura(3,1,1,40);
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-75,20,5);
	loop(30){yield;}
	TLife(0,12,12,60,60,false);
	let spread = [6,[14,14,16,16][Dif],[5,4,3,3][Dif],5,[8,13,16,16][Dif],[4,16,16,10][Dif],[3,2,2,3][Dif]];
	let bturn = [2,75,0.75];
	let btime = [50,60];
	let sM = [[2,2.33,2.33,4][Dif]];

	let dir = -1;
	
	TWall;
	loop
		{	
		TLightning(dir);
		loop(480){yield;}
		dir*=-1;
		}
	task TWall
		{
		let dir = -1;
		loop
			{
			loop(330){yield;}
			let xP = enmx-dir*(30+(dir+1)*20/2);
			let yP = enmy-80;
			let aP = atan2(GetPlayerY-yP,GetPlayerX-xP);
			let aa = -dir*15;
			let da = 16;	
			ascent(n in 0..spread[6])
				{
				ascent(t in 0..spread[5])
					{
					PlaySound("se_Shot2",90);
					ascent(i in 0..spread[4])
						{		
						let a = ((n%2)*2-1)*dir*(i+t*bturn[2]/spread[5])*360/spread[4]-dir*(n%2)*135/spread[4]+aP;
						let x = da*2*cos(a)*cos(aa)-da*sin(a)*sin(aa);
						let y = da*2*cos(a)*sin(aa)+da*sin(a)*cos(aa);
						let shot = CreateBulletA2(xP+x,yP+y,1,a,1/40,0,10,SCALE,6,1.125,SHOT_ORANGE);
						}
					loop(2){yield;}
					}
				}
			loop(70){yield;}
			dir*=-1;
			}
		}
	task TLightning(dir)
		{
		let a = atan2(GetPlayerY-(enmy-35),GetPlayerX-(enmx-45));
		ascent(t in 0..450)
			{
			if(t%spread[0] == 0 && t < 175)
				{
				PlaySound("se_Shot2",90);
				ascent(i in 0..spread[1])
					{
					TBolt(dir,1+(t/(192/spread[0]))*2/spread[0],dir*(i-(t/(192/spread[0]))*1/spread[0])*360/spread[1]+a,(t/(192/spread[0]))*spread[1]+i);
					}
				}
			if(t%spread[2] == 0)
				{
				PlaySound("se_Shot1",60);
				ascent(i in 0..spread[3])
					{
					TStorm(dir,1+(t/(192/spread[2]))*2/spread[2],dir*(i-(t/(192/spread[2]))*90.5/spread[2])*360/spread[3]+a,(t/(192/spread[2]))*spread[3]+i,t/450);
					}
				}
			yield;
			}
		}
	
	task TBolt(dir,d,a,t)
		{
		let aa = -dir*15;
		let da = 8;
		let x = d*da*2*cos(a)*cos(aa)-d*da*sin(a)*sin(aa);
		let y = d*da*2*cos(a)*sin(aa)+d*da*sin(a)*cos(aa);
		let shot = CreateBulletA2(enmx-35+x,enmy-45+y,4,a+dir*bturn[1],-4/btime[1],0,0,ARROW,6,1,SHOT_YELLOW);
		Obj_SetRenderPriority(shot,0.49);
		
		loop(bturn[0])
			{
			loop(btime[1]){yield;}
			if(Obj_IsDeleted(shot)){return;}
			ObjMove_SetAngle(shot,ObjMove_GetAngle(shot)+dir*bturn[1]);
			ObjMove_SetSpeed(shot,4);
			ObjMove_SetAcceleration(shot,-4/btime[0]);
			ObjMove_SetMaxSpeed(shot,0);
			dir*=-1;
			}
		ObjMove_SetSpeed(shot,sM[0]+1);
		ObjMove_SetMaxSpeed(shot,sM[0]);
		}
	task TStorm(dir,d,a,t,s)
		{
		let aa = -dir*15;
		let da = 8;
		let x = d*da*2*cos(a)*cos(aa)-d*da*sin(a)*sin(aa);
		let y = d*da*2*cos(a)*sin(aa)+d*da*sin(a)*cos(aa);
		let shot = CreateBulletA1(enmx-35+x,enmy-45+y,1.5+floor(Dif/3)*0.5+((s*2)^2)*0.25,-a,BL_SMALL,6,1,SHOT_YELLOW);
		Obj_SetRenderPriority(shot,0.49);
		}
}            