script_enemy_main{

let shot1=0;
let bullet1=[];
let timer1=[];

let character="Masahiro";
let cutin=character;
let dispelled=0;
let spellcards=9;
let spellcardnumber=59;
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 SEshotm8=("\script\SoundEffects\shotm8.wav");
let SEshots7=("\script\SoundEffects\shots7.wav");

let BG1=("\script\Images\BackgroundLayers\Nightmare2.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\shotm8.wav");
	LoadSE("\script\SoundEffects\shots7.wav");

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

	SetScore(1000000);
	SetLife(500);
	SetTimer(75);
	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);
SetShotAutoDeleteClip(64,64,64,64);

if(GetCommonData("BombOn")==0){ damagerate=10; if(GetTimer<60){damagerate=25; } }
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("Oneirology [Slow Wave Sleep]",spellcardnumber); 
HealthBar();
Portrait(cutin,1);

if(time%60==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%120==0 && time>=60){
let angle=0;
SetShotDirectionType(PLAYER);
	loop(13){
	let speed=3.5;
		loop(10){
		CreateShot01(GetX+12,GetY-30,speed,angle,44,10);
		speed-=0.25;
		}
	angle+=360/13;
	}
angle+=360/26;
	loop(13){
	let speed=4.5;
		loop(10){
		CreateShot01(GetX+12,GetY-30,speed,angle,44,10);
		speed-=0.25;
		}
	angle+=360/13;
	}
SetShotDirectionType(ABSOLUTE);
usespell=50;
PlaySE(SEshotm8);
}


if(time%4==0 && time>=60){
let xpos=220*cos(time*2.7);
	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,cx+xpos,miny);
	Obj_SetAngle(shot1,90);
	Obj_SetSpeed(shot1,1);
	ObjShot_SetDelay(shot1,15);
	ObjShot_SetBombResist(shot1,true);
	ObjShot_SetGraphic(shot1,27);
	bullet1=bullet1~[shot1];
	timer1=timer1~[shot1];
	timer1[length(bullet1)-1]=0;

	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,cx-xpos,maxy);
	Obj_SetAngle(shot1,270);
	Obj_SetSpeed(shot1,1);
	ObjShot_SetDelay(shot1,15);
	ObjShot_SetBombResist(shot1,true);
	ObjShot_SetGraphic(shot1,31);
	bullet1=bullet1~[shot1];
	timer1=timer1~[shot1];
	timer1[length(bullet1)-1]=0;
if(time%8==0){ PlaySE(SEshots7); }
}

let i=0;
while(i<length(bullet1)){
	if(Obj_BeDeleted(bullet1[i])){
	bullet1=erase(bullet1,i); timer1=erase(timer1,i);
	i--;
	}
	else{
	Obj_SetX(bullet1[i],Obj_GetX(bullet1[i])+0.7*cos(timer1[i]*2));
	timer1[i]=timer1[i]+1;
	}
i++;
}


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

	SetGraphicScale(2,2);
	SetRenderState(ALPHA);
	SetAlpha(bgfade);
	SetColor(255,255,255);
	SetTexture(BG1);
	SetGraphicRect(0,0,300,300);
	SetGraphicAngle(0,0,-time);
	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",3);
	NewPointData(spellcardnumber,6);
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

}