#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 90;

@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(360,30,20,2.5,2.5,15,15);
	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(30){yield;}
	let spread = [5,8];
	let countM = 0;
	PlaySound("se_LaserDelay",110);
	ascent(i in 0..4)
		{
		TEnemy(i*360/4+45,i);
		}
	loop
		{
		countM = 0;
		yield;
		}
	task TEnemy(angle,numb)
		{
		let distM = 175;
		let dist = 0;
		let x = GetStgFrameWidth*0.5;
		let y = GetStgFrameHeight*0.5+40;
		let distD = 0;
		let angleD = 0;
		let obj = CreateBasicEnemyA1(x+dist*cos(angle),y+dist*sin(angle),0,angle,1,0,type_orb,orb_yellow,0);
		ObjEnemy_SetIntersect(obj,false);
		let scale = 0;
		let scaleA = 0;
		let counter = -1;
		while(!Obj_IsDeleted(objEnemy) && ObjEnemy_GetInfo(obj,INFO_LIFE) > 0 && !Obj_IsDeleted(obj))
			{
			counter+=1;
			if(counter < 135)
				{
				angle-=360/330;
				dist += GetStgFrameHeight*0.6/135;
				}
			else if(counter%8 == 0)
				{
				if(numb == 0)
					{
					PlaySound("se_Shot1",75);
					}
				ascent(dir in 0..2)
					{
					dir = dir*2-1;	
					ascent(i in 0..3)
						{
						let angleS = angle+scaleA*dir*(-135+i*(0.55+sin(counter*1.5)*0.45)*90/3);
						CreateBulletA1(ObjMove_GetX(obj)+64*cos(angleS),ObjMove_GetY(obj)+64*sin(angleS),5,angleS,ICE_GREEN-1,8,1.125);
						}
					}
				}
			if(counter >= 135 && counter < 330)
				{
				if(scaleA < 1)
					{	
					scaleA+=1/90;
					}
				angle-=360/330;
				if(counter >= 195)
					{
					dist += (distM-GetStgFrameHeight*0.6)/135;
					ObjEnemy_SetIntersect(obj,true);
					}
				}
			if(counter >= 390 && counter%90 == 0)
				{
				let count = floor(rand(0,4));
				if((count == 0 || counter%360 == numb*90) && countM < 2)
					{	
					if(countM == 0)
						{
						TSound;
						}
					countM+=1;
					let numbA = rand(-1,1)*0.5;
					ascent(i in 0..spread[0])
						{
						i = i-spread[0]/2+0.5+numbA;
						TShot(i*(97.5-Dif*7.5)/spread[0]+angle+180);
						}
					}
				}
			ObjMove_SetDestAtFrame(obj,x+dist*cos(angle),y+dist*sin(angle),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);
				}
			yield;
			}
		if(Obj_IsDeleted(obj)){return;}
		if(Obj_IsDeleted(objEnemy)){Obj_Delete(obj);return;}
		ObjEffect_Charge(ObjMove_GetX(obj),ObjMove_GetY(obj));
		Obj_Delete(obj);
		
		task TShot(angle)
			{
			let shot = CreateBulletA2(ObjMove_GetX(obj),ObjMove_GetY(obj),5,angle,-5/30,0,0,SMALL_YELLOW-1,8,1);
			ObjShot_SetSpellResist(shot,true);
			loop(60){yield;if(Obj_IsDeleted(shot)){return;}}
			ascent(i in 0..spread[1])
				{
				let speed = (1.5-Dif*0.6)+i*(3+Dif*0.6)/spread[1];
				let dist = -(i-1)*48/spread[1];
				CreateBulletA2(ObjMove_GetX(shot)+dist*cos(angle),ObjMove_GetY(shot)+dist*sin(angle),speed*0.25,angle,speed/60,0,speed,PELLET_YELLOW,8,1.2);
				}
			Obj_Delete(shot);
			}
		}
	
	task TSound
		{
		PlaySound("se_Shot4",90);		
		loop(60){yield;}
		PlaySound("se_Shot7",70);
		}
}	