//all

script_enemy_main{

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

let SEshotm5=("script\SoundEffects\shotm5.wav");
let SEdeath=("script\SoundEffects\enemydeath1.wav");

let color=rand_int(0,4);
let width=30;
let size=GetArgument[0];
let speed=GetArgument[1];

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=1;
enemycolor=(enemycolor-1)*40;

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

SetAngle(90);
SetSpeed(rand(4,5));

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

	LoadSE("script\SoundEffects\shotm5.wav");
	LoadSE("script\SoundEffects\enemydeath1.wav");

	SetInvincibility(40);
	SetLife(45);
	SetDamageRate(10,2); 
	SetScore(5000);
	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(time%10==0 && time>=60 && time<450){
let angle1=90;
let shotcolor=[1,23,31,39,60];
	loop(12){
		if(shots%2==0){
		let distance=-(width*size)/2+(width/2);
			loop(size){
			CreateShot01(GetX+distance*cos(angle1+90),GetY+distance*sin(angle1+90),speed,angle1,shotcolor[color],10);
			distance+=30;
			}
		}
		if(shots%2!=0){
		let distance=-(width*size)/2+width;
			loop(size-1){
			CreateShot01(GetX+distance*cos(angle1+90),GetY+distance*sin(angle1+90),speed,angle1,shotcolor[color],10);
			distance+=30;
			}
		}
	angle1+=360/12;
	}
PlaySE(SEshotm5);
shots++;
color++;
if(color>4){ color=0; }
}

} //Onscreen

if(GetSpeed>0 && time<450){ SetSpeed(GetSpeed-0.08); }
if(GetSpeed<0 && time<450){ SetSpeed(0); }
if(time>=450){ SetSpeed(GetSpeed-0.08); }


if(time==0){
let gemcolor=0;
	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];
	gemcolor+=360/6;
	}
}

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){
DeleteEnemyShot(CHILD);
PlaySE(SEdeath);
	loop(10){ CreateEnemyFromFile("script\Functions\itempoint.txt",GetX+rand(-50,50),GetY+rand(-50,50),0,0,0); }
	loop(4){ CreateEnemyFromFile("script\Functions\itempower.txt",GetX+rand(-50,50),GetY+rand(-50,50),0,0,0); }
	if(GetArgument[2]==1){ CreateEnemyFromFile("script\Functions\itembomb.txt",GetX,GetY,0,0,0); }
	if(GetArgument[2]==2){ CreateEnemyFromFile("script\Functions\itemlife.txt",GetX,GetY,0,0,0); }
#include_function "script/Functions/PlayerTypeBonus.txt";
}
}

}