let dirCurrent = GetCurrentScriptDirectory();
let alpha = 0;
let quality = 2;

@Initialize{
	InstallFont(dirCurrent ~ "Impact.ttf");
	SetStgFrame(128, 16, 512, 464, 20, 80);
	SetPlayerClip(16, 16, 384-16, 448-16);
	SetPauseScriptPath(dirCurrent ~ "Parsee_Pause.txt");
	SetEndSceneScriptPath(dirCurrent ~ "Parsee_EndScene.txt");
	InitFrame();
	TTextLife;
	TTextBomb;
	MaxBomb();
	MaxLife();
	BossLife();
	TBossTimer();
	TCurrentFps();
	TReplayFps();}

@MainLoop{
	PlayerLife();
	TPlayerSpell();
	yield;}

@Event{ }

function InitFrame(){
	let path = GetCurrentScriptDirectory() ~ "img/StgBackground.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 0);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);}

task PlayerLife{
        loop(2){yield}

	let space = 0;
	let point = GetPlayerLife();

	loop(point){
	let objL2 = ObjPrim_Create(OBJ_SPRITE_2D);
        let Target = GetCurrentScriptDirectory~"/img/front00.png";
        Obj_SetRenderPriority(objL2,0.01);
	ObjPrim_SetTexture(objL2, Target);

	ObjRender_SetBlendType(objL2, BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(objL2, 240, 0, 256, 18);
	ObjSprite2D_SetDestCenter(objL2);
        ObjRender_SetScaleXYZ(objL2,1,1,0);

	ObjRender_SetX(objL2, 580);
	ObjRender_SetY(objL2, 80+space);
        space+=18;
        loop(2){yield}
	Obj_Delete(objL2);}}

task MaxLife{
	let space = 0;
        loop(9){
	let objL2 = ObjPrim_Create(OBJ_SPRITE_2D);
        let Target = GetCurrentScriptDirectory~"/img/front00.png";
        Obj_SetRenderPriority(objL2,0.01);
	ObjPrim_SetTexture(objL2, Target);

	ObjRender_SetBlendType(objL2, BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(objL2, 160, 0, 176, 18);
	ObjSprite2D_SetDestCenter(objL2);
        ObjRender_SetScaleXYZ(objL2,1,1,0);

	ObjRender_SetX(objL2, 580);
	ObjRender_SetY(objL2, 80+space);
        space+=18;}}

task TTextLife{
	let objGraze = ObjText_Create();
	ObjText_SetText(objGraze, "Life");
	ObjText_SetFontSize(objGraze, 32);
	ObjText_SetFontType(objGraze,"Impact");
	ObjText_SetFontColorTop(objGraze, 255, 255, 255);
	ObjText_SetFontColorBottom(objGraze, 255, 255, 255);
	Obj_SetRenderPriority(objGraze,1);
	ObjRender_SetX(objGraze, 560);
	ObjRender_SetY(objGraze, 30);}

task TPlayerSpell{
        loop(2){yield}
	let space = 0;
	let point = GetPlayerSpell();

	loop(point){
	let objL2 = ObjPrim_Create(OBJ_SPRITE_2D);
        let Target = GetCurrentScriptDirectory~"/img/front00.png";
        Obj_SetRenderPriority(objL2,0.01);
	ObjPrim_SetTexture(objL2, Target);

	ObjRender_SetBlendType(objL2, BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(objL2, 240, 20, 256, 38);
	ObjSprite2D_SetDestCenter(objL2);
        ObjRender_SetScaleXYZ(objL2, 1, 1, 0);

	ObjRender_SetX(objL2, 580);
	ObjRender_SetY(objL2, 280+space);
        space+=18;
        loop(2){yield}
	Obj_Delete(objL2);}}

task MaxBomb{
	let space = 0;
        loop(9){
	let objL2 = ObjPrim_Create(OBJ_SPRITE_2D);
        let Target = GetCurrentScriptDirectory~"/img/front00.png";
        Obj_SetRenderPriority(objL2,0.01);
	ObjPrim_SetTexture(objL2, Target);

	ObjRender_SetBlendType(objL2, BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(objL2, 160, 20, 177, 38);
	ObjSprite2D_SetDestCenter(objL2);
        ObjRender_SetScaleXYZ(objL2, 1, 1, 0);

	ObjRender_SetX(objL2, 580);
	ObjRender_SetY(objL2, 280+space);
        space+=18;}}

task TTextBomb{
	let objGraze = ObjText_Create();
	ObjText_SetText(objGraze, "Spell");
	ObjText_SetFontSize(objGraze, 32);
	ObjText_SetFontType(objGraze,"Impact");
	ObjText_SetFontColorTop(objGraze, 255, 255, 255);
	ObjText_SetFontColorBottom(objGraze, 255, 255, 255);
	Obj_SetRenderPriority(objGraze,1);
	ObjRender_SetX(objGraze, 550);
	ObjRender_SetY(objGraze, 230);}

task BossLife{
	let path = GetCurrentScriptDirectory ~ "img/lifebar.png";
	let countVertex = 74;
	let objScene = ID_INVALID;
	
	let objStar = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objStar, GetCurrentScriptDirectory ~ "img/frame2.png");
	Obj_SetRenderPriority(objStar, 0.7);
	ObjSpriteList2D_SetSourceRect(objStar, 144, 80, 156, 92);

	let objLife = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objLife, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objLife, countVertex);
	ObjRender_SetBlendType(objLife, BLEND_ALPHA);
	Obj_SetRenderPriority(objLife, 0.69);
	ObjPrim_SetTexture(objLife, path);

	let objOutline = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objOutline, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objOutline, countVertex);
	ObjRender_SetBlendType(objOutline, BLEND_ALPHA);
	Obj_SetRenderPriority(objOutline, 0.68);
	ObjPrim_SetTexture(objOutline, path);

	let objKnub = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objKnub, path);
	Obj_SetRenderPriority(objKnub, 0.69);
	ObjSpriteList2D_SetSourceRect(objKnub, 20, 0, 29, 11);
	ObjSpriteList2D_SetDestRect(objKnub, -2.5, -3, 2.5, 3);

	ascent (iVert in 0..countVertex/2){
		let vertex = iVert * 2;
		ObjPrim_SetVertexUVT(objLife, vertex+0, 0, 0);
		ObjPrim_SetVertexUVT(objLife, vertex+1, 10, 32);
		ObjPrim_SetVertexUVT(objOutline, vertex+0, 10, 0);
		ObjPrim_SetVertexUVT(objOutline, vertex+1, 19, 32);}

	let mx = 0;
	let my = 0;
	let fade = 255;
	let fadec = 255;
	let maxRadius = 64;

	let lastRemStep = 0;
	let countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
	let lifeRateRender = 0;
	let outlineRender = 0;

	let lifeTotalMax = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_MAX_LIFE);
	let lifeTotal = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_LIFE);
	let lifeRate = min(lifeTotal / lifeTotalMax, lifeRateRender);



	ObjRender_SetScaleXYZ(objLife, -1, 1, 1);
	ObjRender_SetScaleXYZ(objOutline, -1, 1, 1);

	ObjRender_SetPosition(objStar, 0, 12, 0);

	while(GetCommonData("quality", 2)==quality){
		ObjSpriteList2D_ClearVertexCount(objStar);
		ObjSpriteList2D_ClearVertexCount(objKnub);


		if(outlineRender<1){
			ObjRender_SetAlpha(objStar, min(255, 255*(outlineRender*3)-510));
		}else{
			if(GetPlayerY < max(96, 26+(truncate(countRemStep/12)*12))){
				fade = max(fade-10, 85);
			}else{fade = min(fade+10, 255);}
			ObjRender_SetAlpha(objStar, fade);}

		objScene = GetEnemyBossSceneObjectID;
		if(objScene != ID_INVALID){
			let Boss = GetEnemyBossObjectID;
			Boss = Boss ~ [objLife];
			mx = ObjRender_GetX(Boss[0]);
			my = ObjRender_GetY(Boss[0]);
		}else{
			mx = -400;
			my = -400;
			outlineRender = 0;
			lifeRateRender = 0;}
		RefreshVertex(objLife);

		if(GetDistance(GetPlayerX,GetPlayerY,mx,my)<maxRadius*1.75){
			fadec = max(fadec-10, 85);
		}else{fadec = min(fadec+10, 255);}
		ObjRender_SetAlpha(objKnub, (fadec/100)*(lifeRateRender*100));
		ObjRender_SetAlpha(objOutline, fadec);
		ObjRender_SetAlpha(objLife, fadec);

		if(GetCommonData("ForceLifebar", 0) != 0){
			outlineRender = 0;
			lifeRateRender = 0;}
		lastRemStep = countRemStep;
		yield;}
	Obj_Delete(objStar);
	Obj_Delete(objKnub);
	Obj_Delete(objOutline);
	Obj_Delete(objLife);

	task RefreshVertex(objid){
		countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
		if(lastRemStep != countRemStep){
			lifeRateRender = 0;}
		ascent(iStep in 0 .. countRemStep){
			ObjSpriteList2D_SetSourceRect(objStar, 144, 80, 156, 92);
			let y = truncate((iStep)/12)*12;
			let i = iStep-(12*(y/12));
			ObjSpriteList2D_SetDestRect(objStar, 4 + i * 12, 4+y,
				4 + (i + 1) * 12, 16+y);
			ObjSpriteList2D_AddVertex(objStar);}

		let listLifeDiv = [0]~ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_LIFE_RATE_LIST);
		ascent(iDiv in 0 .. length(listLifeDiv)){
			let kang = 360*(listLifeDiv[iDiv]);
			let kx = mx+(maxRadius*1.05)*cos(kang-90);
			let ky = my+(maxRadius*1.05)*sin(kang-90);
			if(1-listLifeDiv[iDiv]<lifeTotal / lifeTotalMax-0.000001){
				ObjRender_SetPosition(objKnub, kx, ky, 0);
				ObjRender_SetAngleZ(objKnub, kang);
				ObjSpriteList2D_AddVertex(objKnub);}}

		lifeTotalMax = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_MAX_LIFE);
		lifeTotal = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_LIFE);
		lifeRate = min(lifeTotal / lifeTotalMax, lifeRateRender);



		ascent(iVert in 0..countVertex/2){
			let indexVert = iVert * 2;
			let angle = (360 / (countVertex/2 - 1) * iVert);
			angle = min(360*(outlineRender), angle);
			let x = (maxRadius)*cos(angle-90);
			let y = (maxRadius)*sin(angle-90);
			ObjPrim_SetVertexPosition(objOutline, indexVert + 0 , x, y, 0);
			x = (maxRadius*1.13)*cos(angle-90);
			y = (maxRadius*1.13)*sin(angle-90);
			ObjPrim_SetVertexPosition(objOutline, indexVert + 1 , x, y, 0);

			angle = min(360*(lifeRate), angle);
			x = (maxRadius)*cos(angle-90);
			y = (maxRadius)*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife, indexVert + 0 , x, y, 0);
			x = (maxRadius*1.13)*cos(angle-90);
			y = (maxRadius*1.13)*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife, indexVert + 1 , x, y, 0);}
		ObjRender_SetPosition(objLife, mx, my, 0);
		ObjRender_SetPosition(objOutline, mx, my, 0);

		lifeRateRender = min(1, lifeRateRender+0.02);
		outlineRender = min(1, outlineRender+0.016);}}

task TBossTimer{
	let  csd = GetCurrentScriptDirectory;
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Number.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.75);
	ObjRender_SetY(obj, 0);
	let count = 2;
   
	let objScene = ID_INVALID;
	let t = csd~"./se/se_timeout.wav";
	LoadSound(t);
	loop{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID){
			RenderTimer();
		 if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER) > 10){
		   ObjRender_SetColor(obj,255,255,255);}
	
		    if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER)<=10){
		   ObjRender_SetColor(obj,255,120,0);
		   LoadSound(t);
                   PlaySE(t);
                   loop(60){yield;}}
		   if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER)<=3 && ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER)>0){
		   ObjRender_SetColor(obj,255,20,0);
		   t = csd~"./se/se_timeout2.wav";}
		   
		   if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER)==0){
		    ObjRender_SetColor(obj,255,20,0);
		   t = csd~"./se/se_timeout2.wav";
		    PlaySE(t);
		   RenderTimer();
		   if(GetCommonData("sos",2)){loop(9999){yield;}}
		   
		   }
		   
		   
		   
		   }
		yield;}
			

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

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(obj, 352 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 26);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			ObjSpriteList2D_AddVertex(obj);}}}

task TCurrentFps(){
	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 15);
	ObjText_SetFontType(objText,"Impact");
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetMaxWidth(objText, GetScreenWidth() - 8);
	Obj_SetRenderPriority(objText, 1.0);
	ObjRender_SetX(objText, 560);
	ObjRender_SetY(objText, 460);
	loop{
		let fps = GetCurrentFps();
		let text = vtos("1.2f", fps) ~ " fps";
		ObjText_SetText(objText, text);
		yield;}}

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

	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 12);
	ObjText_SetFontType(objText,"Impact");
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	Obj_SetRenderPriority(objText, 1.0);

	let px = GetStgFrameLeft() + GetStgFrameWidth() - 18;
	let py = GetStgFrameTop() + GetScreenHeight() - 14;
	ObjRender_SetX(objText, px);
	ObjRender_SetY(objText, py);

	loop{
		let fps = GetReplayFps();
		let text = vtos("02d", fps);
		ObjText_SetText(objText, text);
		yield;}}


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

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