script_enemy_main{

let shot1=0;
let bullet1=[];
let timer1=[];
let angle1=[];
let speed1=[];
let type1=[];

let effectx=0;
let objectx=[];
let timerx=[];
let typex=[];

let character="Sayuri";
let cutin=character;
let dispelled=0;
let spellcards=2;
let spellcardnumber=15;
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 SEshots5=("script\SoundEffects\shots5.wav");
let BG1=("\script\Images\BackgroundLayers\Sayuri2a.png");
let BG2=("\script\Images\BackgroundLayers\Sayuri4.png");
let GRboss=("\script\Images\CharacterSprites\Sayuri.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\ShotsSayuri1.txt");
	LoadGraphic("\script\Images\CharacterSprites\Sayuri.png");

	LoadSE("script\SoundEffects\shots5.wav");
	LoadGraphic("\script\Images\BackgroundLayers\Sayuri2a.png");
	LoadGraphic("\script\Images\BackgroundLayers\Sayuri4.png");

	SetScore(200000);
	SetLife(250);
	SetTimer(50);
	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(200,200,200,200);

Weakness(character);
#include_function "script/Functions/Weakness.txt";
if(GetCommonData("BombOn")==0){ damagerate=10; }
if(GetCommonData("BombOn")==1){ damagerate=5; }
SetDamageRate(damagerate*weaken,damagerate*weaken);

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("Impromptu [Bizarre Magic] ("~difficulty~")",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+60,miny+120),1.5);
	}
	if(GetPlayerX<=minx+50){
	SetMovePosition01(GetPlayerX+rand(15,30),rand(miny+60,miny+120),1.5);
	}
	if(GetPlayerX>=maxx-50){
	SetMovePosition01(GetPlayerX-rand(15,30),rand(miny+60,miny+120),1.5);
	}
}


if((time+210)%270==0 && time>=60){
let angle=rand(0,360);
	loop(3){
	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,GetX,GetY);
	Obj_SetAngle(shot1,angle);
	Obj_SetSpeed(shot1,2);
	Obj_SetAutoDelete(shot1,false);
	ObjShot_SetBombResist(shot1,true);
	ObjShot_SetGraphic(shot1,80);
	bullet1=bullet1~[shot1];
	timer1=timer1~[shot1];
	timer1[length(bullet1)-1]=0;
	angle1=angle1~[shot1];
	angle1[length(bullet1)-1]=angle;
	speed1=speed1~[shot1];
	speed1[length(bullet1)-1]=3;
	type1=type1~[shot1];
	type1[length(bullet1)-1]=1;
	angle+=360/3;
	}
	loop(3){
	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,GetX,GetY);
	Obj_SetAngle(shot1,angle);
	Obj_SetSpeed(shot1,2);
	Obj_SetAutoDelete(shot1,false);
	ObjShot_SetBombResist(shot1,true);
	ObjShot_SetGraphic(shot1,81);
	bullet1=bullet1~[shot1];
	timer1=timer1~[shot1];
	timer1[length(bullet1)-1]=0;
	angle1=angle1~[shot1];
	angle1[length(bullet1)-1]=angle;
	speed1=speed1~[shot1];
	speed1[length(bullet1)-1]=3;
	type1=type1~[shot1];
	type1[length(bullet1)-1]=2;
	angle+=360/3;
	}
}
if(time%7==0 && time>=60){ PlaySE(SEshots5); }

let i=0;
while(i<length(bullet1)){
	if(Obj_BeDeleted(bullet1[i])){
	bullet1=erase(bullet1,i); timer1=erase(timer1,i); angle1=erase(angle1,i); speed1=erase(speed1,i); type1=erase(type1,i);
	i--;
	}
	else{
	if(timer1[i]<40){ angle1[i]=angle1[i]-4; speed1[i]=speed1[i]+0.01; }
	if(timer1[i]>=40 && timer1[i]<70){ angle1[i]=angle1[i]+3; speed1[i]=speed1[i]-0.04; }
	if(timer1[i]>=70 && timer1[i]<130){ angle1[i]=angle1[i]-5; speed1[i]=speed1[i]+0.02; }
	if(timer1[i]>=130 && timer1[i]<170){ angle1[i]=angle1[i]+1; speed1[i]=speed1[i]-0.03; }
	if(timer1[i]>=170 && timer1[i]<200){ angle1[i]=angle1[i]-3; speed1[i]=speed1[i]+0.03; }
	if(timer1[i]>=200 && timer1[i]<250){ angle1[i]=angle1[i]+1; speed1[i]=speed1[i]-0.02; }

	Obj_SetSpeed(bullet1[i],speed1[i]);
	if(type1[i]==1){ Obj_SetAngle(bullet1[i],angle1[i]); }
	if(type1[i]==2){ Obj_SetAngle(bullet1[i],-angle1[i]); }
	
	if(time%7==0){
		let shotx=0;
		CreateShotA(shotx,Obj_GetX(bullet1[i]),Obj_GetY(bullet1[i]),15);
		if(type1[i]==1){ SetShotDataA(shotx,0,0,angle1[i]-timer1[i]*3,0,0.01,1.7,81); }
		if(type1[i]==2){ SetShotDataA(shotx,0,0,-angle1[i]+timer1[i]*3,0,0.01,1.7,80); }
		FireShot(shotx);
	}
	
	if(timer1[i]>=280){ ObjShot_FadeDelete(bullet1[i]); }
	timer1[i]=timer1[i]+1;
	}
i++;
}


if(time%2==0){
let xsize=39;
let ysize=39;
let color=rand_int(0,360);
let size=rand(0.7,1.3);
	effectx=(Obj_Create(OBJ_EFFECT));
	Obj_SetPosition(effectx,cx+130*cos(time*1.5)+rand(-40,40),miny-10);
	ObjEffect_SetScale(effectx,size,size);
	Obj_SetSpeed(effectx,3*size);
	Obj_SetAngle(effectx,90+rand(-5,5));
	ObjEffect_SetLayer(effectx,1); ObjEffect_SetTexture(effectx,BG2); ObjEffect_SetRenderState(effectx,ADD);
	ObjEffect_SetPrimitiveType(effectx,PRIMITIVE_TRIANGLESTRIP); ObjEffect_CreateVertex(effectx,4);
	ObjEffect_SetVertexXY(effectx,0,-(xsize/2),-(ysize/2)); ObjEffect_SetVertexUV(effectx,0,0,0);
	ObjEffect_SetVertexXY(effectx,1,xsize/2,-(ysize/2)); ObjEffect_SetVertexUV(effectx,1,xsize,0);
	ObjEffect_SetVertexXY(effectx,2,-(xsize/2),ysize/2); ObjEffect_SetVertexUV(effectx,2,0,ysize);
	ObjEffect_SetVertexXY(effectx,3,xsize/2,ysize/2); ObjEffect_SetVertexUV(effectx,3,xsize,ysize);
	ObjEffect_SetVertexColor(effectx,0,180,200+50*cos(color),200+50*cos(color+120),200+50*cos(color+240));
	ObjEffect_SetVertexColor(effectx,1,180,200+50*cos(color),200+50*cos(color+120),200+50*cos(color+240));
	ObjEffect_SetVertexColor(effectx,2,180,200+50*cos(color),200+50*cos(color+120),200+50*cos(color+240));
	ObjEffect_SetVertexColor(effectx,3,180,200+50*cos(color),200+50*cos(color+120),200+50*cos(color+240));
	objectx=objectx~[effectx];
	timerx=timerx~[effectx];
	timerx[length(objectx)-1]=rand_int(0,360);
	typex=typex~[effectx];
	typex[length(objectx)-1]=rand_int(0,1);
}

let i=0;
while(i<length(objectx)){
	if(Obj_BeDeleted(objectx[i])){
	objectx=erase(objectx,i); timerx=erase(timerx,i); typex=erase(typex,i);
	i--;
	}
	else{
	ObjEffect_SetAngle(objectx[i],0,0,timerx[i]);
	if(typex[i]==0){ timerx[i]=timerx[i]+4; }
	if(typex[i]==1){ timerx[i]=timerx[i]-4; }
	}
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; }

	SetGraphicRect(0,0,300,300);
	SetGraphicScale(2,2);
	SetTexture(BG1);
	SetAlpha(bgfade);
	SetColor(255,255,255);
	SetRenderState(ALPHA);
	SetGraphicAngle(0,0,0);
	DrawGraphic(cx,cy);
}

@DrawLoop{
	SetGraphicScale(1,1);
	SetTexture(GRboss);
	SetGraphicAngle(0,0,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("Conversation",1);
	NewPointData(spellcardnumber,GetCommonData("Difficulty"));
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

}