script_enemy_main{

let survivalcirclesize=1;
let survivalcirclespin=1;

let shot1=0;
let bullet1=[];
let timer1=[];
let angle1=[];
let direction1=[];
let accel1=[];
let color1=[];
let radius1=[];

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

let effect3=0;
let object3=[];
let xscale1=0.01;
let yscale1=0.01;
let xscale2=0.01;
let yscale2=0.01;

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

let phase=0;
let resettimer=0;

let shoottime=100;

let character="Tomoko";
let cutin=character;
let dispelled=0;
let spellcards=2;
let spellcardnumber=41;
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 SElaserm4=("\script\SoundEffects\laserm4.wav");
let SEshots5=("\script\SoundEffects\shots5.wav");
let SEshotm2=("\script\SoundEffects\shotm2.wav");
let SEmagics5=("\script\SoundEffects\magics5.wav");
let SEcharge1=("script\SoundEffects\charge1.wav");

let BG1=("\script\Images\BackgroundLayers\Tomoko5.png");
let BG2=("\script\Images\BackgroundLayers\Tomoko1.png");
let BG3=("\script\Images\OtherEffects\SpellCircle1a.png");
let GRboss=("\script\Images\CharacterSprites\"~character~".png");

let EFwings=("\script\Images\OtherEffects\ButterflyWings.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\Shots"~character~"2.txt");
	
	LoadSE("\script\SoundEffects\laserm4.wav");
	LoadSE("\script\SoundEffects\shots5.wav");
	LoadSE("\script\SoundEffects\shotm2.wav");
	LoadSE("\script\SoundEffects\magics5.wav");
	LoadSE("script\SoundEffects\charge1.wav");

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

	LoadGraphic("\script\Images\CharacterSprites\"~character~".png");
	LoadGraphic("\script\Images\BackgroundLayers\Tomoko5.png");
	LoadGraphic("\script\Images\BackgroundLayers\Tomoko1.png");
	LoadGraphic("\script\Images\OtherEffects\SpellCircle1a.png");

	SetScore(500000);
	SetLife(9000);
	SetTimer(55);
	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);

	SetDurableSpellCard;
}
	
@MainLoop{

SetShotAutoDeleteClip(32,32,32,32);

let weaken=1;
if(GetCommonData("BombOn")==0){ damagerate=10; }
if(GetCommonData("BombOn")==1){ damagerate=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("[Evolution Of Danmaku] ("~difficulty~")",spellcardnumber); 
HealthBar();
Portrait(cutin,3);

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

if(time==60){
let angle=rand(0,360);
let nr=0;
let color=0;
	loop(5){
	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,GetPlayerX+50*cos(angle),GetPlayerY+50*sin(angle));
	Obj_SetAngle(shot1,0);
	Obj_SetSpeed(shot1,0);
	ObjShot_SetDelay(shot1,20);
	ObjShot_SetBombResist(shot1,true);
if(nr==0){ ObjShot_SetGraphic(shot1,37); color=1; } //Fire
if(nr==1){ ObjShot_SetGraphic(shot1,47); color=11; } //Metal
if(nr==2){ ObjShot_SetGraphic(shot1,42); color=7; } //Wood
if(nr==3){ ObjShot_SetGraphic(shot1,39); color=3; } //Earth
if(nr==4){ ObjShot_SetGraphic(shot1,48); color=12; } //Water
	bullet1=bullet1~[shot1];
	timer1=timer1~[shot1]; timer1[length(bullet1)-1]=0;
	angle1=angle1~[shot1]; angle1[length(bullet1)-1]=angle;
	direction1=direction1~[shot1]; direction1[length(bullet1)-1]=0;
	accel1=accel1~[shot1]; accel1[length(bullet1)-1]=0;
	color1=color1~[shot1]; color1[length(bullet1)-1]=color;
	radius1=radius1~[shot1]; radius1[length(bullet1)-1]=50;
	angle+=360/5;
	nr++;
	}
frame=0;
}

let i=0;
while(i<length(bullet1)){
	if(Obj_BeDeleted(bullet1[i])){
	bullet1=erase(bullet1,i); timer1=erase(timer1,i); angle1=erase(angle1,i);
	direction1=erase(direction1,i); accel1=erase(accel1,i); color1=erase(color1,i); radius1=erase(radius1,i);
	i--;
	}
	else{
		if(resettimer==1){ timer1[i]=0; }

		if(GetPlayerX<=minx+radius1[i]){ Obj_SetX(bullet1[i],minx+radius1[i]+radius1[i]*cos(angle1[i])); }
		else if(GetPlayerX>=maxx-radius1[i]){ Obj_SetX(bullet1[i],maxx-radius1[i]+radius1[i]*cos(angle1[i])); }
		else{ Obj_SetX(bullet1[i],GetPlayerX+radius1[i]*cos(angle1[i])); }

		if(GetPlayerY<=miny+radius1[i]){ Obj_SetY(bullet1[i],miny+radius1[i]+radius1[i]*sin(angle1[i])); }
		else if(GetPlayerY>=maxy-radius1[i]){ Obj_SetY(bullet1[i],maxy-radius1[i]+radius1[i]*sin(angle1[i])); }
		else{ Obj_SetY(bullet1[i],GetPlayerY+radius1[i]*sin(angle1[i])); }

		Obj_SetAngle(bullet1[i],angle1[i]+direction1[i]);
//phase=3;

if(phase==1){
	if(accel1[i]<3){ accel1[i]=accel1[i]+0.03; }
	angle1[i]=angle1[i]+accel1[i];
	if(direction1[i]<90){ direction1[i]=direction1[i]+1; }
	if(radius1[i]<100){ radius1[i]=radius1[i]+1; }

	if(timer1[i]%120==0 && timer1[i]>=60){
	let angle=rand(0,360);
		loop(15){
		CreateShot01(Obj_GetX(bullet1[i]),Obj_GetY(bullet1[i]),0.7,angle1[i]+direction1[i]+angle,156+color1[i],10);
		angle+=360/15;
		}
	PlaySE(SEshots5);
	}
} //Phase 1

//===========================================================

if(phase==2){
	if(accel1[i]<6 && timer1[i]>=90){ accel1[i]=accel1[i]+0.01; }
	if(direction1[i]<180){ direction1[i]=direction1[i]+2; }
	angle1[i]=angle1[i]+accel1[i];
	if(radius1[i]<90){ radius1[i]=radius1[i]+0.1; }

	if(timer1[i]%6==0 && frame>=90){
		let shotx=0;
		CreateShotA(shotx,Obj_GetX(bullet1[i]),Obj_GetY(bullet1[i]),10);
		SetShotDataA(shotx,0,0.35,rand(0,360),0,0,0,94+color1[i]);
		SetShotKillTime(shotx,320);
		FireShot(shotx);
	if(timer1[i]%12==0){ PlaySE(SEmagics5); }
	}
} //Phase 4

//===========================================================

if(phase==3){
	if(accel1[i]>0.5 && timer1[i]<90){ accel1[i]=accel1[i]-0.05; }
	if(accel1[i]<6.5 && timer1[i]>=90){ accel1[i]=accel1[i]+0.01; }
	if(direction1[i]>0){ direction1[i]=direction1[i]-2; }
	angle1[i]=angle1[i]+accel1[i];
	if(radius1[i]>80){ radius1[i]=radius1[i]-0.5; }

	if(timer1[i]%12==0 && timer1[i]>=90){
	shot2=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot2,Obj_GetX(bullet1[i]),Obj_GetY(bullet1[i]));
	Obj_SetAngle(shot2,angle1[i]+direction1[i]);
	Obj_SetSpeed(shot2,2.1);
	ObjShot_SetGraphic(shot2,168+color1[i]);
	ObjShot_SetDelay(shot2,10);
	bullet2=bullet2~[shot2];
	timer2=timer2~[shot2]; timer2[length(bullet2)-1]=0;
	bounce2=bounce2~[shot2]; bounce2[length(bullet2)-1]=0;
	PlaySE(SEshotm2);
	}
} //Phase 3

//===========================================================

if(phase>=4){
	if(accel1[i]>0.5){ accel1[i]=accel1[i]-0.01; }
	if(accel1[i]<0.5){ accel1[i]=accel1[i]+0.01; }
	if(direction1[i]>-180){ direction1[i]=direction1[i]-2; }
	angle1[i]=angle1[i]+accel1[i];
	if(radius1[i]<140 && timer1[i]<90){ radius1[i]=radius1[i]+0.75; }
	if(radius1[i]>40 && timer1[i]>=90){ radius1[i]=radius1[i]-0.25; }

	if(timer1[i]>=90 && frame==shoottime && GetTimer>0){
	let angle=atan2(GetPlayerY-Obj_GetY(bullet1[i]),GetPlayerX-Obj_GetX(bullet1[i]));
	angle=0;
		loop(7){
		shot3=(Obj_Create(OBJ_LASER));
		Obj_SetPosition(shot3,Obj_GetX(bullet1[i]),Obj_GetY(bullet1[i]));
		Obj_SetAngle(shot3,angle+(timer1[i]*2));
		Obj_SetSpeed(shot3,0);
		ObjShot_SetDelay(shot3,60);
		ObjLaser_SetLength(shot3,500);
		ObjLaser_SetWidth(shot3,10);
		ObjLaser_SetSource(shot3,false);
		ObjShot_SetGraphic(shot3,0+color1[i]);
		bullet3=bullet3~[shot3];
		timer3=timer3~[shot3]; timer3[length(bullet3)-1]=0;
		angle+=360/7;
		}
	PlaySE(SElaserm4);
	}
} //Phase 4

	timer1[i]=timer1[i]+1;
	}
i++;
}


let i=0;
while(i<length(bullet2)){
	if(Obj_BeDeleted(bullet2[i])){
	bullet2=erase(bullet2,i); timer2=erase(timer2,i); bounce2=erase(bounce2,i);
	i--;
	}
	else{
	let obj=bullet2[i];
		if(bounce2[i]<1){
			if(Obj_GetX(obj)<=minx){
			Obj_SetAngle(obj,180-Obj_GetAngle(obj));
			Obj_SetX(obj,Obj_GetX(obj)+1); bounce2[i]=bounce2[i]+1;
			}
			if(Obj_GetX(obj)>=maxx){
			Obj_SetAngle(obj,180-Obj_GetAngle(obj));
			Obj_SetX(obj,Obj_GetX(obj)-1); bounce2[i]=bounce2[i]+1;
			}
			if(Obj_GetY(obj)<=miny){
			Obj_SetAngle(obj,-Obj_GetAngle(obj));
			Obj_SetY(obj,Obj_GetY(obj)+1); bounce2[i]=bounce2[i]+1;
			}
			if(Obj_GetY(obj)>=maxy){
			Obj_SetAngle(obj,-Obj_GetAngle(obj));
			Obj_SetY(obj,Obj_GetY(obj)-1); bounce2[i]=bounce2[i]+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);
	i--;
	}
	else{

	if(timer3[i]>=80){ ObjShot_FadeDelete(bullet3[i]); }

	timer3[i]=timer3[i]+1;
	}
i++;
}

if(frame==shoottime && phase==4){ if(shoottime>65){ shoottime-=6; } frame=0; }

resettimer=0;
if(time==60){ resettimer=1; frame=0; }
if(time==850){ resettimer=1; frame=0; }
if(time==1640){ resettimer=1; frame=0; }
if(time==2430){ resettimer=1; frame=0; }

if(frame==0 && time>=60 && phase<4){
	if(time>60){
	SetColor(255,100,255);
	Concentration01(90);
	SetColor(255,255,255);
	PlaySE(SEcharge1);
	DeleteEnemyShot(SHOT);
	}
	phase++;
}


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

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(object3[0],GetX,GetY-30);
	ObjEffect_SetScale(object3[0],xscale1,yscale1);
	ObjEffect_SetVertexColor(object3[0],0,200,0,255,255); ObjEffect_SetVertexColor(object3[0],1,200,0,255,255);
	ObjEffect_SetVertexColor(object3[0],2,200,255,255,255); ObjEffect_SetVertexColor(object3[0],3,200,255,255,255);
	Obj_SetPosition(object3[1],GetX,GetY-30);
	ObjEffect_SetScale(object3[1],xscale2+0.1*cos(time*2),yscale2+0.2*cos(time*2));
	ObjEffect_SetVertexColor(object3[1],0,200,255,100,255); ObjEffect_SetVertexColor(object3[1],1,200,255,100,255);
	ObjEffect_SetVertexColor(object3[1],2,50,255,100,255); ObjEffect_SetVertexColor(object3[1],3,50,255,100,255);
}


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<150){ 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/2);
	SetGraphicScale(2,2);
	SetColor(bgfade,bgfade,bgfade);
	SetGraphicRect(0,0,350,350);
	SetGraphicAngle(0,0,time/3);
	DrawGraphic(cx,cy-70);

if(survivalcirclesize>0){
	SetGraphicRect(0,0,200,200);
	SetGraphicScale(survivalcirclesize*1.4,survivalcirclesize*1.4);
	SetTexture(BG3);
	SetAlpha(255/1.5);
	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(255/1.5);
	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{
//	SetCommonData("Conversation",1);
	NewPointData(spellcardnumber,GetCommonData("Difficulty"));
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

}