#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 85;

@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(240,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-70,20,5);
	loop(120){yield;}
	let spread = [4];
	let angleP = 0;
	loop
		{
		ascent(dir in 0..2)
			{
			dir = dir*2-1;
			let angleA = 8;
			loop(1)
				{
				PlaySound("se_LaserDelay",100);
				ascent(i in 0..spread[0])
					{
					TEnemy(angleP,i*360/spread[0],45-180/spread[0]+(angleA%16)*(360/spread[0])/16,i%4);
					}
				loop(8){yield;}
				angleA+=dir*6;
				if(angleA < 0)
					{
					angleA+=16;
					}
				}
			loop(240){yield;}
			angleP+=rand(13,17);
			}
		}
	task TEnemy(angleP,angle,angleA,color)
		{
		if(color == 2)
			{
			color = 0;
			}
		else if(color > 2)
			{
			color = 2;
			}
		let orbG = [orb_green,orb_red,orb_blue][color];
		let graph = 0;
		let life = 15;
		if(color == 0)
			{
			graph = 8;
			life = 40;
			}
		else if(color == 2)
			{
			graph = 4;
			}
		let obj = CreateBasicEnemyA1(enmx,enmy,0,0,life,10,type_orb,orbG,0);
		ObjMove_AddPatternA2(obj,0,3,angle+angleA+angleP,-3/45,0,0.5);
		ObjMove_AddPatternA2(obj,90,NO_CHANGE,NO_CHANGE,1/30,0,1.5);
		let scale = 0;
		let counter = -1;
		let count = 0;
		while(!Obj_IsDeleted(objEnemy) && ObjEnemy_GetInfo(obj,INFO_LIFE) > 0 && !Obj_IsDeleted(obj))
			{
			counter+=1;
			if(scale < 1)
				{
				scale+=1/20;
				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(counter >= 60 && counter%(40-Dif*10) == 0 && IsBeyondClip(obj,0) == false)
				{
				TShot;
				}
			if(count < 6 && IsBeyondClip(obj,-scale*15))
				{
				if(ObjMove_GetX(obj) > GetStgFrameWidth-scale*15 || ObjMove_GetX(obj) < scale*15)
					{
					ObjMove_SetAngle(obj,180-ObjMove_GetAngle(obj));	
					}
				else
					{
					ObjMove_SetAngle(obj,-ObjMove_GetAngle(obj));	
					}
				PlaySound("se_Shot7",75);
				count+=1;
				}
			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
			{
			PlaySound("se_Shot1",85);
			let shot = CreateBulletA1(ObjMove_GetX(obj),ObjMove_GetY(obj),0,ObjMove_GetAngle(obj)+180,AMULET_RED+graph,8,0);
			ascent(i in 1..15+1)
				{
				ObjShot_SetScale(shot,i*1.175/15);	
				yield;
				}	
			let speed = 1.5+Dif*0.5;
			ObjMove_SetAcceleration(shot,speed/60);
			ObjMove_SetMaxSpeed(shot,speed);
			return;
			}
		}
}	