#TouhouDanmakufu
#Title[Test Spellcard]
#Text[]
#Player[player\Masahiro\Masahiro.txt]
#ScriptVersion[2]

script_enemy_main{

let frame=-60;
let phase=0;
let angle=rand(0,360);
let bulletcolor=rand_int(1,10);
let shoottime=30;
let shots=0;

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

let character="Masahiro";
let cutin=character;
let dispelled=0;
let spellcards=6;
let spellcardnumber=62;
let damagerate=10;
let outfit=1;
outfit=(128*outfit)-128;
let usespell=0;
let bgfade=0;
let time=0;
let miny=GetClipMinY; let maxy=GetClipMaxY; let minx=GetClipMinX; let maxx=GetClipMaxX;
let cx=GetCenterX; let cy=GetCenterY;

let SEshots4=("\script\SoundEffects\shots4.wav");

let BG1=("\script\Images\BackgroundLayers\Nightmare5.png");
let BG2=("\script\Images\BackgroundLayers\Haruna3a.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\shots4.wav");
	
	LoadGraphic("\script\Images\CharacterSprites\"~character~".png");

	LoadGraphic("\script\Images\BackgroundLayers\Nightmare5.png");
	LoadGraphic("\script\Images\BackgroundLayers\Haruna3a.png");

	SetScore(1000000);
	SetLife(250);
	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(400,400,400,400);
if(GetCommonData("BombOn")==0){ damagerate=10; if(GetTimer<=40){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("Embodied Imagination [Shared Dream]",spellcardnumber); 
HealthBar();
Portrait(cutin,1);

if(time%180==0 && time>=60){
	if(GetPlayerX>minx+50 && GetPlayerX<maxx-50){
	SetMovePosition01(GetPlayerX+rand(-30,30),rand(miny+60,miny+120),1);
	}
	if(GetPlayerX<=minx+50){
	SetMovePosition01(GetPlayerX+rand(15,30),rand(miny+60,miny+120),1);
	}
	if(GetPlayerX>=maxx-50){
	SetMovePosition01(GetPlayerX-rand(15,30),rand(miny+60,miny+120),1);
	}
}


if(frame==shoottime){
	loop(12){
	shot1=(Obj_Create(OBJ_SHOT));
	Obj_SetPosition(shot1,GetX,GetY);
	Obj_SetAngle(shot1,angle);
	Obj_SetSpeed(shot1,4);
	ObjShot_SetGraphic(shot1,bulletcolor);
	bullet1=bullet1~[shot1];
	timer1=timer1~[shot1];
	timer1[length(bullet1)-1]=0;
	bounce1=bounce1~[shot1];
	bounce1[length(bullet1)-1]=0;
	color1=color1~[shot1];
	color1[length(bullet1)-1]=bulletcolor;
	angle+=360/12;
	}

	bulletcolor++;
	if(bulletcolor>10){ bulletcolor=1; }

	if(shoottime>7){ shoottime-=2; }
	if(shoottime<=7){ shots++; }

	if(phase%2==0){ angle+=4.4; }
	if(phase%2!=0){ angle-=4.4; }

	PlaySE(SEshots4);
	frame=0;
	usespell=-40;
}
if(shots>=10){ shoottime=30; shots=0; frame=-30; phase++; }


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

	if(timer1[i]>=45 && bounce1[i]==0){
	let shotx=0;
	CreateShotA(shotx,GetPlayerX+120*cos(Obj_GetAngle(bullet1[i])+180),GetPlayerY+120*sin(Obj_GetAngle(bullet1[i])+180),15);
	SetShotDataA(shotx,0,0,Obj_GetAngle(bullet1[i]),0,0,0,color1[i]+120);
	SetShotDataA(shotx,60,0,NULL,0,0.018,3,color1[i]+120);
	SetShotKillTime(shotx,175);
	FireShot(shotx);
	bounce1[i]=1;
	Obj_Delete(bullet1[i]);
	}

	Obj_SetSpeed(bullet1[i],Obj_GetSpeed(bullet1[i])-0.06);

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

	SetGraphicRect(0,0,300,300);
	SetGraphicScale(3+0.3*cos(time/2),3-0.3*sin(time/3));
	SetAlpha(bgfade);
	SetColor(255,255,255);
	SetRenderState(ALPHA);

	SetTexture(BG1);
	SetGraphicAngle(20*sin(time*1.4),15*cos(time),200*cos(time/5)+time);
	DrawGraphic(cx,cy);
	
	SetTexture(BG2);
	SetGraphicRect(0-time,0,600-time,600);
	SetGraphicScale(1,1);
	SetAlpha(bgfade);
	SetGraphicAngle(0,0,90);
	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",6);
	NewPointData(spellcardnumber,6);
	#include_function "script/Functions/Main Menu/SpellcardDataAndPoints.txt";
}

}