#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 150;
let lifeM = 500/spellLife;
let life = lifeM;
let phase = 0;

@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,9,9);
	TEnd;
}

@MainLoop
{
	life = ObjEnemy_GetInfo(objEnemy,INFO_LIFE);	
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_TIMER) <= 25)
		{
		phase = 4;
		}
	else if(life <= lifeM*0.25)
		{	
		phase = 3;
		}
	else if(life <= lifeM*0.5)
		{	
		phase = 2;
		}
	else if(life <= lifeM*0.75)
		{	
		phase = 1;
		}
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(lifeM);
		}
	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(60){yield;}
	let speedA = 90;
	let counter = -1;
	let distX = 0;
	let distY = 0;
	ascent(t in 0..5)
		{
		if(t == phase)
			{
			loop(60){yield;}
			}
		let distXM = 0;
		let distYM = 0;
		let counterA = 0;

		alternative(t)
			case(1)
				{
				distXM = GetStgFrameWidth*0.25;
				distYM = GetStgFrameWidth*0.05;
				}
			case(2)
				{
				distXM = GetStgFrameWidth*0.275;
				distYM = GetStgFrameWidth*0.175;
				}
			case(3,4)
				{
				distXM = GetStgFrameWidth*0.3;
				distYM = GetStgFrameWidth*0.25;
				}

		speedA = 90;
		let angle = 0;

		while(phase == t)
			{
			if(phase > 0)
				{
				if(distX < distXM)
					{
					distX+=(distXM-distX)/30;
					}
				if(distY < distYM)
					{
					distY+=(distYM-distY)/30;
					}		
				ObjMove_SetDestAtFrame(objEnemy,GetStgFrameWidth*0.5+distX*cos(counter),GetStgFrameHeight*0.5-70+distY*sin(counter*2),1);
				}
			counter+=1;
			counterA+=1;
			if(counter%(30-floor(phase/2)-Dif*9-floor(phase/4)*2) == 0)
				{
				TSound;
				TShot(angle);
				}
			if(counterA > 60 && counter%5 == 0)
				{
				PlaySound("se_Shot3",65);
				ascent(i in 0..5)
					{
					i = i-5/2+0.5;
					let angle = 270+(165+Dif*10+ceil(phase/4)*7.5+phase*7.5)*cos(counter);
					let shot = CreateBulletA1(enmx+32*cos(angle+i*60/5),enmy+32*sin(angle+i*60/5),5.5,angle+i*20/5,ARROWHEAD_BLUE-1,8,1.125);
					Obj_SetRenderPriority(shot,0.51);
					}
				}
			speedA+=1;
			angle+=29/10;
			yield;
			}
		DeleteShotAll(TYPE_CHILD,TYPE_ITEM);
		}

	task TShot(angleM)
		{
		let spread = [3,4];
		let graph = [[SMALL_YELLOW,PELLET_YELLOW],[SMALL_ORANGE,PELLET_ORANGE]];
		ascent(i in 0..spread[0])
			{
			let angle = i*360/spread[0]+angleM;
			ascent(t in 0..2)
				{
				let dist = 64-t*32;
				let speed = 0.75+t*(1+Dif*0.125);
				TSpam(angle,dist,speed,(0.75+(speedA%60)*0.25/60),i,t);
				}	
			}
		task TSpam(angle,dist,speed,rate,i,t)
			{
			let shot = CreateBulletA2(enmx+dist*cos(angle),enmy+dist*sin(angle),0.66,90,((speed+phase*0.25)*rate-0.66)/60,0,(speed+phase*0.25)*rate,graph[t][0],8,1.125);
			ObjShot_SetSpellResist(shot,true);
			loop(60){yield;if(Obj_IsDeleted(shot)){return;}}
			ObjMove_SetAngle(shot,GetAngleToPlayer(shot));
			ObjMove_SetSpeed(shot,speed+0.125+t*floor(phase/4)*0.25);
			ObjMove_SetAcceleration(shot,0);
			}
		}

	task TSound
		{
		let phaseA = phase;
		PlaySound("se_Shot1",95);
		loop(60){if(phaseA != phase){return;}yield;}
		PlaySound("se_Shot7",65);
		}
}	