script_enemy_main{

SetCommonData("Difficulty",6);
if(GetCommonDataDefault("ReachedNightmareBattle",0)==0){ SetCommonData("ReachedNightmareBattle",1); } //to unlock a trump card

let phase=GetCommonDataDefault("NightmareSpellcards",1);
let round=0;

let speed2=0;
let width2=0;
let angle4=rand(0,360);
let xpos4=0;
let speed4=0;
let xpos5=0;
let color7=0;
let angle10=0;
let shots10=0;

let character="Masahiro";
let cutin=character;
let dispelled=0;
let spellcards=11-GetCommonDataDefault("NightmareSpellcards",1);
let spellcardnumber=0;
let damagerate=10;
let outfit=1;
outfit=(128*outfit)-128;
let usespell=0;
let frame=-90; let time=0;
let miny=GetClipMinY; let maxy=GetClipMaxY; let minx=GetClipMinX; let maxx=GetClipMaxX;
let cx=GetCenterX; let cy=GetCenterY;

let SEshots3=("\script\SoundEffects\shots3.wav");
let SEshots5=("\script\SoundEffects\shots5.wav");
let SEshots6=("\script\SoundEffects\shots6.wav");
let SEshots7=("\script\SoundEffects\shots7.wav");
let SEshotm1=("\script\SoundEffects\shotm1.wav");
let SEshotm4=("\script\SoundEffects\shotm4.wav");
let SEshotb5=("\script\SoundEffects\shotb5.wav");
let SEshotb9=("\script\SoundEffects\shotb9.wav");
let SElaserb2=("\script\SoundEffects\laserb2.wav");
let SElaserm2=("\script\SoundEffects\laserm2.wav");

let GRboss=("\script\Images\CharacterSprites\"~character~".png");

#include_function "script/Functions/SpellcardNameLoad.txt";
#include_function "script/Functions/HealthBarLoad.txt";

@Initialize{
	LoadUserShotData("script\shots\ShotsNightmare2.txt");

	LoadSE("\script\SoundEffects\shots3.wav");
	LoadSE("\script\SoundEffects\shots5.wav");
	LoadSE("\script\SoundEffects\shots6.wav");
	LoadSE("\script\SoundEffects\shots7.wav");
	LoadSE("\script\SoundEffects\shotm1.wav");
	LoadSE("\script\SoundEffects\shotm4.wav");
	LoadSE("\script\SoundEffects\shotb5.wav");
	LoadSE("\script\SoundEffects\shotb9.wav");
	LoadSE("\script\SoundEffects\laserb2.wav");
	LoadSE("\script\SoundEffects\laserm2.wav");
	
	LoadGraphic("\script\Images\CharacterSprites\"~character~".png");

	SetLife(250);
	SetTimer(35);
	SetInvincibility(120);
	SetDamageRate(10,10); 
	SetEnemyMarker(true);
	MagicCircle(true);
	SetMovePosition02(cx,miny+100,50);
	
	if(GetCommonDataDefault("NightmareSpellcards",1)==7){ SetCommonData("ItemSpawn",3); }
}
	
@MainLoop{

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

if(GetCommonData("BombOn")==0){ damagerate=10; }
if(GetCommonData("BombOn")==1){ damagerate=3; }
SetDamageRate(damagerate,damagerate);

SpellcardName("[Recurring Dream]",spellcardnumber); 
HealthBar();


if(time>=60){ usespell=-10; }

if(phase>=1 && frame%8==0 && time>=60){
let anglex=time*1.5;
	loop(2){
	let angley=0;
	let speed=2.5;
	let color=0;
		loop(4){
		SetShotColor(255-color,255-color,255-color);
		CreateShot01(GetX,GetY-15,speed,anglex+angley,1,0);
		SetShotColor(255,255,255);
		color+=20;
		angley+=2;
		speed-=0.4;
		}
	anglex+=360/2;
	}
PlaySE(SEshots6);
}

if(phase>=2 && (time+0)%160==0 && time>=60){
let angle=90+(time/2.8);
let shotx=0;
	loop(30){
	let speed=4;
		loop(6){
		CreateShotA(shotx,GetX,GetY-15,0);
		SetShotDataA(shotx,0,4,angle,2,-0.04,speed/2,14);
		SetShotDataA(shotx,45,NULL,NULL,0,-0.04,speed/2,14);
		FireShot(shotx);
		speed-=0.3;
		}
	angle+=360/30;
	}
}
if(phase>=2 && (time+80)%160==0 && time>=60){
let angle=90+(time/2.8);
let shotx=0;
	loop(30){
	let speed=4;
		loop(6){
		CreateShotA(shotx,GetX,GetY-15,0);
		SetShotDataA(shotx,0,4,angle,-2,-0.04,speed/2,14);
		SetShotDataA(shotx,45,NULL,NULL,0,-0.04,speed/2,14);
		FireShot(shotx);
		speed-=0.3;
		}
	angle+=360/30;
	}
PlaySE("\script\SoundEffects\shotm1.wav");
}

if(phase>=3 && frame%60==0 && frame>=0){
let shotx=0;
	CreateShotA(shotx,GetX,GetY-15,0);
	if((frame+0)%120==0){ SetShotDataA(shotx,0,5,0,0,-0.08,0,27); }
	if((frame+60)%120==0){ SetShotDataA(shotx,0,5,180,0,-0.08,0,27); }
	SetShotDirectionType(PLAYER);
	SetShotDataA(shotx,60,3,0,0,0,0,27);
	SetShotDirectionType(ABSOLUTE);
	FireShot(shotx);
	PlaySE(SEshots7);
}

if(phase>=4 && time%14==0 && frame>=310 && frame<450){
let angle=0;
let shotx=0;
	loop(2){
	CreateLaser01(GetX+xpos4+10,GetY-15,2.5+speed4,90+angle,170,12,40,10);
	CreateLaser01(GetX-xpos4-10,GetY-15,2.5+speed4,90-angle,170,12,40,10);
	angle+=180;
	}
xpos4+=20;
speed4+=0.5;
PlaySE(SElaserm2);
}

if(phase>=5 && xpos5<440 && frame>=400){
let randomcolor=53+(rand_int(0,1)*12);
	if(round%2==0){ CreateShot02(minx+xpos5,miny+rand(5,20),0,90+rand(-1,1),rand(0.012,0.02),rand(2,3),randomcolor,15); }
	if(round%2!=0){ CreateShot02(maxx-xpos5,miny+rand(5,20),0,90+rand(-1,1),rand(0.012,0.02),rand(2,3),randomcolor,15); }
xpos5+=rand(6,9);
if(frame%4==0){ PlaySE(SEshotm4); }
}

if(phase>=6 && (frame+60)%240==0 && time>=60){
let angle=atan2(GetPlayerY-(GetY-15),GetPlayerX-GetX)-4;
	loop(5){
	CreateShot02(GetX,GetY-15,3,angle,-0.04,1,78,0);
	angle+=2;
	}
PlaySE(SEshotb5);
}

if(phase>=7 && frame>=30 && frame<90 && frame%5==0 && time>=60){
let xspeed=-0.4;
SetShotColor(255-color7,255-color7,255-color7);
	loop(9){
	CreateShot12(GetX,GetY-15,xspeed,-2,0,0.03,0,5,91,0);
	xspeed+=0.1;
	}
SetShotColor(255,255,255);
color7+=10;
PlaySE(SEshots5);
}
if(phase>=8 && frame==90){
let shot1=0;
	CreateLaserA(shot1,GetX,GetY-15,600,15,104,30);
	SetLaserDataA(shot1,0,270+15,0,0,0,0);
	let speed=0;
	let delay=0;
		loop(20){
		SetLaserDataA(shot1,30+delay,NULL,speed,0,0,0);
		speed+=0.2;
		delay+=2;
		}
		loop(20){
		speed-=0.2;
		SetLaserDataA(shot1,30+delay,NULL,speed,0,0,0);
		delay+=2;
		}
	SetLaserDataA(shot1,30+delay,NULL,0,0,0,0);
	SetShotKillTime(shot1,120+delay);
	FireShot(shot1);
	
	CreateLaserA(shot1,GetX,GetY-15,600,15,104,30);
	SetLaserDataA(shot1,0,270-15,0,0,0,0);
	let speed=0;
	let delay=0;
		loop(20){
		SetLaserDataA(shot1,30+delay,NULL,-speed,0,0,0);
		speed+=0.2;
		delay+=2;
		}
		loop(20){
		speed-=0.2;
		SetLaserDataA(shot1,30+delay,NULL,-speed,0,0,0);
		delay+=2;
		}
	SetLaserDataA(shot1,30+delay,NULL,0,0,0,0);
	SetShotKillTime(shot1,120+delay);
	FireShot(shot1);
PlaySE("\script\SoundEffects\laserb2.wav");
}


if(phase>=9 && time%25==0 && frame<120 && frame>=0){
let angle=0+(time/3.1);
	loop(30){
	CreateShot01(GetX,GetY-15,3,angle,117,0);
	angle+=360/30;
	}
PlaySE(SEshots3);
}

if(phase>=10 && frame>=270 && frame%5==0 && shots10<9){
let shot1=0;
let speed=3.5;
	loop(4){
	CreateShotA(shot1,GetX,GetY-15,0);
	SetShotDataA(shot1,0,speed,90+9+angle10,0,0,0,130);
	FireShot(shot1);
	CreateShotA(shot1,GetX,GetY-15,0);
	SetShotDataA(shot1,0,speed,90-9-angle10,0,0,0,130);
	FireShot(shot1);
	speed-=0.4;
	}
angle10+=18;
shots10++;
PlaySE(SEshotb9);
}

if(frame==480){ round++; angle10=0; shots10=0; angle4=rand(0,360); xpos4=0; speed4=0; xpos5=0; frame=0; color7=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";
}

@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{
}

}