script_enemy_main{

let bgspin=0;
let bgspinon=0;
let bgspinfade=0;

let effect1=0;

let butterflytarget=0;
let newbutterfly=1;
let movebutterfly=0;
let scale=0;
let shrink=1;
let size=180;

let phase=0;
let xpos=0;
let ypos=0;

let phase3angle=0;

let character="Masahiro";
let cutin=character;
let dispelled=0;
let spellcards=5;
let spellcardnumber=63;
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 SEmagics1=("\script\SoundEffects\magics1.wav");
let SEshots3=("\script\SoundEffects\shots3.wav");
let SEshots6=("\script\SoundEffects\shots6.wav");
let SEshotm8=("\script\SoundEffects\shotm8.wav");

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

let BG1a=("\script\Images\BackgroundLayers\Nightmare4a.png");
let BG1b=("\script\Images\BackgroundLayers\Nightmare4b.png");
let BG2=("\script\Images\BackgroundLayers\Setsuko3.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\magics1.wav");
	LoadSE("\script\SoundEffects\shots3.wav");
	LoadSE("\script\SoundEffects\shots6.wav");
	LoadSE("\script\SoundEffects\shotm8.wav");
	
	LoadGraphic("\script\Images\OtherEffects\ButterflyWings.png");
	
	LoadGraphic("\script\Images\CharacterSprites\"~character~".png");
	LoadGraphic("\script\Images\BackgroundLayers\Nightmare4a.png");
	LoadGraphic("\script\Images\BackgroundLayers\Nightmare4b.png");
	LoadGraphic("\script\Images\BackgroundLayers\Setsuko3.png");

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

if(GetCommonData("BombOn")==0){ damagerate=10; if(GetTimer<28){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("[Zhuangzi Dreamed He Was A Butterfly]",spellcardnumber); 
HealthBar();
Portrait(cutin,1);

if(time%60==0 && time>=60){
	if(GetPlayerX>minx+50 && GetPlayerX<maxx-50){
	SetMovePosition01(GetPlayerX+rand(-45,45),rand(miny+80,miny+140),2.5);
	}
	if(GetPlayerX<=minx+50){
	SetMovePosition01(GetPlayerX+rand(20,45),rand(miny+80,miny+140),2.5);
	}
	if(GetPlayerX>=maxx-50){
	SetMovePosition01(GetPlayerX-rand(20,45),rand(miny+80,miny+140),2.5);
	}
}


if((butterflytarget+0)%2==0){
	xpos=GetX;
	ypos=GetY-25;
	if(bgspinon==1){ bgspin+=180/30; bgspinfade+=180/30; }
	if(bgspin%180==0){ bgspinon=0; }
}
if((butterflytarget+1)%2==0){
	xpos=GetPlayerX;
	ypos=GetPlayerY;
	if(bgspinon==1){ bgspin-=180/30; bgspinfade+=180/30; }
	if(bgspin%180==0){ bgspinon=0; }
}

if(shrink==0){
	if((phase+0)%3==0){
		if(frame>=60 && frame%24==0){
		let angle=time*2;
		let speed=2;
		let shotx=0;
			loop(5){
			let color=138;
			if((butterflytarget+1)%2==0){ color=150; }
			CreateShotA(shotx,xpos-60,ypos,30);
			SetShotDataA_XY(shotx,0,0,0,0,0,0,0,168);
			SetShotDataA_XY(shotx,60,speed*cos(angle),speed*sin(angle),-0.02*cos(angle),0.03,0.2*cos(angle),rand(1.5,2.5),color);
			FireShot(shotx);
			CreateShotA(shotx,xpos+60,ypos,30);
			SetShotDataA_XY(shotx,0,0,0,0,0,0,0,168);
			SetShotDataA_XY(shotx,60,speed*cos(-angle),speed*sin(-angle),-0.02*cos(-angle),0.03,0.2*cos(-angle),rand(1.5,2.5),color);
			FireShot(shotx);

			CreateShotA(shotx,xpos-60,ypos,20);
			SetShotDataA_XY(shotx,0,0,0,0,0,0,0,168);
			SetShotDataA_XY(shotx,60,speed*cos(angle),speed*sin(angle),-0.02*cos(angle),-0.03,0.2*cos(angle),-rand(1.5,2.5),color-2);
			FireShot(shotx);
			CreateShotA(shotx,xpos+60,ypos,20);
			SetShotDataA_XY(shotx,0,0,0,0,0,0,0,168);
			SetShotDataA_XY(shotx,60,speed*cos(-angle),speed*sin(-angle),-0.02*cos(-angle),-0.03,0.2*cos(-angle),-rand(1.5,2.5),color-2);
			FireShot(shotx);			
			angle+=360/5;
			}
		if(OnPlayerMissed==false && GetEnemyShotCount>0){ PlayDelayedSE(SEshots6,80); }
		}
	}
	
	if((phase+2)%3==0){
		if(frame>=60 && time%20==0){
		let angle=0;
		let shotx=0;
			loop(12){
			SetShotDirectionType(PLAYER);
			let speed=1.5;
			let color=133;
			if((butterflytarget+1)%2==0){ color=145; }
				loop(7){
				CreateShotA(shotx,xpos-60,ypos,20);
				SetShotDataA(shotx,0,0,angle,0,0,0,168);
				SetShotDataA(shotx,60,speed,angle,0,0,0,color);
				FireShot(shotx);

				CreateShotA(shotx,xpos+60,ypos,20);
				SetShotDataA(shotx,0,0,angle,0,0,0,168);
				SetShotDataA(shotx,60,speed,angle,0,0,0,color);
				FireShot(shotx);
				speed+=0.5;
				}
			SetShotDirectionType(ABSOLUTE);
			angle+=360/12;
			}
		if(OnPlayerMissed==false && GetEnemyShotCount>0){ PlayDelayedSE(SEshotm8,80); }
		}
	}

	if((phase+1)%3==0){
		if(frame>=60 && frame%3==0){
		let angle=-time*9;
		let shotx=0;
		let color=135;
		if((butterflytarget+1)%2==0){ color=147; }
			loop(2){
			CreateShotA(shotx,xpos+60*cos(phase3angle),ypos+60*sin(phase3angle),20);
			SetShotDataA(shotx,0,5,90+angle,0,-0.35,0,color);
			SetShotDirectionType(PLAYER);
			SetShotDataA(shotx,60,0,0,0,0.1,4.5,color);
			SetShotDirectionType(ABSOLUTE);
			FireShot(shotx);
			
			CreateShotA(shotx,xpos+60*cos(phase3angle+180),ypos+60*sin(phase3angle+180),20);
			SetShotDataA(shotx,0,5,90-angle,0,-0.35,0,color);
			SetShotDirectionType(PLAYER);
			SetShotDataA(shotx,60,0,0,0,0.1,4.5,color);
			SetShotDirectionType(ABSOLUTE);
			FireShot(shotx);
			angle+=360/2;
			color-=1;
			}
		if(OnPlayerMissed==false && GetEnemyShotCount>0 && frame%6==0){ PlayDelayedSE(SEshots3,80); }
		if(frame%6==0){ PlaySE(SEshots3); }
		}
	phase3angle+=1.5;
	}
}


if(shrink==1 && size<360 && time>=60){ size+=4; }
if(shrink==1 && size==180 && GetTimer>1){ Obj_Delete(effect1); newbutterfly=1; PlaySE(SEmagics1); }
if(size>=360){ shrink=0; }

if(newbutterfly==1 && GetTimer>1){
let xsize=379;
let ysize=199;
butterflytarget+=1;
if(time>=60){ bgspinon=1; }
	effect1=(Obj_Create(OBJ_EFFECT));
	if((butterflytarget+0)%2==0){ Obj_SetPosition(effect1,GetX,GetY); }
	if((butterflytarget+1)%2==0){ Obj_SetPosition(effect1,GetPlayerX,GetPlayerY); }
	ObjEffect_SetScale(effect1,0,0);
	ObjEffect_SetLayer(effect1,1); ObjEffect_SetTexture(effect1,GReffect); 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,255,255,255,255); ObjEffect_SetVertexColor(effect1,1,255,255,255,255);
	ObjEffect_SetVertexColor(effect1,2,255,255,255,255); ObjEffect_SetVertexColor(effect1,3,255,255,255,255);
newbutterfly=0;
movebutterfly=1;
if(butterflytarget%2==0){ phase++; }
}

if((butterflytarget+0)%2==0){ Obj_SetPosition(effect1,GetX,GetY-25); }
if((butterflytarget+1)%2==0){ Obj_SetPosition(effect1,GetPlayerX,GetPlayerY); }
ObjEffect_SetScale(effect1,scale+0.1*cos(time*3),scale);
scale=0.3+0.3*cos(size);

if(time%300==0 && time>=60){ shrink=1; size=0; frame=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";
#include_function "script/Functions/CutIn.txt";

yield;
}

@BackGround{
	if(bgfade<255){ bgfade+=5; }

	SetGraphicRect(0,0,300,300);
	SetGraphicScale(2,2);
	SetRenderState(ALPHA);

	SetColor(120+120*cos(bgspinfade),120+120*cos(bgspinfade),120+120*cos(bgspinfade));
	SetAlpha(120+120*cos(bgspinfade));
	SetTexture(BG1a);
	SetGraphicAngle(0,0,time*0.7);
	DrawGraphic(cx,cy);

	SetColor(120+120*cos(bgspinfade+180),120+120*cos(bgspinfade+180),120+120*cos(bgspinfade+180));
	SetAlpha(120+120*cos(bgspinfade+180));
	SetTexture(BG1b);
	SetGraphicAngle(0,0,-time*0.7);
	DrawGraphic(cx,cy);


	SetGraphicRect(0,0,300,300);
	SetGraphicScale(1,1);
	SetAlpha(bgfade);
	SetColor(120,120,120);
	SetRenderState(ALPHA);
	SetTexture(BG2);
	SetGraphicAngle(0,0,bgspin);
	DrawGraphic(cx,cy);
}

@DrawLoop{
	SetGraphicScale(1,1);
	SetTexture(GRboss);
	SetGraphicAngle(0,0,0);
	SetColor(120,120,120);
	if((butterflytarget+0)%2==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("NightmareSpellcards",7);
	NewPointData(spellcardnumber,6);
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

task PlayDelayedSE(se,delay){
	loop(delay){ yield; }
	if(OnPlayerMissed==false && GetEnemyShotCount>0){ PlaySE(se); }
}

}