#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("\"Fantasy Drone Wars -Secret-\"");
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-165,15,5);
	loop(90){yield;}
	TLife(150,20,32,150/3,150/3,false);	
	let dir = 1;
	let t = 170;
	let spread = 16;
	loop
		{
		TFam(dir);
		loop(t){yield;}
		if(t > 80)
			{
			t-=10;
			}
		dir*=-1;
		}
	task TFam(dir)
		{
		PlaySound("se_LaserDelay",100);
		let x = GetStgFrameWidth*(0.5+dir*rand(0.35,0.4));
		let y = GetStgFrameHeight*0.5-rand(105,135);
		let a = 90+dir*25;
	
		let xM = enmx;
		let yM = enmy;
		let dM = GetDistance(xM,yM,x,y);
		let aM = atan2(y-yM,x-xM);

		let time = [15,75];

		let shot = CreateBulletA1(enmx,enmy,0,aM,ORB,0,1.125,SHOT_BLUE);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
		ObjShot_SetSpellResist(shot,true);

		ascent(i in 0..spread)
			{
			i = i-spread*0.5+0.5;
			i = i*absolute(i)*2/(spread*spread);
			TLaser(i*360);
			}

		ascent(t in 0..time[1])
			{
			if(t <= time[0])
				{
				ObjRender_SetScaleXYZ(shot,(1-t/time[0])+1.125,(1-t/time[0])+1.125,0);
				}
			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],205)*(1+rand(-1,1)*0.25));
			let sc = 1+rand(-1,1)*0.01;
			ObjRender_SetScaleXYZ(shot,ObjRender_GetScaleX(shot)*sc,ObjRender_GetScaleY(shot)*sc,0);
			yield;
			}	
		PlaySound("se_Laser",70);
		ascent(i in 0..46)
			{
			let spam = CreateBulletA1(x,y,1.5,i*360/46+90,SMALL,6,1,SHOT_BLUE);
			ObjRender_SetBlendType(spam,BLEND_ADD_ARGB);
			}
		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 spam = CreateLaserA2(xM,yM,10,a,GetStgFrameHeight*1.5,26,10^10,KUNI,time[1]-2,SHOT_RED);
			ObjRender_SetBlendType(spam,BLEND_ADD_ARGB);
			ObjStLaser_SetSource(spam,false);
			ascent(t in 0..time[1]-2)
				{
				let aB = t*aA/time[1];
				if(Obj_IsDeleted(shot)){ObjShot_FadeDelete(spam);return;}
				ObjMove_SetPosition(spam,ObjMove_GetX(shot),ObjMove_GetY(shot));
				ObjStLaser_SetAngle(spam,a+aB);
				yield;
				}
			loop(15){yield;}
			ObjMove_SetAngle(spam,a+aA);
			ObjMove_SetSpeed(spam,20);
			}
		}
	
}