#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 98;
life = 12000;

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

	TEnemyWork;
	ResetSTime;
	TTime;
	ObjEnemy_Render(objEnemy);
}
task TEnemyWork
{
	SetBossAura(3,1,1,40);
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-105,20,5);
	loop(30){yield;}
	TLife(0,12,12,120,120,false);	
	loop(60){yield;}
	let spread2 = [20,3,8,5];
	let s2 = [15,2,0.25,10,17];
	let mspeed = [0,1.5,[2.25,2.5,2.75,2.5][Dif]];
	let mtime = 90;
	let mturn = 2;
	let smvary = [0.25,[0.67,1,0.67,1][Dif],10];
	let turn = [2,1.1];
		
	let spread1 = [11,[3,4,7,9][Dif],[1,3,4,6][Dif],1.75];
	let s1 = [6,1/[40,30,30,20][Dif],10];

	let spread3  = [[3,5,6,8][Dif]];
	let s3 = [3,[2.5,2.5,3,3.25][Dif],[1.5,1.5,2,2.25][Dif],-15];
	loop
		{
		Obj_SetValue(objEnemy,"INVINC",true);
		SetBossAura(1,1,10,20);
		ObjRender_SetColorHSV(objEnemy,40,155,155);
		FPhase1;
		Obj_SetValue(objEnemy,"INVINC",false);
		ObjRender_SetColorHSV(objEnemy,40,0,255);
		SetBossAura(10,1,1,20);
		loop(240){yield;}
		Obj_SetValue(objEnemy,"INVINC",true);
		ObjRender_SetColorHSV(objEnemy,40,155,155);
		SetBossAura(1,1,10,20);
		FPhase2;
		Obj_SetValue(objEnemy,"INVINC",false);
		ObjRender_SetColorHSV(objEnemy,40,0,255);
		SetBossAura(10,1,1,20);
		loop(240){yield;}
		Obj_SetValue(objEnemy,"INVINC",true);
		ObjRender_SetColorHSV(objEnemy,40,155,155);
		SetBossAura(1,1,10,20);
		FPhase3;
		Obj_SetValue(objEnemy,"INVINC",false);
		ObjRender_SetColorHSV(objEnemy,40,0,255);
		SetBossAura(10,1,1,20);
		loop(240){yield;}
		}	
	function FPhase3
		{
		let dir = floor(rand(0,2))*2-1;
		let aP = rand(-1,1)*20;
		let d = 32;
		let time = 240;
		ascent(t in 0..time)
			{
			if(t%s3[0] == 0)
				{
				PlaySound("se_Shot1",60);
				let aa = -dir*floor(t/s3[0])*5+aP;
				ascent(i in 0..spread3[0])
					{
					ascent(n in 0..2)
						{
						let a = dir*((i+n*0.5)*360/spread3[0]+floor(t/s3[0])*7);
						let x = enmx+d*2*cos(a)*cos(aa)-d*sin(a)*sin(aa);
						let y = enmy+d*2*cos(a)*sin(aa)+d*sin(a)*cos(aa);
						TShot(x,y,s3[1+n],a,[1.25,1.125][n],[DROP,BL_SMALL][n],[SHOT_BLUE,SHOT_GRAY][n],(n*2-1)*dir);		
						}
					}
				}
			yield;
			}
		task TShot(x,y,s,a,sc,g,gA,dir)
			{
			let shot = CreateBulletA1(x,y,s,a,g,6,sc,gA);
			}
		}
	function FPhase1
		{
		let spread = [[3,3,5,5][Dif],5,180,2];
		let aN = rand(0,360/spread[0]);
		ascent(i in 0..2)
			{
			TStormA(i*2-1);
			}
		loop(180){yield;}
		task TStormA(dir)
			{
			let a = dir*aN;
			let t = spread[1];
			let spreadA = floor(spread[2]/spread[3]);
			loop(spreadA-1)
				{
				t+=1;
				ascent(i in 0..spread[0])
					{
					TStorm(enmx+dir*75,enmy-15,a+i*360/spread[0]+rand(-1,1)*1,mspeed[1]-((t+rand(0,0)*0.1)%spread[1])*smvary[0]/spread[1],mspeed[2]-((spread[1]-t+rand(0,0)*0.1)%spread[1])*smvary[1]/spread[1],dir);
					}
				a-=dir*turn[0];
				if(t%spread[1] == spread[1]-1)
					{
					a-=dir*turn[1];
					}
				PlaySound("se_Shot1",60);
				loop(spread[3]){yield;}
				}
	
			}
		task TStorm(x,y,a,sm,sM,dir)
			{
			let shot = CreateBulletA1(x,y,0,0,SCALE,6,1.25,SHOT_BLUE);	
			if(dir == 1)
				{
				ObjShot_SetGraphic(shot,SCALE[SHOT_GREEN]-1);
				}
			else
				{
				ObjShot_SetGraphic(shot,SCALE[SHOT_BLUE]-1);
				}
			let s = 2;
			let aa = a;
			ascent(t in 0.. mtime)
				{
				let sa = mspeed[0]+t*(mspeed[1]-mspeed[0])/mtime;
				let d = t*sa*(sm/5+0.25);
				ObjMove_SetDestAtFrame(shot,x+d*cos(a),y+d*sin(a),1);	
				aa = a;
				a+=dir*mturn;
				s = ObjMove_GetSpeed(shot);
				if(Obj_IsDeleted(shot)){return;}
				yield;
				}
			ObjMove_SetAngle(shot,ObjMove_GetAngle(shot));
			ObjMove_SetSpeed(shot,s);
			ObjMove_SetAngularVelocity(shot,(a-aa)/smvary[2]);
			ObjMove_SetAcceleration(shot,(sM-s)/60);
			ObjMove_SetMaxSpeed(shot,sM);
			}
		}
	function FPhase2
		{
		let aP = GetAngleToPlayer(objEnemy)+180;
		let aN = 2.6*(360/spread1[0]+13);
		let c = -1;
		ascent(i0 in 0..spread1[0]*4)
			{
			i0 = i0-spread1[0]*0.5+0.5;
			let aA = (i0*6.3)*(aN+6)+aP;
			ascent(i1 in 0..spread1[1])
				{
				i1 = i1-spread1[1]*0.5+0.5;
				TShot(aA+i1*60*spread1[3]/spread1[1],aA);
				}
			ascent(i2 in 0..spread1[2])
				{
				CreateBulletA1(enmx,enmy,1.25+(i2*1.25/spread1[2])*30*s1[1],aA+spread1[1]*0.75*aN*spread1[3]/spread1[1],LARGE,6,1,SHOT_YELLOW);
				}
			PlaySound("se_Shot1",100);
			loop(ceil(45/spread1[0]))
				{
				c+=1;
				yield;
				}
			}
		task TShot(a,aA)
			{
			let shot = CreateBulletA2(enmx,enmy,s1[0],a,-s1[0]/30,0,0,LARGE,0,1,SHOT_GRAY);
			ObjRender_SetColorHSV(shot,50,10,205);
			TDel(shot,30);
			loop(45){yield;}
			ObjMove_SetAngle(shot,aA);
			ObjMove_SetAcceleration(shot,s1[1]);
			ObjMove_SetMaxSpeed(shot,s1[2]);
			}
		}

	task TDel(shot,time)
		{	
		ObjShot_SetIntersection(shot,false);
		let aP = atan2(ObjMove_GetY(shot)-enmy,ObjMove_GetX(shot)-enmx);
		if((IsBeyondClip(shot,8) && cos(ObjMove_GetAngle(shot) - aP) < 0.5) || Obj_IsDeleted(objEnemy))
			{
			return;		
			}	
		let sc = ObjShot_GetScale(shot);
		descent(t in 0..time)
			{
			ObjRender_SetAlpha(shot,255*(1-t/time));
			t = (1+2*t/time);
			ObjRender_SetScaleXYZ(shot,sc*t,sc*t,0);
			if(Obj_IsDeleted(objEnemy)){break;}
			yield;
			}
		ObjShot_SetIntersection(shot,true);
		}
}            