#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 60;

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

	TEnemyWork;
	ObjEnemy_Render;
	TLife(120,20,32,1,1);
	TEnd(false);
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(1350);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(timer);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}
task TEnemyWork
{
	Dif -= 1;
	TXY;
	loop(60){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-150,15,2);
	loop(45){yield;}
	let countM = 35-Dif*5;
	let distM = GetStgFrameWidth*0.15;
	let sM = distM/150;
	let spread = [12];
	let dir = 1;
	let n = 0;

	local
		{
		let v = [];
		ascent(i in 0..distM)
			{
			if(i%countM == 0)
				{
				v = v ~ [rand(0,1)*2-1];
				}	
			}
		ascent(t in 0..spread[0])
			{
			ascent(i in 0..distM)
				{
				if(i%countM == 0)
					{
					i = floor(i/countM);
					ascent(dir in 0..2)
						{
						TSpam(t,i,dir*2-1,v[i]);
						}
					n+=1;
					}
				}
			PlaySound("se_Shot1",75);
			loop(2){yield;}
			}
		task TSpam(t,i,dir,v)
			{	
			let x = t;
			if(dir == -1)
				{
				x = spread[0]-1-t;
				}
			let dist = i*countM;
			let shot = CreateBulletA1(GetStgFrameWidth*(0.5+dir*0.5)-dir*dist+dir*12,(x+0.25+dir*0.25+(n%2)*0.5)*1.1*GetStgFrameHeight/(spread[0]-1)-GetStgFrameHeight*0.05,0,90+dir*90,DOUBLE,6,1.125,SHOT_GREEN);
			if(GetObjectDistance(shot,objPlayer) <= 64){Obj_Delete(shot);return;}
			loop(12+(spread[0]-t)*2+20)
				{
				TColor(shot);
				yield;
				}
			ObjMove_SetAcceleration(shot,sM/20);
			ObjMove_SetMaxSpeed(shot,sM);
			loop(20)
				{
				TColor(shot);
				yield;
				}
			TShot(shot,distM-dist,v);
			}
		}
	loop(40){yield;}
	let c = 0;
	loop
		{
		loop(countM*2)
			{
			c+=1;
			if(c >= 150 && c%(50-Dif*15) == 0)
				{
				PlaySound("se_Shot7",60);
				TAim;
				}
			yield;
			}
		n+=1;
		let v = rand(-1,1);
		PlaySound("se_Shot1",75);
		ascent(t in 0..spread[0])
			{
			ascent(dir in 0..2)
				{
				dir = dir*2-1;
				let shot = CreateBulletA1(GetStgFrameWidth*(0.5+dir*0.5)+dir*12,(t+0.25+dir*0.25+(n%2)*0.5)*1.1*GetStgFrameHeight/(spread[0]-1)-GetStgFrameHeight*0.05,0,90+dir*90,DOUBLE,6,1.125,SHOT_GREEN);
				TShot(shot,distM,v);
				}
			}
		}
	task TShot(shot,dist,v)
		{
		ObjMove_SetSpeed(shot,sM);
		ObjMove_SetAcceleration(shot,0);
		loop(floor(dist/sM))
			{
			TColor(shot);
			yield;
			if(Obj_IsDeleted(shot)){return;}
			}
		ObjMove_SetAngularVelocity(shot,v*(3.5+rand(-1,1)*1)/15);
		let s = 1.75;
		ObjMove_SetAcceleration(shot,(s-ObjMove_GetSpeed(shot))/135);
		ObjMove_SetMaxSpeed(shot,s);
		loop(15)
			{
			TColor(shot);
			yield;
			if(Obj_IsDeleted(shot)){return;}
			}
		ObjMove_SetAngularVelocity(shot,0);
		while(!Obj_IsDeleted(shot))
			{
			TColor(shot);
			yield;
			}
		}
	task TColor(shot)
		{
		ObjRender_SetColorHSV(shot,rand(90,120),155,255);
		}
	task TAim
		{
		let y = rand(50,GetStgFrameHeight*0.5-70);
		if(GetPlayerY < y){y-=75;}
		let shot = CreateBulletA2(GetPlayerX+GetStgFrameWidth*rand(-0.15,0.15),y,0.75,90+rand(-1,1)*5,(4-0.75)/120,0,4,SMALL,0,0,SHOT_BLUE);
		ObjShot_SetGraphic(shot,ORB_BLUE+500);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
		Obj_SetRenderPriority(shot,0.49);
		let time = 20;
		ObjShot_SetIntersection(shot,false);
		ascent(t in 0..time)
			{
			if(Obj_IsDeleted(shot)){return;}
			t = t/(time-1);
			ObjShot_SetScale(shot,(2-t)*1);
			ObjRender_SetAlpha(shot,t*255);
			ObjMove_SetAngle(shot,ObjMove_GetAngle(shot)+6*sin(GetAngleToPlayer(shot)-ObjMove_GetAngle(shot)));
			yield;
			}
		ObjShot_SetIntersection(shot,true);
		loop(20)
			{
			if(Obj_IsDeleted(shot)){return;}
			ObjMove_SetAngle(shot,ObjMove_GetAngle(shot)+6*sin(GetAngleToPlayer(shot)-ObjMove_GetAngle(shot)));
			yield;
			}
		}
	
		
}