
let current = GetCurrentScriptDirectory;
let pathDigit = current ~ "img/SystemDigit.png";
let pathHealth = current ~ "img/Default_System.png";
let circ = LoadScript(current ~ "S_System_MagicCircle.txt");
let psuedoscore = 0;
let objDiff = ID_INVALID;

@Initialize{
	SetStgFrame(128, 16, 512, 464, 20, 80);
	SetPauseScriptPath(current ~ "S_Pause.txt");
	SetEndSceneScriptPath(current ~ "S_EndScene.txt");
	SetReplaySaveSceneScriptPath(current ~ "S_ReplaySaveScene.txt");
	a;
	InitFrame;
	
	//Difficulty(30);
	//TScore();
	//TGraze();
	//TPlayerSpell();
	//TPlayerLife;
	//TestLife;
	//TBossLife;
	BossTimer;
}
task a{
	loop(5){yield}
	SetAreaCommonData("ds_AyaPlayer", "DoubleSpoiler", true);
	SetCommonData("StgFrameOffsetX", 128);
	SetCommonData("StgFrameOffsetY", 16);
}

@MainLoop{
	if(GetCommonData("DifficultyChange", false)==true){
		Obj_Delete(objDiff);
		Difficulty(1);
		SetCommonData("DifficultyChange", false);
	}
	SetCommonData("PsuedoScore", psuedoscore);
	yield;
}

@Event{
	alternative(GetEventType)
	case(EV_START_BOSS_SPELL){
		StartScript(circ);
	}
	case(EV_GAIN_SPELL){
		let objScene = GetEnemyBossSceneObjectID;
		let score = ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE);
		GotSpellCard(score);
		AddScoreEx(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(yielder){
	let path = GetCurrentScriptDirectory ~ "img/frame2.png";
	objDiff = ObjPrim_Create(OBJ_SPRITE_2D);
	
	let blink = 12;
	let blinkcount = 8;
	let alpha = 255;
	let frame = 0;
	
	ObjPrim_SetTexture(objDiff, path);
	Obj_SetRenderPriority(objDiff, 1);
	ObjRender_SetPosition(objDiff, -60, -60, 0);
	
	loop(yielder){yield}
	while(GetCommonData("SelectionSet", 2) != 2){yield;}
	if(GetCommonData("StageDiffic", 36)==12){ObjSprite2D_SetSourceRect(objDiff, 13, 304, 84, 320);}
	if(GetCommonData("StageDiffic", 36)==36){ObjSprite2D_SetSourceRect(objDiff, 13, 320, 84, 336);}
	if(GetCommonData("StageDiffic", 36)==48){ObjSprite2D_SetSourceRect(objDiff, 13, 336, 84, 352);}
	if(GetCommonData("StageDiffic", 36)==72){ObjSprite2D_SetSourceRect(objDiff, 13, 352, 84, 368);}
	if(GetCommonData("StageDiffic", 36)> 72){ObjSprite2D_SetSourceRect(objDiff, 88, 368, 180, 384);}
	ObjSprite2D_SetDestCenter(objDiff);
	
	loop(yielder){yield}
	
	ObjRender_SetPosition(objDiff, GetStgFrameWidth/2+48, 48, 0);
	
	loop((blinkcount*blink)*2){
		if(frame==blink){ObjRender_SetY(objDiff, -60);}
		if(frame==blink*2){ObjRender_SetY(objDiff, 48);frame = 0;}
		frame++;
		yield;
	}
	ObjRender_SetY(objDiff, 48);
	while(alpha>0){
		alpha-=25.5;
		ObjRender_SetAlpha(objDiff, alpha);
		yield;
	}
	loop(45){yield};
	ObjRender_SetPosition(objDiff, 532, 32, 0);
	while(alpha<255){
		alpha+=5;
		ObjRender_SetAlpha(objDiff, alpha);
		yield;
	}
}

task TScore{
	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, 1);
	Obj_SetRenderPriority(objScore, 0.01);
	ObjRender_SetX(objScore, 428);
	ObjRender_SetY(objScore, 48);

	let count = 12;

	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, 72);

	while(true){
		let score = GetScore;
		score = min(score, 999999999999);
		let listNum = DigitToArray(score, count);
		count = GetNumSize(score);
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(obj, 440 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

task TGraze{
	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, 1);
	Obj_SetRenderPriority(objGraze, 0.01);
	ObjRender_SetX(objGraze, 428);
	ObjRender_SetY(objGraze, 98);

	let count = 5;

	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, 122);

	while(true){
		let graze = GetGraze;
		count = GetNumSize(graze);
		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 * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			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, 1);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 202);

	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 * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

task TPlayerLife{
	let objText = ObjText_Create;
	ObjText_SetText(objText, "Player");
	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, 1);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 148);

	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, 170);

	while(true){
		let point = GetPlayerLife;
		point = min(max(0, 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 * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			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 BossTimer{

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.8);
	ObjRender_SetY(obj, 6);
	let count = 3;

	let objScene = ID_INVALID;
	loop{
		objScene = GetEnemyBossSceneObjectID;
		ObjSpriteList2D_ClearVertexCount(obj);
			RenderTimer;
		yield;
	}

	function RenderTimer{
		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER);
		timer = max(0, min(timer, 999.99));
		
		let listNum = DigitToArray(timer, count);
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0..count){
			let num = listNum[iObj];
			ObjSpriteList2D_SetSourceRect(obj, num * 32, 0, (num + 1) * 32, 32);
			
			ObjRender_SetX(obj, GetStgFrameWidth/2 -32 + iObj * 24);
			ObjSpriteList2D_SetDestRect(obj, -32, 0, 0, 32);
				
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
		
	}

}

task GotSpellCard(score){
	let path = GetCurrentScriptDirectory ~ "img/frame2.png";
	let objText = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objText, path);
	ObjSprite2D_SetSourceRect(objText, 0, 99, 264, 131);
	ObjSprite2D_SetDestRect(objText, 0, 0, 264, 32);
	Obj_SetRenderPriority(objText, 0.8);
	ObjRender_SetX(objText, 64);
	ObjRender_SetY(objText, 98);

	let objScore = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	let count = GetNumSize(score);
	let listNum = DigitToArray(score, count);
	ObjPrim_SetTexture(objScore, pathDigit);
	Obj_SetRenderPriority(objScore, 0.8);
	ObjRender_SetY(objScore, 140);
	
	RefreshVertex;
	loop(120){
		yield;
	}
	let alpha = 255;
	while(alpha>0){
		alpha-=15;
		RefreshVertex;
		ObjRender_SetAlpha(objText, alpha);
		ObjRender_SetAlpha(objScore, alpha);
		yield;
	}
	
	ObjSpriteList2D_ClearVertexCount(objScore);
	Obj_Delete(objText);
	Obj_Delete(objScore);

	task RefreshVertex{
		ObjSpriteList2D_ClearVertexCount(objScore);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(objScore, 182 + iObj * 20);
			ObjSpriteList2D_SetSourceRect(objScore, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(objScore, 0, 0, 20, 25);
			ObjSpriteList2D_AddVertex(objScore);
		}
	}
}


function DigitToArray(digit, 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 GetNumSize(value){
	let numsize = 1;
	if(value>=10&&value<100){numsize = 2;}
	if(value>=100&&value<1000){numsize = 3;}
	if(value>=1000&&value<10000){numsize = 4;}
	if(value>=10000&&value<100000){numsize = 5;}
	if(value>=100000&&value<1000000){numsize = 6;}
	if(value>=1000000&&value<10000000){numsize = 7;}
	if(value>=10000000&&value<100000000){numsize = 8;}
	if(value>=100000000&&value<1000000000){numsize = 9;}
	if(value>=1000000000&&value<10000000000){numsize = 10;}
	if(value>=10000000000&&value<100000000000){numsize = 11;}
	if(value>=1000000000000){numsize = 12;}
	return numsize;
}

task AddScoreEx(val){
	let time = val/45;
	while(val>1){
		val -= time;
		AddScore(time);
		yield;
	}
}

task TestLife{
	// I kinda gave up on this
	// Do what you wish with this
	let path = pathHealth;
	let countVertex = 64;
	let listRadius = [];
	loop(countVertex){
		listRadius = listRadius ~ [0];
	}

	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.7);
	ObjPrim_SetTexture(objOutline, path);
	
	ascent (iVert in 0..countVertex/2){
		let left = iVert * 16;
		let vertex = iVert * 2;
		ObjPrim_SetVertexUVT(objOutline, vertex+0, 0, 0);
		ObjPrim_SetVertexUVT(objOutline, vertex+1, 4, 16);		
	}
	
	let dang = 0;
	let dist = 45;
	
	let alpha = 255;
	let mx = 0;
	let my = 0;
	
	let frame = 0;
	let rRate = 1;
	let maxRadius = dist;
	
	while(countVertex>0){
		mx = GetPlayerX;
		my = GetPlayerY - 100;
		RefreshVertex(objOutline, alpha);
		frame++;
		yield;
	}
	
	Obj_Delete(objOutline);

	task RefreshVertex(objid, dalph){	
		ascent (iVert in 0..countVertex/2){
			let indexVert = iVert * 2;
			let angle = (360 / (countVertex / 2 - 1) * iVert);

			let vx1 = listRadius[indexVert] * cos(angle);
			let vy1 = listRadius[indexVert] * sin(angle);
			ObjPrim_SetVertexPosition(objid, indexVert + 0, vx1, vy1, 0);

			let vx2 = listRadius[indexVert+1] * cos(angle);
			let vy2 = listRadius[indexVert+1] * sin(angle);
			ObjPrim_SetVertexPosition(objid, indexVert + 1, vx2, vy2, 0);

			let dr = (maxRadius * rRate - listRadius[indexVert]) / 16;
			listRadius[indexVert] = listRadius[indexVert] + dr;
			if(frame>0){
				listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr;
			}
			ObjPrim_SetVertexAlpha(objid, indexVert+0, dalph);
			ObjPrim_SetVertexAlpha(objid, indexVert+1, dalph);
			dang += 360/countVertex;
		}
		ObjRender_SetPosition(objid, mx, my, 0);
	}
}






