//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
//Functions for the actual stage.
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------

#include "./functions_stage.txt"

@Initialize
{
	TMain;
	SetAutoDeleteObject(true);
}

@MainLoop
{
	yield;
}


task TEndChapter
{
	let enemyCountM = Obj_GetValueD(objPlayer,"enemyCountM",0);
	let enemyCount = Obj_GetValueD(objPlayer,"enemyCount",0);
	if(enemyCountM == 0)
		{
		enemyCountM = 1000000000;
		enemyCount = 1000000000;
		}
	let graze = GetChapterGraze;
	let count = min(enemyCountM,enemyCountM-enemyCount);
	let countM = enemyCountM;

	let array = GetAllEnemyID;
	ascent(i in 0..length(array))
		{
		if(Obj_GetValueD(array[i],"STAGE_SKIP_DELETE",false) == false)
			{
			ObjEnemy_SetLife(array[i],0);
			}
		}

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "img/chapter.png");
	Obj_SetRenderPriority(obj, 0.79);

	let objS = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objS, GetCurrentScriptDirectory ~ "img/chapter.png");
	ObjSprite2D_SetSourceRect(objS,0,32+(4)*32,192,32+(4+1)*32);
	ObjRender_SetPosition(objS,GetStgFrameWidth*0.5-15,GetStgFrameWidth*0.5+90,0);
	Obj_SetRenderPriority(objS, 0.79);

	let color = 0;
	ascent(t in 0..195)
		{
		if(t < 2)
			{
			SetChapterGraze(0);
			}
		if(t < 5)
			{
			DeleteShotAll(TYPE_ALL,TYPE_ITEM);
			Obj_SetValue(objPlayer,"enemyCountM",0);
			Obj_SetValue(objPlayer,"enemyCount",0);
			}
		ObjSpriteList2D_ClearVertexCount(obj);
		TRenderList(t);
		ObjRender_SetPosition(obj,GetStgFrameWidth*0.5,GetStgFrameWidth*0.5,0);
		let scale0 = 0.5-0.5*cos(min(t,15)*180/15);
		let scale1 = 0.5-0.5*cos(max(0,min(t-90,15)*180/15));
		if(t >= 180)
			{
			scale0 = 1-(t-180)/14;
			scale1 = 1-(t-180)/14;
			}
		if(graze*count/countM < 200)
			{
			scale1 = 0;
			}	
		else if(t == 105)
			{
			SetPlayerSpell(GetPlayerSpell+0.2);
			}
		else if(t > 105 && t < 180)
			{
			if(t%10 == 0)
				{
				ObjRender_SetAlpha(objS,105);
				}
			else if(t%10 == 5)
				{
				ObjRender_SetAlpha(objS,255);
				}
			}
		ObjRender_SetScaleXYZ(obj,scale0*0.65,0.65,0);

		ObjRender_SetScaleXYZ(objS,scale1*0.65,0.65,0);
		ObjSprite2D_SetDestCenter(objS);
		yield;
		}

	Obj_Delete(obj);
	Obj_Delete(objS);
	
	task TRenderList(t)
		{
		//Chapter Finish
		ObjSpriteList2D_SetSourceRect(obj,0,0,192,32);
		ObjRender_SetScaleXYZ(obj,1.2,1.2,0);
		ObjRender_SetPosition(obj,-15,-120,0);
		ObjSpriteList2D_SetDestCenter(obj);
		ObjSpriteList2D_AddVertex(obj);

		ascent(i in 0..4)
			{
			let iA = i;
			if(i == 3)
				{
				iA = 2;
				}
			else if(i == 2)
				{
				iA = 3;
				}
			let iB = i-4*0.5+0.5;
			ObjSpriteList2D_SetSourceRect(obj,0,32+iA*32,192,32+(iA+1)*32);
			ObjRender_SetPosition(obj,-80,iB*180/4,0);
			ObjRender_SetScaleXYZ(obj,1,1,0);
			ObjSpriteList2D_SetDestCenter(obj);
			ObjSpriteList2D_AddVertex(obj);

			ascent(i1 in 0..4)
				{
				let num = CutNumber(i,i1,t);
				NumberToRect(obj,num);
				if(num == 12)
					{
					i1 = -1;
					}
				ObjRender_SetPosition(obj,120-i1*17,iB*180/4-4,0);
				ObjRender_SetScaleXYZ(obj,1.125,1.125,0);
				ObjSpriteList2D_SetDestCenter(obj);
				ObjSpriteList2D_AddVertex(obj);
				}
			}
		ObjSpriteList2D_CloseVertex(obj);
		}
	function CutNumber(type,digit,t)
		{
		let num = 0;
		alternative(type)
			case(0)
				{
				num = graze;
				}
			case(1)
				{
				num = trunc(100*count/countM);
				}
			case(2)
				{
				num = 0;
				}
			case(3)
				{
				num = 10;
				}
		if(t < 45)
			{
			num = 0;
			}
		else if(t < 135)
			{
			num = min(num,(t-30)*max(num,100)/90);
			}
		let name = trunc(num/(10^digit))%10;
		if(name == 0 && digit != 0 && num < 10^digit)
			{
			name = 14;
			}
		if(digit == 3 && type == 1)
			{
			name = 12;
			}
		return name;
		}
	task NumberToRect(obj,num)
		{
		num = trunc(num);
		ObjSpriteList2D_SetSourceRect(obj,9+num*16,224,9+(num+1)*16,244);
		}
}