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

#include "./functions2.txt"

timer = 42;

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

	TEnemyWork;
	ObjEnemy_Render; 
	TLife(240,20,20,0,0,25,25);
	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-145,15,2);
	loop(45){yield;}
	ascent(i in 0..4)
		{	
		TShot((i%2)*2-1,45+i*90,i);
		}	
	PlaySound("se_Option2",100);
	task TShot(dir,angleM,numbM)	
		{
		let spread = [1,3+floor(Dif/2)*2,7];
		let obj = CreateBasicEnemyA1(enmx,enmy,0,0,1,0,type_flame,flame_blue,48);
		let speedM = 6;
		let speed = speedM;
		let dist = 0;
		let counter = 0;
		let angleA = angleM;
		let anglem = angleM;
		let distM = 8;
		let distm = 8;
		let type = 0;
		let angleP = 90;
		while(!Obj_IsDeleted(objEnemy) && !Obj_IsDeleted(obj))
			{
			if(counter <= 30)	
				{
				speed-=speedM/30;
				distM+=speed;
				distm=distM;
				}
			if(GetPlayerY < enmy)
				{
				angleP = GetAngleToPlayer(obj);
				}
			else
				{
				angleP = 90;
				}
			dist+=(distm-dist)/10;
			anglem+=15*sin(angleM-anglem);
			ObjMove_SetPosition(obj,enmx+dist*cos(anglem),enmy+dist*sin(anglem));
			counter+=1;
			angleA+=5;
			if(counter > 90)
				{
				let numb = cos(GetAngleToPlayer(objEnemy));
				if((numb > cos(89) && ObjMove_GetX(objEnemy) < GetPlayerX) || (numb < cos(91) && ObjMove_GetX(objEnemy) > GetPlayerX))
					{
					numb = absolute(numb)/numb;
					if(type!= 0)
						{
						type = 0;
						angleM-=135;
						distm = distM;
						}	
					ObjMove_SetDestAtFrame(objEnemy,enmx+numb*1,enmy,1);
					let count = round(absolute((sin(anglem+180)+1)/2));
					if(counter%(30-count*10-Dif*2) == 0)
						{		
						TSound1;
						ascent(i in 0..spread[0])
							{
							i = i-spread[0]/2+0.5;
							let angle = angleP+20*cos(angleA);
							let speed = 1+count*1;
							let shot = CreateBulletOA2(obj,8,angle,-8/45,0,0,DS_MISSILE_PURPLE,8,1.75);
							ObjMove_AddPatternA2(shot,55,speed,angle-numb*45,0,0,0);
							ObjRender_SetColorHSV(shot,300,(1-count)*50,240+count*15);
							}
						}
					}
				else
					{
					if(type!= 1)
						{
						type = 1;
						angleM-=135;
						distm = 16;
						}	
					numb = absolute(round(cos(angleM)));
					let speedM = 2.75;
					let dir = round(rand(0,1))*2-1;
					if(counter%30 == numb*15)
						{
						TSound2;
						ascent(i2 in 0..spread[2])
							{
							ascent(i1 in 0..spread[1])
								{
								i1 = i1-spread[1]/2+0.5;
								let speed = (i2*0.5+spread[2]*0.5)*speedM/spread[2];
								let shot = CreateBulletOA2(obj,speed,angleP+i1*(180-(Dif%2)*50-Dif*15)/spread[1]+dir*(spread[2]-i2)*2/spread[2],((speedM-speed)/spread[2])/20,0,speedM,DS_MISSILE_YELLOW,8,1.125);
								ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
								}
							dir*=-1;
							}
						}
					}
				}
			yield;
			}
		Obj_Delete(obj);
		}

	task TSound1
		{
		PlaySound("se_Shot3",75);
		PlaySound("se_Shot4",30);
		}

	task TSound2
		{
		loop(3)
			{
			PlaySound("se_Shot7",45);
			loop(2){yield;}
			}
		}
}	