#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 181;

@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	ObjEnemy_SetDamageRate(objEnemy,0,0);

	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	bspell = true;

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

@MainLoop
{
	yield;
}

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

task TEnemyWork
{
	let timeA = 1;
	TXY;
	TSpellName;
	TSpellFace(pathSanae);
	loop(60){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-100,20,5);
	loop(120){yield;}
	let spread = [[3,12],[3,5],[3,10],[[1],[2],[3]][Dif]];
	let graph = [KUNAI_PURPLE,KUNAI_BLUE,KUNAI_RED];
	let time = 90;
	let angleB = 0.125;
	let angleA = (2*pi)*angleB;
	loop
		{
		descent(dir0 in 0..2)
			{
			dir0 = dir0*2-1;
			let angleM = 90+dir0*rand(15,45);
			let dir = dir0;
			descent(t in 1..9)
				{
				TSound;
				TShot(enmx,enmy,angleM,0.1+t*0.6/8,0,angleA/spread[1][0],spread[0],0);
				loop(8)
					{
					angleM+=dir0*angleA*4;
					yield;
					}
				dir*=-1;
				}
			loop(240*timeA){yield;}
			ascent(dir1 in 0..2)
				{
				angleM = rand(0,360);
				dir1 = dir0*(dir1*2-1);
				ascent(i in 0..8)
					{
					TSound;
					TShot(GetStgFrameWidth*(0.5+dir1*0.3),GetStgFrameHeight*(0.15+i*0.75/7),angleM,0.25,dir1,0,spread[1],1);
					loop(5){yield;}
					angleM+=dir1*270/8;
					}
				loop(210*timeA){yield;}
				}
			loop(30){yield;}	
			ascent(i in 0..6)
				{
				let dir1 = dir0*((i%2)*2-1);
				TShot(GetStgFrameWidth*(0.5+dir1*0.325),GetStgFrameHeight*(0.15+i*0.75/5),rand(0,360),0.25,dir1,0,spread[2],2);	
				TSound;
				loop(45*(1-((1-timeA)^2))){yield;}
				}
			loop(240*timeA){yield;}
			if(timeA > 0.4)
				{
				timeA-=0.6/2;
				}
			}
		}
	task TShot(xM,yM,angleM,dist,dir,angleA,spread,graphA)
		{
		ascent(i0 in 0..spread[0])
			{
			let angle1 = i0*360/spread[0]+angleM;
			let distA = GetStgFrameWidth*(1-1/spread[0]);
			let x1 = distA*cos(angle1);
			let y1 = distA*sin(angle1);
			let x2 = distA*cos(angle1+2*360/spread[0]);
			let y2 = distA*sin(angle1+2*360/spread[0]);
			distA = GetDistance(x1,y1,x2,y2);
			angle1 = atan2(y2-y1,x2-x1);
			descent(i1 in 0..spread[1])
				{
				let x = x1+distA*(i1/spread[1])*cos(angle1);
				let y = y1+distA*(i1/spread[1])*sin(angle1);
				TSpam(xM,yM,x,y,dist,dir,angleA,graphA);
				}
			}
		}
	task TSpam(x1,y1,x,y,distA,dir,angleA,graphA)
		{	
		let dist = GetDistance(0,0,x,y);
		let speedA = dist*distA/60;
		let speed = dist/time;
		let angle = atan2(y,x);
		let shot = [];
		ascent(i in 0..spread[3][0])
			{
			shot = shot ~ [CreateBulletA2(x1,y1,speedA*1.5,angle+(i-spread[3][0]/2+0.5)*1.75,-speedA/60,0,-speedA*0.1,graph[graphA],8,1.3)];
			ObjRender_SetBlendType(shot[i],BLEND_ADD_ARGB);
			ObjMove_AddPatternA2(shot[i],120,NO_CHANGE,NO_CHANGE,speed/time,dir*angleA,speed*10);
			ObjShot_SetSpellResist(shot[i],true);
			}
		let spam = CreateBulletA2(x1,y1,speedA*1.5,angle,-speedA/60,0,0,LARGE_ORANGE,8,1.125);
		ObjRender_SetBlendType(spam,BLEND_ADD_ARGB);
		ObjShot_SetSpellResist(spam,true);
		loop(75){yield;}
		ObjShot_SetDelay(spam,17);
		ascent(i in 0..length(shot))	
			{
			ObjShot_SetSpellResist(shot[i],false);
			}
		loop(16){yield;}
		Obj_Delete(spam);
		loop(120){yield;}
		ascent(i in 0..length(shot))
			{
			ObjRender_SetBlendType(shot[i],BLEND_ALPHA);
			ObjMove_SetAcceleration(shot[i],speed*2/time);
			ObjMove_SetMaxSpeed(shot[i],speed*10);
			}
		}
	task TSound
		{
		PlaySound("se_Shot1",90);
		loop(75){yield;}
		PlaySound("se_Shot7",60);
		}
}	