#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 130;
life = 3000;

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

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	TSpellName("New Moon \"Rabbit's Mochi Pounding Ceremony\"");
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-105,20,5);
	loop(75){yield;}
	TLife(0,20,32,1000,1000,false);
	ObjRender_SetAlpha2(objEnemy,155);
	ascent(i in 0..5)
		{
		TMask(i*360/5,0);
		}		
	loop(420){yield;}
	ascent(i in 0..4)
		{
		TMask(i*360/4,1);
		}		
	loop(420){yield;}
	ascent(i in 0..7)
		{
		TMask(i*360/7,2);
		}
	task TMask(a,type)
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/Sprite_Mask.png");
		ObjSprite2D_SetSourceRect(obj,0,0,256,256);
		ObjRender_SetScaleXYZ(obj,1/8,1/8,0);
		ObjSprite2D_SetDestCenter(obj);
		alternative(type)
			case(0)
				{
				TFormation0(a,obj);
				}
			case(1)
				{
				TFormation1(a,obj);
				}
			case(2)
				{
				TFormation2(a,obj);
				}
		}
	
	task TFormation0(a,obj)
		{
		let t = -1;
		ObjRender_SetColorHSV(obj,0,155,255);
		let time = [8,15,8][Dif];
		loop
			{
			t+=1;
			ObjRender_SetAngleZ(obj,a);
			let x = enmx+min(t,40)*1.5*cos(a);
			let y = enmy+min(t,40)*1.5*sin(a);
			ObjRender_SetPosition(obj,x,y,0);	
			if(t > 120 && t%time ==0)
				{
				let shot = CreateBulletA2(x,y,3.5,a-t/8+180+10,-1/20,0,0,AMULET,8,1.125,SHOT_RED);
				ObjMove_AddPatternA2(shot,90,NO_CHANGE,NO_CHANGE,1/20,0,2);
				if(Dif >= 1)
					{
					let shot = CreateBulletA2(x,y,3.5,a,-1/20,0,0,AMULET,8,1.125,SHOT_RED);
					ObjShot_SetSpellResist(shot,true);
					ObjMove_AddPatternA2(shot,90,NO_CHANGE,NO_CHANGE,1/20,0,2);
					}
				}
			a+=2;
			yield;
			}	
		Obj_Delete(obj);
		}

	task TFormation1(a,obj)
		{
		let time = [23,28,23][Dif];
		let t = -1;
		ObjRender_SetColorHSV(obj,240,155,255);
		loop
			{
			t+=1;
			ObjRender_SetAngleZ(obj,a);
			let x = enmx+min(t,80)*1.5*cos(a);
			let y = enmy+min(t,80)*1.5*sin(a);
			ObjRender_SetPosition(obj,x,y,0);	
			if(t > 120 && floor(t/1)%time <= 5 && t%1 == 0)
				{
				let shot = CreateBulletA2(x,y,3,a+t/6+180-10,-1/20,0,0,AMULET,8,1.125,SHOT_BLUE);
				ObjMove_AddPatternA2(shot,90,NO_CHANGE,NO_CHANGE,1/20,0,1.25);
				}
			if(t > 120 && t%8 == 0 && Dif >= 1)
				{
				let shot = CreateBulletA2(x,y,3,a,-1/20,0,0,AMULET,8,1.125,SHOT_BLUE);
				ObjShot_SetSpellResist(shot,true);
				ObjMove_AddPatternA2(shot,90,NO_CHANGE,NO_CHANGE,1/20,0,1.25);
				}
			a-=2.6;
			yield;
			}	
		Obj_Delete(obj);
		}

	task TFormation2(a,obj)
		{
		let time = [50,60,50][Dif];
		let t = -1;
		ObjRender_SetColorHSV(obj,60,155,255);
		loop
			{
			t+=1;
			ObjRender_SetAngleZ(obj,a);
			let x = enmx+min(t,120)*1.5*cos(a);
			let y = enmy+min(t,120)*1.5*sin(a);
			ObjRender_SetPosition(obj,x,y,0);	
			if(t > 120 && t%time == time*0.5)
				{
				TSpam(x,y,a,t);
				}
			if(t > 120 && t%time == 0 && Dif >= 1)
				{
				let shot = CreateBulletA2(x,y,2.5,a-90,-1/20,0,0,AMULET,8,1.125,SHOT_YELLOW);
				ObjShot_SetSpellResist(shot,true);
				ObjMove_AddPatternA2(shot,90,NO_CHANGE,NO_CHANGE,1/20,0,0.5);
				}
			a+=0.2;
			yield;
			}	
		Obj_Delete(obj);
		task TSpam(x,y,a,t)
			{
			ascent(i in 0..3)
				{
				let shot = CreateBulletA2(x,y,2.5,a+90,-1/20,0,0,AMULET,8,1.125,SHOT_YELLOW);
				ObjMove_AddPatternA2(shot,90+i*15,NO_CHANGE,NO_CHANGE,1/20,0,0.5);
				loop(3){yield;}
				}
			}
		}
}            