#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Base1"]

#include "./functions2.txt"

timer = 60;

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

	TEnemyWork;
	ObjEnemy_Render; 
	TLife(30,20,20,0,0,15,15);
	TEnd;
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(500);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(timer);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}

task TEnemyWork
{
	TXY;
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-90,15,2);
	loop(45){yield;}
	let spread = [3,5+Dif,4+Dif,5];
	let dir = 1;
	let switch = false;
	loop
		{	
		EnemySpell = 2;
		PlaySound("se_Charge",130);
		loop(3)
			{
			TCharge(objEnemy);
			loop(10){yield;}	
			}
		loop(45){yield;}
		let angleP = GetAngleToPlayer(objEnemy);
		if(switch == true)
			{
			ObjEnemy_Move(objEnemy,20,2.5,5,5,enmy-rand(0,2.5),enmy+rand(0,2.5),125,0);
			TSpam(angleP);
			}
		loop(4)
			{
			angleP+=dir*360/5;
			let dist = 72;
			let x = enmx+dist*cos(angleP);
			let y = enmy+dist*sin(angleP);
			ascent(i0 in 0..spread[0])
				{
				let spreadA = spread[1]-i0;
				ascent(i1 in 0..spreadA)
					{
					i1 = i1-spreadA/2+0.5;
					let angle = GetAngleToPlayer(objEnemy)+i1*105/spread[1];
					ascent(dir in 0..2)
						{
						let scale = 0.6;
						let color = 20;
						if(dir == 0)
							{
							scale = 0.325;		
							color = 20;
							}
						let speed = i0*1.5/spread[0]-(dir-1)*0.125;
						let shot = CreateBulletA2(x,y,4-speed,angle,-2/30,0,2-speed,DS_BALL_L_WHITE,8,scale);	
						ObjRender_SetColorHSV(shot,60,color,255);
						}
					}
				}
			PlaySound("se_Shot3",100);
			PlaySound("se_Shot5B",40);
			loop(10){yield;}
			}
		loop(30){yield;}		
		EnemySpell = 0;
		loop(5)
			{
			PlaySound("se_Charge",130);
			TCharge(objEnemy);
			loop(10){yield;}	
			}
		loop(40){yield;}
		ObjEnemy_Move(objEnemy,20,2.5,75,75,75,105,125,0);
		ascent(i in 0..spread[2])
			{
			i = i-spread[2]/2+0.5;
			TTewi(270+dir*i*120/spread[2]);
			PlaySound("se_Shot3",100);
			PlaySound("se_Shot4",40);
			loop(72/spread[2]){yield;}	
			}
		loop(15){yield;}
		dir*=-1;		
		switch = true;
		}
	task TSpam(angleP)	
		{
		ascent(i in 0..spread[3])
			{
			PlaySound("se_Shot2",120);
			let shot = CreateBulletOA1(objEnemy,1+i*1/spread[3],angleP,DS_RICE_L_WHITE,8,1.125);
			ObjRender_SetColorHSV(shot,60,((spread[3]-i-1)*0.67+(spread[3])*0.33)*50/spread[3],255);
			loop(2){yield;}
			}
		}
	task TTewi(angle)
		{
		let scale = 0;
		let speed = rand(1,3);
		let accel = 1/(18-Dif*5);
		let max = 10;
		let shot = CreateBulletB2(enmx,enmy,speed*cos(angle),speed*sin(angle),0,accel,0,max,DS_TEWI,0,0);
		let angleZ = angle*2;
		let angleA = (round(rand(0,1))*2-1)*rand(2,7);
		let switch = 0;
		while(!Obj_IsDeleted(shot))
			{
			angleZ+=angleA;
			if(scale < 1.125)
				{
				scale+=1.125/15;
				}
			else
				{
				scale = 1.125;
				}
			speed = ObjMove_GetSpeed(shot);
			angle = ObjMove_GetAngle(shot);
			if(switch < 1 && ObjMove_GetY(shot) > GetStgFrameHeight)
				{
				angle = -angle;
				ObjMove_SetY(shot,GetStgFrameHeight);
				ObjMove_AddPatternB2(shot,0,speed*cos(angle),speed*sin(angle),0,accel,0,max);
				PlaySound("se_Shot5",90);
				switch += 1;
				}
			if(switch < 1 && (ObjMove_GetX(shot) < scale*16 || ObjMove_GetX(shot) > GetStgFrameWidth-scale*16))
				{
				angle = 180-angle;
				ObjMove_AddPatternB2(shot,0,speed*cos(angle),speed*sin(angle),0,accel,0,max);
				PlaySound("se_Shot5",90);
				}
			ObjRender_SetAngleZ(shot,angleZ-angle);
			ObjShot_SetScale(shot,scale);
			yield;
			}
		}
}	