
script_enemy_main
{
	#include_script".\..\..\EnemyBossSystem.txt";
	#include_script".\Nue_AnimeLib.txt";
	
	tex_enemy=filedir~"Nue_Main.png";
	tex_spellbg=filedir~"Nue_SpellBG.png";
	
	bg_rect=[0,0,512,512];
	
	@Initialize
	{
		point=450;
		SetLife(800);
		SetTimer(55);
		EnemyBossInitialize;
		SetAnimetion(tex_enemy);
	}
	@MainLoop{EnemyBossMainLoopSystem(50,invincible);yield;}
	@DrawLoop{EnemyBossDrawLoop;}
	@Finalize{EnemyBossFinalize;}
	
	
	
	task MainThread_Standard{}
	task MainThread_Advanced{}
	
	

	task MainThread_Unlimited
	{
		yield;
		SetMovePositionHermite(cenX,minY+120,0,0,0,0,100);
		SetAction(ACT_MOVE,100);
		SetSpellCard("正体不明「ドラゴントライアングル」",29);
		wait(100);
		loop(2){SetTryAngle;}
		let r=1;
		if(rand_int(0,1)){r=-r;}
		loop
		{
			wait(80);
			let sp=0;
			let p=rand(0,360);
			loop(8)
			{
				let way=24;
				let speed=4-sp;
				let angle=p;
				let gra=135;
				let delay=10;
				while(angle<p+360)
				{
					let v=30;
					let sx=GetX+v*cos(angle);
					let sy=GetY+v*sin(angle);
					CreateShot01(sx,sy,speed,angle,gra,delay);
					angle+=360/way;
				}
				sp+=0.4;
				wait(10);
			}
			wait(160);
			SetMovePositionHermite(cenX+rand(-120,120),minY+rand(80,160),0,0,0,0,80);
			SetAction(ACT_MOVE,80);
		}
		
		
		task SetTryAngle
		{
			let px1=GetX;
			let py1=GetY;
			let px2=GetX;
			let py2=GetY;
			let px3=GetX;
			let py3=GetY;
			Sub1(4,rand(0,360));
			Sub2(2.5,rand(0,360));
			Sub3(1,rand(0,360));
			SetLaser;
			SetShot;
			
			task SetShot
			{
				loop
				{
					if(TryangleDotHit(px3,py3,px1,py1,px2,py2,GetPlayerX,GetPlayerY))
					{
						local
						{
							let sx=[px1,px2,px3];
							let sy=[py1,py2,py3];
							let px=(sx[0]+sx[1]+sx[2])/3;
							let py=(sy[0]+sy[1]+sy[2])/3;
							ascent(a in 0..3)
							{
								let p=atan2(py-sy[a],px-sx[a]);
								
								let speed=2.5;
								let angleA=p;
								let gra=145;
								let delay=10;
								while(angleA<=p+180+1)
								{
									let limit=50;
									SetShotDirectionType(ABSOLUTE);
									CreateShotA(1,sx[a],sy[a],delay);
									SetShotDataA(1,0,speed,angleA,0,-speed/limit,0,gra);
									
									local
									{
										SetShotDirectionType(PLAYER);
										let wide=15;
										let way=3;
										let graB=81;
										let angleB=-wide/2;
										while(angleB<=wide/2+1)
										{
											CreateShotA(2,0,0,delay);
											SetShotDataA(2,0,0,angleB,0,speed/50,speed,graB);
											AddShot(limit,1,2,0);
											angleB+=wide/(way-1);
										}
									}
									SetShotKillTime(1,limit+1);
									FireShot(1);
									SetShotDirectionType(ABSOLUTE);
									angleA+=180;
								}
							}
						}
					}
					wait(3);
				}
			}
			
			task SetLaser
			{
				loop
				{
					let alpha=64;
					let width=50;
					let gra=191;
					let del=2;
					local
					{
						let p=atan2(py1-py3,px1-px3);
						let leng=GetGapLength(px3,py3,px1,py1);
						SetLaserObject(px3,py3,leng,width,p,alpha,gra,100,del);
					}
					local
					{
						let p=atan2(py2-py3,px2-px3);
						let leng=GetGapLength(px3,py3,px2,py2);
						SetLaserObject(px3,py3,leng,width,p,alpha,gra,100,del);
					}
					local
					{
						let p=atan2(py1-py2,px1-px2);
						let leng=GetGapLength(px2,py2,px1,py1);
						SetLaserObject(px2,py2,leng,width,p,alpha,gra,100,del);
					}
					wait(1);
				}
			}
			
			task Sub1(let speed,let angle)
			{
				let ref=0;
				let leng=120;
				loop
				{
					
					if(ref%4)
					{
						if(px1<minX-leng&&(angle>90||angle<-90)){angle=180-angle;ref++;}
						if(px1>maxX+leng&&(angle<90||angle>-90)){angle=180-angle;ref++;}
						if(py1>maxY+leng&&angle>0){angle=-angle;ref++;}
						if(py1<minY-leng&&angle<0){angle=-angle;ref++;}
					}
					
					else
					{
						if(px1<minX-leng||px1>maxX+leng||py1<minY-leng||py1>maxY+leng)
						{
							angle=atan2(GetY-py1,GetX-px1);
							
							ref++;
						}
					}
					if(angle>=180){angle+=-360;}
					if(angle<-180){angle+=360;}
					px1+=speed*cos(angle);
					py1+=speed*sin(angle);
					yield;
				}
			}
			
			task Sub2(let speed,let angle)
			{
				let ref=0;
				let leng=120;
				loop
				{
					
					if(ref%4)
					{
						if(px2<minX-leng&&(angle>90||angle<-90)){angle=180-angle;ref++;}
						if(px2>maxX+leng&&(angle<90||angle>-90)){angle=180-angle;ref++;}
						if(py2>maxY+leng&&angle>0){angle=-angle;ref++;}
						if(py2<minY-leng&&angle<0){angle=-angle;ref++;}
					}
					else
					{
						if(px2<minX-leng||px2>maxX+leng||py2<minY-leng||py2>maxY+leng)
						{
							angle=atan2(GetY-py2,GetX-px2);
							
							ref++;
						}
					}
					if(angle>=180){angle+=-360;}
					if(angle<-180){angle+=360;}
					px2+=speed*cos(angle);
					py2+=speed*sin(angle);
					yield;
				}
			}
			
			task Sub3(let speed,let angle)
			{
				let ref=0;
				let leng=120;
				loop
				{
					if(ref%4)
					{
						if(px3<minX-leng&&(angle>90||angle<-90)){angle=180-angle;ref++;}
						if(px3>maxX+leng&&(angle<90||angle>-90)){angle=180-angle;ref++;}
						if(py3>maxY+leng&&angle>0){angle=-angle;ref++;}
						if(py3<minY-leng&&angle<0){angle=-angle;ref++;}
					}
					else
					{
						if(px3<minX-leng||px3>maxX+leng||py3<minY-leng||py3>maxY+leng)
						{
							angle=atan2(GetY-py3,GetX-px3);
							
							ref++;
						}
					}
					if(angle>=180){angle+=-360;}
					if(angle<-180){angle+=360;}
					px3+=speed*cos(angle);
					py3+=speed*sin(angle);
					yield;
				}
			}
		}
		
		function TryangleDotHit(let x1,let y1,let x2,let y2,let x3,let y3,let xa,let ya)
		{
			//三点直線→false
			if(0==x3*(y1-y2)+y3*(x2-x1)+x1*y2-x2*y1){return false;}
			
			//内外判定
			if((x3*(y1-y2)+y3*(x2-x1)+x1*y2-x2*y1)*(xa*(y1-y2)+ya*(x2-x1)+x1*y2-x2*y1)<0){return false;}
			if((x1*(y2-y3)+y1*(x3-x2)+x2*y3-x3*y2)*(xa*(y2-y3)+ya*(x3-x2)+x2*y3-x3*y2)<0){return false;}
			if((x2*(y3-y1)+y2*(x1-x3)+x3*y1-x1*y3)*(xa*(y3-y1)+ya*(x1-x3)+x3*y1-x1*y3)<0){return false;}
			
			
			return true;
		}
	}
	
}