#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 330;
life = 12000/3;

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

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	TSpellName("\"Super MAZINGER SPHE\"");
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,GetStgFrameHeight/2-165,15,5);
	loop(90){yield;}
	TLife(150,20,32,250/3,250/3,false);	

	let spread = [8+Dif*6,3,22+Dif*12,4+Dif*2];
	let dirM = 1;
	let yM = 196;
	let switch = false;
	loop
		{
		ascent(t in 1..4)
			{
			ascent(i in 0..2)
				{
				let dir = i*2-1;
				TFist(dir,dirM*dir,48,0);
				}
			loop(15){yield;}
			ObjEnemy_Move(objEnemy,20,2.25,rand(30,40),rand(165,180),150,2);
			loop(15){yield;}
			let angle = GetAngleToPlayer(objEnemy);
			loop(t)
				{
				if(switch == true)		
					{
					PlaySound("se_Shot7",40);
					ascent(i in 0..spread[3])
						{
						let shot = CreateBulletA2(enmx,enmy,1.25,i*360/spread[3]+angle,1/180,0,10,ORB,0,1.25,SHOT_BLUE+Dif*2+trunc(rand(0,3)));
						ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
						}
					angle+=180/spread[3];
					}
				loop(90/t){yield;}
				}
			loop(15){yield;}
			dirM*=-1;
			}
		loop(60){yield;}
		ascent(i in 0..2)
			{
			let dir = i*2-1;
			TFist(dir,0,78,1);
			}
		loop(240){yield;}
		switch = true;
		}
			
	task TFist(dir,dirY,rad,type)
		{
		PlaySound("se_LaserDelay",100);
		let obj = [];
		let g = SHOT_BLUE+Dif*2+trunc(rand(0,3));
		let graph = [240,180,120,60,30,0,285];
		ascent(i in 0..2)
			{
			obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
			ObjPrim_SetTexture(obj[i],GetCurrentScriptDirectory ~ "img/Sprite_Fist.png");
			ObjSprite2D_SetSourceRect(obj[i],0,(dir+1)*0.5*256+4,256,((dir+1)*0.5+1)*256+4);
			ObjRender_SetAngleZ(obj[i],90+dir*90);
			ObjSprite2D_SetDestRect(obj[i],0,0,1,1);
			Obj_SetRenderPriority(obj[i],0.55-dirY*0.01);
			}
		if(g < SHOT_GRAY)
			{
			ObjRender_SetColorHSV(obj[1],graph[g],125,255);
			}
		ObjRender_SetColorHSV(obj[0],0,0,0);
		let scale = 2;
		let alpha = 0;
		let switch = true;

		let xP = GetPlayerX;
		let yP = GetPlayerY;

		let accel = 0;
		let yD = 0;
		let xD = 0;

		let y = GetPlayerY+dirY*yM;
		let x = GetStgFrameWidth*(0.5+dir*0.5)+dir*64;

		let counter = -1;
		while(!Obj_IsDeleted(obj[0]))
			{
			if(type == 0)
				{
				x += (xP-x)/10;
				}
			if(counter < 45)
				{
				counter+=1;
				if(alpha < 1)
					{
					alpha+=1/15;
					}	
				if(scale > 1)
					{
					scale-=1/15;
					}
				}
			else if(type == 0)
				{
				if(yD < yM-ceil(rad*1.03))
					{
					yD+=accel;
					accel+=1/2;
					}
				else if(counter < 75)
					{
					if(counter == 45)
						{
						TPattern(xP,90+dirY*90,10);
						}
					if(dirY == 1 && counter == 74)
						{
						PlaySound("se_Shot3",50);
						let angle = rand(0,360);
						ascent(i in 0..spread[2])
							{
							CreateBulletA2(xP,yP,0,i*360/spread[2]+angle,1/90,0,10,SMALL,16,1.125,SHOT_BLUE+Dif*2+trunc(rand(0,3)));
							}
						}
					counter+=1;
					}
				else if(alpha > 0)
					{
					switch = false;
					alpha-=1/15;
					scale+=1/15;
					}
				else
					{
					ascent(i in 0..length(obj))
						{
						Obj_Delete(obj[i]);
						}
					break;
					}
				}
			else if(type == 1)
				{
				if(xD < GetStgFrameWidth*0.5+64-ceil(rad*1.03))
					{
					xD+=accel;
					accel+=1/2;
					}
				else if(counter < 75)
					{
					if(counter == 45)
						{
						TPattern(GetStgFrameWidth*0.5,dir*90,90);
						}
					counter+=1;
					}
				else if(alpha > 0)
					{
					switch = false;
					alpha-=1/15;
					scale+=1/15;
					}
				else
					{
					ascent(i in 0..length(obj))
						{
						Obj_Delete(obj[i]);
						}
					break;
					}
				}
			ascent(i in 0..length(obj))
				{
				ObjRender_SetAlpha(obj[i],alpha*(255-i*60));
				ObjRender_SetScaleXYZ(obj[i],scale*rad*(3.667),scale*rad*(3.667),0);
				ObjRender_SetPosition(obj[i],x-dir*xD,y-dirY*yD,0);
				}
			if(switch == true && GetDistance(GetPlayerX,GetPlayerY,ObjRender_GetX(obj[0]),ObjRender_GetY(obj[0])) <= rad)
				{
				let spam = CreateBulletA1(GetPlayerX,GetPlayerY,0,0,BUBBLE,0,1.5,g);
				Obj_SetVisible(spam,false);
				Obj_DelayDelete(spam,2);
				}
			if(Obj_IsDeleted(objEnemy))
				{
				ascent(i in 0..length(obj))
					{
					Obj_Delete(obj[i]);
					}
				}
			yield;
			}
		Obj_Delete(obj[1]);
	
		task TPattern(x,angle,angleA)
			{	
			PlaySound("se_Die5",80);
			PlaySound("se_Shot1",100);
			ascent(i0 in 0..spread[0])
				{
				let num = trunc(rand(0,2));
				ascent(i1 in 0..spread[1])
					{
					let scale = [1.125,0.67][(i1+num)%2];
					i1 = i1-spread[1]*0.5+rand(0,1);
					let shot = CreateBulletA1(x,yP,(0.05+(i0+rand(0,1))*0.95/spread[0])*(4+Dif),angle+i1*angleA/spread[1],SMALL,0,scale,SHOT_BLUE+Dif*2+trunc(rand(0,3)));
					Obj_SetRenderPriority(shot,0.49);
					}
				}
			}
		}	

}