#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 60;
life = 12000;

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

	TEnemyWork;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-120,20,5);
	loop(90){yield;}
	TLife(180,20,32,80,80,false);
	CreateScreenShake(150,30,false);
	loop(155){yield;}
	TArm;
	PlaySound("se_Die3",100);
	loop(90){yield;}
	let spread = [7,4];
	TFistA;
	loop(180){yield;}
	loop
		{
		ascent(dir in 0..2)
			{
			dir = dir*2-1;
			ascent(t in 0..7)
				{
				TShot(270+dir*((t-7*0.5+rand(0,1))*180/7),90-t*5,0);
				loop(5){yield;}
				}
			loop(180){yield;}
			}
		ascent(t in 0..5)
			{
			TShot(90-((t-5*0.5+rand(0,1))*180/5),90-t*6,0);
			loop(6){yield;}
			}
		loop(180){yield;}
		descent(dir in 0..2)
			{
			dir = dir*2-1;
			ascent(t in 0..7)
				{
				TShot(270+dir*((t-7*0.5+rand(0,1))*180/7),90-t*5,0);
				loop(5){yield;}
				}
			loop(180){yield;}
			}
		ascent(t in 0..5)
			{
			TShot(90-((t-5*0.5+0.5)*90/5),70,1);
			}
		loop(210){yield;}
		}
	task TFistA
		{
		loop
			{
			TFist(1,1);
			loop(180){yield;}
			}
		}
	task TShot(a,timeA,type)
		{
		let time = [15,timeA];
		let s = rand(6,8)+2*absolute(cos(a))+(sin(a)+1);
		let shot = CreateBulletA2(enmx,enmy,s,a,-s/time[1],0,0,ORB,0,1.125,SHOT_BLUE);
		ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
		ObjShot_SetSpellResist(shot,true);
		ObjShot_SetIntersection(shot,false);
		ObjRender_SetColorHSV(shot,240,60,255);
		TBounce;
		ascent(t in 0..time[1])
			{
			if(t <= time[0])
				{
				ObjRender_SetScaleXYZ(shot,(1-t/time[0])+1.125,(1-t/time[0])+1.125,0);
				}	
			ObjRender_SetAlpha(shot,min(t*125/time[0],125)*(1+rand(-1,1)*0.25));
			let sc = 1+rand(-1,1)*0.01;
			ObjRender_SetScaleXYZ(shot,ObjRender_GetScaleX(shot)*sc,ObjRender_GetScaleY(shot)*sc,0);
			yield;
			}
		let aP = ObjMove_GetAngle(shot);
		if(type == 1)
			{
			aP = GetAngleToPlayer(shot);
			}
		descent(t in 0..3)
			{
			ascent(i in 0..3)
				{
				ascent(dir in 0..2)
					{
					dir = dir*2-1;
					CreateLaserA2(ObjMove_GetX(shot)+dir*(5+i*8)*cos(aP+90),ObjMove_GetY(shot)+dir*(5+i*8)*sin(aP+90),15,aP,GetStgFrameHeight*1.5,12,12,ST_LASER,45,SHOT_BLUE);
					CreateLaserA2(ObjMove_GetX(shot)+dir*(5+i*8)*cos(aP+90),ObjMove_GetY(shot)+dir*(5+i*8)*sin(aP+90),15,aP+180,GetStgFrameHeight*1.5,12,12,ST_LASER,45,SHOT_BLUE);
					}
				loop(4){yield;if(Obj_IsDeleted(shot)){return;}}
				}
			}
		PlaySound("se_Laser",60);
		loop(10){yield;}
		ascent(t in 0..time[0])
			{
			ObjRender_SetScaleXYZ(shot,t*1/time[0]+1,t*1/time[0]+1,0);
			ObjRender_SetAlpha(shot,(1-t/time[0])*255);
			yield;
			}
		Obj_Delete(shot);
		task TBounce
			{
			while(!Obj_IsDeleted(shot))
				{
				if(ObjMove_GetX(shot) < 0)
					{
					ObjMove_SetAngle(shot,180-ObjMove_GetAngle(shot));
					ObjMove_SetX(shot,0);
					}
				if(ObjMove_GetX(shot) > GetStgFrameWidth)
					{
					ObjMove_SetAngle(shot,180-ObjMove_GetAngle(shot));
					ObjMove_SetX(shot,GetStgFrameWidth);
					}
				if(ObjMove_GetY(shot) < 0)
					{
					ObjMove_SetAngle(shot,-ObjMove_GetAngle(shot));
					ObjMove_SetY(shot,0);
					}
				if(ObjMove_GetY(shot) > GetStgFrameHeight)
					{
					ObjMove_SetAngle(shot,-ObjMove_GetAngle(shot));
					ObjMove_SetX(shot,GetStgFrameHeight);
					}
				yield;
				}
			}
		}
	task TFist(dir,dirY)
		{	
		let rad = 50;
		PlaySound("se_LaserDelay",100);
		let obj = [];
		let g = SHOT_ORANGE;
		let graph = [240,180,120,60,30,0,285];
		if(GetPlayerX >= GetStgFrameWidth*0.5)	
			{
			dir = -1;	
			}
		else
			{
			dir = 1;
			}
		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);
			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 s = 0;
		let accel = 0;
		let yD = 0;
		let xD = 0;

		let y = -rad;
		let x = GetPlayerX;
		let switchA = false;

		let counter = -1;
		while(!Obj_IsDeleted(obj[0]))
			{
			if(counter < 45)
				{
				counter+=1;
				if(alpha < 1)
					{
					alpha+=1/15;
					}	
				if(scale > 1)
					{
					scale-=1/15;
					}
				}
			s+=accel;
			y+=s;
			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(counter == 0)
				{
				accel = 1/10;
				}
			if(switchA == false && y >= GetStgFrameHeight-rad*0.25+6)
				{
				accel = 0;
				s = 0;
				switchA = true;
				PlaySound("se_Die4",100);
				ascent(i0 in 0..spread[0])
					{
					ascent(i1 in 0..spread[1])
						{
						let a = 180+(i0+rand(0,1))*180/spread[0];
						let s = 1+(i1+rand(0,1))*2/spread[1];
						let n = trunc(rand(0,9));
						CreateBulletA1(x+32*cos(a),GetStgFrameHeight+32*sin(a),s,a,[SMALL,SMALL,BL_SMALL,BL_SMALL,DOT,DOT,DOT,DOT,LARGE][n],6,[1,1,1,1,1.125,1.125,1.125,1.125,1][n],SHOT_GRAY);
						}
					}
				ascent(i in 0..length(obj))
					{
					TDel(obj[i],i);
					}

				}
			if(Obj_IsDeleted(objEnemy))
				{
				ascent(i in 0..length(obj))
					{
					Obj_Delete(obj[i]);
					}
				}
			yield;
			}
		Obj_Delete(obj[1]);
		task TDel(obj,n)
			{
			loop(40){yield;}
			let time = 20;
			let radM = rad;
			descent(t in 0..time)
				{
				ObjRender_SetScaleXYZ(obj,t*1/(time-1),t*1/(time-1),0);
				ObjRender_SetAlpha(obj,t*255/(time-1));
				if(n == 0)
					{
					rad = t*radM/time;
					}
				yield;
				}
			Obj_Delete(obj);
			}
		}

	task TArm
		{
		NotifyEventAll(667,true);	
		}
	
}            