//metal

script_enemy_main{

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

let SEshotm2=("script\SoundEffects\shotm2.wav");
let SEmagics5=("script\SoundEffects\magics5.wav");
let SElaserm2=("script\SoundEffects\laserm2.wav");
let SEdeath=("script\SoundEffects\enemydeath1.wav");

let shot1=0;
let bullet1=[];
let bounce1=[];
let color1=[];

let effect2=0;
let scale2=0;
let fade2=255;

let playsound=0;
let dead=0;
let size=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=rand_int(0,40); 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=6;
enemycolor=(enemycolor-1)*40;

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

if(startx<cx){ SetAngle(90-120); }
if(startx>cx){ SetAngle(90+120); }
SetSpeed(5);

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

	LoadSE("script\SoundEffects\shotm2.wav");
	LoadSE("script\SoundEffects\magics5.wav");
	LoadSE("script\SoundEffects\laserm2.wav");

	SetInvincibility(20);
	SetLife(220);
	SetDamageRate(10,2); 
	SetScore(1000);
	SetShotColor(255,255,255);
}

@MainLoop{

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

if((GetX>minx && GetX<maxx) && (GetY>miny && GetY<maxy) && GetLife>200){

SetCollisionA(GetX,GetY,32*scale);
SetCollisionB(GetX,GetY,32*scale);

if(time%6==0 && time>=20){
let angle=0;
if(startx<cx){ angle=time*2; }
if(startx>cx){ angle=-time*2; }
	loop(3){
	SetShotColor(200,200,200);
	CreateShot01(GetX,GetY,2,angle,23,0);
	SetShotColor(255,255,255);
	angle+=360/3;
	}
PlaySE(SEshotm2);
}

} //Onscreen


if(time==60){
let angle=GetAngleToPlayer;
	loop(size){
	shot1=(Obj_Create(OBJ_LASER));
	Obj_SetPosition(shot1,GetX,GetY);
	Obj_SetAngle(shot1,angle);
	Obj_SetSpeed(shot1,3);
	ObjLaser_SetWidth(shot1,15);
	ObjLaser_SetLength(shot1,0);
	ObjLaser_SetSource(shot1,true);
	ObjShot_SetGraphic(shot1,95);
	bullet1=bullet1~[shot1];
	bounce1=bounce1~[shot1];
	bounce1[length(bullet1)-1]=1;
	angle+=360/size;
	}
PlaySE(SElaserm2);
}
let i=0;
	while(i<length(bullet1)){
		if(Obj_BeDeleted(bullet1[i])){
		bullet1=erase(bullet1,i); bounce1=erase(bounce1,i);
		i--;
		}
		else{
		
		if(ObjLaser_GetLength(bullet1[i])<250){ ObjLaser_SetLength(bullet1[i],ObjLaser_GetLength(bullet1[i])+Obj_GetSpeed(bullet1[i])); }
		if(ObjLaser_GetLength(bullet1[i])>=250){
		Obj_SetPosition(bullet1[i],Obj_GetX(bullet1[i])+Obj_GetSpeed(bullet1[i])*cos(Obj_GetAngle(bullet1[i])),Obj_GetY(bullet1[i])+Obj_GetSpeed(bullet1[i])*sin(Obj_GetAngle(bullet1[i])));
		ObjLaser_SetSource(bullet1[i],false);
		}

		if(bounce1[i]>0 && (ObjLaser_GetEndX(bullet1[i])<=minx-5 || ObjLaser_GetEndX(bullet1[i])>=maxx-5)){
			shot1=(Obj_Create(OBJ_LASER));
			Obj_SetPosition(shot1,ObjLaser_GetEndX(bullet1[i]),ObjLaser_GetEndY(bullet1[i]));
			Obj_SetAngle(shot1,180-Obj_GetAngle(bullet1[i]));
			Obj_SetSpeed(shot1,Obj_GetSpeed(bullet1[i]));
			ObjLaser_SetWidth(shot1,ObjLaser_GetWidth(bullet1[i]));
			ObjLaser_SetLength(shot1,0);
			ObjLaser_SetSource(shot1,true);
			ObjShot_SetGraphic(shot1,87);
			bullet1=bullet1~[shot1];
			bounce1=bounce1~[shot1];
			bounce1[length(bullet1)-1]=bounce1[i]-1;
			bounce1[i]=0;
			if(playsound<=0){ PlaySE(SEmagics5); playsound=6; }
			}
		if(bounce1[i]>0 && (ObjLaser_GetEndY(bullet1[i])>=maxy+5 || ObjLaser_GetEndY(bullet1[i])<=miny-5)){
			shot1=(Obj_Create(OBJ_LASER));
			Obj_SetPosition(shot1,ObjLaser_GetEndX(bullet1[i]),ObjLaser_GetEndY(bullet1[i]));
			Obj_SetAngle(shot1,-Obj_GetAngle(bullet1[i]));
			Obj_SetSpeed(shot1,Obj_GetSpeed(bullet1[i]));
			ObjLaser_SetWidth(shot1,ObjLaser_GetWidth(bullet1[i]));
			ObjLaser_SetLength(shot1,0);
			ObjLaser_SetSource(shot1,true);
			ObjShot_SetGraphic(shot1,87);
			bullet1=bullet1~[shot1];
			bounce1=bounce1~[shot1];
			bounce1[length(bullet1)-1]=bounce1[i]-1;
			bounce1[i]=0;
			if(playsound<=0){ PlaySE(SEmagics5); playsound=6; }
			}
		}
	i++;
}
if(playsound>0){ playsound--; }


if(startx<cx && GetSpeed>0 && time<150){ SetAngle(GetAngle+1.5); SetSpeed(GetSpeed-0.03); }
if(startx>cx && GetSpeed>0 && time<150){ SetAngle(GetAngle-1.5); SetSpeed(GetSpeed-0.03); }
if(time>=150){ SetSpeed(GetSpeed+0.02); }

if(GetX<=minx-32 || GetX>=maxx+32 || GetY<=miny-32 || GetY>=maxy+32 && frame>=60){ SetSpeed(0); }
if(time>=600){ VanishEnemy; DeleteEnemyShot(CHILD); }

if(GetLife<200 && dead==0){
let xsize=63;
let ysize=63;
	effect2=(Obj_Create(OBJ_EFFECT));
	Obj_SetPosition(effect2,GetX,GetY);
	ObjEffect_SetScale(effect2,1,1);
	ObjEffect_SetAngle(effect2,rand(0,360),rand(0,360),rand(0,360));
	ObjEffect_SetLayer(effect2,1); ObjEffect_SetTexture(effect2,GRexplosion); ObjEffect_SetRenderState(effect2,ADD);
	ObjEffect_SetPrimitiveType(effect2,PRIMITIVE_TRIANGLESTRIP); ObjEffect_CreateVertex(effect2,4);
	ObjEffect_SetVertexXY(effect2,0,-(xsize/2),-(ysize/2)); ObjEffect_SetVertexUV(effect2,0,0,0);
	ObjEffect_SetVertexXY(effect2,1,xsize/2,-(ysize/2)); ObjEffect_SetVertexUV(effect2,1,xsize,0);
	ObjEffect_SetVertexXY(effect2,2,-(xsize/2),ysize/2); ObjEffect_SetVertexUV(effect2,2,0,ysize);
	ObjEffect_SetVertexXY(effect2,3,xsize/2,ysize/2); ObjEffect_SetVertexUV(effect2,3,xsize,ysize);
	ObjEffect_SetVertexColor(effect2,0,fade2,255,255,255); ObjEffect_SetVertexColor(effect2,1,fade2,255,255,255);
	ObjEffect_SetVertexColor(effect2,2,fade2,255,255,255); ObjEffect_SetVertexColor(effect2,3,fade2,255,255,255);
	loop(7){ CreateEnemyFromFile("script\Functions\itempoint.txt",GetX+rand(-50,50),GetY+rand(-50,50),0,0,0); }
	loop(3){ CreateEnemyFromFile("script\Functions\itempower.txt",GetX+rand(-50,50),GetY+rand(-50,50),0,0,0); }
	PlaySE(SEdeath);
	dead=1;
}

if(dead==1){
	ObjEffect_SetScale(effect2,scale2,scale2);
	ObjEffect_SetVertexColor(effect2,0,fade2,255,255,255); ObjEffect_SetVertexColor(effect2,1,fade2,255,255,255);
	ObjEffect_SetVertexColor(effect2,2,fade2,255,255,255); ObjEffect_SetVertexColor(effect2,3,fade2,255,255,255);
	scale2+=0.2;
	fade2-=16;
	if(fade2<=10){ Obj_Delete(effect2); }
}


if(time==0){
let gemcolor=300;
	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)){
	if(dead==0){ Obj_SetPosition(gem1[i],GetX+30*cos(gemangle),GetY+10*sin(gemangle)); }
	if(dead==1){ Obj_SetPosition(gem1[i],minx-50,miny-50); }
	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{
	if(dead==0){
	SetTexture(GRenemy);
	SetGraphicRect(0+enemycolor,0,40+enemycolor,40);
	SetGraphicAngle(0,0,0);
	SetGraphicScale(1,1);
	DrawGraphic(GetX,GetY);
	}
}

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

}