#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 75;

@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy,0,0);

	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	bspell = true;

	TEnemyWork;
	TBackground;
	ObjEnemy_Render; 
	TLife(400,30,20,0,0,20,20);
	TEnd;
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(500/spellLife);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(timer);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}

task TEnemyWork
{
	TXY;
	TSpellName;
	TSpellFace(pathSanae);
	loop(60){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-150,20,5);
	loop(120){yield;}
	let spread = [6];
	loop
		{
		ascent(dir in 0..2)
			{
			dir = dir*2-1;
			let counter = floor(rand(0,120));
			ascent(i in 0..spread[0]+1)
				{
				PlaySound("se_LaserDelay",100);
				TEnemy(-dir,i*GetStgFrameHeight/spread[0],(dir+1)/2,counter);
				loop(30){yield;}
				}
			loop(270+Dif*10){yield;}
			ascent(i in 0..spread[0]+1)
				{
				PlaySound("se_LaserDelay",100);
				let dirA = dir*((i%2)*2-1);
				TEnemy(-dirA,i*GetStgFrameHeight/spread[0],(dirA+1)/2,0);
				loop(10){yield;}
				}
			loop(270+Dif*10){yield;}
			}
		}
	task TEnemy(dir,y,color,counter)
		{
		let obj = CreateBasicEnemyA1((0.5+dir*0.5)*GetStgFrameWidth,y,0.5,90+dir*90,10,5,type_orb,orb_red+color*5,0);
		ObjMove_AddPatternA2(obj,40,NO_CHANGE,NO_CHANGE,1/40,0,10);
		let scale = 0;

		let angleA = dir*120;
		while(!Obj_IsDeleted(objEnemy) && ObjEnemy_GetInfo(obj,INFO_LIFE) > 0 && !Obj_IsDeleted(obj))
			{
			counter+=1;
			if(scale < 1)
				{
				scale+=1/40;
				ObjEnemy_SetScale(obj,scale*40);
				ObjRender_SetColorHSV(obj,0,0,(0.5+scale*0.5)*255);
				ObjRender_SetAlpha(obj,(0.5+scale*0.5)*255);
				}
			if(IsBeyondClip(obj,128))
				{
				Obj_Delete(obj);
				}
			if(IsBeyondClip(obj,0) == false)
				{
				if(counter%(8-Dif) == 0)
					{
					TShot(angleA);
					}
				angleA-=dir*2.75;
				}
			yield;
			}
		if(Obj_IsDeleted(obj)){return;}
		if(Obj_IsDeleted(objEnemy)){Obj_Delete(obj);return;}
		ObjEffect_Explosion(ObjMove_GetX(obj),ObjMove_GetY(obj));
		Obj_Delete(obj);
		PlaySound("se_Die2",60);
		task TShot(angle)
			{
			let shot = CreateBulletA2(ObjMove_GetX(obj),ObjMove_GetY(obj),3,ObjMove_GetAngle(obj)+180,-3/20,0,0,SMALL_RED+color*2*2,8,1.125);
			ObjShot_SetSpellResist(shot,true);
			loop(135){yield;if(Obj_IsDeleted(shot) || ObjMove_GetX(shot) > GetStgFrameWidth || ObjMove_GetX(shot) < 0){Obj_Delete(shot);return;}}
			let shot2 = CreateBulletA1(ObjMove_GetX(shot),ObjMove_GetY(shot),0,0,AMULET_RED+color*2*2,0,1.175);
			angle+=GetAngleToPlayer(shot2);
			Obj_Delete(shot);
			loop(Dif+1)
				{
				PlaySound("se_Shot7",65);
				ObjShot_SetDelay(shot2,8);
				ObjMove_AddPatternA3(shot2,0,3,GetAngleToPlayer(shot2),-3/20,0,0,AMULET_ORANGE-color*8);
				loop(45){yield;if(Obj_IsDeleted(shot2)){return;}}
				}
			CreateBulletA2(ObjMove_GetX(shot2),ObjMove_GetY(shot2),0.33,angle,1/60,0,1,PELLET_RED+color*2*2,8,1.125);
			Obj_Delete(shot2);
			}
		}
}	