#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 76;

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

	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	bspell = true;

	TEnemyWork;
	ObjEnemy_Render; 
	TLife(450,30,20,1.5,1.5,13.5,13.5);
	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-110,20,5);	
	loop(90){yield;}
	let time = 360-Dif*90;
	let graph = [[SMALL_BLUE,S_STAR_RED],[SMALL_RED,S_STAR_PURPLE],[SMALL_PURPLE,S_STAR_RED]][Dif];
	let spread = [40,90,24];
	let counter = -1;
	let counterS = -1;
	let counterM = 180;
	let angleM = 180;
	let angleA = 1.5*360;
	let dirM = 1;
	loop
		{
		ascent(dir in 0..2)
			{
			dir = dir*2-1;
			let n = 0;
			ascent(i0 in 0..spread[0])
				{
				let x = GetStgFrameWidth*(-0.625+i0*2.25/spread[0]);
				if(x < GetStgFrameWidth*0.5-spread[1]*0.5 || x > GetStgFrameWidth*0.5+spread[1]*0.5)
					{
					TShot(x,dir,n);
					n+=1;
					}
				}
			PlaySound("se_Shot4",100);
			loop(ceil(time*0.35-Dif*0.05))
				{
				angleM+=angleA/time;
				if(angleM%360 == 180)
					{
					dirM*=-1;
					}
				counter+=1;	
				if(counter > ceil(time*1.25))
					{
					counterS+=1;	
					if(counter%300 == 0)
						{
						ObjEnemy_Move(objEnemy,90,2.25,rand(20,45),rand(130,150),150,2);
						}
					if(counterS == counterM)
						{
						PlaySound("se_Shot1",90);
						PlaySound("se_Shot4",80);
						ascent(i in 0..spread[2])	
							{
							CreateBulletA1(enmx,enmy,1+Dif*0.25,i*360/spread[2]+GetAngleToPlayer(objEnemy),graph[1],8,1.125);
							}
						counterS = 0;	
						if(counterM > 30)
							{
							counterM -= 5;
							}	
						else
							{
							counterM = 30;
							}
						}
					}
				yield;
				}
			}
		}
	task TShot(x,dir,n)
		{
		let y = -16;
		let shot = CreateBulletA1(x,y,0,0,graph[0],0,1.125);
		ObjShot_SetAutoDelete(shot,false);
		ObjShot_SetSpellResist(shot,true);
		while(!Obj_IsDeleted(shot))
			{		
			y+=GetStgFrameHeight/time;
			ObjMove_SetDestAtFrame(shot,x+GetStgFrameWidth*dirM*dir*0.3*(1+cos(angleM)),y,1);
			if(y > GetStgFrameHeight)
				{
				break;
				}
			yield;
			}
		if(Obj_IsDeleted(shot)){return;}	
		if(n == 0)
			{
			PlaySound("se_Shot7",60);
			}
		ObjShot_SetAutoDelete(shot,true);
		ObjShot_SetSpellResist(shot,false);
		ObjMove_AddPatternA2(shot,floor(time*rand(0.25,0.75)),NO_CHANGE,270+rand(-1,1)*3,1/rand(45,75),0,1+rand(-1,1)*0.25);
		}
}	