//earth

script_enemy_main{

let GRenemy=("\script\Images\Enemies\Gems.png");

let SEshots7=("script\SoundEffects\shots7.wav");
let SEshotm7=("script\SoundEffects\shotm7.wav");
let SEdeath=("script\SoundEffects\enemydeath1.wav");

let angle1=0;
let type=GetArgument;

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 shots=0;

let scale=1;
let anispeed=4;
let animation=0;
let gemangle=0;
let EFgem=0;
let gem1=[];
let enemycolor=4;
enemycolor=(enemycolor-1)*40;

let shotcolor=0; let shotspeed=0;
let oldX=0; let oldY=0;
let invincible=125;

SetAngle(atan2(cy-GetY,cx-GetX)+rand(-10,10));
SetSpeed(rand(4,6));

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

	LoadSE("script\SoundEffects\shots7.wav");
	LoadSE("script\SoundEffects\shotm7.wav");

	SetInvincibility(25);
	SetLife(17);
	SetDamageRate(10,2); 
	SetScore(1000);
	SetShotColor(255,255,255);
}

@MainLoop{

let weaken=1;
	if(GetCommonData("PlayerType")==2){ weaken=1.5; } //Metal>Earth
	if(GetCommonData("PlayerType")==1){ weaken=0.5; } //Fire<Earth
SetDamageRate(10*weaken,2.5*weaken);

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(GetSpeed==0 && shots==0){ angle1=GetAngleToPlayer; }

if(GetSpeed==0 && shots<10 && type==1){
	loop(15){
		loop(5){
		let angle2=rand(0,360);
		let radius=rand(10,45);
		let color=rand_int(255,200);
		SetShotColor(color,color,color);
		CreateShot02(GetX+radius*cos(angle2),GetY+radius*sin(angle2),0,angle1,0.04,6,39,rand_int(0,15));
		SetShotColor(255,255,255);
		}
	let angle2=rand(0,360);
	let radius=rand(10,30);
	let color=rand_int(255,200);
	SetShotColor(color,color,color);
	CreateShot02(GetX+radius*cos(angle2),GetY+radius*sin(angle2),0,angle1,rand(0.025,0.035),6,40,rand_int(0,15));
	SetShotColor(255,255,255);
	angle1+=360/15;
	}
if(frame%3==0){ PlaySE(SEshots7); }
shots++;
}

if(GetSpeed==0 && shots<10 && type==2){
	loop(15){
		loop(6){
		let angle2=rand(0,360);
		let radius=rand(10,45);
		let color=rand_int(255,200);
		SetShotColor(color,color,color);
		CreateShot02(GetX+radius*cos(angle2),GetY+radius*sin(angle2),0,angle1,0.08,12,40,rand_int(0,15));
		SetShotColor(255,255,255);
		}
	let angle2=rand(0,360);
	let radius=rand(10,30);
	let color=rand_int(255,200);
	SetShotColor(color,color,color);
	CreateShot02(GetX+radius*cos(angle2),GetY+radius*sin(angle2),0,angle1,rand(0.05,0.07),12,39,rand_int(0,15));
	SetShotColor(255,255,255);
	angle1+=360/15;
	}
if(frame%3==0){ PlaySE(SEshotm7); }
shots++;
}

if(GetSpeed>0 && time<120){ SetSpeed(GetSpeed-0.1); }
if(GetSpeed<0 && time<120){ SetSpeed(0); }
if(GetSpeed>-7 && time>=120){ SetSpeed(GetSpeed-0.04); }

} //Onscreen


if(time==0){
let gemcolor=60;
	loop(6){
	EFgem=(Obj_Create(OBJ_EFFECT));
	Obj_SetPosition(EFgem,GetX,GetY);
	ObjEffect_SetLayer(EFgem,3); ObjEffect_SetTexture(EFgem,GRenemy); ObjEffect_SetRenderState(EFgem,ALPHA);
	ObjEffect_SetPrimitiveType(EFgem,PRIMITIVE_TRIANGLESTRIP); ObjEffect_CreateVertex(EFgem,4);
	ObjEffect_SetVertexXY(EFgem,0,-6,-10); ObjEffect_SetVertexUV(EFgem,0,0,40);
	ObjEffect_SetVertexXY(EFgem,1,6,-10); ObjEffect_SetVertexUV(EFgem,1,12,40);
	ObjEffect_SetVertexXY(EFgem,2,-6,10); ObjEffect_SetVertexUV(EFgem,2,0,60);
	ObjEffect_SetVertexXY(EFgem,3,6,10); ObjEffect_SetVertexUV(EFgem,3,12,60);
	ObjEffect_SetVertexColor(EFgem,0,255,127+128*cos(gemcolor),127+128*cos(gemcolor-120),127+128*cos(gemcolor-240));
	ObjEffect_SetVertexColor(EFgem,1,255,127+128*cos(gemcolor),127+128*cos(gemcolor-120),127+128*cos(gemcolor-240));
	ObjEffect_SetVertexColor(EFgem,2,255,127+128*cos(gemcolor),127+128*cos(gemcolor-120),127+128*cos(gemcolor-240));
	ObjEffect_SetVertexColor(EFgem,3,255,127+128*cos(gemcolor),127+128*cos(gemcolor-120),127+128*cos(gemcolor-240));
	gem1=gem1~[EFgem];
	}
}

if(time>=0){
let i=0;
	while(i<length(gem1)){
	Obj_SetPosition(gem1[i],GetX+30*cos(gemangle),GetY+10*sin(gemangle));
	if(Obj_GetY(gem1[i])<GetY){ ObjEffect_SetLayer(gem1[i],1); }
	if(Obj_GetY(gem1[i])>=GetY){ ObjEffect_SetLayer(gem1[i],2); }

	ObjEffect_SetVertexUV(gem1[i],0,0+animation,40);
	ObjEffect_SetVertexUV(gem1[i],1,12+animation,40);
	ObjEffect_SetVertexUV(gem1[i],2,0+animation,60);
	ObjEffect_SetVertexUV(gem1[i],3,12+animation,60);

	gemangle+=360/length(gem1);
	i++;
	}
gemangle+=5;
if(time%anispeed==0){
	animation+=12;
	if(animation>=72){ animation=0; }
	}
}


oldX=GetX;
oldY=GetY;
frame++;
time++;

if(GetTimeOfInvincibility<25 && invincible>0){ invincible=GetTimeOfInvincibility*5; }

}

@DrawLoop{
	SetTexture(GRenemy);
	SetGraphicRect(0+enemycolor,0,40+enemycolor,40);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(1,1);
	DrawGraphic(GetX,GetY);
}

@Finalize{
if(BeVanished==false){
PlaySE(SEdeath);
let random=rand_int(0,2);
	loop(5){ CreateEnemyFromFile("script\Functions\itempoint.txt",GetX+rand(-40,40),GetY+rand(-40,40),0,0,0); }
	loop(3){ CreateEnemyFromFile("script\Functions\itempower.txt",GetX+rand(-40,40),GetY+rand(-40,40),0,0,0); }
#include_function "script/Functions/PlayerTypeBonus.txt";
}
}

}