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 spin=rand_int(0,360);
let nr=1;
let angle=rand(0,360);
let anglephase=0;
let changephase=0;
let color=rand_int(1,10);
let shoottime=57;

let character="Tomoko";
let cutin=character;
let dispelled=0;
let spellcards=4;
let spellcardnumber=38;
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 SEshotb7=("script\SoundEffects\shotb7.wav");
let SEcharge1=("script\SoundEffects\charge1.wav");

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

let BG1=("\script\Images\BackgroundLayers\Tomoko6.png");
let BG2=("\script\Images\BackgroundLayers\Tomoko1.png");
let BG3=("\script\Images\OtherEffects\SpellCircle1a.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\shotb7.wav");

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

	LoadGraphic("\script\Images\CharacterSprites\Tomoko.png");
	LoadGraphic("\script\Images\BackgroundLayers\Tomoko6.png");
	LoadGraphic("\script\Images\BackgroundLayers\Tomoko1.png");
	LoadGraphic("\script\Images\OtherEffects\SpellCircle1a.png");
	
	SetScore(350000);
	SetLife(300);
	SetTimer(50);
	SetInvincibility(90);
	SetDamageRate(10,10);
	SetEnemyMarker(true);
	MagicCircle(true);
	#include_function "script/Functions/Focus.txt";
	Focus(character);
	SetEffectForZeroLife(60,100,1);
	SetMovePosition02(cx,miny+90,50);
	
	SetCommonData("ExtinctionPhase",0);
	SetCommonData("ExtinctionTime",0);
}
	
@MainLoop{

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

let weaken=1;
if(GetCommonData("BombOn")==0){ damagerate=10+(GetCommonData("ExtinctionPhase")*2); }
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("Natural Selection [Extinction] ("~difficulty~")",spellcardnumber); 
HealthBar();
Portrait(cutin,3);

if(time%120==0 && time>=60){
	if(GetPlayerX>minx+50 && GetPlayerX<maxx-50){
	SetMovePosition01(GetPlayerX+rand(-30,30),rand(miny+80,miny+100),1.5);
	}
	if(GetPlayerX<=minx+50){
	SetMovePosition01(GetPlayerX+rand(15,30),rand(miny+80,miny+100),1.5);
	}
	if(GetPlayerX>=maxx-50){
	SetMovePosition01(GetPlayerX-rand(15,30),rand(miny+80,miny+100),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(time<420 && time%60==0 && time>=60){
	CreateEnemyFromFile(GetCurrentScriptDirectory~"Tomoko - Extinction - Familiar.txt",GetX,GetY,0,0,[nr,spin]);
	nr++;
}

if(GetCommonData("ExtinctionTime")%440==0 && GetCommonData("ExtinctionPhase")<6 && time>=90){
	SetColor(255,100,255);
	Concentration01(90);
	SetColor(255,255,255);
	PlaySE(SEcharge1);

	changephase=80;
	shoottime-=5;
	frame=0;
	SetCommonData("ExtinctionPhase",GetCommonData("ExtinctionPhase")+1);
}

if(frame>=shoottime && time>=60 && changephase==0){
	let basesize=20;
	let loopsize=basesize+(GetCommonData("ExtinctionPhase")*3);
	let angle2=0;
	if(anglephase%2==0){ angle2=360/(loopsize*2); }
		loop(loopsize){
		CreateShot01(GetX,GetY,2+(GetCommonData("ExtinctionPhase")/5),angle+angle2,192+color,0);
		angle+=360/loopsize;
		}
	color-=1;
	if(color<1){ color=10; }
	PlaySE(SEshotb7);
	frame=0; 
	anglephase++;
}

if(changephase>0){ changephase--; frame=0; }


SetCommonData("ExtinctionTime",GetCommonData("ExtinctionTime")+1);

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<100){ bgfade+=5; }
	
	SetColor(bgfade,bgfade,bgfade);
	SetTexture(BG2);
	SetAlpha(bgfade);
	SetGraphicScale(2,2);
	SetGraphicRect(0,0,350,350);
	SetGraphicAngle(0,0,time/3);
	DrawGraphic(cx,cy-70);

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

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

}