#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 330;
life = 12000/3;

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

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	TSpellName("Drone Sign \"Attack Familiar\"");
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-60,20,5);
	loop(90){yield;}
	TLife(150,20,32,160/3,160/3,false);	
	let spread = [2,5];
	descent(i in 0..spread[1])
		{
		let x = (i+0.5)*GetStgFrameWidth/spread[1];
		TShot(atan2(GetStgFrameHeight-enmy,x-enmx),1);
		}
	loop(140){yield;}
	let a = atan2(GetStgFrameHeight-enmy,GetStgFrameWidth-enmx);
	let t = 100000;
	let c = 0;
	let dir = 1;
	loop	
		{
		if(t >= 35)
			{
			c = 50;			
			t = 0;		
			dir*=-1;			
			loop(90){yield;}
			a = GetAngleToPlayer(objEnemy);
			}
		TShot(a,dir);
		loop(c){yield;}
		a+=dir*23;
		if(c > 4)
			{
			c-=2;
			}
		t+=1;
		}
	task TShot(aM,dir)
		{
		PlaySound("se_LaserDelay",100);
		let time = [15,75,30];
		let aP = 90-dir*540;
		let xM = enmx;
		let yM = enmy;
		let dM = 0;
		let x = xM;
		let y = yM;
		while(x > 0 && x < GetStgFrameWidth && y > 0 && y < GetStgFrameHeight){x+=2*cos(aM);y+=2*sin(aM);dM+=2;}

		let shot = CreateBulletA1(enmx,enmy,0,aM,BUBBLE,0,1,SHOT_BLUE);
		ObjShot_SetSpellResist(shot,true);
		ascent(i in 0..4)
			{
			TLaser(i*90);
			}
		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 = 90-dir*540*(1-((1-t/time[1])^2));
			let d = dM*((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)){return;}}
		PlaySound("se_Laser",50);
		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[1]+time[2],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;
				}
			if(Obj_IsDeleted(shot)){ObjShot_FadeDelete(spam);return;}
			let n = 0.5;
			ascent(i in 0..spread[0])
				{
				i = i-spread[0]*0.5+n;
				let spamA = CreateBulletA1(ObjMove_GetX(shot),ObjMove_GetY(shot),1.5-absolute(i)*0.5/(spread[0]*0.5),aA+aP+i*90/spread[0],SMALL,6,1,SHOT_YELLOW);
				ObjRender_SetBlendType(spamA,BLEND_ADD_ARGB);
				}
			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);
			}
		}
}