script_enemy_main{

let bgscale=1;
let bgchangefade=180;
let bgchange=0;
let newbg=1;

let survivalcirclesize=1;
let survivalcirclespin=1;

let effect1=0;
let object1=[];
let timer1=[];
let angle1=[];
let accel1=[];
let radius1=[];
let movetime1=[];
let xpos1=[];
let ypos1=[];

let phase=1;
let arrayphase=0;
let movetime=0;

let array=[
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
];

let character="Masahiro";
let cutin=character;
let dispelled=0;
let spellcards=1;
let spellcardnumber=67;
let damagerate=10;
let outfit=1;
outfit=(128*outfit)-128;
let usespell=0;
let bgfade=255;
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 SEfocus1=("\script\SoundEffects\focus1.wav");
let SEshots3=("\script\SoundEffects\shots3.wav");
let SEshots5=("\script\SoundEffects\shots5.wav");
let SEshots7=("\script\SoundEffects\shots7.wav");
let SEshotm2=("\script\SoundEffects\shotm2.wav");
let SElaserm4=("\script\SoundEffects\laserm4.wav");
let SElaserb2=("\script\SoundEffects\laserb2.wav");

let GReffect=("\script\Images\OtherEffects\SpellCircle1a.png");

let BG1=("\script\Images\BackgroundLayers\Nightmare1.png");
let BG2=("\script\Images\BackgroundLayers\Nightmare2.png");
let BG3=("\script\Images\BackgroundLayers\Nightmare3.png");
let BG4=("\script\Images\BackgroundLayers\Nightmare4a.png");
let BG5=("\script\Images\BackgroundLayers\Nightmare6.png");

let BG6=("\script\Images\OtherEffects\SpellCircle1a.png");
let GRboss=("\script\Images\CharacterSprites\"~character~".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\ShotsNightmare1.txt");

	LoadSE("\script\SoundEffects\focus1.wav");
	LoadSE("\script\SoundEffects\shots3.wav");
	LoadSE("\script\SoundEffects\shots5.wav");
	LoadSE("\script\SoundEffects\shots7.wav");
	LoadSE("\script\SoundEffects\shotm2.wav");
	LoadSE("\script\SoundEffects\laserm4.wav");
	LoadSE("\script\SoundEffects\laserb2.wav");

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

	LoadGraphic("\script\Images\CharacterSprites\"~character~".png");
	LoadGraphic("\script\Images\BackgroundLayers\Nightmare1.png");
	LoadGraphic("\script\Images\BackgroundLayers\Nightmare2.png");
	LoadGraphic("\script\Images\BackgroundLayers\Nightmare3.png");
	LoadGraphic("\script\Images\BackgroundLayers\Nightmare4a.png");
	LoadGraphic("\script\Images\BackgroundLayers\Nightmare6.png");

	SetScore(2000000);
	SetLife(9999);
	SetTimer(101);
	SetInvincibility(120);
	SetDamageRate(10,10);
	SetEnemyMarker(true);
	MagicCircle(true);
	#include_function "script/Functions/Focus.txt";
	Focus(character);
	SetEffectForZeroLife(120,180,1);
	SetMovePosition02(cx,miny+100,50);

	SetDurableSpellCard;
}
	
@MainLoop{

SetShotAutoDeleteClip(64,64,64,64);

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("Parasomnia [Night Terror]",spellcardnumber); 
HealthBar();
Portrait(cutin,1);

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==45){ CreateEnemyFromFile(("script\Functions\spellcardwarning.txt"),0,0,0,0,0); }


if(phase==1){
array=[
0,3,1,0,0,0,1,3,0,
2,0,0,0,0,0,0,0,2,
0,0,0,0,3,0,0,0,0,
0,0,0,1,0,1,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,2,0,0,0,0,0,2,0,
1,0,0,0,0,0,0,0,1
];
movetime=300;
}

if(phase==2){
array=[
2,0,0,0,1,0,0,0,2,
0,1,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
1,0,0,0,3,0,0,0,1,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,1,0,
2,0,0,0,1,0,0,0,2,
];
movetime=60;
}

if(phase==3){
array=[
1,3,2,3,1,3,2,3,1,
3,0,0,0,0,0,0,0,3,
2,0,0,0,0,0,0,0,2,
3,0,0,0,0,0,0,0,3,
1,0,0,0,0,0,0,0,1,
3,0,0,0,0,0,0,0,3,
2,0,0,0,0,0,0,0,2,
3,0,0,0,0,0,0,0,3,
1,3,2,3,1,3,2,3,1
];
movetime=120;
}

if(phase==4){
array=[
0,0,4,0,1,0,4,0,0,
0,2,0,0,0,0,0,2,0,
4,0,1,0,5,0,1,0,4,
0,0,0,3,0,3,0,0,0,
1,0,5,0,4,0,5,0,1,
0,0,0,3,0,3,0,0,0,
4,0,1,0,5,0,1,0,4,
0,2,0,0,0,0,0,2,0,
0,0,4,0,1,0,4,0,0
];
movetime=90;
}


if(frame==90 && GetTimer>0){
let i=0;
let xpos=minx;
let ypos=miny;
	loop(length(array)^0.5){
	xpos=minx;
		loop(length(array)^0.5){
			if(array[i]>0 && array[i]<=arrayphase){
			let xsize=199;
			let ysize=199;
			effect1=(Obj_Create(OBJ_EFFECT));
			Obj_SetPosition(effect1,GetX,GetY);
			Obj_SetAutoDelete(effect1,false);
			ObjEffect_SetScale(effect1,0.25,0.25);
			ObjEffect_SetLayer(effect1,3); ObjEffect_SetTexture(effect1,GReffect); ObjEffect_SetRenderState(effect1,ALPHA);
			ObjEffect_SetPrimitiveType(effect1,PRIMITIVE_TRIANGLESTRIP); ObjEffect_CreateVertex(effect1,4);
			ObjEffect_SetVertexXY(effect1,0,-(xsize/2),-(ysize/2)); ObjEffect_SetVertexUV(effect1,0,0,0);
			ObjEffect_SetVertexXY(effect1,1,xsize/2,-(ysize/2)); ObjEffect_SetVertexUV(effect1,1,xsize,0);
			ObjEffect_SetVertexXY(effect1,2,-(xsize/2),ysize/2); ObjEffect_SetVertexUV(effect1,2,0,ysize);
			ObjEffect_SetVertexXY(effect1,3,xsize/2,ysize/2); ObjEffect_SetVertexUV(effect1,3,xsize,ysize);
			ObjEffect_SetVertexColor(effect1,0,255,255,0,100); ObjEffect_SetVertexColor(effect1,1,255,255,0,100);
			ObjEffect_SetVertexColor(effect1,2,255,255,0,100); ObjEffect_SetVertexColor(effect1,3,255,255,0,100);
			object1=object1~[effect1];
			timer1=timer1~[effect1];
			timer1[length(object1)-1]=0;
			angle1=angle1~[effect1];
			angle1[length(object1)-1]=rand(0,360);
			accel1=accel1~[effect1];
			accel1[length(object1)-1]=0;
			radius1=radius1~[effect1];
			radius1[length(object1)-1]=0;
			movetime1=movetime1~[effect1];
			movetime1[length(object1)-1]=movetime;
			xpos1=xpos1~[effect1];
			xpos1[length(object1)-1]=xpos;
			ypos1=ypos1~[effect1];
			ypos1[length(object1)-1]=ypos;
			}
		xpos+=(maxx-minx)/((length(array)^0.5)-1);
		i++;
		}
	ypos+=(maxy-miny)/((length(array)^0.5)-1);
	}
PlaySE(SEfocus1);
}

if(time>=60){
let i=0;
	while(i<length(object1)){
		if(Obj_BeDeleted(object1[i])){
		object1=erase(object1,i); timer1=erase(timer1,i); angle1=erase(angle1,i); accel1=erase(accel1,i);
		radius1=erase(radius1,i); movetime1=erase(movetime1,i); xpos1=erase(xpos1,i); ypos1=erase(ypos1,i); 
		i--;
		}
		else{
		ObjEffect_SetAngle(object1[i],0,0,time*5);
			if(timer1[i]==0){
			radius1[i]=(((xpos1[i]-Obj_GetX(object1[i]))^2+(ypos1[i]-Obj_GetY(object1[i]))^2)^0.5)/movetime1[i]*(pi/2);
			Obj_SetAngle(object1[i],atan2(ypos1[i]-Obj_GetY(object1[i]),xpos1[i]-Obj_GetX(object1[i])));
			}
			if(timer1[i]<movetime1[i]){
			accel1[i]=accel1[i]+90/movetime1[i];
			Obj_SetSpeed(object1[i],radius1[i]*cos(accel1[i]));
			}
			if(Obj_GetSpeed(object1[i])<0){ Obj_SetSpeed(object1[i],0); }

if(phase==1){
	if((timer1[i]+10)%16==0 && timer1[i]>0){
	let angle=0;
		loop(4){
		SetShotColor(180,180,180);
		CreateShot01(Obj_GetX(object1[i]),Obj_GetY(object1[i]),3-frame/150,angle,130,10);
		SetShotColor(255,255,255);
		angle+=360/4;
		}
	PlaySE(SEshots5);
	}
}

if(phase==2){
	if(Obj_GetSpeed(object1[i])==0 && timer1[i]%60==0 && timer1[i]>0){
	let angle=(time*2)+rand(-30,30);
	let shot=0;
		loop(4){
		CreateLaserA(shot,Obj_GetX(object1[i]),Obj_GetY(object1[i]),500,10,49,60);
		SetLaserDataA(shot,0,angle,0,0,0,0);
		SetShotKillTime(shot,120);
		FireShot(shot);
		angle+=360/4;
		}
	PlayDelayedSE(SElaserm4,60);
	}
	if(timer1[i]==60){
	let angle=rand(0,360);
		loop(28){
		SetShotColor(210,185,170);
		CreateShot02(Obj_GetX(object1[i]),Obj_GetY(object1[i]),2,angle,-0.1,0.5,47,0);
		SetShotColor(255,255,255);
		angle+=360/28;
		}
	PlaySE(SEshots7);
	}
}

if(phase==3){
	if(timer1[i]==120){
	SetShotDirectionType(PLAYER);
	SetShotColor(255,200,200);
	CreateShot01(Obj_GetX(object1[i]),Obj_GetY(object1[i]),1.2,rand(-60,60),180,0);
	SetShotColor(255,255,255);
	SetShotDirectionType(ABSOLUTE);
	}

	if(Obj_GetSpeed(object1[i])==0 && timer1[i]%15==0){
	SetShotDirectionType(PLAYER);
	CreateShot01(Obj_GetX(object1[i]),Obj_GetY(object1[i]),1,0,181,0);
	SetShotDirectionType(ABSOLUTE);
	PlaySE(SEshots3);
	}
}

if(phase==4){
if(Obj_GetX(object1[i])<cx){ angle1[i]=angle1[i]+2; }
if(Obj_GetX(object1[i])>=cx){ angle1[i]=angle1[i]-2; }
	if(Obj_GetSpeed(object1[i])==0 && timer1[i]%10==0 && timer1[i]>0){
	SetShotColor(255,110,110);
	CreateShot01(Obj_GetX(object1[i]),Obj_GetY(object1[i]),1,angle1[i],144,0);
	SetShotColor(255,255,255);
	PlaySE(SEshotm2);
	}
	if(timer1[i]==90){
	let angle=0;	
	let shot=0;
		loop(4){
		CreateLaserA(shot,Obj_GetX(object1[i]),Obj_GetY(object1[i]),500,15,49,30);
		SetLaserDataA(shot,0,angle,0,0,0,0);
		SetShotKillTime(shot,210);
		FireShot(shot);
		angle+=360/4;
		}
	PlaySE(SElaserb2);
	}
}

		timer1[i]=timer1[i]+1;
		if(timer1[i]==300){ Obj_Delete(object1[i]); frame=-30; timer1[i]=0; if(arrayphase==3 && phase<4){ arrayphase=0; phase++; bgchange=1; } }

		}
	i++;
	}
}

if(frame==0){ arrayphase++; }

if(bgchange==1){ bgchangefade+=360/30; bgfade+=255/30; }
if(bgchangefade%360==0){ newbg++; }
if((bgchangefade+180)%360==0){ bgchange=0; }


bgscale+=1/15000;


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,"Tomoko2"); dispelled=1; }

yield;
}

@BackGround{
if(newbg==1){
	SetGraphicRect(0-time,0,300-time,300);
	SetGraphicScale(2,2);
	SetTexture(BG1);
	SetAlpha(120+120*cos(bgchangefade+180));
	SetColor(255,255,255);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,0);
	DrawGraphic(cx,cy);
}
if(newbg==2){
	SetGraphicRect(0,0,300,300);
	SetGraphicScale(2,2);
	SetTexture(BG2);
	SetAlpha(120+120*cos(bgchangefade+180));
	SetColor(255,255,255);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,time);
	DrawGraphic(cx,cy);
}
if(newbg==3){
	SetGraphicRect(0,0,300,300);
	SetGraphicScale(4,4);
	SetTexture(BG3);
	SetAlpha(120+120*cos(bgchangefade+180));
	SetColor(255,0,0);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,-time*3);
	DrawGraphic(cx,cy);
}
if(newbg==4){
	SetGraphicRect(0,0,300,300);
	SetGraphicScale(2,2);
	SetTexture(BG4);
	SetAlpha(120+120*cos(bgchangefade+180));
	SetColor(255,255,0);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,time*0.7);
	DrawGraphic(cx,cy);
}
	SetGraphicRect(0,0,400,400);
	SetGraphicScale(1.5*bgscale,1.5*bgscale);
	SetTexture(BG5);
	SetAlpha(bgfade/1.7);
	SetColor(255,255,255);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,0);
//	DrawGraphic(cx+rand(-3,3),cy+rand(-3,3));

if(survivalcirclesize>0){
	SetGraphicRect(0,0,200,200);
	SetGraphicScale(survivalcirclesize*1.4,survivalcirclesize*1.4);
	SetRenderState(ALPHA);
	SetTexture(BG6);
	SetAlpha(bgfade/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(BG6);
	SetAlpha(bgfade/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(120,120,120);
	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,6);
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

task PlayDelayedSE(se,delay){
	loop(delay){ yield; }
	PlaySE(se);
}

}