#e
#Title[uGWFEnCDv]
#Text[]
#Image[]
#BackGround[]
#BGM[]
#PlayLevel[Extra]
#ScriptVersion[2]
#Player[FREE]

script_enemy_main
{
let wt=160;
let usebomb=0;

	//GoɈxs镔
	@Initialize
	{
	//   eϐ

	//   XyJ[hf[^
	SetLife(1000);
	SetScore(200000);
	SetDamageRate(30,  0);
	SetTimer(88);
	SetInvincibility(250);
	SetGraphicRect(64,1,127,64);
	LoadGraphic("script\img\ExRumia.png");
	SetText("uGWFEnCDv");
	SetShotAutoDeleteClip(20,20,20,20);
	}
	//   ʒu܂ňړ
	SetMovePosition02(GetCenterX, GetCenterY, 60);

	AtackTask;
	MoveTask;
	ControlTask;
	//G̃Ct0ɂȂ܂ŁA̕1t[1s
	@MainLoop
	{
		if(OnBomb==false){
			SetCollisionA(GetX, GetY, 32);
			SetCollisionB(GetX, GetY, 24);
			usebomb=0;
		}
		else{usebomb++;}
		if(usebomb==1){BombBarrier;}
		yield;

	}
	
	//`惋[vB1t[1s
	@DrawLoop
	{
		SetGraphicRect(0,1,63,64);
		if(GetSpeedX<0){
			SetGraphicRect(128,1,191,64);
		}
		if(GetSpeedX>0){
			SetGraphicRect(192,1,255,64);
		}
		SetColor(255,255,255);
		SetAlpha(255);
		if(OnBomb==true){SetAlpha(96);}
		SetTexture("script\img\ExRumia.png");//摜͔q
		DrawGraphic(GetX(),GetY());//G̍WEx[~A`;
	}
		
	//GłuԂɈxs镔
	@Finalize
	{
		DeleteGraphic("script\img\ExRumia.png");
	}
	//XyJ[h̔wi
	//Ă悢
	@BackGround
	{
	}


	task AtackTask{
		wait(60);
		SetColor(255,255,0);
		Concentration01(60);
		wait(60);
		let nway=24;
		loop{
			let dir=rand(0,360);
			let rotx=rand(0.4,0.8);
			let roty=rand(0,0.7);
			let rotz=rand(0.3,0.6);
			if(rand_int(0,1)==0){rotx=-rotx;}
			if(rand_int(0,1)==0){roty=-roty;}
			if(rand_int(0,1)==0){rotz=-rotz;}
			let rotq=MakeRotate(rand(0,360),rand(0,360),rand(0,360));
			loop(nway){
				let x=cos(dir);
				let y=sin(dir);
				let rotp=Rotate3D_A(x,y,0,rotq);
				RotateShot3D(GetCenterX,GetCenterY,rotp[1],rotp[2],rotp[3],rotx,roty,rotz,0.7,5,YELLOW22,20);
				dir+=360/nway;
			}
			wait(wt);
		}
	}

	task RotateShot3D(	//w_𒆐SɉȂLĂe
		cx,	//SW
		cy,
		x,	//ԍW
		y,
		z,
		rotx,	//ẻ]
		roty,
		rotz,
		expspd,	//WJx
		dis,	//_
		grf,
		delay
	){
		let obj=Obj_Create(OBJ_SHOT);
		ObjShot_SetDelay(obj,delay);
		ObjShot_SetGraphic(obj,grf);
		let expand=0;
		let rotq=MakeRotate(rotx,roty,rotz);
		let pastx=cx;
		let pasty=cy;
		while(!Obj_BeDeleted(obj)){
			let rotp=Rotate3D_A(x,y,z,rotq);
			x=rotp[1];
			y=rotp[2];
			z=rotp[3];
			Obj_SetPosition(obj,Perspective(x,z,dis)*expand+cx,Perspective(y,z,dis)*expand+cy);
			Obj_SetAngle(obj,atan2(Obj_GetY(obj)-pasty,Obj_GetX(obj)-pastx));
			pastx=Obj_GetX(obj);
			pasty=Obj_GetY(obj);
		/*
			if(z<-0.3){	//͓̂蔻OĂ݂
				Obj_SetCollisionToPlayer(obj,false);
				Obj_SetAlpha(obj,96);
			}
			else{
				Obj_SetCollisionToPlayer(obj,true);
				Obj_SetAlpha(obj,255);
			}
		*/
			yield;
			expand+=expspd;
		}
	}

	task BombBarrier{	//{oAGtFNg
		let killtime=GetTimeOfPlayerInvincibility;
		let expand=80;
		let width=6;
		let grf=GREEN01;
		let delay=60;
		let size=80;
		let dis=300;
		let rotq=MakeRotate(0.877,0.488,1.55);
		let va=[0,0,6^0.5/2*size];
		let vb=[3^0.5*2/3*size,0,-6^0.5/6*size];
		let vc=[-3^0.5/3*size,size,-6^0.5/6*size];
		let vd=[-3^0.5/3*size,-size,-6^0.5/6*size];
		OrbitLaser3D(va[0],va[1],va[2],		vb[0],vb[1],vb[2],	rotq,expand,width,killtime,dis,grf,delay);
		OrbitLaser3D(va[0],va[1],va[2],		vc[0],vc[1],vc[2],	rotq,expand,width,killtime,dis,grf,delay);
		OrbitLaser3D(va[0],va[1],va[2],		vd[0],vd[1],vd[2],	rotq,expand,width,killtime,dis,grf,delay);
		OrbitLaser3D(vb[0],vb[1],vb[2],		vc[0],vc[1],vc[2],	rotq,expand,width,killtime,dis,grf,delay);
		OrbitLaser3D(vc[0],vc[1],vc[2],		vd[0],vd[1],vd[2],	rotq,expand,width,killtime,dis,grf,delay);
		OrbitLaser3D(vd[0],vd[1],vd[2],		vb[0],vb[1],vb[2],	rotq,expand,width,killtime,dis,grf,delay);
		OrbitLaser3D(-va[0],-va[1],-va[2],	-vb[0],-vb[1],-vb[2],	rotq,expand,width,killtime,dis,grf,delay);
		OrbitLaser3D(-va[0],-va[1],-va[2],	-vc[0],-vc[1],-vc[2],	rotq,expand,width,killtime,dis,grf,delay);
		OrbitLaser3D(-va[0],-va[1],-va[2],	-vd[0],-vd[1],-vd[2],	rotq,expand,width,killtime,dis,grf,delay);
		OrbitLaser3D(-vb[0],-vb[1],-vb[2],	-vc[0],-vc[1],-vc[2],	rotq,expand,width,killtime,dis,grf,delay);
		OrbitLaser3D(-vc[0],-vc[1],-vc[2],	-vd[0],-vd[1],-vd[2],	rotq,expand,width,killtime,dis,grf,delay);
		OrbitLaser3D(-vd[0],-vd[1],-vd[2],	-vb[0],-vb[1],-vb[2],	rotq,expand,width,killtime,dis,grf,delay);
	}

	task OrbitLaser3D(	//{X̎3D]郌[U[B{oAopB
		sx,		//n[W	WJ̔{1ƂB
		sy,
		sz,
		ex,		//I[W
		ey,
		ez,
		rotq,		//ǂIuWFNgœp^[]Ȃ̂ł܂Ƃ߂Ďl
		expand,		//WJ܂ł̎
		width,		//[U[
		killtime,	//IԁBȂXyI܂ŁB
		dis,		//_
		grf,
		delay
	){
		let obj=Obj_Create(OBJ_LASER);
		Obj_SetAutoDelete(obj,false);
		ObjShot_SetGraphic(obj,grf);
		ObjShot_SetDelay(obj,delay);
		ObjShot_SetBombResist(obj,true);
		ObjLaser_SetWidth(obj,width);
		ObjLaser_SetSource(obj,false);
		let spos=[0,sx,sy,sz];
		let epos=[0,ex,ey,ez];
		if(expand<1){expand=1;}	//[
		let expspd=1/expand;
		expand=0;	//݂̓WJ{Ƃčėp
		while(!Obj_BeDeleted(obj)){
			spos=Rotate3D_B(spos,rotq);
			epos=Rotate3D_B(epos,rotq);
			Obj_SetPosition(obj,Perspective(spos[1],spos[3],dis)*expand+GetX,Perspective(spos[2],spos[3],dis)*expand+GetY);
			Obj_SetAngle(obj,atan2(Perspective(epos[2],epos[3],dis)*expand+GetY-Obj_GetY(obj),Perspective(epos[1],epos[3],dis)*expand+GetX-Obj_GetX(obj)));
			ObjLaser_SetLength(obj,Distance(Obj_GetX(obj),Obj_GetY(obj),Perspective(epos[1],epos[3],dis)*expand+GetX,Perspective(epos[2],epos[3],dis)*expand+GetY));
			Obj_SetAlpha(obj,(spos[3]+epos[3])+172);
			yield;
			if(expand<1){expand+=expspd;}
			if(killtime>0){killtime--;}
			if(killtime==0){Obj_Delete(obj);}
		}
	}

	task ControlTask{
		wait(250);
		while(GetEnemyLife>750&&GetTimer>20){yield;}
		wt=140;
		while(GetEnemyLife>500&&GetTimer>20){yield;}
		wt=120;
		while(GetEnemyLife>250&&GetTimer>20){yield;}
		wt=100;
		while(GetTimer>20){yield;}
		wt=80;
	}

	task MoveTask{
		wait(120);
		while(true){
			SetMovePosition02(rand(GetClipMinX+60,GetCenterX-60), rand(GetClipMinY+60,GetCenterY-40), 80);
			wait(180);
			SetMovePosition02(rand(GetCenterX-60,GetCenterX+60), rand(GetClipMinY+60,GetCenterY-40), 80);
			wait(180);
			SetMovePosition02(rand(GetCenterX+60,GetClipMaxX-60), rand(GetClipMinY+60,GetCenterY-40), 80);
			wait(180);
			SetMovePosition02(rand(GetCenterX-60,GetCenterX+60), rand(GetClipMinY+60,GetCenterY-40), 80);
			wait(180);
		}
	}

	function Distance(xa,ya,xb,yb){
			return(((xa-xb)^2+(ya-yb)^2)^0.5);
	}
	function wait(n){
		loop(n){yield;}
	}

	#include_function".\rotate3d.dnh"
}

