#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 61;

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

	TEnemyWork;
	ObjEnemy_Render; 
	TLife(180,20,20,0,0,75,15);
	TEnd;
	SetShotAutoDeleteClip(8,8,8,8);
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(500);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(timer);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}
task TEnemyWork
{
	TXY;
	loop(60){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-90,15,2);
	loop(45){yield;}
	let spread = [16];
	let angleAM;
	let speedSM = 0;
	loop
		{
		ascent(dir in 0..2)
			{
			dir = dir*2-1;
			let angle = 90+dir*90+rand(-1,1)*3*180/spread[0];
			let time = 270;
			let speed = 2.5;
			let angleA = dir*45;
			angleAM = 1;
			ascent(i in 0..spread[0])
				{	
				PlaySound("se_LaserDelay",120);
				TEnemy(speed,i*360/spread[0]+angle,angleA,floor(((i%4)+1)/4),dir,time);
				loop(5)
					{
					time-=1;
					angleAM+=1/(spread[0]*5);
					angleA+=dir*angleAM;
					speed-=speedSM;
					yield;
					}
				}
			loop(225){yield;}
			ObjEnemy_Move(objEnemy,20,3,rand(20,30),rand(75,105),150,2);
			loop(75){yield;}
			}
		}
	task TEnemy(speedS,angle,angleA,color,dir,time)
		{
		let graph = [AMULET_RED,AMULET_BLUE][color];
		let x = enmx;
		let y = enmy;
		let obj = CreateBasicEnemyA1(x,y,0,angle,10,7.5,type_orb,orb_red+color*5,0);
		let scale = 0;

		let dist = 0;
		let speedE = 1;
		let angleE;

		let counter = 270-time;
		let counterA = 8-Dif*2-color*2;
		while(!Obj_IsDeleted(objEnemy) && ObjEnemy_GetInfo(obj,INFO_LIFE) > 0 && !Obj_IsDeleted(obj))
			{
			counter+=1;
			dist+=min((75-dist)/60,5);
			if(counter < 270)
				{
				ObjMove_SetDestAtFrame(obj,x+dist*cos(angle+angleA),y+dist*sin(angle+angleA),1);
				angleA+=dir*angleAM;
				}
			else if(counter == 270)
				{
				ObjMove_SetSpeed(obj,speedE);
				ObjMove_SetAngle(obj,angleE);
				ObjMove_SetAcceleration(obj,-speedE*0.33/20);
				ObjMove_SetMaxSpeed(obj,speedE*0.67);
				counterA = 15-Dif*4;
				}
			else
				{
				angleA+=dir*0.5;
				}
			speedE = ObjMove_GetSpeed(obj);
			angleE = ObjMove_GetAngle(obj);
			if(scale < 1)
				{
				scale+=1/60;
				ObjEnemy_SetScale(obj,scale*32);
				ObjRender_SetColorHSV(obj,0,0,(0.5+scale*0.5)*255);
				ObjRender_SetAlpha(obj,(0.5+scale*0.5)*255);
				}
			if(IsBeyondClip(obj,64))
				{
				Obj_Delete(obj);
				}
			if(counter > 20 && IsBeyondClip(obj,0) == false)
				{
				speedS-=speedSM;
				if(counter%counterA == 0)
					{
					PlaySound("se_Shot3",75);
					TShot;
					}
				}
			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
			{
			let shot = CreateBulletA1(ObjMove_GetX(obj),ObjMove_GetY(obj),max(0.5,speedS),angle+angleA*(0.75+color*0.5),graph,8,0);
			ascent(i in 1..15+1)
				{
				ObjShot_SetScale(shot,i*1.175/15);
				if(Obj_IsDeleted(shot)){return;}
				yield;
				}
			return;
			}
		}
}