#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 120;

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

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

@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);
	if(Practice == true)
		{
		CreateBossAura(0);
		}
	loop(60){yield;}
	TEnemyWork;
	ObjEnemyBossScene_SetSpellTimer(objScene,timer*60);
	TBackground; 
	TSpellName;
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	TEnd(true);
}

task TEnemyWork
{
	loop(90){yield;}
	let spread = [6,8,2,28+Dif*8,6];
	let angleM = [0,0,90-180/spread[0],90-180/spread[0]];
	let angleMA = [0,0];
	let dirM = [-1,1];
	let mspeed = [10,3];
	let accel = 1/(80-Dif*20);
	
	let counter = -1;
	loop
		{
		counter+=1;
		if(counter%(7-Dif*2) == 0)
			{
			PlaySound("se_Shot1",60);
			ascent(i in 0..spread[0])
				{
				i += 0.5;
				TShot1(i*360/spread[0]+90+angleM[0]*202.5/spread[0],0);
				}
			}
		if(counter == 4*60)
			{
			PlaySound("se_Shot7",70);
			}
		if(counter > 4*60)
			{
			if(counter%(8-Dif*3) == 0)
				{
				ascent(i in 0..spread[0])
					{
					let shot = CreateBulletA2(enmx,enmy,1,i*360/spread[0]+angleM[2],accel*1.25,0,100,PELLET_BLUE,8,1.25);
					}
				}
			angleM[2] = angleM[2]-7/6;
			}
		if(counter == 13*60)
			{
			PlaySound("se_Shot7",70);
			}
		if(counter > 13*60)
			{
			if(counter%(12-Dif*2) == 0)
				{
				PlaySound("se_Shot2",40);
				ascent(i0 in 0..spread[0])
					{
					i0+=0.5;
					ascent(i2 in 0..spread[2])
						{
						i2 = i2-spread[2]*0.5+rand(0,1);	
						let shot = CreateBulletA2(enmx,enmy,1,i0*360/spread[0]+90+angleM[0]*202.5/spread[0]+i2*(90/spread[1])/spread[2],accel,0,100,ARROWHEAD_AQUA,8+Dif*4,1.25);
						}
					}
				}
			angleM[3] = angleM[3]+13/4;
			}
		if(counter == 22*60)
			{
			PlaySound("se_Shot7",70);
			}
		if(counter > 22*60 && floor(counter/75)%5 < 4 && counter%75 == 0)
			{
			PlaySound("se_Shot8",80);
			TShot3;
			}
		if(counter == 32*60)
			{
			PlaySound("se_Shot7",70);
			}
		if(counter > 32*60 && counter%(10-Dif*4) == 0)
			{
			PlaySound("se_Shot4",40);
			ascent(i in 0..spread[1])
				{
				i += 0.5;
				TShot1(i*360/spread[1]+90+angleM[1]*90/spread[0],1);
				}
			}
		if(counter == 44*60)
			{
			PlaySound("se_Shot7",70);
			}
		if(counter > 44*60)
			{
			if(counter%(10-Dif*2) == 0)
				{
				ascent(i in 0..spread[4])
					{
					let shot = CreateBulletA2(enmx,enmy,1.75,i*360/spread[4]-angleM[2]*0.75-30,accel*0.175,0,100,PELLET_AQUA,8,1.25);
					}
				}
			}
		ascent(i in 0..2)
			{
			if(counter >= (3+i*31)*60)
				{
				angleM[i] = angleM[i]+dirM[i]*angleMA[i];	
				if(angleMA[i] < 1/25)
					{
					angleMA[i]=angleMA[i]+(1/25)/(60+i*30);
					}
				if(angleM[i] < -1)
					{
					angleM[i] = -1;
					dirM[i] = 1;
					}	
				else if(angleM[i] > 1)
					{
					angleM[i] = 1;
					dirM[i] = -1;
					}
				}	
			else
				{
				if(counter >= i*32*60 && mspeed[i] > 1)
					{
					mspeed[i]=mspeed[i]+(1-mspeed[i])/37;
					}
				}
			}
		yield;
		}

	task TShot1(angle,type)
		{
		let graph = [LARGE_YELLOW,LARGE_GRAY];
		let shot = CreateBulletA2(enmx,enmy,1,angle,mspeed[type]*accel*(1+type*0.25),0,100,graph[type],0,1);
		Obj_SetRenderPriority(shot,0.51-type*0.02);
		if(type == 1)
			{
			ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
			}
		let time = 12;
		ObjShot_SetIntersection(shot,false);
		ObjShot_SetSpellResist(shot,true);
		ascent(t in 1..time+1)
			{
			ObjShot_SetScale(shot,(1)*(2-t/time));
			ObjRender_SetAlpha(shot,t*255/time);
			yield;
			if(Obj_IsDeleted(shot)){return;}
			}
		ObjShot_SetIntersection(shot,true);
		ObjShot_SetSpellResist(shot,false);
		}

	task TShot3
		{
		ascent(i in 0..spread[3])
			{
			let angle = (i+0.5)*360/spread[3]+GetAngleToPlayer(objEnemy);
			let shot = CreateBulletA2(enmx,enmy,1,angle,accel,rand(-0.5,1)*((i%2)*2-1)*(90/spread[3])*accel,1.25,SMALL_BLUE,8,1.125);
			ObjMove_AddPatternA2(shot,2/accel,NO_CHANGE,NO_CHANGE,accel*0.5,0,10);
			}
		}
}	