#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 38.5;
life = 13500;

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

	TEnemyWork;
	ResetSTime;
	TTime;
	ObjEnemy_Render(objEnemy);
}
task TEnemyWork
{
	SetBossAura(3,1,1,40);
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-75,20,5);
	loop(45){yield;}
	TLife(0,12,12,80,80,false);
	let mspeed = [0,[1.5,1.5,1.5,1.5][Dif],[1.5,1.5,1.875,2.75][Dif]];
	let mtime = 135;
	let mturn = 2;
	let smvary = [[0.25,0.25,0.33,0.25][Dif],[0.4,0.4,0.75,0.75][Dif],[13,13,13,5][Dif]];
	
	let spread = [[2,3,3,3][Dif],5,[300,270,300,270][Dif],[32,32,48,32][Dif],[2,4,4,7][Dif],[2,2,1,1][Dif]];
	let turn = [2,5.1];

	let dir = -1;
	
	TLightning;
	loop
		{	
		TStormA(dir);
		loop(315){yield;}
		dir*=-1;
		//spread[2] = spread[2]+20;
		}
	task TStormA(dir)
		{
		let a = rand(-1,1)*15;
		let t = spread[1];
		let spreadA = floor(spread[2]/spread[5]);
		loop(spreadA-1)
			{
			t+=1;
			ascent(i in 0..spread[0])
				{
				TStorm(enmx-30,enmy-35,a+i*360/spread[0]+rand(-1,1)*1,mspeed[1]-((t+rand(0,0)*0.1)%spread[1])*smvary[0]/spread[1],mspeed[2]-((spread[1]-t+rand(0,0)*0.1)%spread[1])*smvary[1]/spread[1],dir);
				}
			a-=dir*turn[0];
			if(t%spread[1] == spread[1]-1)
				{
				a-=dir*turn[1];
				}
			loop(spread[5]){yield;}
			}

		}
	task TStorm(x,y,a,sm,sM,dir)
		{
		let shot = CreateBulletA1(x,y,0,0,SCALE,6,1.25,SHOT_BLUE);
		let s = 2;
		let aa = a;
		ascent(t in 0.. mtime)
			{
			let sa = mspeed[0]+t*(mspeed[1]-mspeed[0])/mtime;
			let d = t*sa*(sm/5+0.25);
			ObjMove_SetDestAtFrame(shot,x+d*cos(a),y+d*sin(a),1);	
			aa = a;
			a+=dir*mturn;
			s = ObjMove_GetSpeed(shot);
			if(Obj_IsDeleted(shot)){return;}
			yield;
			}
		ObjMove_SetAngle(shot,ObjMove_GetAngle(shot));
		ObjMove_SetSpeed(shot,s);
		ObjMove_SetAngularVelocity(shot,(a-aa)/smvary[2]);
		ObjMove_SetAcceleration(shot,(sM-s)/60);
		ObjMove_SetMaxSpeed(shot,sM);
		}

	task TLightning
		{
		let dir = -1;
		loop(60){yield;}
		loop
			{
			loop(225){yield;}
			let x = enmx+46;
			let y = enmy-60;
			let a = 0;
			let aP = atan2(y-GetPlayerY,x-GetPlayerX);
			ascent(t in 0..spread[3])
				{
				TLightningFrag(x,y,a,aP,-dir);
				PlaySound("se_Shot1",100);
				loop(2){yield;}
				a+=dir*(360/32+120);
				}
			dir *= -1;
			}
		task TLightningFrag(x,y,aA,aP,dir)
			{
			let a = (aA+3600)%360;
			ascent(i in 0..spread[4])
				{
				let aM = a*absolute(a*0.05)+7.5;
				let xA = 80*cos(aM);
				let yA = 40*sin(aM);
				local
					{
					let d = GetDistance(0,0,xA,yA);
					let a = atan2(xA,yA);
					xA = x+d*cos(a+aP);
					yA = y+d*sin(a+aP);
					}
				let shot = CreateBulletA2(xA,yA,6,180-a+90+aP,(1.5-6)/60,0,1.5,SCALE,6,1.25,SHOT_YELLOW);
				ObjShot_SetGraphic(shot,SCALE[SHOT_YELLOW]-1);
				a+=dir*1.9;
				a = (a+360)%180;
				loop(3){yield;}
				}
			}
		}
}            