#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 55;

@Initialize
{
	Obj_SetValue(GetEnemyBossSceneObjectID,"INIT",false);
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy,0,0);

	TEnemyStart;
	ObjEnemy_Render; 
	TLife(240,20,20,0,0,125,125);
}

@MainLoop
{
	yield;
}

task TEnemyStart
{
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-105,15,2);
	loop(60){yield;}
	if(Practice == false)
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "../../../system/img/WhiteSquare.png");
		ObjSprite2D_SetSourceRect(obj,0,0,1,1);
		ObjRender_SetColorHSV(obj,240,10,255);
		ObjSprite2D_SetDestRect(obj,-8,-8,GetScreenWidth+8,GetScreenHeight+8);
		Obj_SetRenderPriority(obj,1);
		ascent(t in 0..115)
			{
			ObjRender_SetAlpha(obj,(t+1)*255/105);
			if(t == 60)
				{
				CreateBossAura(0);
				TAngle;
				}
			if(t == 15)
				{
				PlaySound("se_Shatter",100);
				}
			yield;
			}
		SetCommonData("BACKGROUND_COLD",true);
		yield;
		Obj_Delete(obj);
		}
	else
		{
		CreateBossAura(0);
		TAngle;
		loop(30){yield;}
		}
	loop(85){yield;}
	TEnemyWork;
	ObjEnemyBossScene_SetSpellTimer(objScene,timer*60);
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-135,15,2);
	TEnd(false);
}

task TAngle
	{
	loop
		{
		ascent(i in 0..2)
			{
			famAngle[i] = famAngle[i]+(i*2-1)*(0.3+i*0.6)*1.5;
			}
		Obj_SetValue(objScene,"FAM_ANGLE",famAngle);
		yield;
		}
	}
@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(5000);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(200);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}
task TEnemyWork
{
	TXY;
	let spread = [10,4+Dif*2,6,1+Dif];
	let dir = 1;
	loop
		{
		descent(t in 0..spread[0])
			{
			let x = dir*((t%2)*2-1)*(rand(0,0.1)+t*0.35/spread[0]);
			x = GetStgFrameWidth*(0.5+x);
			let num = rand(-1,1)*0.125;
			let y = GetStgFrameHeight*0.1+(t+num)*(t+num)*(GetStgFrameHeight*1.125)/(spread[0]*spread[0]-1);
			TSound;
			ascent(i1 in 0..spread[1])
				{
				i1 = i1-spread[1]*0.5+0.5;
				TShot(x+i1*GetStgFrameWidth*(0.6-t*0.575/spread[0])/spread[1],y,270);
				}
			loop(15){yield;}
			}
		loop(60){yield;}
		let angleM = GetAngleToPlayer(objEnemy)-dir*90;
		ascent(t in 0..181)
			{
			if(t%90 == 0)
				{
				ObjEnemy_Move(objEnemy,20,4,rand(30,40),rand(130,140),150,2);
				}
			if(t%(8+Dif*3) == 0)
				{
				PlaySound("se_Shot1",65);
				ascent(i in 0..spread[3])
					{
					let shot = CreateBulletA2(enmx,enmy,5,i*165/spread[3]+dir*t*570/181+angleM,(0.5-5)/30,0,0.5,LEN_PURPLE,8,0.75);
					Obj_SetRenderPriority(shot,0.49);
					}
				}
			yield;
			}
		loop(59){yield;}
		dir*=-1;
		}
	task TShot(x,y,angleM)
		{
		loop(rand(0,15)){yield;}
		let time = 150;
		CreateIceDelay(x,y,angleM,120,time);
		x+=time*0.125*cos(angleM);
		y+=time*0.125*sin(angleM);
		loop(time*0.5){yield;}
		ascent(i in 0..spread[2])
			{
			let angle = i*360/spread[2]+90+rand(-1,1)*2.5;
			TSpam(angle)
			}	

		task TSpam(angle)
			{
			let shot = CreateBulletA2(x+4*cos(angle),y+4*sin(angle),0,angle,1/45,0,2.25+Dif*0.125,ICE_BLUE,8,1.125);
			let count = 0;
			while(!Obj_IsDeleted(shot) && count < 1)
				{
				if(ObjMove_GetX(shot) < -8)
					{
					ObjMove_SetX(shot,-8);
					ObjMove_SetAngle(shot,180-ObjMove_GetAngle(shot));
					count+=1;
					}
				if(ObjMove_GetX(shot) > GetStgFrameWidth+8)
					{
					ObjMove_SetX(shot,GetStgFrameWidth+8);
					ObjMove_SetAngle(shot,180-ObjMove_GetAngle(shot));
					count+=1;
					}
				yield;
				}
			}
		}
	task TSound
		{
		loop(30){yield;}
		PlaySound("se_Shot8",90);
		loop(7){yield;}
		PlaySound("se_Shot8",90);
		}

}