#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 68;

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

	TEnemyWork;
	ObjEnemy_Render; 
	TLife(0,20,20,0,0,85,85);
	TEnd(false);
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(5000);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(timer);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}
task TEnemyWork
{
	TXY;
	loop(60){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-120,15,2);
	loop(45){yield;}
		
	let spread = [3+Dif*2,3,1+Dif*2,3+Dif*3];
	let graph = [BL_SMALL_BLUE,BL_SMALL_WHITE];

	let dir = [1,1];
	let counter = -1;
	loop
		{
		loop(2)
			{
			ascent(t in 0..420)
				{
				if(t%210 == 0)
					{
					TFam(dir[1]*dir[0],rand(0.5,1));
					dir[0] = dir[0]*-1;
					}
				if(t > 90 && t%10 == 0)
					{
					PlaySound("se_Shot1",80);
					let t1 = t-90;
					ascent(i1 in 0..spread[1])
						{
						i1 = i1-spread[1]*0.5+0.5;
						ascent(i2 in 0..spread[2])
							{
							i2 = i2-spread[2]*0.5+0.5;
							let angle = i1*120/spread[1]+i2*10+90+dir[1]*(-90+t1*570/(420-90));
							let dist = (spread[2]-absolute(i2))/spread[2];
							let speed = dist*0.5+1.5+t1*0.5/(420-90);
							let shot = CreateBulletA2(enmx+dist*20*cos(angle),enmy+dist*20*sin(angle),speed,angle,-speed*0.5/240,0,speed*0.5,PELLET_GREEN,8,1.125);
							Obj_SetRenderPriority(shot,0.51);
							}
						}
					}
				if(t > 90 && t%(15-Dif*3) == 0)
					{
					let t1 = t-90;
					ascent(i3 in 0..spread[3])
						{
						let angle = 90+dir[1]*(90-t1*570/(420-90));
						let dist = i3/spread[3];
						let speed = dist*(1+Dif*0.5)+1.5+t1*0.5/(420-90);
						CreateBulletA1(enmx+8*cos(angle),enmy+8*sin(angle),speed,angle,SMALL_BLUE-1,8,1);
						}
					}
				yield;
				}	
			loop(30){yield;}
			ObjEnemy_Move(objEnemy,30,4,rand(30,40),rand(105,125),150,2);
			loop(45){yield;}
			dir[1] = dir[1]*-1;
			}
		loop(45){yield;}
		let yS = rand(0.5,1);
		ascent(i in 0..2)
			{
			TFam(i*2-1,yS);
			}
		loop(135){yield;}
		ObjEnemy_Move(objEnemy,30,4,rand(30,40),rand(105,125),150,2);
		loop(180){yield;}
		dir[1] = dir[1]*-1;
		}
	task TFam(dir,yS)
		{
		let x = enmx;
		let y = enmy;
		let xS = 3;
	
		let counter = -1;
		let angle = atan2(yS,-dir*xS)-dir*90;
		loop(210)
			{
			counter+=1;
			x+=dir*xS;
			y+=yS;
			xS-=8.75/210;
			angle+=dir*3;
			if(counter%5 == 0)
				{
				TShot(x,y,angle);
				}
			if(counter%7 == 0)
				{
				TSound;
				}
			yield;
			}
		task TSound
			{
			loop(45){yield;}
			PlaySound("se_Shot8",70);
			}
		task TShot(x,y,angleM)
			{
			let time = 120;
			CreateIceDelay(x,y,angleM,120,time);
			x+=time*0.25*cos(angleM);
			y+=time*0.25*sin(angleM);
			loop(time*0.5){yield;}
			ascent(i in 0..spread[0])
				{
				i = i-spread[0]*0.5+0.5;
				let speed = rand(1.5,2.5);
				let shot = CreateBulletA2(x,y,5,i*135/spread[0]+angleM,-4.5/15,0,0.5,graph[absolute(i)%2],0,1);
				Obj_SetRenderPriority(shot,0.52);
				ObjMove_AddPatternA2(shot,15,NO_CHANGE,NO_CHANGE,-1.5/90,0,-1.5);
				ObjMove_AddPatternA2(shot,90,NO_CHANGE,NO_CHANGE,-1.5/90,0,-speed);
				}
			}
		}

}