script_enemy_main{

let phase=0;
let size1=10;
let size2=0;
let size3=0;
let lasercolor=rand_int(0,9);

let shot2=0;
let bullet2=[];
let timer2=[];
let angle2=[];

let shot3=0;
let bullet3=[];
let timer3=[];
let angle3=[];

let bigdial=0;
let bigdialpart=[];
let bigdialcog=[];
let bigdialtimer=[];
let dialfade=0;
let dialcolor=120;

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

let EFbigdial=("\script\Images\BackgroundLayers\Tomoko2a.png");
let EFbigdialcogs=("\script\Images\BackgroundLayers\Tomoko2b.png");

let SEsparkle=("script\SoundEffects\magics5.wav");
let SEtweet=("script\SoundEffects\magics1.wav");
let SEshots4=("script\SoundEffects\shots4.wav");
let SElaserb2=("script\SoundEffects\laserb2.wav");

let BG1=("\script\Images\BackgroundLayers\Tomoko1.png");
let GRboss=("\script\Images\CharacterSprites\Tomoko.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\ShotsTomoko1.txt");

	LoadSE("script\SoundEffects\magics5.wav");
	LoadSE("script\SoundEffects\magics1.wav");
	LoadSE("script\SoundEffects\shots4.wav");
	LoadSE("script\SoundEffects\laserb2.wav");
	
	LoadGraphic("\script\Images\BackgroundLayers\Tomoko2a.png");
	LoadGraphic("\script\Images\BackgroundLayers\Tomoko2b.png");
	LoadGraphic("\script\Images\CharacterSprites\Tomoko.png");

	SetScore(1500000);
	SetLife(900);
	SetTimer(73);
	SetInvincibility(120);
	SetDamageRate(10,10);
	SetEnemyMarker(true);
	MagicCircle(true);
	#include_function "script/Functions/Focus.txt";
	Focus(character);
	SetEffectForZeroLife(60,100,1);
	
	SetX(GetCommonData("Boss1X"));
	SetY(GetCommonData("Boss1Y"));
	SetMovePosition02(cx,miny+100,50);

	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<20){ 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("[Circle Of Life And Death]",spellcardnumber); 
HealthBar();
Portrait(cutin,2);

if(time==45){ CreateEnemyFromFile(("script\Functions\spellcardwarning.txt"),0,0,0,0,0); }


if(frame==360){
	if(GetPlayerX>minx+100 && GetPlayerX<maxx-100){
	SetMovePosition01(GetX+rand(-20,20),rand(miny+85,miny+115),0.5);
	}
	if(GetX<=minx+100){
	SetMovePosition01(GetX+rand(10,20),rand(miny+85,miny+115),0.5);
	}
	if(GetX>=maxx-100){
	SetMovePosition01(GetX-rand(10,20),rand(miny+85,miny+115),0.5);
	}
}


if(time==60){
let xsize=399;
let ysize=399;
let part=0;
let randomtime=0;
	loop(6){
	bigdial=(Obj_Create(OBJ_EFFECT));
	Obj_SetPosition(bigdial,cx,cy);
	ObjEffect_SetScale(bigdial,1,1);
	ObjEffect_SetLayer(bigdial,0); ObjEffect_SetTexture(bigdial,EFbigdial); ObjEffect_SetRenderState(bigdial,ALPHA);
	ObjEffect_SetPrimitiveType(bigdial,PRIMITIVE_TRIANGLESTRIP); ObjEffect_CreateVertex(bigdial,4);
	ObjEffect_SetVertexXY(bigdial,0,-(xsize/2),-(ysize/2)); ObjEffect_SetVertexUV(bigdial,0,0+part,0);
	ObjEffect_SetVertexXY(bigdial,1,xsize/2,-(ysize/2)); ObjEffect_SetVertexUV(bigdial,1,xsize+part,0);
	ObjEffect_SetVertexXY(bigdial,2,-(xsize/2),ysize/2); ObjEffect_SetVertexUV(bigdial,2,0+part,ysize);
	ObjEffect_SetVertexXY(bigdial,3,xsize/2,ysize/2); ObjEffect_SetVertexUV(bigdial,3,xsize+part,ysize);
	ObjEffect_SetVertexColor(bigdial,0,dialfade,dialcolor,dialcolor,dialcolor); ObjEffect_SetVertexColor(bigdial,1,dialfade,dialcolor,dialcolor,dialcolor);
	ObjEffect_SetVertexColor(bigdial,2,dialfade,dialcolor,dialcolor,dialcolor); ObjEffect_SetVertexColor(bigdial,3,dialfade,dialcolor,dialcolor,dialcolor);
	bigdialpart=bigdialpart~[bigdial];
	bigdialtimer=bigdialtimer~[bigdial];
	bigdialtimer[length(bigdialpart)-1]=randomtime;
	
if(part==0){
let xsize2=79;
let ysize2=79;
let size2=0;
let part2=0;
let dialfade=255;
let dialcolor=120;
	loop(12){
	bigdial=(Obj_Create(OBJ_EFFECT));
	if(part2==0){ Obj_SetPosition(bigdial,cx-90,cy-100); ObjEffect_SetScale(bigdial,1,1); }
	if(part2==1){ Obj_SetPosition(bigdial,cx-35,cy-80); ObjEffect_SetScale(bigdial,1,1); }
	if(part2==2){ Obj_SetPosition(bigdial,cx-55,cy-25); ObjEffect_SetScale(bigdial,1.5,1.5); }
	if(part2==3){ Obj_SetPosition(bigdial,cx+60,cy-50); ObjEffect_SetScale(bigdial,1.3,1.3); }
	if(part2==4){ Obj_SetPosition(bigdial,cx+15,cy-110); ObjEffect_SetScale(bigdial,1,1); }
	if(part2==5){ Obj_SetPosition(bigdial,cx+80,cy-10); ObjEffect_SetScale(bigdial,0.8,0.8); }
	if(part2==6){ Obj_SetPosition(bigdial,cx+45,cy+75); ObjEffect_SetScale(bigdial,0.7,0.7); }
	if(part2==7){ Obj_SetPosition(bigdial,cx+75,cy+48); ObjEffect_SetScale(bigdial,1,1); }
	if(part2==8){ Obj_SetPosition(bigdial,cx-120,cy); ObjEffect_SetScale(bigdial,0.7,0.7); }
	if(part2==9){ Obj_SetPosition(bigdial,cx-100,cy+30); ObjEffect_SetScale(bigdial,1,1); }
	if(part2==10){ Obj_SetPosition(bigdial,cx-45,cy+75); ObjEffect_SetScale(bigdial,1,1); }
	if(part2==11){ Obj_SetPosition(bigdial,cx-10,cy+100); ObjEffect_SetScale(bigdial,1,1); }
	ObjEffect_SetLayer(bigdial,0); ObjEffect_SetTexture(bigdial,EFbigdialcogs); ObjEffect_SetRenderState(bigdial,ALPHA);
	ObjEffect_SetPrimitiveType(bigdial,PRIMITIVE_TRIANGLESTRIP); ObjEffect_CreateVertex(bigdial,4);
	ObjEffect_SetVertexXY(bigdial,0,-(xsize2/2),-(ysize2/2)); ObjEffect_SetVertexUV(bigdial,0,0+size2,0);
	ObjEffect_SetVertexXY(bigdial,1,xsize2/2,-(ysize2/2)); ObjEffect_SetVertexUV(bigdial,1,xsize2+size2,0);
	ObjEffect_SetVertexXY(bigdial,2,-(xsize2/2),ysize2/2); ObjEffect_SetVertexUV(bigdial,2,0+size2,ysize2);
	ObjEffect_SetVertexXY(bigdial,3,xsize2/2,ysize2/2); ObjEffect_SetVertexUV(bigdial,3,xsize2+size2,ysize2);
	ObjEffect_SetVertexColor(bigdial,0,dialfade,dialcolor,dialcolor,dialcolor); ObjEffect_SetVertexColor(bigdial,1,dialfade,dialcolor,dialcolor,dialcolor);
	ObjEffect_SetVertexColor(bigdial,2,dialfade,dialcolor,dialcolor,dialcolor); ObjEffect_SetVertexColor(bigdial,3,dialfade,dialcolor,dialcolor,dialcolor);
	bigdialcog=bigdialcog~[bigdial];
	size2+=80;
	part2++;
	}
}
	part+=400;
	}
}

let i=0;
while(i<length(bigdialpart)){
	if(Obj_BeDeleted(bigdialpart[i])){
	bigdialpart=erase(bigdialpart,i); bigdialtimer=erase(bigdialtimer,i);
	i--;
	}
	else{
	ObjEffect_SetVertexColor(bigdialpart[i],0,dialfade,dialcolor,dialcolor,dialcolor);
	ObjEffect_SetVertexColor(bigdialpart[i],1,dialfade,dialcolor,dialcolor,dialcolor);
	ObjEffect_SetVertexColor(bigdialpart[i],2,dialfade,dialcolor,dialcolor,dialcolor);
	ObjEffect_SetVertexColor(bigdialpart[i],3,dialfade,dialcolor,dialcolor,dialcolor);
	
	ObjEffect_SetAngle(bigdialpart[3],0,0,-bigdialtimer[i]/40);
	ObjEffect_SetAngle(bigdialpart[4],0,0,bigdialtimer[i]/24);
	ObjEffect_SetAngle(bigdialpart[5],0,0,bigdialtimer[i]/2);
	if(GetLife>0){ bigdialtimer[i]=bigdialtimer[i]+2; }
	}
i++;
}
if(time>=60 && dialfade<255){ dialfade+=5; }


let i=0;
while(i<length(bigdialcog)){
	if(Obj_BeDeleted(bigdialcog[i])){
	bigdialcog=erase(bigdialcog,i);
	i--;
	}
	else{
	ObjEffect_SetAngle(bigdialcog[0],0,0,time);
	ObjEffect_SetAngle(bigdialcog[1],0,0,-time);
	ObjEffect_SetAngle(bigdialcog[2],0,0,time/2);
	ObjEffect_SetAngle(bigdialcog[3],0,0,time/1.5);
	ObjEffect_SetAngle(bigdialcog[4],0,0,time);
	ObjEffect_SetAngle(bigdialcog[5],0,0,-time);
	ObjEffect_SetAngle(bigdialcog[6],0,0,time*1.3);
	ObjEffect_SetAngle(bigdialcog[7],0,0,time);
	ObjEffect_SetAngle(bigdialcog[8],0,0,-time);
	ObjEffect_SetAngle(bigdialcog[9],0,0,-time/2);
	ObjEffect_SetAngle(bigdialcog[10],0,0,time);
	ObjEffect_SetAngle(bigdialcog[11],0,0,-time/2);
	
	ObjEffect_SetVertexColor(bigdialcog[i],0,dialfade,dialcolor,dialcolor,dialcolor);
	ObjEffect_SetVertexColor(bigdialcog[i],1,dialfade,dialcolor,dialcolor,dialcolor);
	ObjEffect_SetVertexColor(bigdialcog[i],2,dialfade,dialcolor,dialcolor,dialcolor);
	ObjEffect_SetVertexColor(bigdialcog[i],3,dialfade,dialcolor,dialcolor,dialcolor);
	}
i++;
}



if(frame>=270 && frame<300 && frame%5==0){ PlaySE(SEsparkle); }
if(frame==360){ phase+=1; size1+=2; size2+=1; size3+=5; lasercolor+=1; frame=60; }
if(lasercolor>=10){ lasercolor-=10; }


if(frame==60 && GetTimer>1){
let shot1=0; let shot2=1; let shot3=2;
let speed=0;
let angle=GetAngleToPlayer;
let newangle=0;
let color=0;
	loop(3){
	SetShotColor(255-color,255-color,255-color);
		loop(size1){
		CreateShotA(shot1,GetX+20,GetY-45,0);
		SetShotDataA(shot1,0,2.3+speed,angle+newangle,0,-0.03,0,9);
		SetShotKillTime(shot1,121);
		let random=rand_int(120,150);
			loop(2){
			CreateShotA(shot2,0,0,0);
			SetShotDataA(shot2,0,0,rand(0,360),rand(-2,2),0.01,3,22);
			SetShotDataA(shot2,90,NULL,NULL,0,0.01,3,22);
			SetShotDataA(shot2,random-20,NULL,NULL,0,0.01,3,24);
			SetShotKillTime(shot2,random+1);
				SetShotColor(255,255,255);
				let loopnr=rand_int(1,2);
				loop(loopnr){
				let angle2=rand(0,360);
				CreateShotA(shot3,0,0,0);
				SetShotDataA_XY(shot3,0,3*cos(angle2),rand(-1,-2),-0.03*cos(angle2),0.02,0,rand(1,1.5),93);
				AddShot(random,shot2,shot3,0);
				}
				SetShotColor(255-color,255-color,255-color);
			AddShot(90,shot1,shot2,0);
			}
		FireShot(shot1);
		angle+=360/size1;
		}
	speed+=0.5;
	newangle+=360/(size1*3);
	color+=60;
	}
usespell=60;
PlaySE(SEsparkle);
SetShotColor(255,255,255);
}


if(frame==150 && GetTimer>1){ PlaySE(SEtweet); }

if((time+120)%300==0 && length(bullet2)<12 && GetTimer>1){
let angle=time;
	loop(25+size2){
	shot2=(Obj_Create(OBJ_LASER));
	Obj_SetPosition(shot2,GetX,GetY);
	Obj_SetAngle(shot2,angle);
	Obj_SetSpeed(shot2,0);
	ObjLaser_SetLength(shot2,500);
	ObjLaser_SetWidth(shot2,15);
	ObjLaser_SetSource(shot2,false);
	ObjShot_SetDelay(shot2,60);
	ObjShot_SetGraphic(shot2,157+lasercolor);
	bullet2=bullet2~[shot2];
	timer2=timer2~[shot2];
	timer2[length(bullet2)-1]=0;
	angle2=angle2~[shot2];
	angle2[length(bullet2)-1]=angle;
	angle+=360/(25+size2);
	}
}
if((time+70)%300==0 && length(bullet2)<12 && GetTimer>1){ PlaySE(SElaserb2); }

if((time+120)%300==0 && length(bullet3)<12 && GetTimer>1){
let angle=time;
	loop(25+size2){
	shot3=(Obj_Create(OBJ_LASER));
	Obj_SetPosition(shot3,GetX,GetY);
	Obj_SetAngle(shot3,angle);
	Obj_SetSpeed(shot3,0);
	ObjLaser_SetLength(shot3,500);
	ObjLaser_SetWidth(shot3,15);
	ObjLaser_SetSource(shot3,false);
	ObjShot_SetDelay(shot3,60);
	ObjShot_SetGraphic(shot3,157+lasercolor);
	bullet3=bullet3~[shot3];
	timer3=timer3~[shot3];
	timer3[length(bullet3)-1]=0;
	angle3=angle3~[shot3];
	angle3[length(bullet3)-1]=angle;
	angle+=360/(25+size2);
	}
}
if((time+70)%300==0 && length(bullet3)<12 && GetTimer>1){ PlaySE(SElaserb2); }


let i=0;
while(i<length(bullet2)){
	if(Obj_BeDeleted(bullet2[i])){
	bullet2=erase(bullet2,i); timer2=erase(timer2,i); angle2=erase(angle2,i);
	i--;
	}
	else{
	Obj_SetPosition(bullet2[i],GetX+8*cos(angle2[i]),GetY+8*sin(angle2[i]));
	Obj_SetAngle(bullet2[i],angle2[i]);
	if((phase+0)%2==0){ Obj_Delete(bullet2[i]); }
	angle2[i]=angle2[i]+0.1;
	timer2[i]=timer2[i]+1;
	}
i++;
}
let i=0;
while(i<length(bullet3)){
	if(Obj_BeDeleted(bullet3[i])){
	bullet3=erase(bullet3,i); timer3=erase(timer3,i); angle3=erase(angle3,i);
	i--;
	}
	else{
	Obj_SetPosition(bullet3[i],GetX+8*cos(angle3[i]),GetY+8*sin(angle3[i]));
	Obj_SetAngle(bullet3[i],angle3[i]);
	if((phase+1)%2==0){ Obj_Delete(bullet3[i]); }
	angle3[i]=angle3[i]-0.1;
	timer3[i]=timer3[i]+1;
	}
i++;
}
	

if(frame>=(200-size3) && frame<300 && time%2==0 && GetTimer>1){
usespell=-20;
let angle=0;
if((phase+0)%2==0){ angle=time*1.3; }
if((phase+1)%2==0){ angle=-time*1.3; }
	loop(8){
	let shotx=0;
	let minspeed=rand(0.6,1);
	let color=rand(220,120);
	SetShotColor(255,color,color);
	CreateShotA(shotx,GetX,GetY,10);
	SetShotDataA(shotx,0,rand(5,7),angle+rand(-3,3),rand(-1,1),-0.09,minspeed,227);
	SetShotDataA(shotx,120,NULL,NULL,0,-0.06,minspeed,227);
	FireShot(shotx);
	SetShotColor(255,255,255);
	angle+=360/8;
	}
if(time%4==0){ PlaySE(SEshots4); }
}


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

@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("NightmareUnlocked",true);
	SaveCommonData;

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

}