#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 64;
life = 13500/3;

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

	TEnemyWork;
	TTime;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	loop(15){yield;}
	alternative(Dif)
		case(0)
			{
			TSpellName("Divinity \"Four Inch Dragon Appearing Before Roben\"");
			}
		case(1)
			{
			TSpellName("Divinity \"Eight Inch Dragon Appearing Before Roben\"");
			}
		case(2)
			{
			TSpellName("Divinity \"Twelve Inch Dragon Appearing Before Roben\"");
			}
		case(3)
			{
			TSpellName("Divinity \"Double Black Dragons Appearing Before Roben\"");
			}
	SetBossAura(5,1,1,40);
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-180,20,5);
	loop(90){yield;}
	TLife(0,12,12,210/3,210/3,false);

	let spread = [2,[4,4,3,2][Dif]];
	let sAM = [3.75,4,4,3.5][Dif];
	let aAM = [15,15,25,18][Dif];
	let scY = [64,152,210,128][Dif];
	let scX = 96;	
	loop
		{	
		ascent(n in 0..2)
			{
			TDragon(n*GetStgFrameWidth+(n*2-1)*scY*0.5,GetPlayerY,n*180);
			if(Dif == 3)
				{
				TDragon((1-n)*GetStgFrameWidth+((1-n)*2-1)*scY*0.5,GetPlayerY,(1-n)*180);
				loop(60){yield;}
				}
			loop(165){yield;}
			TDragon(GetPlayerX,n*GetStgFrameHeight+(n*2-1)*scY*0.5,90+n*180);
			if(Dif == 3)
				{
				TDragon(GetPlayerX,(1-n)*GetStgFrameHeight+((1-n)*2-1)*scY*0.5,90+(1-n)*180);
				loop(60){yield;}
				}
			loop(165){yield;}
			}
		}

	task TDragon(x,y,a)
		{	
		PlaySound("se_LaserDelay",100);
		let g = SHOT_PURPLE;
		let graph = [240,180,120,60,30,0,285];

		let switch = true;

		let s = scY/35;
		let sM = s;
		let accel = -s/60;
		let yD = 0;
		let xD = 0;

		let gc = -1;

		let switchA = false;

		let c = -1;

		
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/sprite_onbashira.png");
		ObjRender_SetAngleZ(obj,a+90);
		ObjRender_SetScaleXYZ(obj,scX,scY,0);
		ObjSprite2D_SetDestRect(obj,0,0,1,1);	
		Obj_SetRenderPriority(obj,0.49);

		let counter = -1;
		while(!Obj_IsDeleted(obj) && c < 180)
			{
			c+=1;
			ObjSprite2D_SetSourceRect(obj,13+(floor(c/8)%2)*54,4,61+(floor(c/8)%2)*54,126);
			ObjRender_SetPosition(obj,x,y,0);
			s+=accel;
			x+=s*cos(a);
			y+=s*sin(a);	
			if(Obj_GetValueD(GetPlayerObjectID,"DAMAGE",0) == 0 && GetPlayerInvincibilityFrame <= 0 && IsIntersected_Line_Circle(x-scY*0.98*0.5*cos(a),y-scY*0.98*0.5*sin(a),x+scY*0.98*0.5*cos(a),y+scY*0.98*0.5*sin(a),0.5*0.58*scX,GetPlayerX,GetPlayerY,GetPlayerGrazebox))
				{
				if(IsIntersected_Line_Circle(x-scY*0.98*0.5*cos(a),y-scY*0.98*0.5*sin(a),x+scY*0.98*0.5*cos(a),y+scY*0.98*0.5*sin(a),0.5*0.58*scX,GetPlayerX,GetPlayerY,GetPlayerHitbox))
					{
					Obj_SetValue(GetPlayerObjectID,"DAMAGE",1);
					}
				gc+=1;
				if(gc%5 == 0)
					{
					AddChapterGraze(1);
					AddGraze(1);
					PlaySound("se_Graze",100);
					}
				}
			if(c%spread[1] == 0)
				{
				let xM = x+scY*0.98*0.5*cos(a);
				let yM = y+scY*0.98*0.5*sin(a);
				ascent(dir in 0..2)
					{
					dir = dir*2-1;
					ascent(i in 0..spread[0])
						{	
						let aA = c*13+dir*i*360/spread[0];
						let shot = CreateBulletA2(xM+scX*0.5*0.58*sin(a)*cos(aA),yM+scX*0.5*0.58*cos(a)*cos(aA),0.25,a-(sin(a)*2-1)*aAM*cos(aA),sAM/40,0,sAM,PELLET,6,1,SHOT_BLUE);
						if(IsBeyondClip(shot,scX*0.5*0.67))
							{
							Obj_Delete(shot);
							}
						else if(absolute(cos(a)) >= 0.25)
							{
							ObjShot_SetGraphic(shot,PELLET[SHOT_GRAY]);
							ObjMove_AddPatternB2(shot,40,NO_CHANGE,NO_CHANGE,-cos(a)/20,0,-cos(a)*10,0);
							}
						else if(absolute(sin(a)) >= 0.25)
							{
							ObjMove_AddPatternB2(shot,40,NO_CHANGE,NO_CHANGE,0,-sin(a)/20,0,-sin(a)*10);
							}
						}
					}
				}
			if(counter >= 30 && x > -8 && x < GetStgFrameWidth+8 && y > -8 && y < GetStgFrameHeight+8)
				{
				//PlaySound("se_Die4",100);
				}			
			if(switchA == false && counter >= 30)
				{
				accel = 1/15;
				}
			if(Obj_IsDeleted(objEnemy))
				{
				Obj_Delete(obj);
				}
			yield;
			}
		Obj_Delete(obj);
		}
}