#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 \"Support Familiar\"");
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-120,20,5);
	loop(90){yield;}
	TLife(150,20,32,180/3,180/3,false);
	let spread = [8,5];
	let spreadA = [10];	
	let dir = 1;
	loop
		{
		let aP = GetAngleToPlayer(objEnemy);	
		ascent(n in 0..15)
			{
			ascent(i in 0..spreadA[0])
				{
				if(n%3 == 0)
					{
					TShot(enmx,enmy,1,(i+n*0.5/3)*360/spreadA[0]+aP);		
					}
				CreateBulletA1(enmx,enmy,2.33,-dir*(i+n*0.0875)*360/spreadA[0]+aP,BL_SMALL,6,1,SHOT_GRAY);
				CreateBulletA1(enmx,enmy,2.66,dir*(i+n*0.2+0.5)*360/spreadA[0]+aP,BL_SMALL,6,1,SHOT_BLUE);
					
				}
			PlaySound("se_Shot1",70);
			loop(20){yield;}
			}
		loop(15){yield;}
		dir*=-1;
		}
	task TShot(x,y,s,a)
		{
		loop(60){yield;}
		let d = 72;
		let shot2 = CreateBulletA1(x,y,0,0,ORB,0,0,SHOT_BLUE);
		ObjRender_SetBlendType(shot2,BLEND_ADD_ARGB);
		ObjShot_SetIntersection(shot2,false);
		ObjRender_SetColorHSV(shot2,240,255,255);

		let shot = CreateBulletA2(x,y,s,a,1/60,0,10,ORB,0,1,SHOT_BLUE);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);

		let time = [15,60];
		let t = -1;
		while(!Obj_IsDeleted(shot))
			{	
			t+=1;
			let tA = min(time[1],t);
			if(tA <= time[0])
				{
				let scA = (1-tA/time[0])+1.125;
				ObjRender_SetScaleXYZ(shot,scA,scA,0);
				ObjRender_SetScaleXYZ(shot2,scA*d/32,scA*d/32,0);
				}	
			let alph = min(tA*255/time[0],205)*(1+rand(-1,1)*0.25);
			ObjRender_SetAlpha(shot2,alph*0.67);
			ObjRender_SetAlpha(shot,alph);
			ObjMove_SetPosition(shot2,ObjMove_GetX(shot),ObjMove_GetY(shot));
			let sc = 1+rand(-1,1)*0.01;
			ObjRender_SetScaleXYZ(shot,ObjRender_GetScaleX(shot)*sc,ObjRender_GetScaleY(shot)*sc,0);
			if(t >= time[0] && GetObjectDistance(shot,GetPlayerObjectID) <= d*0.8)
				{
				PlaySound("se_LaserDelay",100);
				break;
				}
			yield;
			}
		if(Obj_IsDeleted(shot)){Obj_Delete(shot2);return;}
		x = ObjMove_GetX(shot);
		y = ObjMove_GetY(shot);
		let aP = atan2(GetPlayerY-y,GetPlayerX-x);
		let aA = ObjMove_GetAngle(shot);
		ObjMove_SetSpeed(shot,0);
		ObjMove_SetAngle(shot,aP);
		ObjMove_SetAcceleration(shot,1/60);
		ObjMove_SetMaxSpeed(shot,10);
		TDel;
		ascent(t in 0..spread[0])
			{
			let d = 4+t*10;
			ascent(i in 0..spread[1])
				{
				let a = aP+(i)*360/spread[1];
				let spam = CreateBulletA1(x+t*2*cos(a),y+t*2*sin(a),0,a,SMALL,6,1,SHOT_BLUE);
				ObjRender_SetBlendType(spam,BLEND_ADD_ARGB);
				ObjMove_AddPatternA2(spam,30+t*5,NO_CHANGE,NO_CHANGE,1/60,0,10);
				}
			loop(2){yield;}
			}
		task TDel
			{
			ObjMove_SetSpeed(shot,0);
			ascent(t in 0..time[0])
				{
				let alph = (1-t/time[0])*255;
				let scA = t*1/time[0]+1;
				ObjRender_SetScaleXYZ(shot,scA,scA,0);
				ObjRender_SetAlpha(shot,alph);

				ObjRender_SetScaleXYZ(shot2,scA*d/32,scA*d/32,0);
				ObjRender_SetAlpha(shot2,alph*0.67);
				yield;
				}
			Obj_Delete(shot);
			Obj_Delete(shot2);
			}
		}
}