#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 66;

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

	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	bspell = true;

	TEnemyWork;
	ObjEnemy_Render; 
	TLife(420,30,20,0,0,20,20);
	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
{
	TXY;
	TSpellName;
	TSpellFace(pathSanae);
	loop(60){yield;}
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-150,20,5);
	loop(120){yield;}
	let dir = 180;	
	let spread = 10;
	let time = 270;
	loop
		{
		TFire(dir);
		loop(time){yield;}
		dir+=180;
		time=390;
		TWall(dir+180);
		TSpam;
		}
	task TSpam
		{
		let spread = 8+Dif*4;
		loop(time){yield;}
		ascent(t in 0..time)
			{
			if(t%135 == 0)
				{
				ascent(i in 0..spread)
					{
					let shot = CreateBulletA1(enmx,enmy,1,90+i*360/spread+180/spread,STAR_RED,8,1);
					ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
					ObjRender_SetColorHSV(shot,0,25,255);
					}
				}
			yield;
			}
		}
	task TFire(dir)
		{
		let x = GetStgFrameWidth*(0.5+cos(dir)*0.5);
		let y = GetStgFrameHeight*(0.5+sin(dir)*0.5);
		let xM = GetStgFrameWidth*absolute(sin(dir))*0.525;
		let yM = GetStgFrameHeight*absolute(cos(dir))*0.525;
		let dist = 0;
		let counter = -1;
		let dirA = floor(rand(0,2))*2-1;
		while(dist < max(xM,yM))
			{
			counter+=1;
			dist+=max(xM,yM)/60;
			if(counter%(3-Dif) == 0)
				{
				ascent(i in 0..2)
					{
					i = i*2-1;
					let speed = [rand(2,4.5),-rand(0.25,0.25)];
					let shot = CreateBulletA2(x+i*dist*cos(dir+90),y+i*dist*sin(dir+90),speed[0],dir+180+dirA*rand(15,25),(speed[1]-speed[0])/45,0,speed[1],FIRE_ORANGE,8,1);
					ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
					ObjMove_AddPatternA4(shot,105,0,0,1/(25-Dif*10),0,6,GetPlayerObjectID,NO_CHANGE);
					ObjShot_SetSpellResist(shot,true);
					}
				dirA*=-1;
				TSound;
				}
			yield;
			}
		task TSound
			{
			PlaySound("se_Shot1",90);
			loop(105){yield;}
			PlaySound("se_Shot7",60);
			}
		}
	task TWall(dir)
		{
		let x = GetStgFrameWidth*(0.5+cos(dir)*0.4875);
		let y = GetStgFrameHeight*(0.5+sin(dir)*0.4875);
		let xM = GetStgFrameWidth*absolute(sin(dir))*0.525;
		let yM = GetStgFrameHeight*absolute(cos(dir))*0.525;
		let dist = 0;
		let count = 20-Dif*5;
		let counter = floor(rand(0,count))-1;
		let dirA = floor(rand(0,2))*2-1;
		let iA = floor(rand(0,1));
		while(dist < max(xM,yM))
			{
			counter+=1;
			dist+=max(xM,yM)/90;
			if(counter%count == 0)
				{
				ascent(i0 in 0..2)
					{
					i0 = i0*2-1;
					let angle = dir+180+i0*dirA*(5+5+Dif*5+rand(0,2.5));
					ascent(i1 in 0..spread)
						{
						TShot(i0,angle,i1,dirA);
						}
					}
				PlaySound("se_Shot2",70);
				dirA*=-1;
				}
			yield;
			}
		loop(time-270){yield;}
		task TShot(i0,angle,i1,dirA)
			{
			let speed = [(i1+iA)*(3+Dif)/(spread-1),(i1+iA)*(1.5+Dif*0.5)/spread];
			let shot = CreateBulletA2(x+i0*dist*cos(dir+90),y+i0*dist*sin(dir+90),speed[0],angle,(speed[1]-speed[0])/45,0,speed[1],S_STAR_BLUE,8,1.25);
			ObjMove_AddPatternA2(shot,180,NO_CHANGE,NO_CHANGE,(4-speed[1])/120,0,4);
			while(!Obj_IsDeleted(shot) && ObjMove_GetX(shot) <= GetStgFrameWidth && ObjMove_GetX(shot) >= 0){yield;}
			if(Obj_IsDeleted(shot)){return;}	
			let speedA = ObjMove_GetSpeed(shot);
			angle = 180-ObjMove_GetAngle(shot);
			ObjMove_SetAngle(shot,angle);
			ObjShot_SetGraphic(shot,S_STAR_BLUE-1);
			ObjShot_SetDelay(shot,8);
			PlaySound("se_Shot7",60);
			ObjMove_AddPatternB2(shot,0,NO_CHANGE,NO_CHANGE,-speed*cos(angle)/35,0,-speed*cos(angle),0);
			}
		}
}	