
let dir = GetCurrentScriptDirectory;
let pathHealth = dir ~ "img/lifebar.png";
let exlife = 0;

@Initialize
{
	SetPauseScriptPath(dir ~ "Default_Pause.txt");
	SetEndSceneScriptPath(dir ~"Default_EndScene.txt");
	SetStgFrame(90, 10, 550, 470, 20, 80);
	InitFrame();
	TScore();
	TGraze();
	TPlayerLife();
	TPlayerSpell();
	TBossLife();
	TBossTimer();
	if(GetReplayFps<=0){TCurrentFps();}
	TReplayFps();
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_PLAYER_SHOOTDOWN)
	{
		/*if(GetPlayerLife<0)
		{
			exlife++;
			SetPlayerLife(2);
		}*/
	}
}

function InitFrame()
{
	StgFrame;
	
	function StgFrame
	{
		let path = dir ~ "img/SystemBackground.png";
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, path);
		Obj_SetRenderPriority(obj, 0);
		ObjSprite2D_SetSourceRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
		ObjSprite2D_SetDestRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
		ObjRender_SetAlpha(obj,256);
	}
}

task TScore()
{
	let text=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=dir~"img/SystemPicture.png";
	ObjPrim_SetTexture(text,img);
	ObjSprite2D_SetSourceRect(text,416,32,512,64);
	ObjSprite2D_SetDestCenter(text);
	Obj_SetRenderPriority(text,1);
	ObjRender_SetColor(text,128,128,255);
	ObjRender_SetScaleXYZ(text,0.8,0.8,0);
	ObjRender_SetPosition(text,595,30,0);

	let count = 8;
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, img);
	ObjRender_SetScaleXYZ(obj,0.8,0.6,0);
	Obj_SetRenderPriority(obj, 1);
	while(true)
	{
		let score = GetScore();
		score = min(score, 999999999);
		let listNum = DigitToArray(score, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj,550 + iObj *10);
			ObjRender_SetY(obj, 50);
			ObjSpriteList2D_SetSourceRect(obj,(num*32),0,((num+1)*32),32);
			ObjSpriteList2D_SetDestRect(obj, 0, -16, 32, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

task TGraze()
{
	let text=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=dir~"img/SystemPicture.png";
	ObjPrim_SetTexture(text,img);
	ObjSprite2D_SetSourceRect(text,416,64,512,96);
	ObjSprite2D_SetDestCenter(text);
	Obj_SetRenderPriority(text,1);
	ObjRender_SetColor(text,128,255,255);
	ObjRender_SetScaleXYZ(text,0.8,0.8,0);
	ObjRender_SetPosition(text,595,80,0);

	let count = 6;
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, img);
	ObjRender_SetScaleXYZ(obj,0.8,0.6,0);
	Obj_SetRenderPriority(obj, 1);
	while(true)
	{
		let graze = GetGraze();
		graze = min(graze, 999999);
		let listNum = DigitToArray(graze, count);
		count = length(listNum);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj,(550+10) + iObj *10);
			ObjRender_SetY(obj, 100);
			ObjSpriteList2D_SetSourceRect(obj,(num*32),0,((num+1)*32),32);
			ObjSpriteList2D_SetDestRect(obj, 0, -16, 32, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

task TPlayerLife
{
	TText;
	task TText
	{
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		let img=dir~"img/SystemPicture.png";
		ObjPrim_SetTexture(obj,img);
		ObjSprite2D_SetSourceRect(obj,320,32,416,64);
		ObjSprite2D_SetDestCenter(obj);
		Obj_SetRenderPriority(obj,1);
		ObjRender_SetColor(obj,255,128,255);
		ObjRender_SetScaleXYZ(obj,0.8,0.8,0);
		ObjRender_SetPosition(obj,595,130,0);
		DrawPicture;
		task DrawPicture
		{
			let objy=ObjPrim_Create(OBJ_SPRITE_2D);
			let objx=ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(objy,img);
			ObjPrim_SetTexture(objx,img);
			ObjRender_SetScaleXYZ(objy,0.3,0.5,0);
			ObjRender_SetScaleXYZ(objx,0.3,0.5,0);
			Obj_SetRenderPriority(objy,1);
			Obj_SetRenderPriority(objx,1);
			let c=1;
			let cc=1/5;
			while(true)
			{
				let lifelimit=7;
				if(GetPlayerLife>lifelimit){SetPlayerLife(lifelimit);}
				if(GetPlayerLife<=0){SetPlayerLife(0);}
				ObjSprite2D_SetSourceRect(objy,0,64,32*lifelimit,96);
				ObjSprite2D_SetSourceRect(objx,0,32,32*GetPlayerLife,64);
				ObjSprite2D_SetDestCenter(objy);
				ObjSprite2D_SetDestRect(objx,0,-16,32*GetPlayerLife,16);
				ObjRender_SetPosition(objy,595,150,0);
				ObjRender_SetPosition(objx,595-112*0.3,150,0);
				ObjRender_SetColor(objx,255,192+64*c,255);
				c-=cc;
				if(c<0 || c>1){cc*=-1;}
				yield;
			}
		}
	}
}

task TPlayerSpell
{
	TText;
	task TText
	{
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		let img=dir~"img/SystemPicture.png";
		ObjPrim_SetTexture(obj,img);
		ObjSprite2D_SetSourceRect(obj,320,64,416,96);
		ObjSprite2D_SetDestCenter(obj);
		Obj_SetRenderPriority(obj,1);
		ObjRender_SetColor(obj,128,255,128);
		ObjRender_SetScaleXYZ(obj,0.8,0.8,0);
		ObjRender_SetPosition(obj,595,180,0);
		DrawPicture;
		task DrawPicture
		{
			let objy=ObjPrim_Create(OBJ_SPRITE_2D);
			let objx=ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(objy,img);
			ObjPrim_SetTexture(objx,img);
			ObjRender_SetScaleXYZ(objy,0.3,0.5,0);
			ObjRender_SetScaleXYZ(objx,0.3,0.5,0);
			Obj_SetRenderPriority(objy,1);
			Obj_SetRenderPriority(objx,1);
			let c=1;
			let cc=1/5;
			while(true)
			{
				let spelllimit=7;
				if(GetPlayerSpell>spelllimit){SetPlayerSpell(spelllimit);}
				if(GetPlayerSpell<=0){SetPlayerSpell(0);}
				ObjSprite2D_SetSourceRect(objy,0,64,32*spelllimit,96);
				ObjSprite2D_SetSourceRect(objx,0,32,32*GetPlayerSpell,64);
				ObjSprite2D_SetDestCenter(objy);
				ObjSprite2D_SetDestRect(objx,0,-16,32*GetPlayerSpell,16);
				ObjRender_SetPosition(objy,595,200,0);
				ObjRender_SetPosition(objx,595-112*0.3,200,0);
				ObjRender_SetColor(objx,192+64*c,255,192+64*c);
				c-=cc;
				if(c<0 || c>1){cc*=-1;}
				yield;
			}
		}
	}
}

task TBossLife
{
	let path1 = dir ~ "img/Lifebar.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, path1);
	Obj_SetRenderPriority(obj, 1);

	let RateRender = 0;

	let objScene = ID_INVALID;
	let alpha=256;
	loop
	{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID)
		{
			RenderBar();
		}
		if(GetPlayerY<=20 && alpha>0){alpha-=256/15;}
		if(GetPlayerY>20 && alpha<256){alpha+=256/15;}
		yield;
	}


	function RenderBar()
	{
		let TotalMax = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_MAX_LIFE);
		let Total = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_LIFE);
		let Rate = min(Total / TotalMax, RateRender);
		ObjSpriteList2D_SetSourceRect(obj,0,16,512,32);
		ObjSpriteList2D_SetDestRect(obj,0,-8,400,8);
		ObjRender_SetScaleXYZ(obj,1.04,0.7,1);
		ObjRender_SetPosition(obj,510,10,0);
		ObjRender_SetAlpha(obj,alpha/2);
		ObjSpriteList2D_AddVertex(obj);
		ObjSpriteList2D_SetSourceRect(obj,0,0,512,16);
		ObjSpriteList2D_SetDestRect(obj,0,-8,Rate*400,8);
		ObjRender_SetScaleXYZ(obj,1.03,0.4,1);
		ObjRender_SetPosition(obj,508,10,0);
		ObjRender_SetAlpha(obj,alpha/2);
		ObjSpriteList2D_AddVertex(obj);
		ObjRender_SetAngleZ(obj,180);

		RateRender += 0.01;
		RateRender = min(RateRender, 1);
	}
}

task TBossTimer
{
	let path = dir ~ "img/SystemPicture.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 1);
	ObjRender_SetY(obj,10);
	ObjRender_SetScaleXYZ(obj,0.8,0.6,0);
	let count = 2;
	
	let objScene = ID_INVALID;
	let alpha=256;
	loop
	{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID)
		{
			RenderTimer;
		}
		if(GetPlayerY<=20 && alpha>0){alpha-=256/15;}
		if(GetPlayerY>20 && alpha<256){alpha+=256/15;}
		yield;
	}

	function RenderTimer
	{
		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER);
		timer = min(timer, 60);
		let listNum = DigitToArray(timer, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj,(535-20)+iObj*10);
			ObjSpriteList2D_SetSourceRect(obj,(num*32),0,((num+1)*32),32);
			ObjSpriteList2D_SetDestRect(obj, 0, -16, 32, 16);
			ObjSpriteList2D_AddVertex(obj);
			ObjRender_SetAlpha(obj,alpha);
		}
	}
}

task TCurrentFps()
{
	let path = dir ~ "img/SystemPicture.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 1);
	ObjRender_SetY(obj,465);
	ObjRender_SetScaleXYZ(obj,0.8,0.6,0);
	let count = 2;
	texttime;
	
	task texttime
	{
		let objx = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(objx, path);
		Obj_SetRenderPriority(objx, 1);
		ObjRender_SetPosition(objx,610,465,0);
		ObjSprite2D_SetSourceRect(objx,288,96,352,128);
		ObjSprite2D_SetDestCenter(objx);
		ObjRender_SetScaleXYZ(objx,0.6,0.6,0);
		loop
		{
			RenderTimer();
			yield;
		}
	}

	function RenderTimer()
	{
		let timer = GetCurrentFps();
		timer = min(timer, 99.99);
		let listNum = DigitToArray(timer, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj,(585-15)+iObj*10);
			ObjSpriteList2D_SetSourceRect(obj,(num*32),0,((num+1)*32),32);
			ObjSpriteList2D_SetDestRect(obj, 0, -16, 32, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
	}
}

task TReplayFps()
{
	if(!IsReplay()){return;}

	let path = dir ~ "img/SystemPicture.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 1);
	ObjRender_SetY(obj,465);
	ObjRender_SetScaleXYZ(obj,0.8,0.6,0);
	let count = 2;
	texttime;
	
	task texttime
	{
		let objx = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(objx, path);
		Obj_SetRenderPriority(objx, 1);
		ObjRender_SetPosition(objx,610,465,0);
		ObjSprite2D_SetSourceRect(objx,288,96,352,128);
		ObjSprite2D_SetDestCenter(objx);
		ObjRender_SetScaleXYZ(objx,0.6,0.6,0);
		let alp=0;
		loop
		{
			RenderTimer();
			yield;
		}
	}

	function RenderTimer()
	{
		let timer = GetReplayFps();
		timer = min(timer, 99.99);
		let listNum = DigitToArray(timer, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj,(585-15)+iObj*10);
			ObjSpriteList2D_SetSourceRect(obj,(num*32),0,((num+1)*32),32);
			ObjSpriteList2D_SetDestRect(obj, 0, -16, 32, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
	}
}

function DigitToArray(let digit,let count)
{
	let res = [];
	digit = truncate(digit);

	loop
	{
		let tnum = truncate(digit % 10);
		digit /= 10;
		res = [tnum] ~ res;
		if(truncate(digit) == 0){break;}
	}

	loop(max(0, count - length(res))){res = [0] ~ res;}

	return res;
}

task PlaySound(str,v,w)
{
	let s = ObjSound_Create;
	if(str == "select"){ObjSound_Load(s,dir~"sound/select.wav");}
	if(str == "ok"){ObjSound_Load(s,dir~"sound/ok.wav");}
	loop(w){yield;}
	ObjSound_SetSoundDivision(s,SOUND_SE);
	ObjSound_SetVolumeRate(s,v);
	ObjSound_Play(s);
}

function ReturnColor(type)
{
	if(type=="RED"){return [255,0,0];}
	if(type=="ORANGE"){return [255,128,0];}
	if(type=="YELLOW"){return [255,255,0];}
	if(type=="GREEN"){return [0,255,0];}
	if(type=="BLUE"){return [0,0,255];}
	if(type=="PURPLE"){return [255,0,255];}
	if(type=="WHITE"){return [255,255,255];}
}
