#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 55;
let angleM;

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

	TEnemyStart;
	ObjEnemy_Render;
	TLife(400,30,20,0,0,105,40);
}

@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-105,15,2);
	angleM = Obj_GetValueD(objScene,"FAM_ANGLE",[0,0]);	
	angleM = angleM ~ [0,45+rand(-1,1)*15,90];
	TAngle;
	if(Practice == true)
		{
		CreateBossAura(0);
		loop(15){yield;}
		}
	loop(45){yield;}
	TEnemyWork;
	ObjEnemyBossScene_SetSpellTimer(objScene,timer*60);
	TBackground; 
	TSpellName;
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	TEnd(true);
}

task TEnemyWork
{
	loop(90){yield;}
	let spreadA = [4,2+floor(Dif/2)];
	let timeA = [5,8-Dif,5];
	let spreadB = [3,1+ceil(Dif/2)];
	let timeB = [5,9,2];
	let spreadC = [2,3,5][Dif];
	let counter = -1;
	let graph = [SMALL_GREEN,SMALL_BLUE,SMALL_PURPLE,SMALL_PURPLE+1];
	let g = 0;
	loop
		{
		counter+=1;
		ascent(i1 in 0..spreadA[1])
			{
			let num = i1/spreadA[1];
			i1 = i1-0.5;
			if(counter%(timeA[2]) == 0 && floor(counter/timeA[2]-num*3)%timeA[1] < timeA[0])
				{
				PlaySound("se_Shot2",60);
				ascent(i0 in 0..spreadA[0])
					{
					let angle1 = i0*360/spreadA[0]+angleM[0];
					let angle2 = angle1+i1*20+90;
					let shot = CreateBulletA2(enmx+150*cos(angle1),enmy+150*sin(angle1),1,angle2,1/180,0,1.7,ICE_GRAY,8,1.25);
					}
				}
			}
		ascent(i1 in 0..spreadB[1])
			{
			let num = i1/spreadB[1];
			i1 = i1-0.5;
			if(counter > 30 && counter%(timeB[2]) == 0 && floor(counter/timeB[2]-num*5)%timeB[1] < timeB[0])
				{
				ascent(i0 in 0..spreadB[0])
					{
					let angle1 = i0*360/spreadB[0]+angleM[1];
					let angle2 = angle1+i1*30-90;
					CreateBulletA2(enmx+79*cos(angle1),enmy+79*sin(angle1),1,angle2,1/180,0,1.7,ICE_GRAY,8,1.25);
					}
				}
			}
		if(counter > 215 && counter%(5-Dif*2) == 0)
			{
			PlaySound("se_Shot7",50);
			ascent(i in 0..spreadC)
				{
				let angle = i*360/spreadC+angleM[0]*2+angleM[3];
				let shot = CreateBulletA2(enmx+8*cos(angle),enmy+8*sin(angle),5,angle,-4.5/15,0,0.5,graph[g%length(graph)],8,1.125);
				ObjMove_AddPatternA2(shot,45,NO_CHANGE,angle+cos(angleM[2])*90+(g%2)*90,-1/75,0,-1.75);
				ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
				}
			g+=1;
			}
		if(counter > 300 && counter%240 == 0)
			{
			ObjEnemy_Move(objEnemy,20,2,10,rand(95,115),120,2);
			}
		yield;
		}
}	

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