#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 60;
life = 3000;

@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-105,20,5);
	EnemyWave = true;
	loop(60){yield;}
	TLife(180,20,32,42,42,false);

	let spread = [[24,30,36][Dif],4,[1,1,2][Dif]];
	let spreadM = [[56,62,68][Dif],31];
	let accel = [[1/32,1/27,1/24][Dif],1/75];
	let time = [6,3,3][Dif];
	let dir = 1;	
	let n = 0;	
	TMaskM;
	TMove;
	loop
		{
		let a = GetAngleToPlayer(objEnemy)+180/spread[1];
		let spreadA = spread[0];
		if(n == 0)
			{
			spreadA -= [10,14,22][Dif];
			}
		descent(t in 0..spreadA)
			{
			ascent(i in 0..spread[1])
				{
				TShot(i*360/spread[1]+a,(t+3)*(time+1));
				}
			a+=dir*((180+3.5*9)/spread[1]);
			loop(time){yield;}
			}
		loop(90-Dif*30){yield;}
		dir*=-1;
		n+=1;
		}
	task TShot(a,t)
		{
		let shot = CreateBulletA2(enmx,enmy,0,a,accel[0],0,10,ORB,8,0.75,SHOT_BLUE);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
		TFlicker(shot);
		loop(8+t){yield;if(Obj_IsDeleted(shot)){return;}}
		let s = ObjMove_GetSpeed(shot);
		ascent(i in 0..spread[2])
			{
			i = i-spread[2]*0.5+0.5;
			let shot2 = CreateBulletA2(ObjMove_GetX(shot),ObjMove_GetY(shot),s,a,-s/90,0,0,SMALL,0,1,SHOT_GREEN);
			ObjMove_AddPatternA2(shot2,100,NO_CHANGE,a+i*10/3,accel[1],0,1.5);
			}
		ObjShot_SetDelay(shot,9);
		loop(8){yield;}
		Obj_Delete(shot);
		}
	task TFlicker(shot)
		{
		let c = trunc(rand(0,9));
		ObjRender_SetColorHSV(shot,240,rand(0,100),255);
		while(!Obj_IsDeleted(shot))
			{
			c+=1;
			ObjShot_SetScale(shot,0.65+(floor(c/4)%2)*0.05);
			ObjRender_SetAlpha(shot,rand(200,255));
			yield;
			}
		}
	task TMaskM
		{	
		loop(300){yield;}
		let c = -1;
		let n = 2;
		let g = 2;	
		let ga = g;
		loop
			{
			c+=1;
			if(c%(180-Dif*15) == 0)
				{
				n+=1;
				TMask(n,g);
				while(g == ga)
					{
					g = trunc(rand(0,3));
					}		
				ga = g;
				}
			yield;
			}
		}
	task TMask(n,g)
		{	
		let dir = (n%2)*2-1;
		let xM = GetStgFrameWidth*(0.5+dir*0.4);	
		let yM = GetStgFrameHeight*(0.5+((n*2)%3)*0.2);
		let x = enmx;
		let y = enmy;
		local
			{
			let d = GetDistance(x,y,xM,yM)*0.33;
			let a = atan2(yM-y,xM-x);
			x+=d*cos(a);
			y+=d*sin(a);
			}
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/Sprite_Boss.png");
		ObjSprite2D_SetSourceRect(obj,512+(g%3)*32,384,544+(g%3)*32,418);
		Obj_SetRenderPriority(obj,0.3);
		if(g == 2)
			{
			ObjRender_SetColorHSV(obj,60,155,255);
			}
		else if(g == 0)
			{
			ObjRender_SetColorHSV(obj,240,95,255);
			}
		let c = -1;
		let s = 0;
		let a = rand(-1,1)*7.5;
		while(!Obj_IsDeleted(objEnemy))
			{	
			c+=1;	
			x+=(xM-x)/40;
			y+=(yM-y)/40;
			if(c >= 330)
				{
				if(s > 0)
					{
					s-=1/10;
					}
				else
					{
					break;
					}
				}
			else if(s < 1)
				{
				s+=1/20;
				}
			if(g == 0 && c == 150)
				{
				let a = rand(0,360);
				ascent(i in 0..spreadM[0])
					{		
					let shot = CreateBulletA1(x,y,1+Dif*0.25,i*360/spreadM[0]+a,FIRE,8,1,SHOT_BLUE);
					ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
					ObjRender_SetColorHSV(shot,240,(i%2)*100,255);
					ObjMove_AddPatternA2(shot,120,NO_CHANGE,NO_CHANGE,0,((i%2)*2-1)*90/800,0);
					}
				}
			if(g == 1 && c >= 150 && c < 255 && c%(15-Dif*3) == 0)
				{
				let spreadA = floor((c-150)/(15-Dif*3))+1;
				ascent(i in 0..spreadA)
					{
					i = i-spreadA*0.5+0.5;
					ascent(n in -1..2)
						{
						CreateBulletA1(x,y,0.25+(spreadA+rand(0,1))*(0.35-Dif*0.05),atan2(GetPlayerY-y,GetPlayerX-x)+i*90/spreadM[1]+n*60,MISSILE,8,1.25,SHOT_RED);
						}
					}
				}
			if(g == 2 && c == 150)
				{
				TYellowSpam(x,y);
				}
			ObjRender_SetPosition(obj,x,y,0);	
			ObjRender_SetScaleX(obj,dir*s);
			ObjSprite2D_SetDestCenter(obj);
			yield;
			}
		Obj_Delete(obj);
		}
	task TYellowSpam(x,y)
		{
		let a = rand(-1,1)*2.5;
		let c = -1;
		loop(90)
			{
			c+=1;
			if(c%2 == 0)
				{
				ascent(i in 0..4)
					{
					TSpam(c,a+i*90);
					}
				}
			if(c%3 == 0)
				{
				yield;
				}
			}
		task TSpam(d,a)
			{
			let dir = (floor(d/2)%2)*2-1;
			d*=GetStgFrameWidth/90;
			let aa = a+180+dir*45+rand(-1,1)*3;
			let da = 9+Dif*13.5;
			let shot = CreateBulletA1(x+d*cos(a)+da*cos(aa),y+d*sin(a)+da*sin(aa),-0.15-Dif*0.225,aa,ICE,8,1.25,SHOT_YELLOW);
			ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
			loop(120){yield;}
			ObjShot_SetDelay(shot,9);
			loop(8){yield;}
			Obj_Delete(shot);
			}
		}
	task TMove
		{
		loop(150){yield;}
		loop
			{
			ObjEnemy_Move(objEnemy,20,4,rand(20,40),rand(80,100),GetStgFrameWidth*0.4,0);
			loop(165){yield;}
			}
		}
}            