//Aims/misaims random bullets

script_enemy_main{

let GRenemy=("\script\Images\Enemies\Ghosts.png");
let SEshots4=("script\SoundEffects\shots4.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(startx>minx && startx<maxx){ SetAngle(90+rand(-15,15)); }
if(startx<minx){ SetAngle(0+rand(0,15)); }
if(startx>maxx){ SetAngle(180+rand(-15,0)); }
SetSpeed(rand(2,3));

@Initialize{
	LoadGraphic("\script\Images\Enemies\Ghosts.png");

	LoadSE("script\SoundEffects\shots4.wav");

	SetInvincibility(30);
	SetLife(7);
	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(frame>=20 && frame<150 && frame%4==0){
	let angle=0;
	if(type==1){ angle=GetAngleToPlayer; }
	if(type==2){ angle=GetAngleToPlayer+(360/20); }
		loop(8){
		if(type==1){ CreateShot02(GetX,GetY,0,angle,0.04,3,135,10); }
		if(type==2){ CreateShot02(GetX,GetY,0,angle,0.04,3,75,10); }
		angle+=360/8;
		}
	}
	if(frame>=20 && frame<150 && frame%4==0){ PlaySE(SEshots4); }
} //Easy

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

if(GetCommonData("Difficulty")==2){
	if(frame>=20 && frame<150 && frame%4==0){
	let angle=0;
	if(type==1){ angle=GetAngleToPlayer; }
	if(type==2){ angle=GetAngleToPlayer+(360/20); }
		loop(10){
		if(type==1){ CreateShot02(GetX,GetY,0,angle+rand(-2,2),rand(0.04,0.05),4,135,10); }
		if(type==2){ CreateShot02(GetX,GetY,0,angle+rand(-2,2),rand(0.04,0.05),4,75,10); }
		angle+=360/10;
		}
	}
	if(frame>=20 && frame<150 && frame%4==0){ PlaySE(SEshots4); }
} // Normal

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

if(GetCommonData("Difficulty")==3){
	if(frame>=20 && frame<150 && frame%2==0){
	let angle=0;
	if(type==1){ angle=GetAngleToPlayer; }
	if(type==2){ angle=GetAngleToPlayer+(360/20); }
		loop(10){
		if(type==1){ CreateShot02(GetX,GetY,0,angle+rand(-3,3),rand(0.05,0.06),6,135,10); }
		if(type==2){ CreateShot02(GetX,GetY,0,angle+rand(-3,3),rand(0.05,0.06),6,75,10); }
		angle+=360/10;
		}
	}
	if(frame>=20 && frame<150 && frame%4==0){ PlaySE(SEshots4); }
} // Hard

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

if(GetCommonData("Difficulty")==4){
	if(frame>=20 && frame<150){
	let angle=0;
	if(type==1){ angle=GetAngleToPlayer; }
	if(type==2){ angle=GetAngleToPlayer+(360/20); }
		loop(12){
		if(type==1){ CreateShot02(GetX,GetY,0,angle+rand(-4,4),rand(0.08,0.1),8,135,10); }
		if(type==2){ CreateShot02(GetX,GetY,0,angle+rand(-4,4),rand(0.08,0.1),8,75,10); }
		angle+=360/12;
		}
	}
	if(frame>=20 && frame<150 && frame%4==0){ PlaySE(SEshots4); }
} // Lunatic
} // OnScreen


if(startx>=cx && startx<maxx){ SetAngle(GetAngle+1); }
if(startx<cx && startx>minx){ SetAngle(GetAngle-1); }
if(startx<minx){ SetAngle(GetAngle-0.5); }
if(startx>maxx){ SetAngle(GetAngle+0.5); }
SetSpeed(GetSpeed+0.01);

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

}

@DrawLoop{
	SetTexture(GRenemy);
		if(animation>=ani*0 && animation<ani*1){ SetGraphicRect(0,151,44,210); }
		if(animation>=ani*1 && animation<ani*2){ SetGraphicRect(44,151,87,210); }
		if(animation>=ani*2 && animation<ani*3){ SetGraphicRect(88,151,132,210); }
		if(animation>=ani*3 && animation<ani*4){ SetGraphicRect(133,151,176,210); }
		if(animation>=ani*4 && animation<ani*5){ SetGraphicRect(177,151,220,210); }
		if(animation>=ani*5 && animation<ani*6){ SetGraphicRect(221,151,264,210); }
		if(animation>=ani*6 && animation<ani*7){ SetGraphicRect(265,151,308,210); }
	SetGraphicAngle(0,0,0);
	SetColor(0,255,255);
	SetGraphicScale(1,1);
	SetRenderState(ADD);
	DrawGraphic(GetX,GetY);
}

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

}