script_enemy_main{

let distance=0;
let sonarspin=180;
let sonarspeed=2.5;
let sonarangle=0;

let effect1=0;
let object1=[];
let timer1=[];
let scale1=[];

let character="Mai";
let cutin=character;
let spellcards=10;
let spellcardnumber=48;
let dispelled=0;
let damagerate=10;
let outfit=5;
outfit=(128*outfit)-128;
let usespell=0;
let bgfade=0;
let frame=0;
let time=0;
let miny=GetClipMinY; let maxy=GetClipMaxY; let minx=GetClipMinX; let maxx=GetClipMaxX;
let cx=GetCenterX; let cy=GetCenterY;

let GRbeep=("\script\Images\OtherEffects\Impact.png");
let BG1=("\script\Images\BackgroundLayers\Mai4a.png");
let BG2=("\script\Images\BackgroundLayers\Mai4b.png");
let GRboss=("\script\Images\CharacterSprites\Mai.png");

let SEsonar=("script\SoundEffects\sonar1.wav");
let SEshot1=("script\SoundEffects\shotm4.wav");

#include_function "script/Functions/SetSpellcardCommonData.txt";
#include_function "script/Functions/SpellcardNameLoad.txt";
#include_function "script/Functions/HealthBarLoad.txt";
#include_function "script/Functions/CutInLoad.txt";

@Initialize{
	LoadUserShotData("script\shots\ShotsMai1.txt");

	LoadSE("script\SoundEffects\sonar1.wav");
	LoadSE("script\SoundEffects\shotm4.wav");

	LoadGraphic("\script\Images\BackgroundLayers\Mai4a.png");
	LoadGraphic("\script\Images\BackgroundLayers\Mai4b.png");
	LoadGraphic("\script\Images\OtherEffects\Impact.png");

	LoadGraphic("\script\Images\CharacterSprites\Mai.png");

	SetScore(750000);
	SetLife(400);
	SetTimer(60);
	SetInvincibility(120);
	SetDamageRate(10,10);
	SetEnemyMarker(true);
	MagicCircle(true);
	#include_function "script/Functions/Focus.txt";
	Focus(character);
	SetEffectForZeroLife(60,100,1);
	SetMovePosition02(cx,miny+80,50);

	SetX(GetCommonData("Boss1X"));
	SetY(GetCommonData("Boss1Y"));
	SetCommonData("Boss1Vanish",1);
}
	
@MainLoop{

SetCollisionA(GetX,GetY,16);
SetCollisionB(GetX,GetY,16);
SetShotAutoDeleteClip(32,32,32,32);

Weakness(character);
#include_function "script/Functions/Weakness.txt";
if(GetCommonData("BombOn")==0){ damagerate=10; if(GetTimer<=35){ damagerate=20; } }
if(GetCommonData("BombOn")==1){ damagerate=2.5; }
SetDamageRate(damagerate*weaken,damagerate*weaken);

let difficulty="";
if(GetCommonDataDefault("Difficulty",2)==1){ difficulty="Easy"; }
if(GetCommonDataDefault("Difficulty",2)==2){ difficulty="Normal"; }
if(GetCommonDataDefault("Difficulty",2)==3){ difficulty="Hard"; }
if(GetCommonDataDefault("Difficulty",2)==4){ difficulty="Lunatic"; }

SpellcardName("Echolocation [Sonar]",spellcardnumber); 
HealthBar();
Portrait(cutin,2);

if(time%60==0 && time>=60){
	if(GetPlayerX>minx+50 && GetPlayerX<maxx-50){
	SetMovePosition01(GetPlayerX+rand(-30,30),rand(miny+60,miny+80),1.5);
	}
	if(GetPlayerX<=minx+50){
	SetMovePosition01(GetPlayerX+rand(15,30),rand(miny+60,miny+80),1.5);
	}
	if(GetPlayerX>=maxx-50){
	SetMovePosition01(GetPlayerX-rand(15,30),rand(miny+60,miny+80),1.5);
	}
}


if(time==0){
	let shot1=0;
	CreateLaserA(shot1,cx,cy,300,10,101,120);
	SetLaserDataA(shot1,0,180,0.8,0,0,0);
	FireShot(shot1);
}

let random=rand_int(0,3);
if(random>0 && time%2==0 && time>120 && GetLife>0){
	let shot1=0;
	let location=rand(0,256);
	CreateShotA(shot1,cx+location*cos((time*0.8)+180),cy+location*sin((time*0.8)+180),10);
	SetShotDataA(shot1,0,0,0,0,0,0,112);
	SetShotKillTime(shot1,435);
	FireShot(shot1);
}
if(time%6==0 && time>=120){ PlaySE(SEshot1); }

let random=rand_int(0,3);
if(random>0 && time%3==0 && time>120 && GetLife>0){
	let shot1=0;
	let location=rand(0,256);
	CreateShotA(shot1,cx+location*cos((time*0.8)+180),cy+location*sin((time*0.8)+180),10);
	SetShotDataA(shot1,0,0,0,0,0,0,112);
	SetShotKillTime(shot1,435);
	FireShot(shot1);
}


sonarangle=atan2(GetPlayerY-cy,GetPlayerX-cx);

if(sonarspin<sonarangle+6 && sonarspin>sonarangle-6 && length(object1)<1){
let xsize=128;
let ysize=128;
	effect1=(Obj_Create(OBJ_EFFECT));
	Obj_SetPosition(effect1,GetPlayerX,GetPlayerY);
	ObjEffect_SetScale(effect1,0,0);
	ObjEffect_SetLayer(effect1,1); ObjEffect_SetTexture(effect1,GRbeep); 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,255,255); ObjEffect_SetVertexColor(effect1,1,255,255,255,255);
	ObjEffect_SetVertexColor(effect1,2,255,255,255,255); ObjEffect_SetVertexColor(effect1,3,255,255,255,255);
	object1=object1~[effect1];
	timer1=timer1~[effect1];
	timer1[length(object1)-1]=0;
	scale1=scale1~[effect1];
	scale1[length(object1)-1]=0;

usespell=-30;
let angle=GetAngleToPlayer-20;
	loop(3){
	CreateShot02(GetX,GetY,0,angle,0.03,5,121,10);
	angle+=20;
	}
PlaySE(SEsonar);
}

sonarspin+=sonarspeed;
if(sonarspin>=180){ sonarspin-=360; }
sonarspeed+=0.002;

if(time>=60){
let i=0;
	while(i<length(object1)){
		if(Obj_BeDeleted(object1[i])){
		object1=erase(object1,i); timer1=erase(timer1,i); scale1=erase(scale1,i);
		i--;
		}
		else{
		ObjEffect_SetVertexColor(object1[i],0,255-(timer1[i]*8.5),255,255,255); ObjEffect_SetVertexColor(object1[i],1,255-(timer1[i]*8.5),255,255,255);
		ObjEffect_SetVertexColor(object1[i],2,255-(timer1[i]*8.5),255,255,255); ObjEffect_SetVertexColor(object1[i],3,255-(timer1[i]*8.5),255,255,255);
		ObjEffect_SetScale(object1[i],scale1[i],scale1[i]);
		scale1[i]=scale1[i]+0.05;
		timer1[i]=timer1[i]+1;
		if(timer1[i]>=30){ Obj_Delete(object1[i]); }
		}
	i++;
	}
}


time++; frame++; 
if(usespell>0){ usespell--; } if(usespell<0){ usespell++; }
SetCommonData("Boss1X",GetX); SetCommonData("Boss1Y",GetY);

#include_function "script/Functions/SpellcardName.txt";
#include_function "script/Functions/HealthBar.txt";
#include_function "script/Functions/CutIn.txt";

if(GetLife==0 && dispelled==0){ CreateEnemyFromFile("script\Functions\dispel.txt",GetX,GetY,0,0,character); dispelled=1; }
}

@BackGround{
	if(bgfade<255){ bgfade+=5; }

	SetGraphicRect(0,0,300,300);
	SetGraphicScale(2,2);
	SetTexture(BG1);
	SetAlpha(bgfade);
	SetColor(0,255,0);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,0);
	DrawGraphic(cx,cy);

	SetTexture(BG2);
	SetGraphicAngle(0,0,sonarspin);
	DrawGraphic(cx,cy);
}

@DrawLoop{
	SetGraphicScale(1,1);
	SetTexture(GRboss);
	SetGraphicAngle(0,0,0);
	SetColor(255,255,255);
	SetRenderState(ALPHA);

	if(usespell>=1){ SetGraphicRect(384,outfit,512,outfit+128); }
	if(usespell<=-1){ SetGraphicRect(512,outfit,640,outfit+128); }
	if(usespell==0){
		if(GetSpeedX<=0.5 && GetSpeedX>=-0.5){ SetGraphicRect(0,outfit,128,outfit+128); }
		else if(GetSpeedX<-0.5){ SetGraphicRect(128,outfit,256,outfit+128); }
		else if(GetSpeedX>0.5){ SetGraphicRect(256,outfit,384,outfit+128); }
	}
	DrawGraphic(GetX,GetY);
}

@Finalize{
//	SetCommonData("Conversation",1);
	NewPointData(spellcardnumber,5);
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

}