//Flies by, shoots, and turns around.

script_enemy_main{

let GRenemy=("\script\Images\Enemies\Ghosts.png");
let SEshotm2=("script\SoundEffects\shotm2.wav");
let SEdeath=("script\SoundEffects\enemydeath1.wav");
let miny=GetClipMinY; let maxy=GetClipMaxY; let minx=GetClipMinX; let maxx=GetClipMaxX;
let cx=GetCenterX; let cy=GetCenterY;
let startx=GetX; let starty=GetY;
let frame=0; let time=0; let animation=0;
let ani=4;
let scale=1;
let shots=0;
let shotcolor=0; let shotspeed=0;
let oldX=0; let oldY=0;
let invincible=125;

let type=GetArgument[0];
let direction=GetArgument[1];

SetSpeed(3);
if(type==1){ SetAngle(90); }
if(type==2 && startx<cx){ SetAngle(0); }
if(type==2 && startx>cx){ SetAngle(180); }

@Initialize{
	LoadGraphic("\script\Images\Enemies\Ghosts.png");
	LoadSE("script\SoundEffects\shotm2.wav");

	if(type==1){ SetInvincibility(20); }
	if(type==2){ SetInvincibility(20); }
	SetLife(6);
	SetDamageRate(10,3); 
	SetScore(1000);
	SetShotColor(255,255,255);
}

@MainLoop{

if(GetX<=minx-32 || GetX>=maxx+32 || GetY<=miny-32 || GetY>=maxy+32 && frame>=60){ VanishEnemy; }

if((GetX>minx && GetX<maxx) && (GetY>miny && GetY<maxy)){
SetCollisionA(GetX,GetY,32*scale);
SetCollisionB(GetX,GetY,32*scale);


if(GetCommonData("Difficulty")==1){
	if(time==45 && type==1){
	let angle=time*5;
		loop(15){
		CreateShot02(GetX,GetY,1,angle,0.02,3,44,0);
		angle+=360/15;
		}
	PlaySE(SEshotm2);
	}
	if(time==75 && type==2){
	let angle=(time*5)+rand(-10,10);
		loop(15){
		CreateShot02(GetX,GetY,1,angle,0.02,2,46,0);
		angle+=360/15;
		}
	PlaySE(SEshotm2);
	}
} //Easy

//=========================================================================================================

if(GetCommonData("Difficulty")==2){
	if(time==45 && type==1){
	let angle=time*5;
		loop(22){
		CreateShot02(GetX,GetY,1,angle,0.025,4,44,0);
		angle+=360/22;
		}
	PlaySE(SEshotm2);
	}
	if(time==75 && type==2){
	let angle=(time*5)+rand(-10,10);
		loop(22){
		CreateShot02(GetX,GetY,1,angle,0.025,3,46,0);
		angle+=360/22;
		}
	PlaySE(SEshotm2);
	}
} // Normal

//=========================================================================================================

if(GetCommonData("Difficulty")==3){
	if(time==45 && type==1){
	let angle=time*5;
		loop(33){
		CreateShot02(GetX,GetY,1,angle,0.03,4,44,0);
		angle+=360/33;
		}
	PlaySE(SEshotm2);
	}
	if(time==75 && type==2){
	let angle=(time*5)+rand(-10,10);
		loop(33){
		CreateShot02(GetX,GetY,1,angle,0.03,4,46,0);
		angle+=360/33;
		}
	PlaySE(SEshotm2);
	}
} // Hard

//=========================================================================================================

if(GetCommonData("Difficulty")==4){
	if(time==45 && type==1){
	let angle=time*5;
		loop(44){
		CreateShot02(GetX,GetY,1,angle,0.03,5,44,0);
		angle+=360/44;
		}
	PlaySE(SEshotm2);
	}
	if(time==75 && type==2){
	let angle=(time*5)+rand(-10,10);
		loop(44){
		CreateShot02(GetX,GetY,1,angle,0.03,5,46,0);
		angle+=360/44;
		}
	PlaySE(SEshotm2);
	}
} // Lunatic
} // OnScreen

if(type==1){
	if(direction==1 && time>=30 && time<60){ SetAngle(GetAngle+3); }
	if(direction==2 && time>=30 && time<60){ SetAngle(GetAngle-3); }
}
if(type==2){
	if(startx<cx && time>=45 && time<105){ SetAngle(GetAngle-3); }
	if(startx>cx && time>=45 && time<105){ SetAngle(GetAngle+3); }
}

oldX=GetX;
oldY=GetY;
frame++;
time++;
animation++;
if(animation>=ani*6){ animation=0; }
if(GetTimeOfInvincibility<25 && invincible>0){ invincible=GetTimeOfInvincibility*5; }

}

@DrawLoop{
let side=0;
	SetTexture(GRenemy);
		if(oldX<GetX || oldX>GetX){ side=32; }
		SetGraphicRect(256+side,212,288+side,244);
	SetGraphicAngle(0,0,0);
	if(oldX<GetX){ SetGraphicAngle(180,0,0); }
	SetGraphicScale(scale,scale);
	SetColor(255-invincible,255-invincible,255);
	DrawGraphic(GetX,GetY);
}

@Finalize{
if(BeVanished==false){
PlaySE(SEdeath);
	CreateEnemyFromFile("script\Functions\itempower.txt",GetX+rand(-25,25),GetY+rand(-25,25),0,0,0);
	CreateEnemyFromFile("script\Functions\itempoint.txt",GetX+rand(-25,25),GetY+rand(-25,25),0,0,0);
#include_function "script/Functions/PlayerTypeBonus.txt";
}
}

}