let CSD = GetCurrentScriptDirectory;
InstallFont(CSD ~ "font/Eurosti.ttf");

SetAutoDeleteObject(true);
#include "./Conc.txt"
#include "./AnimateBoss.dnh"

task NameFuto{

	let textObj = ObjText_Create;
	let spin = 0;
	InstallFont(GetCurrentScriptDirectory ~ "font/Russell Square Regular.ttf");
	let p = 255*sin(spin);
	
	ObjText_SetFontType(textObj,"Russell Square Regular");
	ObjText_SetText(textObj, "Mononobe no Futo");
	ObjText_SetFontSize(textObj, 12);
	Obj_SetRenderPriorityI(textObj,70);
	ObjText_SetHorizontalAlignment(textObj,ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(textObj,170);
	ObjText_SetLinePitch(textObj,0);
	ObjText_SetFontColorTop(textObj, 128, 255, 128);
	ObjText_SetFontColorBottom(textObj, 0, 192, 0);
	ObjRender_SetPosition(textObj,GetStgFrameWidth/2-85,32,0);
	loop(128){
	spin+=1;
	p = 255*sin(spin);
	ObjRender_SetAlpha(textObj,p);
	yield;
	}
	while(!Obj_IsDeleted(bossObj)){yield;}
	Obj_Delete(textObj);
}

// These are all from Ozzy's(ExPorygon's) Len Laser Kit! Thanks!!
function CreateCurvyLaserA1(xpos,ypos,speed,angle,len,width,trailgraphic,delay) {
	let obj = CreateShotA1(xpos,ypos,speed,angle,0,delay);
	ObjShot_SetIntersectionEnable(obj,false);
	LaserTrail;
	return obj;
	
	task LaserTrail {
		let del = len;
		let speed = 0;
		while(!Obj_IsDeleted(obj)) {
			speed = ObjMove_GetSpeed(obj);
			if(absolute(speed)>0) {
				if(round(9/absolute(speed))>=0) {
					FadeLaser(width,del);
					loop(round(9/absolute(speed))) { yield; }
				}
			}
			else { yield; }
		}
	}
	task FadeLaser(initwidth,del) {
		let objTrail = CreateStraightLaserA1(ObjMove_GetX(obj)-40*cos(ObjMove_GetAngle(obj)),ObjMove_GetY(obj)-40*sin(ObjMove_GetAngle(obj)),ObjMove_GetAngle(obj),120,initwidth,1200,trailgraphic,0);
		ObjStLaser_SetSource(objTrail,false);
		
		let initlength = 120;
		let width = initwidth;
		let len = initlength;
		let alpha = 255;
		loop(del) {
			//if(alpha<255) { alpha+=255/8; }
			width-=initwidth/del;
			len-=initlength/del;
			ObjRender_SetColor(objTrail,alpha,alpha,alpha);
			ObjLaser_SetIntersectionWidth(objTrail,width);
			ObjLaser_SetRenderWidth(objTrail,width);
			ObjLaser_SetLength(objTrail,len);
			yield;
		}
		Obj_Delete(objTrail);
	}
}
function CreateCurvyLaserA2(xpos,ypos,speed,angle,len,width,bulletgraphic,trailgraphic,delay) { //The bulletgraphic parameter is the graphic of the head bullet
	let obj = CreateShotA1(xpos,ypos,speed,angle,bulletgraphic,delay);
	LaserTrail;
	return obj;
	
	task LaserTrail {
		let del = len;
		let speed = 0;
		while(!Obj_IsDeleted(obj)) {
			speed = ObjMove_GetSpeed(obj);
			if(absolute(speed)>0) {
				if(round(9/absolute(speed))>=0) {
					FadeLaser(width,del);
					loop(round(9/absolute(speed))) { yield; }
				}
			}
			else { yield; }
		}
	}
	task FadeLaser(initwidth,del) {
		let objTrail = CreateStraightLaserA1(ObjMove_GetX(obj)-40*cos(ObjMove_GetAngle(obj)),ObjMove_GetY(obj)-40*sin(ObjMove_GetAngle(obj)),ObjMove_GetAngle(obj),120,initwidth,1200,trailgraphic,0);
		ObjStLaser_SetSource(objTrail,false);
		
		let initlength = 120;
		let width = initwidth;
		let len = initlength;
		let alpha = 255;
		loop(del) {
			//if(alpha<255) { alpha+=255/8; }
			width-=initwidth/del;
			len-=initlength/del;
			ObjRender_SetColor(objTrail,alpha,alpha,alpha);
			ObjLaser_SetIntersectionWidth(objTrail,width);
			ObjLaser_SetRenderWidth(objTrail,width);
			ObjLaser_SetLength(objTrail,len);
			yield;
		}
		Obj_Delete(objTrail);
	}
}
function CreateCurvyLaserB1(xpos,ypos,speed,angle,len,width,trailgraphic,trailrate,delay) { //The trailrate parameter is the spawn rate of the trail effect
	let obj = CreateShotA1(xpos,ypos,speed,angle,0,delay);
	if(trailrate<1) { RaiseError("The laser trail spawn rate must not be less than 1"); }
	ObjShot_SetIntersectionEnable(obj,false);
	LaserTrail;
	return obj;
	
	task LaserTrail {
		let del = len;
		let speed = 0;
		while(!Obj_IsDeleted(obj)) {
			FadeLaser(width,del);
			loop(trailrate) { yield; }
		}
	}
	task FadeLaser(initwidth,del) {
		let objTrail = CreateStraightLaserA1(ObjMove_GetX(obj)-40*cos(ObjMove_GetAngle(obj)),ObjMove_GetY(obj)-40*sin(ObjMove_GetAngle(obj)),ObjMove_GetAngle(obj),120,initwidth,1200,trailgraphic,0);
		ObjStLaser_SetSource(objTrail,false);
		
		let initlength = 120;
		let width = initwidth;
		let len = initlength;
		let alpha = 255;
		loop(del) {
			//if(alpha<255) { alpha+=255/8; }
			width-=initwidth/del;
			len-=initlength/del;
			ObjRender_SetColor(objTrail,alpha,alpha,alpha);
			ObjLaser_SetIntersectionWidth(objTrail,width);
			ObjLaser_SetRenderWidth(objTrail,width);
			ObjLaser_SetLength(objTrail,len);
			yield;
		}
		Obj_Delete(objTrail);
	}
}
function CreateCurvyLaserB2(xpos,ypos,speed,angle,len,width,bulletgraphic,trailgraphic,trailrate,delay) { //The bulletgraphic parameter is the graphic of the head bullet
	let obj = CreateShotA1(xpos,ypos,speed,angle,bulletgraphic,delay);
	if(trailrate<1) { RaiseError("The laser trail spawn rate must not be less than 1"); }
	LaserTrail;
	return obj;
	
	task LaserTrail {
		let del = len;
		let speed = 0;
		while(!Obj_IsDeleted(obj)) {
			FadeLaser(width,del);
			loop(trailrate) { yield; }
		}
	}
	task FadeLaser(initwidth,del) {
		let objTrail = CreateStraightLaserA1(ObjMove_GetX(obj)-40*cos(ObjMove_GetAngle(obj)),ObjMove_GetY(obj)-40*sin(ObjMove_GetAngle(obj)),ObjMove_GetAngle(obj),120,initwidth,1200,trailgraphic,0);
		ObjStLaser_SetSource(objTrail,false);
		
		let initlength = 120;
		let width = initwidth;
		let len = initlength;
		let alpha = 255;
		loop(del) {
			//if(alpha<255) { alpha+=255/8; }
			width-=initwidth/del;
			len-=initlength/del;
			ObjRender_SetColor(objTrail,alpha,alpha,alpha);
			ObjLaser_SetIntersectionWidth(objTrail,width);
			ObjLaser_SetRenderWidth(objTrail,width);
			ObjLaser_SetLength(objTrail,len);
			yield;
		}
		Obj_Delete(objTrail);
	}
}

task MC{
		let mcObj = ObjPrim_Create(OBJ_SPRITE_2D);
		let spin = 0;
		let p = 0;
		let s = 0;
		ObjPrim_SetTexture(mcObj,GetCurrentScriptDirectory ~ "img/MC.png");
		ObjSprite2D_SetDestRect(mcObj,0,0,256,256);
		ObjSprite2D_SetSourceRect(mcObj,0,0,256,256);
		ObjSprite2D_SetDestCenter(mcObj);
		ObjRender_SetBlendType(mcObj,BLEND_ADD_RGB);
		ObjRender_SetAlpha(mcObj,255);
		while(!Obj_IsDeleted(GetEnemyBossObjectID)){
		s = 0.25*sin(spin/3.5)+0.75;
		spin+=7;
		ObjRender_SetPosition(mcObj,ObjMove_GetX(bossObj),ObjMove_GetY(bossObj),0);
		ObjRender_SetAngleXYZ(mcObj,0,0,spin);
		ObjRender_SetScaleXYZ(mcObj,s,s,s);
		yield;
		}
		Obj_Delete(mcObj);
	}

task CutIn(text){
	let textObj = ObjText_Create;
	let finalx = 374;
	let finaly = 52;
	let spin = 0;
	let e = 255*sin(spin);
	let t = 255*sin(spin);
	
	SCAttack;
	ObjText_SetText(textObj,text);
	ObjText_SetFontType(textObj,"Eurostile");
	ObjText_SetFontSize(textObj,16);
	ObjText_SetFontColorTop(textObj,247,148,29);
	ObjText_SetFontColorBottom(textObj,255,255,255);
	ObjText_SetFontBorderType(textObj,BORDER_SHADOW);
	ObjText_SetFontBorderWidth(textObj,1);
	ObjText_SetFontBorderColor(textObj,200,10,70);
	ObjText_SetHorizontalAlignment(textObj,ALIGNMENT_CENTER);
	ObjText_SetMaxWidth(textObj,ObjText_GetTotalWidth(textObj));
	ObjRender_SetBlendType(textObj,BLEND_ALPHA);
	Obj_SetRenderPriorityI(textObj,70);
	ObjText_SetAutoTransCenter(textObj,true);
	
	let spellObj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(spellObj,69);
	ObjPrim_SetTexture(spellObj,GetCurrentScriptDirectory ~ "IMG/spellbar.png");
	ObjRender_SetBlendType(spellObj,BLEND_ALPHA);
	ObjRender_SetAlpha(spellObj,0);
	ObjSprite2D_SetSourceRect(spellObj,0,0,256,38);
	ObjSprite2D_SetSourceRect(spellObj,0,0,256,38);
	ObjSprite2D_SetDestCenter(spellObj);
	ObjRender_SetScaleXYZ(spellObj,1,1,1);
	
	
	let spellObj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(spellObj2,69);
	ObjPrim_SetTexture(spellObj2,GetCurrentScriptDirectory ~ "IMG/spellbar.png");
	ObjRender_SetBlendType(spellObj2,BLEND_ALPHA);
	ObjRender_SetAlpha(spellObj2,0);
	ObjSprite2D_SetSourceRect(spellObj2,0,0,256,38);
	ObjSprite2D_SetSourceRect(spellObj2,0,0,256,38);
	ObjSprite2D_SetDestCenter(spellObj2);
	ObjRender_SetScaleXYZ(spellObj2,1,1,1);
	ObjRender_SetAngleY(spellObj2,180);
	
	loop(250){
		spin+=1;
		e = 255*sin(spin);
		ObjRender_SetPosition(spellObj,ObjRender_GetX(textObj)+150,ObjRender_GetY(textObj),0);
		ObjRender_SetPosition(spellObj2,ObjRender_GetX(textObj)+150,ObjRender_GetY(textObj),0);
		ObjRender_SetPosition(textObj,GetStgFrameWidth/2-ObjText_GetTotalWidth(textObj)/2,GetStgFrameHeight/2-5,0);
		ObjRender_SetAlpha(textObj,e);
		yield;
	}
	
	loop(128){
		spin+=0.01;
		t = 255*sin(spin*128);
		ObjRender_SetPosition(spellObj,ObjRender_GetX(textObj)+ObjText_GetTotalWidth(textObj)/2,ObjRender_GetY(textObj)+1,0);
		ObjRender_SetPosition(spellObj2,ObjRender_GetX(textObj)+ObjText_GetTotalWidth(textObj)/2,ObjRender_GetY(textObj)+1,0);
		ObjRender_SetPosition(textObj,GetStgFrameWidth/2-ObjText_GetTotalWidth(textObj)/2,finaly,0);
		ObjRender_SetAlpha(textObj,t);
		ObjRender_SetAlpha(spellObj,t);
		ObjRender_SetAlpha(spellObj2,t);
		yield;
	}
	
	while(!Obj_IsDeleted(bossObj) && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID,INFO_IS_SPELL)){
		yield;
	}
	Obj_Delete(textObj);
	Obj_Delete(spellObj);
	Obj_Delete(spellObj2);
}

task SCAttack{
	let i = 0;
	let x = 0;
	let dir = 0;
	let spin = 0;
	let a = 8;
	let p = 200*sin(dir);
	let t = 105*sin(dir);
	let spell = CSD ~ "./se/cardcall.wav";
	
	Play(spell);
	
	dark;
	ascent(i in 0..7){
		loop(a){
			scattack(GetStgFrameWidth/2,GetStgFrameHeight/2,140+i*60,dir);
			scattack2(GetStgFrameWidth/2,GetStgFrameHeight/2,170+i*60,dir);

			x++;
			dir+=360/a;
		}
		dir = 0;
		x = 0;
	}
	
	task scattack(x,y,w,l){
		let scObj = ObjPrim_Create(OBJ_SPRITE_2D);
		
		ObjPrim_SetTexture(scObj,GetCurrentScriptDirectory ~ "img/SCA.png");
		ObjSprite2D_SetDestRect(scObj,0,0,128,16);
		ObjSprite2D_SetSourceRect(scObj,0,0,128,16);
		ObjSprite2D_SetDestCenter(scObj);
		ObjRender_SetBlendType(scObj,BLEND_ALPHA);
		ObjRender_SetAlpha(scObj,155);
		loop(255){
		spin+=0.02;
		dir+=360/a;
		p = 200*sin(spin/1.5);
		ObjRender_SetPosition(scObj,x+w*cos(dir+spin),y+w*sin(dir+spin),0);
		ObjRender_SetAngleXYZ(scObj,0,0,dir+spin+90);
		ObjRender_SetScaleXYZ(scObj,1,1,1);
		ObjRender_SetAlpha(scObj,p);
		yield;
		}
		Obj_Delete(scObj);
	}
	
	task scattack2(x,y,w,l){
		let scObj = ObjPrim_Create(OBJ_SPRITE_2D);
		
		ObjPrim_SetTexture(scObj,GetCurrentScriptDirectory ~ "img/SCA.png");
		ObjSprite2D_SetDestRect(scObj,0,0,128,16);
		ObjSprite2D_SetSourceRect(scObj,0,0,128,16);
		ObjSprite2D_SetDestCenter(scObj);
		ObjRender_SetBlendType(scObj,BLEND_ALPHA);
		ObjRender_SetAlpha(scObj,155);
		loop(255){
		ObjRender_SetPosition(scObj,x+w*cos(dir-spin),y+w*sin(dir-spin),0);
		ObjRender_SetAngleXYZ(scObj,180,180,dir-spin-90);
		ObjRender_SetScaleXYZ(scObj,1,1,1);
		ObjRender_SetAlpha(scObj,p);
		yield;
		}
		Obj_Delete(scObj);
	}
	
	task dark{
		let scObj = ObjPrim_Create(OBJ_SPRITE_2D);
		let scObj2 = ObjPrim_Create(OBJ_SPRITE_2D);
		
		ObjPrim_SetTexture(scObj2,GetCurrentScriptDirectory ~ "img/SCAlay.png");
		ObjSprite2D_SetDestRect(scObj2,0,0,2000,2000);
		ObjSprite2D_SetSourceRect(scObj2,0,0,2000,2000);
		ObjSprite2D_SetDestCenter(scObj2);
		ObjRender_SetBlendType(scObj2,BLEND_ALPHA);
		ObjRender_SetAlpha(scObj2,25);
		
		ObjPrim_SetTexture(scObj,GetCurrentScriptDirectory ~ "img/SCAlay.png");
		ObjSprite2D_SetDestRect(scObj,0,0,2000,2000);
		ObjSprite2D_SetSourceRect(scObj,0,0,2000,2000);
		ObjSprite2D_SetDestCenter(scObj);
		ObjRender_SetBlendType(scObj,BLEND_ALPHA);
		ObjRender_SetAlpha(scObj,25);
		
		loop(510){
		t = 65*sin(spin/1.5);
		ObjRender_SetPosition(scObj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
		ObjSprite2D_SetDestRect(scObj,0,0,2000,2000-spin*2);
		ObjSprite2D_SetSourceRect(scObj,0,0,2000,2000-spin*2);
		ObjSprite2D_SetDestCenter(scObj);
		ObjRender_SetScaleXYZ(scObj,0.4,0.4,0.4);
		ObjRender_SetAlpha(scObj,t);
		
		ObjRender_SetPosition(scObj2,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
		ObjRender_SetScaleXYZ(scObj2,0.4,0.4,0.4);
		ObjSprite2D_SetDestRect(scObj2,0,0,2000,2000+spin*2);
		ObjSprite2D_SetSourceRect(scObj2,0,0,2000,2000+spin*2);
		ObjSprite2D_SetDestCenter(scObj2);
		ObjRender_SetAlpha(scObj2,t);
		yield;
		}
		Obj_Delete(scObj);
	}
}

task Effect_RefractAura(obj, size){
//arby26's heat wave aura
	let rt = GetReservedRenderTargetName(0);
	RenderToTextureA1(rt,20,22,false); 

	let obj_core = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	Obj_SetRenderPriorityI(obj_core, 25);
	ObjPrim_SetPrimitiveType(obj_core, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetTexture(obj_core, rt);

	let obj_edge = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj_edge, PRIMITIVE_TRIANGLESTRIP);
	Obj_SetRenderPriorityI(obj_edge, 25);
	ObjPrim_SetTexture(obj_edge, rt);

	let r=100;

	let sides=6;
	ObjPrim_SetVertexCount(obj_core, 2+sides);
	ObjPrim_SetVertexCount(obj_edge, 2*(sides+1));

	let f=0;
	let objScene = GetEnemyBossSceneObjectID();
	while(!Obj_IsDeleted(obj)){
		let spellCard=objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL);
		Obj_SetVisible(obj_core, !spellCard);
		Obj_SetVisible(obj_edge, !spellCard);
		if(!spellCard){
			RenderToTextureA1(rt,20,22,true); 
			let ex=ObjMove_GetX(obj);
			let ey=ObjMove_GetY(obj);
			ObjRender_SetPosition(obj_core, ex,ey,0);
			ObjRender_SetPosition(obj_edge, ex,ey,0);
			let rx=ex+GetStgFrameLeft;
			let ry=ey+GetStgFrameTop;
			ObjPrim_SetVertexPosition(obj_core, 0, 0, 0, 0);
			ObjPrim_SetVertexUVT(obj_core, 0, rx, ry);
			let rr=-12;
			ascent(i in 0..sides+1){
				let odd=(i%2==0)*2-1;

				let ang=i*360/sides+4*f;
				let cosine2=cos(ang);
				let sine2=sin(ang);
				ObjPrim_SetVertexPosition(obj_core, 1+i, cosine2*(r+rr*odd), sine2*(r+rr*odd), 0);
				ObjPrim_SetVertexUVT(obj_core, 1+i, rx+cosine2*r, ry+sine2*r);

				let i2=i*2;
				ObjPrim_SetVertexPosition(obj_edge, i2, cosine2*(r+rr*odd), sine2*(r+rr*odd), 0);
				ObjPrim_SetVertexUVT(obj_edge, i2, rx+cosine2*r, ry+sine2*r);
				ObjPrim_SetVertexPosition(obj_edge, i2+1, cosine2*2*r, sine2*2*r, 0);
				ObjPrim_SetVertexUVT(obj_edge, i2+1, rx+cosine2*2*r, ry+sine2*2*r);
				ObjPrim_SetVertexAlpha(obj_edge, i2+1, 0);
			}
			f++;
		}
		yield;
	}
	Obj_Delete(obj_core);
	Obj_Delete(obj_edge);
}

function Play(path){
	LoadSound(path);
	PlaySE(path);
}

// This Function is from Darkness1's Function Library. Thanks!
function RandMove(ObjID, Xdist, Ydist, moveweight, maxispeed, left_bound, top_bound, right_bound, bottom_bound){
let setrand;
let Xdir;
let Ydir;


setrand = rand(0,360);

if( ObjMove_GetX(ObjID)>(right_bound-Xdist) ){
setrand = rand(200,250);}

if( ObjMove_GetX(ObjID)<(left_bound+Xdist) ){
setrand = rand(-70,70);}


Xdir = ObjMove_GetX(ObjID)+cos(setrand)*(Xdist);
Ydir = ObjMove_GetY(ObjID)+sin(setrand)*(Ydist);


if(Xdir>right_bound){Xdir = right_bound;}
if(Xdir<left_bound){Xdir = left_bound;}
if(Ydir>bottom_bound){Ydir = bottom_bound;}
if(Ydir<top_bound){Ydir = top_bound;}



ObjMove_SetDestAtWeight(ObjID, Xdir, Ydir, moveweight, maxispeed);}
