#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 80;
let angleMB;

@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	bspell = true;

	TEnemyStart;
	ObjEnemy_Render;
	TLife(400,30,20,0,0,80,50);
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(5000/spellLife);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(200);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}

task TEnemyStart
{
	TXY;
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-120,15,2);
	angleMB = Obj_GetValueD(objScene,"FAM_ANGLE",[0,0]);
	TAngle;
	if(Practice == true)
		{
		CreateBossAura(0);
		}
	loop(45){yield;}
	TEnemyWork;
	ObjEnemyBossScene_SetSpellTimer(objScene,timer*60);
	TBackground; 
	TSpellName;
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	TEnd(true);
}
task TEnemyWork
{
	loop(90){yield;}
	let spread = [16,8,5,7];
	let num = 0;
	let counter = -1;
	let counterA = 0;	
	let count = 300;
	let angleM = 0;
	let angleMA = 0;
	let type = 0;
	let dist = 110+Dif*40;

	loop
		{
		counter+=1;
		counterA+=1;
		if(counter%(6-Dif*2) == 0)
			{
			PlaySound("se_Shot8",65);
			let accel = 1/360;
			if(num%spread[0] < spread[0]*0.2)
				{
				accel = 1/60;
				}
			else if(num%spread[0] < spread[0]*0.375)
				{
				accel = 1/150;
				}
			else if(num%spread[0] < spread[0]*0.5)
				{
				accel = 1/240;
				}
			ascent(dir in 0..2)
				{
				dir = dir*2-1;
				CreateBulletA2(GetStgFrameWidth*(0.5+dir*0.5),((num%spread[0])+rand(0,1))*(GetStgFrameHeight+128)/spread[0]-64,rand(0.25,0.75),90+dir*90+sin(angleM)*22.5,accel,0,2,SMALL_WHITE,8,0.625+floor(rand(0,2))*0.25);
				}
			num+=1;
			}
		if(counter > 130)
			{
			if(angleMA < 1)
				{
				angleMA+=1/120;
				}
			angleM+=angleMA*(0.25+0.125*absolute(sin(angleM)));
			if(counterA%floor(count) == 0)
				{
				TShot(GetPlayerX,GetPlayerY,0.125+300/count,type);
				if(count > 105)
					{
					count-=27.5;
					}
				else
					{
					count = 105;
					}
				counterA = 0;
				type = 1-type;
				}
			}
		yield;
		}

	task TShot(xP,yP,speed,type)
		{
		loop(5)
			{
			ascent(i1 in 0..spread[1])
				{
				let angle1 = i1*360/spread[1]+angleMB[0];
				let x = enmx+dist*cos(angle1);
				let y = enmy+dist*sin(angle1);
				ascent(i2 in 0..spread[2])
					{
					i2 = i2-spread[2]/2+(spread[2]%2)*0.5;
					let speedA = speed+(0.5-0.5*sin(angle1))*0.25-absolute(i2)*(speed*0.6)*2/spread[2];
					let shot = CreateBulletA2(x,y,0.5,i2*360/spread[3]+atan2(yP-y,xP-x),1/(75-Dif*15),0,speedA,ICE_BLUE-type*6,8,1.25);
					if(type == 1)
						{
						ObjRender_SetColorHSV(shot,240,15,255);
						}
					Obj_SetRenderPriority(shot,0.49);
					}
				}
			PlaySound("se_Shot1",90);
			loop(10){yield;}
			}
		}
}	

task TAngle
{
	loop
		{
		ascent(i in 0..2)
			{
			angleMB[i] = angleMB[i]+(i*2-1)*(0.3+i*0.6)*1.5;
			}
		yield;
		}
}