
let current = GetCurrentScriptDirectory();
let pathDigit = GetCurrentScriptDirectory() ~ "img/Cirno_SystemDigit.png";
let pathHealth = GetCurrentScriptDirectory() ~ "img/Default_System.png";

@Initialize{
	SetStgFrame(128, 16, 512, 464, 20, 80);
	InitFrame;
	SetPauseScriptPath(current ~ "Cirno_Pause.txt");
	SetEndSceneScriptPath(current ~ "Cirno_EndScene.txt");
	SetReplaySaveSceneScriptPath(current ~ "Cirno_ReplaySaveScene.txt");
	Motivation;
	IcePower;
	PerfectFreeze;
	IceArea;
	Difficulty;
	//TScore();
	//TGraze();
	//TPlayerSpell();
	TBossLife;
	TBossTimer;
}
let CommonDataSet = 10;

@MainLoop{
	if(CommonDataSet==0){
		SetAreaCommonData("CirnoPlayer", "SystemType", "Cirno_System");
	}
	CommonDataSet--;
	yield;
}

@Event{
	alternative(GetEventType)
	case(EV_START_BOSS_SPELL){
		let path = current ~ "Cirno_System_MagicCircle.txt";
		let id = LoadScript(path);
		StartScript(id);
	}
	case(EV_GAIN_SPELL){
		let objScene = GetEnemyBossSceneObjectID;
		let score = ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE);
		TGainSpell(score);
	}
}



function InitFrame{
	let path = GetCurrentScriptDirectory ~ "img/frame.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);
}

task Difficulty{
	let path = GetCurrentScriptDirectory ~ "img/frame2.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 1);
	ObjRender_SetPosition(obj, -60, -60, 0);
	loop(30){yield}
	while(GetCommonData("SelectionSet", 2) != 2){yield;}
	if(GetCommonData("StageDiffic", 36)==12){
		ObjSprite2D_SetSourceRect(obj, 268, 305, 326, 321);
	}
	if(GetCommonData("StageDiffic", 36)==36){
		ObjSprite2D_SetSourceRect(obj, 268, 321, 326, 337);
	}
	if(GetCommonData("StageDiffic", 36)==48){
		ObjSprite2D_SetSourceRect(obj, 268, 337, 326, 353);
	}
	if(GetCommonData("StageDiffic", 36)==72){
		ObjSprite2D_SetSourceRect(obj, 268, 353, 326, 369);
	}
	ObjRender_SetPosition(obj, 576, 32, 0);
	ObjSprite2D_SetDestRect(obj, -27, -8, 27, 8);
}

task TScore{
	//Score文字列
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, "Score");
	ObjText_SetFontSize(objScore, 20);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 255, 255);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,128, 128, 128);
	ObjText_SetFontBorderWidth(objScore, 2);
	Obj_SetRenderPriority(objScore, 0.01);
	ObjRender_SetX(objScore, 428);
	ObjRender_SetY(objScore, 48);

	//得点
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 12;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 72);

	while(true)
	{
		let score = GetScore();
		score = min(score, 999999999999);
		let listNum = DigitToArray(score, count);

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

task TGraze{
	//Graze文字列
	let objGraze = ObjText_Create();
	ObjText_SetText(objGraze, "Graze");
	ObjText_SetFontSize(objGraze, 20);
	ObjText_SetFontBold(objGraze, true);
	ObjText_SetFontColorTop(objGraze, 255, 255, 255);
	ObjText_SetFontColorBottom(objGraze, 255, 255, 255);
	ObjText_SetFontBorderType(objGraze, BORDER_FULL);
	ObjText_SetFontBorderColor(objGraze,128, 128, 128);
	ObjText_SetFontBorderWidth(objGraze, 2);
	Obj_SetRenderPriority(objGraze, 0.01);
	ObjRender_SetX(objGraze, 428);
	ObjRender_SetY(objGraze, 98);

	//得点
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 5;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 122);

	while(true)
	{
		let graze = GetGraze();
		graze = min(graze, 99999);
		let listNum = DigitToArray(graze, count);

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


task Motivation{
	let count = 3;
	let MainX = 554;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 70);

	while(true){
		let point = GetAreaCommonData("CirnoPlayer", "Motivation", GetPlayerLife*100);
		point = min(point, 999);
		point = max(0, point);

		if(point>=100){count = 3; MainX = 548;}
		if(point>=10 && point<100){count = 2; MainX = 558;}
		if(point<10){count = 1; MainX = 564;}
		
		let listNum = DigitToArray(point, count);
		
		listNum = listNum ~ [10];
		
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count+1){
			let num = listNum[iObj];
			ObjRender_SetX(obj, MainX + iObj * 16);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 16);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

task IcePower{
	let count = 3;
	let MainX = 554;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 118);

	while(true){
		let point = GetAreaCommonData("CirnoPlayer", "IcePower", 0);
		point = min(point, 100);
		point = max(0, point);

		if(point>=100){count = 3; MainX = 548;}
		if(point>=10 && point<100){count = 2; MainX = 558;}
		if(point<10){count = 1; MainX = 564;}
		
		let listNum = DigitToArray(point, count);
		
		listNum = listNum ~ [10];
		
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count+1){
			let num = listNum[iObj];
			ObjRender_SetX(obj, MainX + iObj * 16);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 16);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

task PerfectFreeze{
	let count = 3;
	let MainX = 554;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 168);

	while(true){
		let point = GetAreaCommonData("CirnoPlayer", "PerfectFreeze", GetPlayerSpell*100);
		point = min(point, 999);
		point = max(0, point);

		if(point>=100){count = 3; MainX = 548;}
		if(point>=10 && point<100){count = 2; MainX = 558;}
		if(point<10){count = 1; MainX = 564;}
		
		let listNum = DigitToArray(point, count);
		
		listNum = listNum ~ [10];
		
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count+1){
			let num = listNum[iObj];
			ObjRender_SetX(obj, MainX + iObj * 16);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 16);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

task IceArea{
	let count = 4;
	let MainX = 554;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 356);

	while(true){
		let playerpower = GetPlayerPower*100;
		if(playerpower>9999){
			playerpower = GetPlayerPower*10
		}
		let point = GetAreaCommonData("CirnoPlayer", "IceArea", playerpower);
		point = min(point, 9999);
		point = max(0, point);

		if(point>=1000){count = 4; MainX = 538;}
		if(point>=100 && point < 1000){count = 3; MainX = 548;}
		if(point>=10 && point<100){count = 2; MainX = 558;}
		if(point<10){count = 1; MainX = 564;}
		
		let listNum = DigitToArray(point, count);
		
		listNum = listNum ~ [10];
		
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count+1){
			let num = listNum[iObj];
			ObjRender_SetX(obj, MainX + iObj * 16);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 16);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}


task TPlayerSpell{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Spell");
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,128, 128, 128);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 202);

	//スペル
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 2;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 226);

	while(true)
	{
		let point = GetPlayerSpell();
		point = min(point, 99);
		let listNum = DigitToArray(point, count);

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


task TBossLife{
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathHealth);
	Obj_SetRenderPriority(obj, 0.7);

	let lastRemStep = -1;
	let lifeRateRender = 0;

	let objScene = ID_INVALID;
	loop
	{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID)
		{
			RenderLife();
		}
		yield;
	}


	function RenderLife()
	{
		//残りステップ
		let countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
		if(lastRemStep != countRemStep)
		{
			//ステップが変化
			lifeRateRender = 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);
		ObjSpriteList2D_SetSourceRect(obj, 1, 1, 127, 11);
		ObjSpriteList2D_SetDestRect(obj, 72, 8, 72 + 270 * lifeRate, 12);
		ObjSpriteList2D_AddVertex(obj);

		//ライフの区切り表示
		ObjSpriteList2D_SetSourceRect(obj, 132, 1, 137, 11);
		let listLifeDiv = [0] ~ ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_LIFE_RATE_LIST);
		ascent(iDiv in 0 .. length(listLifeDiv))
		{
			let rate = listLifeDiv[iDiv];
			let x = 72 + 270 * (1-rate);
			ObjSpriteList2D_SetDestRect(obj, x-1, 4, x + 1, 14);
			ObjSpriteList2D_AddVertex(obj);
		}

		//残りステップ表示
		ObjSpriteList2D_SetSourceRect(obj, 1, 1, 127, 11);
		ascent(iStep in 0 .. countRemStep)
		{
			let remStepRate = 58 / countRemStep;
			ObjSpriteList2D_SetDestRect(obj, 4 + iStep * remStepRate + 2, 8,
				4 + (iStep + 1) * remStepRate, 12);
			ObjSpriteList2D_AddVertex(obj);
		}

		lifeRateRender += 0.01;
		lifeRateRender = min(lifeRateRender, 1);
		lastRemStep = countRemStep;
	}
}

task TBossTimer{

	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;
	loop{
		objScene = GetEnemyBossSceneObjectID;
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID){
			RenderTimer;
		}
		yield;
	}

	function RenderTimer{
		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER);
		timer = min(timer, 99);
		timer = max(0, timer);
		
		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, 16);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
		
	}

}

task TGainSpell(score){
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Spell Card Bonus!");
	ObjText_SetFontSize(objText, 32);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 0.6);
	ObjRender_SetX(objText, 32);
	ObjRender_SetY(objText, 98);

	let strScore = "+" ~ IntToString(score);
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, strScore);
	ObjText_SetFontSize(objScore, 32);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 128, 128);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,255, 255, 255);
	ObjText_SetFontBorderWidth(objScore, 1);
	Obj_SetRenderPriority(objScore, 0.6);
	ObjRender_SetX(objScore, 180);
	ObjRender_SetY(objScore, 140);

	loop(120)
	{
		yield;
	}
	Obj_Delete(objText);
	Obj_Delete(objScore);
}


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

