script_enemy_main{

let survivalcirclesize=1;
let survivalcirclespin=1;

let random=rand_int(1,6);
let shuffle=1;
let random1=0;
let random2=0;
let shufflespeed=20;
let shuffleamount=7;
let loc=[0,0,0];
let safe=0;
let safeon=0;
let start=0;

let effect2=0;
let object2=[];
let timer3=[];
let fade2=[];

let shot1=0;
let bullet1=[];
let timer1=[];
let movetime1=[];
let movecounter1=[];
let radius1=[];
let accel1=[];
let dest1x=[];
let ypos1=[];
let shufflecount1=[];
let correct1=[];
let timer2=[];

let character="Tomomi";
let cutin=character;
let dispelled=0;
let spellcards=1;
let spellcardnumber=74;
let damagerate=10;
let outfit=1;
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 SEmagics1=("script\SoundEffects\magics1.wav");
let SEreflect=("script\SoundEffects\reflect1.wav");
let SEshots4=("script\SoundEffects\shots4.wav");
let SEshotb1=("script\SoundEffects\shotb1.wav");

let EFsafespot=("\script\Images\OtherEffects\SafeCircle3.png");

let BG1=("\script\Images\BackgroundLayers\Tomomi3.png");
let BG3=("\script\Images\OtherEffects\SpellCircle1a.png");
let GRboss=("\script\Images\CharacterSprites\Tomomi.png");

#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\ShotsTomomi1.txt");

	LoadGraphic("\script\Images\BackgroundLayers\Tomomi3.png");
	LoadGraphic("\script\Images\OtherEffects\SafeCircle3.png");
	LoadGraphic("\script\Images\OtherEffects\SpellCircle1a.png");
	
	LoadGraphic("\script\Images\CharacterSprites\Tomomi.png");

	LoadSE("script\SoundEffects\magics1.wav");
	LoadSE("script\SoundEffects\reflect1.wav");
	LoadSE("script\SoundEffects\shots4.wav");
	LoadSE("script\SoundEffects\shotb1.wav");
	
	SetScore(2500000);
	SetLife(10000);
	SetTimer(79);
	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);

	SetDurableSpellCard;
}
	
@MainLoop{

SetShotAutoDeleteClip(32,32,32,32);

Weakness(character);
#include_function "script/Functions/Weakness.txt";
if(GetCommonData("BombOn")==0){ damagerate=10; }
if(GetCommonData("BombOn")==1){ damagerate=4; }
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("[Three Card Monte]",spellcardnumber); 
HealthBar();
Portrait(cutin,1);

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


if(frame==90 && GetTimer>2){
let xpos=-80;
let random=rand_int(0,2);
let shots=0;
	loop(3){
	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,cx+xpos,miny+140);
	Obj_SetAngle(shot1,0);
	Obj_SetSpeed(shot1,0);
	Obj_SetAutoDelete(shot1,false);
	ObjShot_SetDelay(shot1,20);
	ObjShot_SetBombResist(shot1,true);
	if(shots!=random){ ObjShot_SetGraphic(shot1,169); }
	if(shots==random){ ObjShot_SetGraphic(shot1,171); }
	bullet1=bullet1~[shot1];
	timer1=timer1~[shot1];
	timer1[length(bullet1)-1]=0;
	movetime1=movetime1~[shot1];
	movetime1[length(bullet1)-1]=60;
	movecounter1=movecounter1~[shot1];
	movecounter1[length(bullet1)-1]=0;
	radius1=radius1~[shot1];
	radius1[length(bullet1)-1]=0;
	accel1=accel1~[shot1];
	accel1[length(bullet1)-1]=0;
	dest1x=dest1x~[shot1];
	dest1x[length(bullet1)-1]=0;
	ypos1=ypos1~[shot1];
	ypos1[length(bullet1)-1]=0;
	shufflecount1=shufflecount1~[shot1];
	shufflecount1[length(bullet1)-1]=0;
	correct1=correct1~[shot1];
	correct1[length(bullet1)-1]=random;
	timer2=timer2~[shot1];
	timer2[length(bullet1)-1]=0;
	xpos+=80;
	shots++;
	}
usespell=-30;
PlaySE(SEmagics1);
}
if(time==60){
let xpos=-80;
let xsize=159;
let ysize=159;
	loop(3){
	effect2=(Obj_Create(OBJ_EFFECT));
	Obj_SetPosition(effect2,cx+xpos,maxy-100);
	ObjEffect_SetScale(effect2,0.5,0.5);
	ObjEffect_SetLayer(effect2,3); ObjEffect_SetTexture(effect2,EFsafespot); 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,100,255,255,255); ObjEffect_SetVertexColor(effect2,1,100,255,255,255);
	ObjEffect_SetVertexColor(effect2,2,100,255,255,255); ObjEffect_SetVertexColor(effect2,3,100,255,255,255);
	object2=object2~[effect2];
	timer3=timer3~[effect2];
	timer3[length(object2)-1]=0;
	fade2=fade2~[effect2];
	fade2[length(object2)-1]=0;
	xpos+=80;
	}
}


let i=0;
while(i<length(bullet1)){
	if(Obj_BeDeleted(bullet1[i])){
	bullet1=erase(bullet1,i); timer1=erase(timer1,i); movetime1=erase(movetime1,i); movecounter1=erase(movecounter1,i);
	radius1=erase(radius1,i); accel1=erase(accel1,i); dest1x=erase(dest1x,i); ypos1=erase(ypos1,i);
	shufflecount1=erase(shufflecount1,i); correct1=erase(correct1,i); timer2=erase(timer2,i);
	i--;
	}
	else{
	if(timer1[i]==60){ ObjShot_SetGraphic(bullet1[i],169); }

	if(movecounter1[i]==movetime1[i] && shufflecount1[i]<shuffleamount){
		loc=[0,0,0];
		random1=rand_int(0,2);
		random2=random1+rand_int(1,2);
		if(random2>2){ random2-=2; }

		dest1x[i]=cx+(dest1x[i]*80);
		movecounter1[i]=0;
		movetime1[i]=shufflespeed;
		Obj_SetAngle(bullet1[i],atan2((miny+150)-Obj_GetY(bullet1[i]),dest1x[i]-Obj_GetX(bullet1[i])));
		accel1[i]=0;
		radius1[i]=(((dest1x[i]-Obj_GetX(bullet1[i]))^2+((miny+150)-Obj_GetY(bullet1[i]))^2)^0.5)/movetime1[i]*(pi/2);
		PlaySE(SEshots4);
	}

	if(movecounter1[i]<movetime1[i]){
		accel1[i]=accel1[i]+(180/movetime1[i]);
		Obj_SetSpeed(bullet1[i],radius1[i]*sin(accel1[i]));
		Obj_SetY(bullet1[random1],Obj_GetY(bullet1[random1])+1.5*cos(ypos1[random1]));
		Obj_SetY(bullet1[random2],Obj_GetY(bullet1[random2])-1.5*cos(ypos1[random2]));
		movecounter1[i]=movecounter1[i]+1;
		ypos1[i]=ypos1[i]+180/movetime1[i];
	}

	if(movecounter1[i]==movetime1[i] && shufflecount1[i]<shuffleamount){ shufflecount1[i]=shufflecount1[i]+1; shuffle=1; }
		if(shufflecount1[i]==shuffleamount){
		timer2[i]=timer2[i]+1;
			if(timer2[i]==30){
				if(correct1[i]==i){ safe=Obj_GetX(bullet1[i]); }
			loc[i]=Obj_GetX(bullet1[i]);
			Obj_Delete(bullet1[i]);
			frame=45;
			random1=0;
			random2=0;
			}
		}
	timer1[i]=timer1[i]+1;
	}
i++;
}

if(shuffle==1){
random=random+rand_int(1,5);
if(random>6){ random-=6; }
	if(random==1){ dest1x=[-1,0,1]; }
	if(random==2){ dest1x=[-1,1,0]; }
	if(random==3){ dest1x=[0,-1,1]; }
	if(random==4){ dest1x=[0,1,-1]; }
	if(random==5){ dest1x=[1,0,-1]; }
	if(random==6){ dest1x=[1,-1,0]; }
shuffle=0;
}



if(frame==45 && time>=60){
let i=0;
	loop(3){
	let angle=rand(0,360);
		loop(180){
		CreateShot01(loc[i],miny+150,6,angle,68,0);
		angle+=360/180;
		}
	i++;
	}
shuffleamount+=2;
if(shufflespeed>14){ shufflespeed--; }
PlaySE(SEshotb1);
usespell=30;
}

if(frame==70 && time>70){ start=1; safeon=1; }
if(frame>=70 && frame<80 && time%3==0 && time>90){ PlaySE(SEreflect); }
if(frame==140){ safeon=0; }


let i=0;
while(i<length(object2)){
	if(Obj_BeDeleted(object2[i])){
	object2=erase(object2,i); timer3=erase(timer3,i); fade2=erase(fade2,i);
	i--;
	}
	else{
let safenr=4;
if(start==1){
if(safe<cx-10){ safenr=0; }
if(safe>cx-10 && safe<cx+10){ safenr=1; }
if(safe>cx+10){ safenr=2; }
}
	if(i!=safenr){ fade2[i]=100; }
	if(i==safenr && safeon==1){ if(fade2[i]<255){ fade2[i]=fade2[i]+15; } }
	if(i==safenr && safeon==0){ if(fade2[i]>100){ fade2[i]=fade2[i]-5; } }
	ObjEffect_SetVertexColor(object2[i],0,fade2[i],255,255,255); ObjEffect_SetVertexColor(object2[i],1,fade2[i],255,255,255);
	ObjEffect_SetVertexColor(object2[i],2,fade2[i],255,255,255); ObjEffect_SetVertexColor(object2[i],3,fade2[i],255,255,255);
	DeleteEnemyShotImmediatelyInCircle(ALL,safe,Obj_GetY(object2[i]),40);

	ObjEffect_SetAngle(object2[i],0,0,time*10);
	timer3[i]=timer3[i]+1;
	}
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(3,3);
	SetTexture(BG1);
	SetAlpha(bgfade/2);
	SetRenderState(ALPHA);
	SetColor(120+80*cos((time+240)/3),120+80*cos((time+120)/3),120+80*cos((time+0)/3));
	SetGraphicAngle(0,0,360*cos(time/10));
	DrawGraphic(cx,cy-150);
	SetGraphicAngle(0,0,-360*cos((time+180)/10));
	DrawGraphic(cx,cy+150);

if(survivalcirclesize>0){
	SetGraphicRect(0,0,200,200);
	SetGraphicScale(survivalcirclesize*1.4,survivalcirclesize*1.4);
	SetTexture(BG3);
	SetAlpha(bgfade/2);
	SetColor(70+60*cos((time+0)/2),70+60*cos((time+120)/2),70+60*cos((time+240)/2));
	SetGraphicAngle(0,0,survivalcirclespin*1.2);
	DrawGraphic(cx,cy);
	
	SetGraphicRect(0,0,200,200);
	SetGraphicScale(survivalcirclesize*2,survivalcirclesize*2);
	SetTexture(BG3);
	SetAlpha(bgfade/2);
	SetColor(170+60*cos((time+0)/2),170+60*cos((time+120)/2),170+60*cos((time+240)/2));
	SetGraphicAngle(0,0,-survivalcirclespin*0.7);
	DrawGraphic(cx,cy);
}
survivalcirclespin++;
if(GetLife<=0 && survivalcirclesize>0){ survivalcirclesize-=0.03; }

}

@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{
	if(GotSpellCardBonus){ SetCommonData("LastTrumpCard",7); }
	NewPointData(spellcardnumber,7);
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

}