script_enemy_main{

let effect1=0;
let object1=[];
let xscale1=0.01;
let yscale1=0.01;
let xscale2=0.01;
let yscale2=0.01;

let colorx=0;
let angle0=rand(0,360);
let shoottime=0;
let anglez=rand(0,360);

let color=rand_int(1,10);
let shot2=0;
let bullet2=[];
let timer2=[];
let splittime2=[];
let color2=[];

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

let character="Tomoko";
let cutin=character;
let dispelled=0;
let spellcards=5;
let spellcardnumber=37;
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 SEshots5=("script\SoundEffects\shots5.wav");
let SEshotm2=("script\SoundEffects\shotm2.wav");
let SEshotb7=("\script\SoundEffects\shotb7.wav");
let SEmagics1=("script\SoundEffects\magics1.wav");
let SEmagics5=("script\SoundEffects\magics5.wav");
let SEcharge1=("script\SoundEffects\charge1.wav");

let EFwings=("\script\Images\OtherEffects\ButterflyWings.png");

let BG1=("\script\Images\BackgroundLayers\Tomoko5.png");
let BG2=("\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\shots5.wav");
	LoadSE("script\SoundEffects\shotm2.wav");
	LoadSE("\script\SoundEffects\shotb7.wav");
	LoadSE("script\SoundEffects\magics1.wav");
	LoadSE("script\SoundEffects\magics5.wav");
	LoadSE("script\SoundEffects\charge1.wav");

	LoadGraphic("\script\Images\OtherEffects\ButterflyWings.png");

	LoadGraphic("\script\Images\BackgroundLayers\Tomoko5.png");
	LoadGraphic("\script\Images\BackgroundLayers\Tomoko1.png");
	LoadGraphic("\script\Images\CharacterSprites\Tomoko.png");

	SetScore(350000);
	SetLife(500);
	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+100,50);
}
	
@MainLoop{

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

let weaken=1;
if(GetCommonData("BombOn")==0){ damagerate=10; if(GetTimer<30){ damagerate=20; } }
if(GetCommonData("BombOn")==1){ damagerate=8; }
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("Evolutionary Variation [Genetic Drift] ("~difficulty~")",spellcardnumber); 
HealthBar();
Portrait(cutin,3);

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

if(time==60){
	loop(2){
	effect1=(Obj_Create(OBJ_EFFECT));
	Obj_SetPosition(effect1,GetX,GetY);
	ObjEffect_SetScale(effect1,xscale1,yscale1);
	ObjEffect_SetLayer(effect1,1); ObjEffect_SetTexture(effect1,EFwings); ObjEffect_SetRenderState(effect1,ADD);
	ObjEffect_SetPrimitiveType(effect1,PRIMITIVE_TRIANGLESTRIP); ObjEffect_CreateVertex(effect1,4);
	ObjEffect_SetVertexXY(effect1,0,-190,-100); ObjEffect_SetVertexUV(effect1,0,0,0);
	ObjEffect_SetVertexXY(effect1,1,190,-100); ObjEffect_SetVertexUV(effect1,1,380,0);
	ObjEffect_SetVertexXY(effect1,2,-190,100); ObjEffect_SetVertexUV(effect1,2,0,200);
	ObjEffect_SetVertexXY(effect1,3,190,100); ObjEffect_SetVertexUV(effect1,3,380,200);
	ObjEffect_SetVertexColor(effect1,0,180,255,255,255); ObjEffect_SetVertexColor(effect1,1,180,255,255,255);
	ObjEffect_SetVertexColor(effect1,2,180,255,255,255); ObjEffect_SetVertexColor(effect1,3,180,255,255,255);
	object1=object1~[effect1];
	}
}

if(time>=60){
	if(xscale1<1){ xscale1+=(xscale1*0.1); }
	if(yscale1<1){ yscale1+=(yscale1*0.2); }
	if(xscale2<1.1){ xscale2+=(xscale2*0.09); }
	if(yscale2<1.2){ yscale2+=(yscale2*0.18); }
	Obj_SetPosition(object1[0],GetX,GetY-30);
	ObjEffect_SetScale(object1[0],xscale1,yscale1);
	ObjEffect_SetVertexColor(object1[0],0,200,0,255,255); ObjEffect_SetVertexColor(object1[0],1,200,0,255,255);
	ObjEffect_SetVertexColor(object1[0],2,200,255,255,255); ObjEffect_SetVertexColor(object1[0],3,200,255,255,255);
	Obj_SetPosition(object1[1],GetX,GetY-30);
	ObjEffect_SetScale(object1[1],xscale2+0.1*cos(time*2),yscale2+0.2*cos(time*2));
	ObjEffect_SetVertexColor(object1[1],0,200,255,100,255); ObjEffect_SetVertexColor(object1[1],1,200,255,100,255);
	ObjEffect_SetVertexColor(object1[1],2,50,255,100,255); ObjEffect_SetVertexColor(object1[1],3,50,255,100,255);
}


if(frame==130){
	SetColor(255,100,255);
	Concentration01(90);
	SetColor(255,255,255);
	PlaySE(SEcharge1);
}

if(frame==210){
let angle=rand(0,360);
let shot1=0;
	loop(12){
	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,GetX,GetY-10);
	Obj_SetAngle(shot1,angle);
	Obj_SetSpeed(shot1,2);
	Obj_SetAlpha(shot1,0);
	ObjShot_SetBombResist(shot1,true);
	Obj_SetCollisionToPlayer(shot1,false);
	ObjShot_SetGraphic(shot1,1);
	bullet1=bullet1~[shot1];
	timer1=timer1~[shot1]; timer1[length(bullet1)-1]=0;
	timer1x=timer1x~[shot1]; timer1x[length(bullet1)-1]=rand_int(0,360);
	size1=size1~[shot1]; size1[length(bullet1)-1]=0;
	speed1=speed1~[shot1]; speed1[length(bullet1)-1]=2;
	angle1=angle1~[shot1]; angle1[length(bullet1)-1]=angle;
	angle+=360/12;
	}
usespell=-30;
}

let i=0;
while(i<length(bullet1)){
	if(Obj_BeDeleted(bullet1[i])){
	bullet1=erase(bullet1,i); timer1=erase(timer1,i); timer1x=erase(timer1x,i); size1=erase(size1,i);
	speed1=erase(speed1,i); angle1=erase(angle1,i);
	i--;
	}
	else{
		if(time%1==0){
		let radius=0+size1[i]*cos(timer1x[i]);
		CreateShotA(shot1,Obj_GetX(bullet1[i])+radius*cos(Obj_GetAngle(bullet1[i])+90),Obj_GetY(bullet1[i])+radius*sin(Obj_GetAngle(bullet1[i])+90),10);
		SetShotDataA(shot1,0,0,Obj_GetAngle(bullet1[i])+180-45*cos(timer1x[i]),0,0,0,145);
		SetShotDataA(shot1,10,0,Obj_GetAngle(bullet1[i])+180-45*cos(timer1x[i]),0,0.01,0.5,131);
		SetShotKillTime(shot1,60);
		FireShot(shot1);
		CreateShotA(shot1,Obj_GetX(bullet1[i])+radius*cos(Obj_GetAngle(bullet1[i])-90),Obj_GetY(bullet1[i])+radius*sin(Obj_GetAngle(bullet1[i])-90),10);
		SetShotDataA(shot1,0,0,Obj_GetAngle(bullet1[i])+180+45*cos(timer1x[i]),0,0,0,152);
		SetShotDataA(shot1,10,0,Obj_GetAngle(bullet1[i])+180+45*cos(timer1x[i]),0,0.01,0.5,138);
		SetShotKillTime(shot1,60);
		FireShot(shot1);
		}
	if(time%6==0){ PlaySE(SEshots5); }

	if(size1[i]<50){ size1[i]=size1[i]+0.4; }

	timer1[i]=timer1[i]+1;
	timer1x[i]=timer1x[i]+speed1[i]*2;
	}
i++;
}

if(frame>=400 && length(bullet2)>0 && time%15==0){
	CreateShot02(GetX,GetY,5,anglez,-0.1,2,rand_int(119,121),0);
	anglez+=rand(30,40);
	PlaySE(SEshotb7);
	usespell=-15;
}

if(frame>=60 && frame<150 && frame%5==0 && length(bullet2)<=60){
	shot2=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot2,GetX,GetY-10);
	Obj_SetAngle(shot2,angle0);
	Obj_SetSpeed(shot2,0);
	ObjShot_SetDelay(shot2,15);
	ObjShot_SetBombResist(shot2,true);
	ObjShot_SetGraphic(shot2,color);
	bullet2=bullet2~[shot2];
	timer2=timer2~[shot2];
	timer2[length(bullet2)-1]=0;
	splittime2=splittime2~[shot2];
	splittime2[length(bullet2)-1]=rand_int(150,210);
	color2=color2~[shot2];
	color2[length(bullet2)-1]=color;
angle0+=rand(90,180);
usespell=-20;
PlaySE(SEmagics5);
}

if(time>=60){
let i=0;
	while(i<length(bullet2)){
		if(Obj_BeDeleted(bullet2[i])){
		bullet2=erase(bullet2,i); timer2=erase(timer2,i); splittime2=erase(splittime2,i); color2=erase(color2,i);
		i--;
		}
		else{
		let obj=bullet2[i];
		if(Obj_GetX(obj)>=maxx+16 || Obj_GetY(obj)>=maxy+16 || Obj_GetX(obj)<=minx-16 || Obj_GetY(obj)<=miny-16){ Obj_Delete(bullet2[i]); }

		if(timer2[i]<splittime2[i]){ if(Obj_GetSpeed(bullet2[i])<1){ Obj_SetSpeed(bullet2[i],Obj_GetSpeed(bullet2[i])+0.015); } }
		if(timer2[i]>=splittime2[i]){ if(Obj_GetSpeed(bullet2[i])>0){ Obj_SetSpeed(bullet2[i],Obj_GetSpeed(bullet2[i])-0.015); } }
		if(timer2[i]==splittime2[i]+60){

		loop(rand_int(2,4)){
		let color=color2[i]+rand_int(-1,1);
		if(color>10){ color=1; }
		if(color<1){ color=10; }
			shot2=(Obj_Create(OBJ_SHOT));
			Obj_SetPosition(shot2,Obj_GetX(bullet2[i]),Obj_GetY(bullet2[i]));
			Obj_SetAngle(shot2,Obj_GetAngle(bullet2[i])+rand(-160,160));
			Obj_SetSpeed(shot2,0);
			ObjShot_SetDelay(shot2,15);
			ObjShot_SetBombResist(shot2,true);
			ObjShot_SetGraphic(shot2,color);
			bullet2=bullet2~[shot2];
			timer2=timer2~[shot2];
			timer2[length(bullet2)-1]=0;
			splittime2=splittime2~[shot2];
			splittime2[length(bullet2)-1]=rand_int(150,210);
			color2=color2~[shot2];
			color2[length(bullet2)-1]=color;
		}
		if(shoottime<=0){ PlaySE(SEshotm2); shoottime=5; }

		let shot2=0;
		let angle=atan2(GetPlayerY-Obj_GetY(bullet2[i]),GetPlayerX-Obj_GetX(bullet2[i]));

			loop(4){
			CreateShotA(shot2,Obj_GetX(bullet2[i]),Obj_GetY(bullet2[i]),0);
			SetShotDataA(shot2,0,0,angle,0,0.01,1,106+color2[i]);
			FireShot(shot2);
			angle+=360/4;
			}

			Obj_Delete(bullet2[i]);
			}
		timer2[i]=timer2[i]+1;
		}
	i++;
	}
}
if(shoottime>0){ shoottime--; }

if(length(bullet2)>60){
let i=0;
DeleteEnemyShot(SHOT);
	while(i<length(bullet2)){

	let shot2=0;
	let angle=atan2(GetPlayerY-Obj_GetY(bullet2[i]),GetPlayerX-Obj_GetX(bullet2[i]));
	SetShotColor(255,200,200);
		loop(3){
		CreateShotA(shot2,Obj_GetX(bullet2[i]),Obj_GetY(bullet2[i]),20);
		SetShotDataA(shot2,0,0,angle,0,0.03,0.5,191);
		SetShotDataA(shot2,45,NULL,angle+rand(-3,3),0,0.015,rand(1.5,2),191);
		FireShot(shot2);
		angle+=360/3;
		}
	SetShotColor(255,255,255);

	ObjShot_FadeDelete(bullet2[i]);
	bullet2=erase(bullet2,i); timer2=erase(timer2,i); splittime2=erase(splittime2,i); color2=erase(color2,i);
	}
PlaySE(SEmagics1);
usespell=-30;
}
if(length(bullet2)==0 && frame>=60){
	color=rand_int(1,10);
	colorx=0;
	frame=0;
}


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

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

	SetGraphicRect(0,0,250,250);
	SetGraphicScale(2,2);
	SetColor(bgfade,bgfade,bgfade);
	SetTexture(BG1);
	SetAlpha(255);
	SetColor(150+30*cos((time/2)),150+30*cos((time/2)+120),150+30*cos((time/2)+240));
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,-time/2);
	DrawGraphic(cx,cy);
		
	SetTexture(BG2);
	SetAlpha(bgfade);
	SetGraphicScale(2,2);
	SetColor(bgfade,bgfade,bgfade);
	SetGraphicRect(0,0,350,350);
	SetGraphicAngle(0,0,time/3);
	DrawGraphic(cx,cy-70);
}

@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,GetCommonData("Difficulty"));
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

}