#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include "./functions.txt"

timer = 64;
life = 12000/3;

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

	TEnemyWork;
	TTime;
	ObjEnemy_Render(objEnemy);
}

task TEnemyWork
{
	loop(15){yield;}
	if(Dif <= 1)
		{
		TSpellName("Zhen \"Strike, Like Lightning Within a Storm");
		}	
	else
		{
		TSpellName("Zhen \"Blossoming at the Sign of Thunder\"");
		}
	SetBossAura(5,1,1,40);
	ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-180,20,5);
	loop(90){yield;}
	TLife(0,12,12,200/3,200/3,false);
	let spread = [1,[3,2,1,1][Dif],1,3,[28,36,40,28][Dif],12,2,2,[0.7,0.7,0.75,0.75][Dif]];
	let mspeed = [1.25,4];
	let ab = 1;
	if(Dif <= 2)
		{
		ab = 0;
		}
	
	let dirX = 1;
	let dirY = 1;	
	loop
		{	
		TFist(dirX,dirY);
		loop(300){yield;}
		dirX*=-1;
		}

	task TFist(dirX,dirY)
		{	
		let rad = 50;
		PlaySound("se_LaserDelay",100);
		let obj = [];
		let g = SHOT_PURPLE;
		let graph = [240,180,120,60,30,0,285];

		let scale = 2;
		let alpha = 0;
		let switch = true;

		let s = 0.33;
		let accel = 0;
		let yD = 0;
		let xD = 0;

		let x = GetStgFrameWidth*(0.5-dirX*0.5)+dirX*75;
		let y = GetStgFrameHeight*(0.5-dirY*0.5)+dirY*50;

		let xP = x;
		let yP = GetStgFrameHeight*(0.5+dirY*0.5)-dirY*50;

		let switchA = false;

		let aP = atan2(yP-y,xP-x);
		let c = -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,(-dirX+1)*0.5*256+4,256,((-dirX+1)*0.5+1)*256+4);
			ObjRender_SetAngleZ(obj[i],aP);
			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],50,30,205);
			}
		ObjRender_SetColorHSV(obj[0],0,0,0);

		let counter = -1;
		while(!Obj_IsDeleted(obj[0]))
			{
			c+=1;
			if(counter < 45)
				{
				counter+=1;
				if(alpha < 1)
					{
					alpha+=1/15;
					}	
				if(scale > 1)
					{
					scale-=1/15;
					}
				}
			s+=accel;
			x+=s*cos(aP);
			y+=s*sin(aP);
			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,y,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 >= 30 && x > -8 && x < GetStgFrameWidth+8 && y > -8 && y < GetStgFrameHeight+8)
				{
				//PlaySound("se_Die4",100);
				if(c%spread[1] == 0)
					{
					ascent(i0 in 0..spread[2])
						{
						ascent(i1 in 0..spread[3])
							{
							let a = 270+dirX*((floor(c/spread[1])*1.13)%7-3)*30/3.5;
							let s = 1.75+(i1+rand(0,1))*2/spread[3];
							let n = trunc(rand(0,9));
							let shot = CreateBulletA1(x+32*cos(a),y+32*sin(a),s,a,[SMALL,SMALL,BL_SMALL,BL_SMALL,DROP,DROP,DROP,DROP,LARGE][n],6,[1,1,1,1,1.125,1.125,1.125,1.125,1][n],SHOT_GRAY);
							alternative(n)
								case(0,1,8)
									{
									ObjRender_SetColorHSV(shot,50,10,trunc(rand(0,2))*50+155);
									}
								case(2)
									{
									ObjRender_SetColorHSV(shot,50,30,205);
									}
								others
									{
									ObjShot_SetGraphic(shot,DROP[SHOT_BLUE]);
									}
							ObjMove_AddPatternB2(shot,0,NO_CHANGE,NO_CHANGE,0,1/90,0,10);
							}
						}
					}
				}			
			if(switchA == false && counter >= 30)
				{
				accel = 1/15;
				}
			if(switchA == false && (x > GetStgFrameWidth+rad*0.1 || x < -rad*0.1  || y > GetStgFrameHeight+rad*0.1  || y < -rad*0.1))
				{
				accel = 0;
				s = 0;
				let a = rand(0,360);
				switchA = true;
				ascent(i in 0..spread[4])	
					{
					ascent(dir in 0..ab+1)
						{
						if(Dif > 2)
							{
							dir = dir*2-1;
							}
						let s = 6;
						let aa = i*360/spread[4]+a;
						let shot = CreateBulletA2(x,y,s,aa,-4.25/20,0,s*0.5-2.5,ORB,0,1,SHOT_YELLOW);
						ObjMove_AddPatternA2(shot,90,NO_CHANGE,ObjMove_GetAngle(shot)+ab*dir*60,1/360,0,s*0.25);
						ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);	
						}
					}	
				ascent(i in 0..spread[5])
					{
					TBolt(x,y,i*360/spread[5]+atan2(GetPlayerY-y,GetPlayerX-x),1,0);
					}	
				ascent(i in 0..length(obj))
					{
					TDel(obj[i],i)
					}
				PlaySound("se_Die5",100);
				TSound;
				}
			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 TSound
		{
		loop(45){yield;}
		loop(5)
			{
			loop(3){yield;}
			PlaySound("se_Laser",80);
			}
		}
	task TBolt(x,y,a,p,dir)
		{
		let d = 110*p;
		let w = 6+18*p;
		a += dir*45;
		
		let n = 0.6;
		TBoltA;
		loop(3){yield;}
		if(n < p)
			{
			ascent(i in 0..2)
				{
				TBolt(x+d*cos(a),y+d*sin(a),a,p*spread[8],i*2-1);
				}
			}
		if(p == 1 && x+d*cos(a) > 0 && x+d*cos(a) < GetStgFrameWidth && y+d*sin(a) > 0 && y+d*sin(a) < GetStgFrameHeight)
			{
			TBolt(x+d*cos(a),y+d*sin(a),a,1,0);
			}
		task TBoltA
			{
			let beam = CreateLaserA2(x,y,d/3,a,d+8,w,9,C_LASER,45,SHOT_YELLOW);
			ObjStLaser_SetSource(beam,false);
			loop(45){yield;}
			if(Obj_IsDeleted(beam)){return;}
			let t = 0;
			let shot = CreateBulletA1(x-t*((d/6)/6)*cos(a),y-t*((d/6)/6)*sin(a),d/6,a,SCALE,0,1,SHOT_YELLOW);
			ObjShot_SetGraphic(shot,SCALE[SHOT_YELLOW]-1);
			Obj_DelayDelete(shot,6);
			}
		}
}