//water

script_enemy_main{

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

let SEshots6=("script\SoundEffects\shots6.wav");
let SEmagics1=("script\SoundEffects\magics1.wav");
let SEdeath=("script\SoundEffects\enemydeath1.wav");

let shot1=0;
let bullet1=[];
let timer1=[];
let speed1=[];
let angle1=[];

let phase=0;
let offscreen=0;

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

let dead=0;

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

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

if(startx<cx){ SetAngle(0+GetArgument); }
if(startx>cx){ SetAngle(180-GetArgument); }
SetSpeed(rand(3,4));

@Initialize{
	LoadGraphic("\script\Images\Enemies\Gems.png");
	LoadGraphic("\script\Images\OtherEffects\EnemyDeath.png");
	
	LoadSE("script\SoundEffects\shots6.wav");
	LoadSE("script\SoundEffects\magics1.wav");
	LoadSE("script\SoundEffects\enemydeath1.wav");

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

@MainLoop{

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

if(GetX<=minx-32 || GetX>=maxx+32 || GetY<=miny-32 || GetY>=maxy+32 && frame>=60){ SetSpeed(0); offscreen=1; }

if((GetX>minx && GetX<maxx) && (GetY>miny && GetY<maxy) && offscreen==0 && dead==0){
SetCollisionA(GetX,GetY,32*scale);
SetCollisionB(GetX,GetY,32*scale);
} //Onscreen


if(time%2==0 && offscreen==0 && dead==0){
let angle=rand(0,360);
	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,GetX,GetY);
	Obj_SetAngle(shot1,rand(0,360));
	Obj_SetSpeed(shot1,rand(0.5,1.5));
	ObjShot_SetGraphic(shot1,128);
	bullet1=bullet1~[shot1];
	timer1=timer1~[shot1];
	timer1[length(bullet1)-1]=0;
	speed1=speed1~[shot1];
	speed1[length(bullet1)-1]=rand(1.5,2);
	angle1=angle1~[shot1];
	angle1[length(bullet1)-1]=rand(0,5);
if(time%4==0){ PlaySE(SEshots6); }
}

if(time%2==0 && offscreen==0 && dead==0){
CreateShot01(GetX,GetY,rand(1.5,2.5),270+rand(-30,30),83,10);
}

let i=0;
while(i<length(bullet1)){
	if(Obj_BeDeleted(bullet1[i])){
	bullet1=erase(bullet1,i); timer1=erase(timer1,i); speed1=erase(speed1,i); angle1=erase(angle1,i);
	i--;
	}
	else{
		if(phase==0){
		if(Obj_GetAngle(bullet1[i])>=270){ Obj_SetAngle(bullet1[i],Obj_GetAngle(bullet1[i])-360); }
		if(Obj_GetAngle(bullet1[i])>90+angle1[i]){ Obj_SetAngle(bullet1[i],Obj_GetAngle(bullet1[i])-2); }
		if(Obj_GetAngle(bullet1[i])<90-angle1[i]){ Obj_SetAngle(bullet1[i],Obj_GetAngle(bullet1[i])+2); }
		if(Obj_GetSpeed(bullet1[i])>speed1[i]){ Obj_SetSpeed(bullet1[i],Obj_GetSpeed(bullet1[i])-0.01); }
		if(Obj_GetSpeed(bullet1[i])<speed1[i]){ Obj_SetSpeed(bullet1[i],Obj_GetSpeed(bullet1[i])+0.01); }
		}
		if(phase==1){
		let anglex=-30;
			loop(2){
			CreateShot01(Obj_GetX(bullet1[i]),Obj_GetY(bullet1[i]),rand(1,1.5),Obj_GetAngle(bullet1[i])-anglex,60,10);
			anglex+=60;
			}
		Obj_Delete(bullet1[i]);
		}
	timer1[i]=timer1[i]+1;
	}
i++;
}

if(GetLife<200 && phase==0){ phase=1; PlaySE(SEmagics1); }

if(startx<cx){ SetAngle(GetAngle-0.3); }
if(startx>cx){ SetAngle(GetAngle+0.3); }


if(time>=900){ 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=240;
	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){
PlaySE(SEdeath);
	loop(2){ CreateEnemyFromFile("script\Functions\itempoint.txt",GetX+rand(-40,40),GetY+rand(-40,40),0,0,0); }
	loop(2){ CreateEnemyFromFile("script\Functions\itempower.txt",GetX+rand(-40,40),GetY+rand(-40,40),0,0,0); }
#include_function "script/Functions/PlayerTypeBonus.txt";
}
}

}