#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 120;
life = 3000;

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

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	TSpellName("Empty Dance \"Dead Demon Consuming Dance\"");
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-105,20,5);
	EnemyWave = true;
	loop(60){yield;}
	TLight;
	loop(150){yield;}
	TLife(390,20,32,33,33,false);
	let timeM = [90,60,36];	
	let timeMA = timeM[0]+timeM[1]+timeM[2];
	let c = -1;
	let spread = [31,4,30+Dif*16,1+Dif,17,21+Dif*5,24];
	let dir = [trunc(rand(0,2))*2-1,1];
	loop
		{
		loop(180)
			{
			c+=1;	
			TPattern;
			yield;
			}
		TDark;
		dir[1] = dir[1]*-1;
		loop(timeMA+timeM[0]*0.45){yield;}
		loop(timeM[0])
			{
			c+=1;
			TPattern;
			yield;
			}
		}

	task TPattern
		{
		if(c == 0)
			{
			ascent(i0 in 0..spread[0])
				{
				ascent(i1 in 0..spread[1])
					{
					let shot = CreateBulletA2(enmx,enmy,0,i0*360/spread[0]+GetAngleToPlayer(objEnemy)+180,1/(75-i1*i1*3),0,1.5+i1*0.67,BUBBLE,0,1,SHOT_GRAY);
					ObjRender_SetBlendType(shot,BLEND_ALPHA);
					ObjRender_SetColorHSV(shot,0,0,0);
					}
				}
			}
		if(c >= 180+timeM[0] && c%50 == 0)
			{
			let x = GetStgFrameWidth*(0.5+dir[0]*rand(0.2,0.4));
			let y = GetStgFrameHeight*0.5-rand(75,105);
			let aM = rand(0,360);
			ascent(i2 in 0..spread[2])
				{
				let a = aM+i2*360/spread[2];
				ascent(i3 in 0..spread[3])
					{
					let d = 0.5+i3*0.5/spread[3];
					let shot = CreateBulletA1(x+0*d*cos(a),y+0*d*sin(a),(1.75+Dif*0.25)*(0.67+0.33*d),a,BL_SMALL,0,1,SHOT_RED);
					ObjRender_SetColorHSV(shot,0,0,0,);	
					}
				}		
			dir[0] = dir[0]*-1;
			}
		if(c >= (180+timeM[0])*2 && c%(30-Dif*10) == 0)
			{
			ascent(i4 in 0..spread[4])
				{
				let shot = CreateBulletA1(enmx,enmy,2.5,(i4+0.5)*360/spread[4]+dir[1]*c*(360/17)/180,SMALL,0,1,SHOT_GRAY);
				ObjRender_SetColorHSV(shot,0,0,0);
				}
			}
		if(c >= (180+timeM[0])*4 && c%(40-Dif*15) == 0)
			{
			ascent(i5 in 0..spread[5])
				{
				let a = (i5+0.5)*360/spread[5]-c*(540/17)/180;
				let shot = CreateBulletA1(GetStgFrameWidth*(0.5+0.5*cos(a)),-6,rand(1.75,2.25)+Dif*0.25,a,PELLET,0,1,SHOT_GRAY);
				ObjRender_SetColorHSV(shot,0,0,0);
				}
			}
		if(c >= (180+timeM[0])*6 && c%(105-Dif*5) == 0)
			{
			ascent(i6 in 0..spread[6])
				{
				let shot = CreateBulletA1(enmx,enmy,1,i6*360/spread[6]+GetAngleToPlayer(objEnemy),ORB,0,1,SHOT_RED);
				ObjShot_SetSpellResist(shot,true);
				ObjRender_SetColorHSV(shot,0,0,0);
				}
			}
		}

















	task TDark
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,"");
		Obj_SetRenderPriority(obj,0.6);
		ObjSprite2D_SetSourceRect(obj,0,0,1,1);
		ObjSprite2D_SetDestRect(obj,-64,-64,GetStgFrameWidth+64,GetStgFrameHeight+64);
		ObjRender_SetColorHSV(obj,0,0,0);
		ObjRender_SetAlpha(obj,0);
		TShotStop;
		ascent(t in 0..timeM[0])
			{
			ObjRender_SetAlpha(obj,t*255/(timeM[0]-1));
			yield;if(Obj_IsDeleted(objEnemy)){Obj_Delete(obj);return;}
			}
		loop(timeM[1]){yield;if(Obj_IsDeleted(objEnemy)){Obj_Delete(obj);return;}}
		PlaySound("se_Event2",100);
		loop(timeM[2]-5){yield;if(Obj_IsDeleted(objEnemy)){Obj_Delete(obj);return;}}
		TShotStart;
		descent(t in 0..5)
			{
			ObjRender_SetAlpha(obj,t*255/5);
			yield;if(Obj_IsDeleted(objEnemy)){Obj_Delete(obj);return;}
			}
		Obj_Delete(obj);
		}
	task TShotStop
		{
		let array = GetShotIdInCircleA2(GetStgFrameWidth*0.5,GetStgFrameHeight*0.5,GetStgFrameHeight*0.8,TARGET_ENEMY);
		ascent(i in 0..length(array))
			{
			let shot = array[i];
			Obj_SetValue(shot,"S_SPEED",ObjMove_GetSpeed(shot));
			Obj_SetValue(shot,"S_ANGLE",ObjMove_GetAngle(shot));
			ObjMove_SetAcceleration(shot,-ObjMove_GetSpeed(shot)/timeM[0]);
			ObjMove_SetMaxSpeed(shot,0);
			}
		}
	task TShotStart
		{
		let array = GetShotIdInCircleA2(GetStgFrameWidth*0.5,GetStgFrameHeight*0.5,GetStgFrameHeight*0.8,TARGET_ENEMY);
		ascent(i in 0..length(array))
			{
			let shot = array[i];
			let s = Obj_GetValueD(shot,"S_SPEED",1);
			let a = Obj_GetValueD(shot,"S_ANGLE",1);
			ObjMove_SetSpeed(shot,-s*1.5);
			ObjMove_SetAcceleration(shot,s*3/timeM[0]);
			ObjMove_SetMaxSpeed(shot,s);
			ObjMove_AddPatternA2(shot,timeM[0]*0.5,0,a,s*3/timeM[0],0,s);
			}
		}

	task TLight
		{
		let count = 64;
		let time = 150;
		let time2 = 105;
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetTexture(obj, "");
		Obj_SetRenderPriority(obj,0.29);
		ObjPrim_SetVertexCount(obj, count+2);
		ascent(i in 0..(count+1)*0.5)
			{
			ObjPrim_SetVertexUVT(obj, i*2, i*1024/(count*0.5-1), 0);
			ObjPrim_SetVertexUVT(obj, i*2+1, i*1024/(count*0.5-1), 1);
			}
		ObjRender_SetPosition(obj,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),0);	
		let t = -1;
		let t2 = time2;
		let tA = 0;
		while(!Obj_IsDeleted(objEnemy))
			{
			if(t < time)	
				{
				t+=1;
				}
			if(t >= 75 && t2 > 0)
				{
				t2 -=1;
				}
			tA+=1;
			ascent(i in 0..(count+1)*0.5)
				{
				let iA = ((i%(count*0.5))*4)%7;
				let iB = (((0.67+0.33*((i%(count*0.5))+tA))*tA)%300)/300;
				let d = (t*(1+iA*0.33))*GetStgFrameHeight*2/time;
				let a = i*360/(count*0.5)+35;
				ObjPrim_SetVertexPosition(obj,i*2+0,0,0,0);
				ObjPrim_SetVertexAlpha(obj,i*2+0,255);
				ObjPrim_SetVertexPosition(obj,i*2+1,d*cos(a),d*sin(a),0);
				ObjPrim_SetVertexAlpha(obj,i*2+1,(170+85*iB)*t/time);
				}
			ObjRender_SetColor2(objEnemy,0,0,t2*255/time2);
			Obj_SetValue(objPlayer,"COLORHSV",t2*255/time2);
			yield;
			}
		Obj_SetValue(objPlayer,"COLORHSV",255);
		Obj_Delete(obj);
		}
}            