#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-120,20,5);
	loop(90){yield;}
	TLife(180,20,32,70,70,false);
	CreateScreenShake(150,30,true);
	loop(155){yield;}
	ascent(t in 5..45)
		{
		if(t == 7)
			{
			PlaySound("se_Die3",100);
			TWing;
			}
		let spreadA = 3+trunc(t*4/44);
		if(t%4 == 0)
			{
			ascent(dir in 0..2)
				{
				dir = dir*2-1;
				let a = 90+dir*t*90/44;
				loop(spreadA)
					{
					let d = rand(0,118);
					let shot = CreateBulletA1(enmx+d*cos(a),enmy+d*sin(a),rand(4,5)-t*2/44,a+dir*(30+t*70/44)+rand(-1,1)*7.5,BL_SMALL,6,1,SHOT_GRAY);
					ObjMove_AddPatternB2(shot,0,NO_CHANGE,NO_CHANGE,0,1/(75-t*50/44),0,rand(2.75,3.5));
					}
				}
			}
		yield;
		}
	loop(110){yield;}
	TSpam;
	loop(180){yield;}
	loop
		{
		TPattern1;
		TPattern2;
		TPattern3(1);
		TPattern1;
		TPattern3(-1);
		}
	function TPattern1
		{
		let dir = 1;
		local
			{
			let x = GetStgFrameWidth*0.5-135;
			if(GetPlayerX < GetStgFrameWidth*0.5)
				{
				x = GetStgFrameWidth-x;
				dir = -1;
				}
			TShot(x,GetStgFrameHeight*0.5+15,dir);
			}
		loop(180){yield;}
		local
			{
			let x = GetStgFrameWidth*0.5+dir*135;
			TShot(x,GetStgFrameHeight*0.5+15,-dir);
			}
		loop(150){yield;}
		}
	function TPattern2
		{
		ascent(t in 0..3)
			{
			ascent(dir in 0..2)
				{
				dir = dir*2-1;
				let a = 90+dir*(30+t*30);
				TShot(enmx+128*cos(a),enmy+128*sin(a),dir);
				}
			loop(13){yield;}
			}
		loop(150){yield;}
		}
	function TPattern3(dir)
		{
		ascent(t in 0..9)
			{
			ascent(n in 0..2)
				{
				let a = n*180+dir*t*32;
				TShot(enmx+150*cos(a),enmy+150*sin(a),dir);
				}
			loop(8){yield;}
			}
		loop(180){yield;}
		}
	task TShot(x,y,dir)
		{
		PlaySound("se_LaserDelay",100);
		let time = [15,60,30];

		let aP = atan2(GetPlayerY-y,GetPlayerX-x)-dir*540;
		let xM = enmx;
		let yM = enmy;
		let dM = GetDistance(x,y,xM,yM);
		let aM = atan2(y-yM,x-xM);

		let shot = CreateBulletA1(enmx,enmy,0,aM,BUBBLE,0,1,SHOT_BLUE);
		ascent(i in 0..5)
			{
			TLaser(i*72+180);
			}
		ascent(t in 0..time[1])
			{
			if(t <= time[0])
				{
				ObjRender_SetScaleXYZ(shot,(1-t/time[0])+1,(1-t/time[0])+1,0);
				}
			aP = GetAngleToPlayer(shot)-dir*540*(1-((1-t/time[1])^2));
			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],255));
			let sc = 1+rand(-1,1)*0.01;
			ObjRender_SetScaleXYZ(shot,ObjRender_GetScaleX(shot)*sc,ObjRender_GetScaleY(shot)*sc,0);
			yield;
			}
		loop(time[2]){yield;}
		if(!Obj_IsDeleted(shot)){PlaySound("se_Laser",60);}
		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 TLaser(aA)
			{
			let d = 60;
			let spam = CreateLaserA2(xM,yM,d/time[0],aP+aA,d,26,10^10,KUNI,time[0],SHOT_RED);
			ObjRender_SetBlendType(spam,BLEND_ADD_ARGB);
			loop(time[1]+time[2])
				{
				if(Obj_IsDeleted(shot))
					{
					ObjShot_FadeDelete(spam);
					return;
					}		
				ObjStLaser_SetAngle(spam,aP+aA);
				ObjMove_SetPosition(spam,ObjMove_GetX(shot),ObjMove_GetY(shot));
				yield;
				}
			ObjLaser_SetMaxLength(spam,GetStgFrameHeight*0.67);
			ObjLaser_SetSpeed(spam,GetStgFrameHeight/20);
			loop(14){yield;}
			ObjStLaser_SetSource(spam,false);
			ObjMove_SetAngle(spam,aA+aP);
			ObjMove_SetSpeed(spam,GetStgFrameHeight*0.67/20);
			}
		}
	task TSpam
		{
		loop
			{
			ascent(i in 0..5)
				{
				let shot = CreateBulletA1((i+rand(0,1))*GetStgFrameWidth/5,-12,rand(0,1),90+rand(-1,1)*90,BL_SMALL,6,1,SHOT_GRAY);
				ObjMove_AddPatternB2(shot,0,NO_CHANGE,NO_CHANGE,0,1/25,0,rand(2.75,3.5));
				Obj_SetRenderPriority(shot,0.51);
				}
			loop(15){yield;}
			}
		}
	task TWing
		{
		NotifyEventAll(666,true);	
		}
	
}            