#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 75;
life = 3000;

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

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{	
	TSpellName("Sarugaku \"Drunken Board\"");
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-120,20,5);
	loop(90){yield;}
	let time = [84,72,60][Dif];
	TLife(time*1.5,20,32,175,175,false);
	
	let spread = [5];
	let timeA = [332,265,210][Dif];
	let d = GetStgFrameWidth/spread[0];
	let s = d*0.5/time;
	let aMA = 0;
	let aMB = [5,9,15][Dif];
	let aM = 0;
	let t = -1;
	let xM = 0;
	let n = 3;
	let nA = 3;
	loop
		{
		aMA+=1;
		aM=sin(aMA)*aMB;
		t+=1;
		if(t%time == 0)
			{
			let dir = trunc(rand(0,2))*2-1;
			ascent(i in -1..spread[0]+2)
				{
				TShot(i*GetStgFrameWidth/spread[0],i,n%(spread[0]-3)+2,dir);
				}
			n+=nA;
			nA+=1;
			}
		if(t >= 360 && (t-360)%105 == 0)
			{
			ObjEnemy_Move(objEnemy,20,4,rand(30,40),rand(105,120),100,0);
			}
		yield;
		}
	task TShot(xA,i,n,dir)
		{
		let yM = 96;
		let g = trunc(rand(0,3));
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,imgB);
		ObjSprite2D_SetSourceRect(obj,512+(g%3)*32,382,544+(g%3)*32,418);
		ObjSprite2D_SetDestCenter(obj);	
		Obj_Delete(obj);

		let shot = CreateBulletA1(-16,-16,0,0,FIRE,0,1,SHOT_BLUE);
		ObjShot_SetSpellResist(shot,true);
		ObjShot_SetAutoDelete(shot,false);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
		let laser = CreateLaserA2(-16,-16,1000,90,d+1,14,10^10,ICE,0,SHOT_GRAY);
		if((i == -1 && dir == 1) || (i ==spread[0]+1 && dir == -1))
			{
			Obj_Delete(laser);
			}
		else
			{
			ObjRender_SetBlendType(laser,BLEND_ALPHA);
			ObjShot_SetAutoDelete(laser,false);
			ObjStLaser_SetSource(laser,false);
			Obj_SetRenderPriority(laser,0.49);
			}

		
		let laser2 = CreateLaserA2(-16,-16,1000,90,d+1,14,10^10,ICE,0,SHOT_GRAY);
		if(i == -1 || i ==spread[0]+1)
			{
			ObjRender_SetBlendType(laser2,BLEND_ALPHA);
			ObjShot_SetAutoDelete(laser2,false);
			ObjStLaser_SetSource(laser2,false);
			Obj_SetRenderPriority(laser2,0.49);
			}
		else
			{
			Obj_Delete(laser2);
			}

		let tA = -1;
		while(!Obj_IsDeleted(objEnemy) && yM < GetStgFrameHeight+50)
			{	
			tA+=1;
			yM+=s;
			let x = xA;
			let y = yM;
			local
				{
				let d1 = GetDistance(0,GetStgFrameHeight*0.5,x,y);
				let a1 = atan2(y-GetStgFrameHeight*0.5,x)+aM;
				x = GetStgFrameWidth*0.233*sin(t)+d1*cos(a1);	
				y = s*time*0.75*(sin(t*1.52348)-1.5)+yM+d1*sin(a1);
				}
			if(!Obj_IsDeleted(shot))
				{
				ObjMove_SetPosition(shot,x,y);
				ObjRender_SetPosition(obj,x,y,0);		
				ObjMove_SetAngle(shot,90);	
				}

			if(!Obj_IsDeleted(laser))
				{
				let aMB = aM+90+dir*90;
				if(i == n && tA >= timeA)
					{
					aMB+=dir*trunc(min(tA-timeA,16))*90/8;
					}		
				ObjMove_SetPosition(laser,x,y);	
				ObjStLaser_SetAngle(laser,aMB);
				}
			if(!Obj_IsDeleted(laser2))
				{		
				ObjMove_SetPosition(laser2,x,y);
				ObjStLaser_SetAngle(laser2,aM*0.5-90);
				}
			if(!Obj_IsDeleted(shot) && ObjMove_GetY(shot) >= GetStgFrameHeight+20)
				{
				loop(1)
					{
					TSpam(shot,dir,xA,yM,tA);
					}
				Obj_Delete(shot);
				Obj_Delete(obj);
				}
			yield;
			}
		if(!Obj_IsDeleted(laser))
			{
			Obj_Delete(laser);
			}
		if(!Obj_IsDeleted(laser2))
			{
			Obj_Delete(laser2);
			}
		if(!Obj_IsDeleted(shot))
			{
			Obj_Delete(shot);
			}
		Obj_Delete(obj);
		}
	task TSpam(obj,dir,xA,yM,tA)
		{
		if(Obj_IsDeleted(obj)){return;}
		let sA = rand(0.75,1.25);
		let a = 270+rand(-1,1)*35;
		let shot = CreateBulletA1(ObjMove_GetX(obj),ObjMove_GetY(obj)-18,sA,a,ORB,0,0.33,SHOT_BLUE);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
		ObjMove_AddPatternB2(shot,0,NO_CHANGE,NO_CHANGE,0,1/300,0,-0.4+Dif*0.033);	
		ObjShot_SetSpellResist(shot,true);
		let c = trunc(rand(0,9));
		ObjRender_SetColorHSV(shot,240,30,255);
		while(!Obj_IsDeleted(shot))
			{
			c+=1;
			ObjShot_SetScale(shot,0.4+(floor(c/4)%2)*0.05);
			ObjRender_SetAlpha(shot,rand(200,255));
			yield;
			}
		}
}            