//Flies by and aims

script_enemy_main{

let GRenemy=("\script\Images\Enemies\Fairy1-01.png");
let SEshots5=("script\SoundEffects\shots5.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=0.5;
let shots=0;
let shotcolor=0; let shotspeed=0;
let oldX=0; let oldY=0;
let invincible=125;

let type=GetArgument;

if(type==1 || type==2){
	if(startx<cx){ SetAngle(45); }
	if(startx>cx){ SetAngle(135); }
	SetSpeed(4);
}
if(type==3){
	if(startx<cx){ SetAngle(0); }
	if(startx>cx){ SetAngle(180); }
	SetSpeed(5);
}

@Initialize{
	LoadGraphic("\script\Images\Enemies\Fairy1-01.png");
	LoadSE("script\SoundEffects\shots5.wav");

	SetInvincibility(20);
	SetLife(5);
	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%24==0 || (time+1)%30==0 && time>=30 && shots<6){
		let angle=-22;
		loop(3){
		if(type==1){ CreateShot01(GetX,GetY,2,GetAngleToPlayer+angle,30,0); }
		if(type>1){ CreateShot01(GetX,GetY,2,GetAngleToPlayer+angle,27,0); }
		angle+=22;
		}
	shots++;
	}
	if(time%24==0 && time>=30 && shots<6){PlaySE(SEshots5); }
} //Easy

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

if(GetCommonData("Difficulty")==2){
	if(time%20==0 || (time+1)%20==0 && time>=30 && shots<8){
		let angle=-18;
		loop(3){
		if(type==1){ CreateShot01(GetX,GetY,2.5,GetAngleToPlayer+angle,30,0); }
		if(type>1){ CreateShot01(GetX,GetY,2.5,GetAngleToPlayer+angle,27,0); }
		angle+=18;
		}
	shots++;
	}
	if(time%24==0 && time>=30 && shots<8){PlaySE(SEshots5); }
} // Normal

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

if(GetCommonData("Difficulty")==3){
	if(time%12==0 || (time+1)%12==0 && time>=30 && shots<16){
		let angle=-14;
		loop(3){
		if(type==1){ CreateShot01(GetX,GetY,4,GetAngleToPlayer+angle,30,0); }
		if(type>1){ CreateShot01(GetX,GetY,4,GetAngleToPlayer+angle,27,0); }
		angle+=14;
		}
	shots++;
	}
	if(time%12==0 && time>=30 && shots<16){PlaySE(SEshots5); }
} // Hard

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

if(GetCommonData("Difficulty")==4){
	if(time%8==0 || (time+1)%8==0 && time>=30 && shots<16){
		let angle=-10;
		loop(3){
		if(type==1){ CreateShot01(GetX,GetY,5,GetAngleToPlayer+angle,30,0); }
		if(type>1){ CreateShot01(GetX,GetY,5,GetAngleToPlayer+angle,27,0); }
		angle+=10;
		}
	shots++;
	}
	if(time%12==0 && time>=30 && shots<16){  PlaySE(SEshots5); }
} // Lunatic
} // OnScreen

if(type==1){
	if(startx<cx && time<45){ SetAngle(GetAngle-1); }
	if(startx>cx && time<45){ SetAngle(GetAngle+1); }
}
if(type==2){
	if(startx<cx && time>=40 && time<95){ SetAngle(GetAngle-4); }
	if(startx>cx && time>=40 && time<95){ SetAngle(GetAngle+4); }
}
if(type==3){
	if(startx<cx){ SetAngle(GetAngle+3*cos(time*3)); }
	if(startx>cx){ SetAngle(GetAngle-3*cos(time*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=128; }
		if(animation>=ani*0 && animation<ani*1){ SetGraphicRect(1,side,256,128+side); }
		if(animation>=ani*1 && animation<ani*2){ SetGraphicRect(257,side,512,128+side); }
		if(animation>=ani*2 && animation<ani*3){ SetGraphicRect(513,side,768,128+side); }
		if(animation>=ani*3 && animation<ani*4){ SetGraphicRect(769,side,1024,128+side); }
		if(animation>=ani*4 && animation<ani*5){ SetGraphicRect(513,side,768,128+side); }
		if(animation>=ani*5 && animation<ani*6){ SetGraphicRect(257,side,512,128+side); }
	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\itemscore.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";
}
}

}