script_enemy_main{

let phase=0;
let fake=0;
let speed=0;
let radius=0;
let shots=0;
let realityfield=100;
let fieldgrow=0;

let effect1=0;

let shot1=0;
let bullet1=[];
let timer1=[];
let phase1=[];
let bounce1=[];
let direction1=[];

let character="Masahiro";
let cutin=character;
let dispelled=0;
let spellcards=3;
let spellcardnumber=65;
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 SEcharge1=("\script\SoundEffects\charge1.wav");
let SElaserb2=("\script\SoundEffects\laserb2.wav");
let SEshotm7=("\script\SoundEffects\shotm7.wav");
let SEshotb1=("\script\SoundEffects\shotb1.wav");
let SEshots7=("\script\SoundEffects\shots7.wav");
let SEshotb9=("\script\SoundEffects\shotb9.wav");

let EFrealityfield=("\script\Images\OtherEffects\SafeCircle4.png");

let BG1=("\script\Images\BackgroundLayers\Nightmare1.png");
let BG2=("\script\Images\BackgroundLayers\Nightmare4a.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\charge1.wav");
	LoadSE("\script\SoundEffects\laserb2.wav");
	LoadSE("\script\SoundEffects\shotb1.wav");
	LoadSE("\script\SoundEffects\shotm7.wav");
	LoadSE("\script\SoundEffects\shots7.wav");
	LoadSE("\script\SoundEffects\shotb9.wav");
	
	LoadGraphic("\script\Images\OtherEffects\SafeCircle4.png");

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

	SetScore(1000000);
	SetLife(800);
	SetTimer(90);
	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);

if(GetCommonData("BombOn")==0){ damagerate=10; if(GetTimer<15){damagerate=20; }  }
if(GetCommonData("BombOn")==1){ damagerate=0; }
SetDamageRate(damagerate,damagerate);

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("Lucid Dream [Reality Check]",spellcardnumber);

HealthBar();
Portrait(cutin,1);


if(time==60){
let xsize=159;
let ysize=159;
let fade=220;
	effect1=(Obj_Create(OBJ_EFFECT));
	Obj_SetPosition(effect1,GetPlayerX,GetPlayerY);
	ObjEffect_SetScale(effect1,1,1);
	ObjEffect_SetLayer(effect1,2); ObjEffect_SetTexture(effect1,EFrealityfield); ObjEffect_SetRenderState(effect1,ADD);
	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,fade,fade,fade,fade); ObjEffect_SetVertexColor(effect1,1,fade,fade,fade,fade);
	ObjEffect_SetVertexColor(effect1,2,fade,fade,fade,fade); ObjEffect_SetVertexColor(effect1,3,fade,fade,fade,fade);
}

Obj_SetPosition(effect1,GetPlayerX,GetPlayerY);
ObjEffect_SetAngle(effect1,0,0,time*5);
ObjEffect_SetScale(effect1,(fieldgrow*(realityfield/60))*0.8,(fieldgrow*(realityfield/60))*0.8);

if(time==210){ PlaySE(SElaserb2); usespell=-40; }
if(fieldgrow<1 && time>=225){ fieldgrow+=0.025; }

if(realityfield>30){ realityfield-=80/(30*60); }
if(frame<30 && realityfield<100){ realityfield+=3; }


if((GetTimer<=60 || GetLife<=550) && phase==0){
	#include_function "script/Functions/Focus.txt";
	PlaySE(SEcharge1);
	DeleteEnemyShot(ALL);
	phase=1;
	frame=0;
}
if((GetTimer<=30 || GetLife<=300) && phase==1){
	#include_function "script/Functions/Focus.txt";
	PlaySE(SEcharge1);
	DeleteEnemyShot(ALL);
	phase=2;
	frame=0;
}


if(phase==0){
SetShotAutoDeleteClip(255,255,255,255);

	if(time%75==0 && time>=60){
	let angle1=time+rand(-20,20);
	let color=[109,113,116];
	fake=rand_int(0,2);
	shots=0;
		loop(3){
			if(shots==fake){
			let radius=20;
				loop(22){
				shot1=(Obj_Create(OBJ_SHOT));
				Obj_SetPosition(shot1,cx+250*cos(angle1)+radius*cos(angle1-90),cy+250*sin(angle1)+radius*sin(angle1-90));
				Obj_SetAngle(shot1,angle1+180);
				Obj_SetSpeed(shot1,0.9);
				ObjShot_SetGraphic(shot1,color[shots]);
				ObjShot_SetDelay(shot1,15);
				bullet1=bullet1~[shot1];
				timer1=timer1~[shot1];
				timer1[length(bullet1)-1]=0;
				phase1=phase1~[shot1];
				if(fake!=shots){ phase1[length(bullet1)-1]=0; }
				if(fake==shots){ phase1[length(bullet1)-1]=1; }
				if(time%150==0){ radius+=15; }
				if(time%150!=0){ radius-=15; }
				}
			}
			if(shots!=fake){
				let radius=0;
				loop(22){
				CreateShot01(cx+250*cos(angle1)+radius*cos(angle1-90),cy+250*sin(angle1)+radius*sin(angle1-90),0.9,angle1+180,color[shots],15);
				if(time%150==0){ radius+=15; }
				if(time%150!=0){ radius-=15; }
				}
			}
			angle1+=360/3;
			shots++;
		}
		
	
	PlaySE(SEshotm7);
	}

	let i=0;
	while(i<length(bullet1)){
		if(Obj_BeDeleted(bullet1[i])){
		bullet1=erase(bullet1,i); timer1=erase(timer1,i); phase1=erase(phase1,i);
		i--;
		}
		else{

		if((((GetPlayerX-Obj_GetX(bullet1[i]))^2+(GetPlayerY-Obj_GetY(bullet1[i]))^2)^0.5)<(fieldgrow*realityfield) && phase1[i]%2!=0){
		Obj_SetAlpha(bullet1[i],100);
		Obj_SetCollisionToPlayer(bullet1[i],false);
		}
		else{
		Obj_SetAlpha(bullet1[i],255);
		Obj_SetCollisionToPlayer(bullet1[i],true);
		}

		timer1[i]=timer1[i]+1;
		}
	i++;
	}
} // phase 0

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

if(phase==1){
SetShotAutoDeleteClip(15,15,15,15);

	if(frame==90){ fake=rand_int(0,2); speed=1.25; radius=30; shots=0; }

	if(frame>=90 && frame%3==0 && frame<340){
	let angle1=-(time*4.34);
	let color=[109,113,116];
	shots=0;
		loop(3){
			if(shots==fake){
			shot1=(Obj_Create(OBJ_SHOT));
			Obj_SetPosition(shot1,GetX,GetY-20);
			Obj_SetAngle(shot1,angle1);
			Obj_SetSpeed(shot1,speed);
			ObjShot_SetGraphic(shot1,color[shots]);
			ObjShot_SetDelay(shot1,15);
			bullet1=bullet1~[shot1];
			timer1=timer1~[shot1];
			timer1[length(bullet1)-1]=0;
			phase1=phase1~[shot1];
			phase1[length(bullet1)-1]=1;

			shot1=(Obj_Create(OBJ_SHOT));
			Obj_SetPosition(shot1,GetX,GetY-20);
			Obj_SetAngle(shot1,angle1+180);
			Obj_SetSpeed(shot1,speed);
			ObjShot_SetGraphic(shot1,color[shots]);
			ObjShot_SetDelay(shot1,15);
			bullet1=bullet1~[shot1];
			timer1=timer1~[shot1];
			timer1[length(bullet1)-1]=0;
			phase1=phase1~[shot1];
			phase1[length(bullet1)-1]=1;
			}
			if(shots!=fake){
			CreateShot01(GetX,GetY-20,speed,angle1,color[shots],15);
			CreateShot01(GetX,GetY-20,speed,angle1+180,color[shots],15);
			}
		angle1+=360/3;
		shots++;
		}
	speed+=0.04;
	if(frame%6==0){ PlaySE(SEshots7); }
	usespell=20;
	}

	if(frame>=370 && frame%8==0 && time>=60){
	shots=0;
	let angle1=time*5.2;
	let color=[109,113,116];
	let angle2=0;
		loop(6){
			if(shots==fake){
			let angle3=rand(0,360);
				loop(18){
				shot1=(Obj_Create(OBJ_SHOT));
				Obj_SetPosition(shot1,GetX+radius*cos(angle3),GetY+radius*sin(angle3));
				Obj_SetAngle(shot1,angle1+angle2);
				Obj_SetSpeed(shot1,5);
				ObjShot_SetGraphic(shot1,color[shots]);
				ObjShot_SetDelay(shot1,5);
				bullet1=bullet1~[shot1];
				timer1=timer1~[shot1];
				timer1[length(bullet1)-1]=0;
				phase1=phase1~[shot1];
				phase1[length(bullet1)-1]=1;
				angle3+=360/18;
				}
			}
			if(shots!=fake){
			let angle3=rand(0,360);
				loop(18){
				CreateShot01(GetX+radius*cos(angle3),GetY+radius*sin(angle3),5,angle1+angle2,color[shots],5);
				angle3+=360/18;
				}
			}
		angle2+=360/6;
		shots++;
		if(shots>2){ shots=0; }
		}
	radius+=3;
	PlaySE(SEshotb9);
	}

	if(frame>=475){ frame=60; }

	let i=0;
	while(i<length(bullet1)){
		if(Obj_BeDeleted(bullet1[i])){
		bullet1=erase(bullet1,i); timer1=erase(timer1,i); phase1=erase(phase1,i);
		i--;
		}
		else{

		if((((GetPlayerX-Obj_GetX(bullet1[i]))^2+(GetPlayerY-Obj_GetY(bullet1[i]))^2)^0.5)<(fieldgrow*realityfield) && phase1[i]%2!=0){
		Obj_SetAlpha(bullet1[i],100);
		Obj_SetCollisionToPlayer(bullet1[i],false);
		}
		else{
		Obj_SetAlpha(bullet1[i],255);
		Obj_SetCollisionToPlayer(bullet1[i],true);
		}

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

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

if(phase==2){
SetShotAutoDeleteClip(15,15,15,15);

	if(time%7==0 && frame>=60){
	let angle=rand(0,360);
	let color=[109,113,116];
		loop(2){
		angle+=360/3;
			loop(10){
			CreateShot01(GetX,GetY,rand(1.5,2.5),angle,color[rand_int(0,2)],0);
			angle+=360/10;
			}
		}
		loop(10){
			shot1=(Obj_Create(OBJ_SHOT));
			Obj_SetPosition(shot1,GetX,GetY);
			Obj_SetAngle(shot1,angle);
			Obj_SetSpeed(shot1,rand(1.5,2.5));
			ObjShot_SetGraphic(shot1,color[rand_int(0,2)]);
			bullet1=bullet1~[shot1];
			timer1=timer1~[shot1];
			timer1[length(bullet1)-1]=0;
			phase1=phase1~[shot1];
			phase1[length(bullet1)-1]=1;
		angle+=360/10;
		}
	PlaySE(SEshotb1);
	}

	let i=0;
	while(i<length(bullet1)){
		if(Obj_BeDeleted(bullet1[i])){
		bullet1=erase(bullet1,i); timer1=erase(timer1,i); phase1=erase(phase1,i);
		i--;
		}
		else{

		if((((GetPlayerX-Obj_GetX(bullet1[i]))^2+(GetPlayerY-Obj_GetY(bullet1[i]))^2)^0.5)<(fieldgrow*realityfield) && phase1[i]%2!=0){
		Obj_SetAlpha(bullet1[i],100);
		Obj_SetCollisionToPlayer(bullet1[i],false);
		}
		else{
		Obj_SetAlpha(bullet1[i],255);
		Obj_SetCollisionToPlayer(bullet1[i],true);
		}

		timer1[i]=timer1[i]+1;
		}
	i++;
	}
} // phase 2


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<255){ bgfade+=5; }

	SetGraphicRect(0+(time/(5*cos(time*0.8))),0,300+(time/(5*cos(time*0.8))),300);
	SetGraphicScale(2,2);
	SetTexture(BG1);
	SetAlpha(bgfade/2);
	SetColor(255,255,255);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,0);
	DrawGraphic(cx,cy);

	SetGraphicRect(0,0,300,300);
	SetGraphicScale(2.5+0.5*cos(time/3),2.5+0.5*cos(time/3));
	SetTexture(BG2);
	SetAlpha(bgfade/3);
	SetColor(255,255,255);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,-time*2);
	DrawGraphic(cx,cy);

	SetGraphicScale(2.5+0.5*cos(time/3),2.5+0.5*cos(time/3));
	SetGraphicAngle(0,0,(time+40)*2);
	DrawGraphic(cx,cy);
}

@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("NightmareSpellcards",9);
	NewPointData(spellcardnumber,6);
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

}