//fire

script_enemy_main{

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

let SEshotm4=("script\SoundEffects\shotm4.wav");
let SEshotm7=("script\SoundEffects\shotm7.wav");
let SEdeath=("script\SoundEffects\enemydeath1.wav");

let delay=25;
let size=GetArgument[0];
let type=GetArgument[1];

let effect1=0;
let familiar1=[];
let timer1=[];
let xpos1=[];
let ypos1=[];

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

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

if(type==1){
	SetAngle(90+rand(-20,20));
	SetSpeed(1.5);
}
if(type==2){
	SetSpeed(1.5);
}
if(type==3 || type==4){
	if(startx<cx){ SetAngle(0); }
	if(startx>cx){ SetAngle(180); }
	delay=20;
	SetSpeed(2);
}

@Initialize{
	LoadGraphic("\script\Images\Enemies\Gems.png");
	LoadGraphic("\script\Images\OtherEffects\SpellCircle1b.png");
	
	LoadSE("script\SoundEffects\shotm4.wav");
	LoadSE("script\SoundEffects\shotm7.wav");

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

@MainLoop{

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

if(GetX<=minx-(GetArgument[0]*40) || GetX>=maxx+(GetArgument[0]*40) || GetY<=miny-(GetArgument[0]*40) || GetY>=maxy+(GetArgument[0]*40) && frame>=60){ VanishEnemy; }

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


if(time==0){
let xsize=199;
let ysize=199;
	loop(size){
	effect1=(Obj_Create(OBJ_EFFECT));
	Obj_SetPosition(effect1,GetX,GetY);
	ObjEffect_SetScale(effect1,0.15,0.15);
	ObjEffect_SetLayer(effect1,1); ObjEffect_SetTexture(effect1,GRspellcircle1b); ObjEffect_SetRenderState(effect1,ADD);
	ObjEffect_SetPrimitiveType(effect1,PRIMITIVE_TRIANGLESTRIP); ObjEffect_CreateVertex(effect1,4);
	ObjEffect_SetVertexXY(effect1,0,-(xsize/2),-(ysize/2)); ObjEffect_SetVertexUV(effect1,0,0,0);
	ObjEffect_SetVertexXY(effect1,1,xsize/2,-(ysize/2)); ObjEffect_SetVertexUV(effect1,1,xsize,0);
	ObjEffect_SetVertexXY(effect1,2,-(xsize/2),ysize/2); ObjEffect_SetVertexUV(effect1,2,0,ysize);
	ObjEffect_SetVertexXY(effect1,3,xsize/2,ysize/2); ObjEffect_SetVertexUV(effect1,3,xsize,ysize);
	ObjEffect_SetVertexColor(effect1,0,255,255,0,0); ObjEffect_SetVertexColor(effect1,1,255,255,0,0);
	ObjEffect_SetVertexColor(effect1,2,255,255,0,0); ObjEffect_SetVertexColor(effect1,3,255,255,0,0);
	familiar1=familiar1~[effect1];
	timer1=timer1~[effect1];
	timer1[length(familiar1)-1]=0;
	xpos1=xpos1~[effect1];
	xpos1[length(familiar1)-1]=GetX;
	ypos1=ypos1~[effect1];
	ypos1[length(familiar1)-1]=GetY;
	}
}

let i=0;
	while(i<length(familiar1)){
	
	DelayedPosition(GetX,GetY,i,delay+(i*delay));
	Obj_SetPosition(familiar1[i],xpos1[i],ypos1[i]);
	ObjEffect_SetAngle(familiar1[i],0,0,timer1[i]*7);
	if(i>0){ Obj_SetAngle(familiar1[i],atan2(Obj_GetY(familiar1[i-1])-Obj_GetY(familiar1[i]),Obj_GetX(familiar1[i-1])-Obj_GetX(familiar1[i]))); }
	if(i==0){ Obj_SetAngle(familiar1[i],atan2(GetY-Obj_GetY(familiar1[i]),GetX-Obj_GetX(familiar1[i]))); }
	
	if(timer1[i]%9==0){
	CreateShot02(Obj_GetX(familiar1[i]),Obj_GetY(familiar1[i]),0,rand(0,360),0.02,1.5,62,10);
	PlaySE(SEshotm4);
	}
		
	timer1[i]=timer1[i]+1;
	i++;
	}

	
if(type==2 && time<420){ SetAngle(GetAngleToPlayer); }

if(type==3){
	if(startx<cx){
		if(time>=165 && time<210){ SetAngle(GetAngle+2); }
	}
	if(startx>cx){
		if(time>=165 && time<210){ SetAngle(GetAngle-2); }
	}
}

oldX=GetX;
oldY=GetY;

if(type==4){
	if(startx<cx){
	SetX(GetX+1.5);
	SetY(GetY+1.5*cos(time*4));
	}
	if(startx>cx){
	SetX(GetX-1.5);
	SetY(GetY-1.5*cos(time*4));
	}
}


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];
	}
}

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; }
	}
}


frame++;
time++;

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

yield;
}

@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);
	DeleteEnemyShot(CHILD);
	loop(3){ CreateEnemyFromFile("script\Functions\itempower.txt",GetX+rand(-25,25),GetY+rand(-25,25),0,0,0); }
	
	let i=0;
	while(i<length(familiar1)){
	let angle=rand(0,360);
		loop(20){
		CreateShot02(Obj_GetX(familiar1[i]),Obj_GetY(familiar1[i]),rand(5,2),angle,-0.1,rand(1.5,2),1,rand_int(0,10));
		angle+=360/20;
		}
	PlaySE(SEshotm7);
	CreateEnemyFromFile("script\Functions\itempoint.txt",Obj_GetX(familiar1[i]),Obj_GetY(familiar1[i]),0,0,0);
	i++;
	}
#include_function "script/Functions/PlayerTypeBonus.txt";
}
}

task DelayedPosition(xpos,ypos,nr,delay){
	loop(delay){ yield; }
	xpos1[nr]=xpos;
	ypos1[nr]=ypos;
}

}