#include"./lib/lib_img/_PathImage.txt"
#include"./lib/lib_sound/_PathSound.txt"

let dir 	= GetCurrentScriptDirectory();
let frmtime = 5;
let diff	= 1;
let seObj	= ObjSound_Create;
let seVol	= 100;

task playSE(path,vol)
{
	let obj = ObjSound_Create;
	ObjSound_Load				(obj,path);
	ObjSound_SetLoopEnable		(obj,false);
	ObjSound_SetRestartEnable	(obj,false);
	ObjSound_Play				(obj);
	let time = 0;
	if(path == seTimeout0)		{time = 9616;}
	if(path == seShot0)			{time = 15056;}
	if(path == seShot1)			{time = 15056;}
	if(path == seShot2)			{time = 11636;}
	if(path == seEnemyDeath0)	{time = 28152;}
	if(path == seEnemyDeath1)	{time = 57536;}
	if(path == seShiny0)		{time = 35066;}
	if(path == seShiny1)		{time = 21784;}
	if(path == seShiny2)		{time = 12812;}
	if(path == seExplode0)		{time = 35545;}
	if(path == seSpell0)		{time = 42052;}
	if(path == seCharge0)		{time = 43711;}
	time = (time/1500*4)+60;
	ObjSound_SetVolumeRate		(obj,vol*seVol/100);
	loop(time){yield;}
	Obj_Delete(obj);
}

task charge(image,blend,r,g,b,scale,posx,posy,time,alp,spinz,angz)
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture			(obj,image);
	let sr = [0,0,512,512];
	ObjSprite2D_SetSourceRect	(obj,sr[0],sr[1],sr[2],sr[3]);
	ObjSprite2D_SetDestCenter	(obj);
	ObjRender_SetBlendType		(obj,blend);
	ObjRender_SetColor			(obj,r,g,b);
	ObjRender_SetScaleXYZ		(obj,scale/(sr[2]-sr[0]),scale/(sr[3]-sr[1]),1);
	ObjRender_SetPosition		(obj,posx,posy,0);
	let c = 0;
	let m = 1;
	loop(2)
	{
		loop(time/2)
		{
			c += 1 / time * m;
			angz += spinz;
			ObjRender_SetAlpha		(obj,alp * c);
			ObjRender_SetAngleZ		(obj,angz);
			yield;
		}
		m *= -1;
	}
	Obj_Delete(obj);
}

task spriteIdle(obj,duration,img,scale)
{
	ObjPrim_SetTexture(obj,img);
	let xx = 64;
	let yy = 64;
	let c = [1,1];
	let lp = duration/frmtime;
	ObjRender_SetScaleXYZ(obj,scale,scale,1);
	loop(lp)
	{
		if(c[0] > 4){c[0] = 1;}
		loop(frmtime)
		{
			ObjSprite2D_SetSourceRect(obj,xx*(c[0]-1),yy*(c[1]-1),xx*c[0],yy*c[1]);
			ObjSprite2D_SetDestCenter(obj);
			yield;
		}
		c[0] = c[0] + 1;
	}
}

task spriteHold(obj,duration,img,type,scale)
{
	ObjPrim_SetTexture(obj,img);
	let xx = 64;
	let yy = 64;
	let c = [1,2];
	ObjRender_SetScaleXYZ(obj,scale,scale,1);
	//Going Left
	if(type == 0){ObjRender_SetScaleXYZ(obj,-scale,scale,1);}
	//Going Right
	if(type == 1){ObjRender_SetScaleXYZ(obj,scale,scale,1);}
	if(type == 0 || type == 1)
	{
		loop(3)
		{
			loop(frmtime)
			{
				ObjSprite2D_SetSourceRect(obj,xx*(c[0]-1),yy*(c[1]-1),xx*c[0],yy*c[1]);
				ObjSprite2D_SetDestCenter(obj);
				yield;
			}
			c[0] = c[0] + 1;
		}
		c[0] = 4;
		loop(duration-frmtime*6)
		{
				ObjSprite2D_SetSourceRect(obj,xx*(c[0]-1),yy*(c[1]-1),xx*c[0],yy*c[1]);
			ObjSprite2D_SetDestCenter(obj);
			yield;
		}
		c[0] = 3;
		loop(3)
		{
			loop(frmtime)
			{
				ObjSprite2D_SetSourceRect(obj,xx*(c[0]-1),yy*(c[1]-1),xx*c[0],yy*c[1]);
				ObjSprite2D_SetDestCenter(obj);
				yield;
			}
			c[0] = c[0] - 1;
		}
	}
}

task enemyMarker(enemy)
{
	let fontsize = 10;
	let obj = ObjText_Create;
	Obj_SetRenderPriority			(obj,52);
	ObjText_SetFontSize				(obj,fontsize);
	ObjRender_SetBlendType			(obj,BLEND_INV_DESTRGB);
	ObjText_SetHorizontalAlignment	(obj,ALIGNMENT_CENTER);
	ObjText_SetText					(obj,"enemy");
	ObjText_SetMaxWidth				(obj,ObjText_GetTextLength(obj)*fontsize);
	let length = [ObjText_GetTextLength(obj)*fontsize/2,ObjText_GetTotalHeight(obj)/2];
	while (ObjEnemy_GetInfo(enemy, INFO_LIFE) > 0)
	{
		ObjRender_SetPosition		(obj,ObjMove_GetX(enemy)-length[0],475-length[1],0);
		yield;
	}
	Obj_Delete(obj);
}

task spellName(boss,text)
{
	let fontsize = 10;
	let obj = ObjText_Create;
	Obj_SetRenderPriority			(obj,52);
	ObjText_SetFontSize				(obj,fontsize);
	ObjText_SetHorizontalAlignment	(obj,ALIGNMENT_CENTER);
	ObjText_SetText					(obj,text);
	ObjText_SetMaxWidth				(obj,ObjText_GetTextLength(obj)*fontsize);
	let time = 60;
	let c = 0;
	let m = 1;
	let y = 5+time;
	while (time > 0)
	{
		if(c < 1 && m == 1)		{c += 1 / time;}
		if(c > 0 && m == -1)	{c -= 1 / time;}
		if(y%480 != 5)
		{
			y --;
			let length = [ObjText_GetTextLength(obj)*fontsize/2,ObjText_GetTotalHeight(obj)/2];
			ObjRender_SetPosition			(obj,320-length[0],(y%480)-length[1]-1,0);
		}
		ObjRender_SetAlpha				(obj,c * 255);
		if(ObjEnemy_GetInfo(boss, INFO_LIFE) <= 0){time--;m = -1;}
		yield;
	}
	Obj_Delete(obj);
}

function chatBox(x,y,text,time,key,portrait)
{
	let imgSize = 300;
	let fontsize = 10;
	let objImage 	= ObjPrim_Create(OBJ_SPRITE_2D);
	let objBox 	= ObjPrim_Create(OBJ_SPRITE_2D);
	let objText = ObjText_Create;
	ObjText_SetFontSize				(objText,fontsize);
	ObjText_SetHorizontalAlignment	(objText,ALIGNMENT_CENTER);
	ObjText_SetText					(objText,text);
	ObjText_SetMaxWidth				(objText,ObjText_GetTextLength(objText)*fontsize);
	let length = [ObjText_GetTextLength(objText)*fontsize/2,ObjText_GetTotalHeight(objText)/2];
	ObjRender_SetPosition			(objText,320-length[0],420-length[1]-1,0);
	ObjRender_SetColor				(objText,255,255,255);
	ObjRender_SetAlpha				(objText,0);
	ObjPrim_SetTexture				(objBox,sfxSquare);
	ObjSprite2D_SetSourceRect		(objBox,0,0,length[0]+fontsize*2,length[1]+fontsize*2);
	ObjSprite2D_SetDestCenter		(objBox);
	ObjRender_SetColor				(objBox,0,0,0);
	ObjRender_SetPosition			(objBox,320+fontsize/2,420,0);
	ObjRender_SetAlpha				(objBox,0);
	let path = cutPortrait;
	ObjPrim_SetTexture				(objImage,path);
	let dim = [0,0,0,0];
	if(portrait %9 == 1)	{dim = [0,0,512,512];}
	if(portrait %9 == 2)	{dim = [512,0,1024,512];}
	if(portrait %9 == 3)	{dim = [1024,0,1536,512];}
	if(portrait %9 == 4)	{dim = [0,512,512,1024];}
	if(portrait %9 == 5)	{dim = [512,512,1024,1024];}
	if(portrait %9 == 6)	{dim = [1024,512,1536,1024];}
	if(portrait %9 == 7)	{dim = [0,1024,512,1536];}
	if(portrait %9 == 8)	{dim = [512,1024,1024,1536];}
	if(portrait %9 == 0)	{dim = [1024,1028,1536,1536];}
	if(portrait <= 9)		{dim[0] = dim[0]+1536;dim[2]=dim[2]+1536;}
	if(portrait >= 7 && portrait <= 8)		{dim[1] = dim[1]+1;dim[3]=dim[3]+1;}
	ObjSprite2D_SetSourceRect		(objImage,dim[0],dim[1],dim[2],dim[3]);
	ObjSprite2D_SetDestCenter		(objImage);
	ObjRender_SetPosition			(objImage,x,470-imgSize/2,0);
	ObjRender_SetAlpha				(objImage,0);
	let c = 0;
	let delay = 15;
	while(time > 0 && portrait > 0)
	{
		if(c < 1)
		{
			c += 1/delay;
			ObjRender_SetAlpha			(objText,c*255);
			ObjRender_SetAlpha			(objBox,c*180);
			ObjRender_SetAlpha			(objImage,c*255);
			ObjRender_SetScaleXYZ		(objText,c,1,1);
			ObjRender_SetScaleXYZ		(objBox,c,1,1);
			ObjRender_SetScaleXYZ							(objImage,imgSize/512*c,imgSize/512,1);
			if(portrait <= 9)	{ObjRender_SetScaleXYZ	(objImage,-imgSize/512*c,imgSize/512,1);}
		}
		if(c >= 1 && key == true && (GetVirtualKeyState(VK_SHOT) == KEY_PUSH || GetVirtualKeyState(VK_SHOT) == KEY_HOLD)){time = 0;}
		time --;
		yield;
	}
	while(c > 0 && portrait > 0)
	{
		c -= 1/delay;
		ObjRender_SetAlpha			(objText,c*255);
		ObjRender_SetAlpha			(objBox,c*180);
		ObjRender_SetAlpha			(objImage,c*255);
		ObjRender_SetScaleXYZ		(objText,c,1,1);
		ObjRender_SetScaleXYZ		(objBox,c,1,1);
		ObjRender_SetScaleXYZ							(objImage,imgSize/512*c,imgSize/512,1);
		if(portrait <= 9)	{ObjRender_SetScaleXYZ	(objImage,-imgSize/512*c,imgSize/512,1);}
		yield;
	}
	Obj_Delete(objText);
	Obj_Delete(objBox);
	Obj_Delete(objImage);
}

function GetDistance(x1,y1,x2,y2){return(((x2-x1)^2+(y2-y1)^2)^(1/2))}

task ScreenShake(time,shake)
{
	let c = 1;
	loop(time)
	{
		c -= 1/time;
		Set2DCameraFocusX(GetStgFrameWidth/2+rand(-shake,shake)*c);
		Set2DCameraFocusY(GetStgFrameHeight/2+rand(-shake,shake)*c);
		yield;
	}
	Set2DCameraFocusX(GetStgFrameWidth/2);
	Set2DCameraFocusY(GetStgFrameHeight/2);
}

task Snapshot(count)
{
	let range 		= 460;
	let renderTexture = "Snapshot"~IntToString(count);
	CreateRenderTarget			(renderTexture);
	RenderToTextureA1			(renderTexture,20,80,true);
    let obj = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriorityI		(obj,80);
    ObjPrim_SetTexture			(obj,renderTexture);
    ObjSprite2D_SetSourceRect	(obj,90,10,550,470);
	ObjSprite2D_SetDestCenter	(obj);
	ObjRender_SetPosition		(obj,320,240,0);
	let c = 1;
	let ang = 360;
	loop(30)
	{
		c -= 1/30;
		ObjRender_SetPosition	(obj,45+275*c,(480/9*count)+(240-(480/9*count))*c,0);
		ObjRender_SetScaleXYZ	(obj,(80+380*c)/460,(80+380*c)/460,1);
		ObjRender_SetAngleZ		(obj,ang*(1-c));
		ObjRender_SetAlpha		(obj,255*(1-c));
		yield;
	}
    Obj_SetRenderPriorityI(obj,10);
    Obj_SetRenderPriority(obj,1);
}