let numimg=GetCurrentScriptDirectory~"img\Numbers.png";
LoadGraphic(numimg);
let spscorearray=[];
let spscore;
let spscore1;
let spscore2;
let spscore3;
let spscore4;
let spscore5;
let spscore6;
let spscore7;
let spellbonustotal=0;
let spbonus=0;
let timer1;
let timer2;
let timer3;
let timer4;
let timerp;
let playerlife=GetPlayerLife;
let playerbomb=GetPlayerBomb;
let spellbonusarray=[];
let timerarray=[];
let timerarrayb=[];

task spellbonus(bonus)
{
	yield;
	spbonus=bonus;
	spscore=Obj_Create(OBJ_EFFECT);
	spscore1=Obj_Create(OBJ_EFFECT);
	spscore2=Obj_Create(OBJ_EFFECT);
	spscore3=Obj_Create(OBJ_EFFECT);
	spscore4=Obj_Create(OBJ_EFFECT);
	spscore5=Obj_Create(OBJ_EFFECT);
	spscore6=Obj_Create(OBJ_EFFECT);
	spscore7=Obj_Create(OBJ_EFFECT);
	spscorearray=spscorearray~[spscore];
	spscorearray=spscorearray~[spscore1];
	spscorearray=spscorearray~[spscore2];
	spscorearray=spscorearray~[spscore3];
	spscorearray=spscorearray~[spscore4];
	spscorearray=spscorearray~[spscore5];
	spscorearray=spscorearray~[spscore6];
	spscorearray=spscorearray~[spscore7];
	ascent(i in 0..8)
	{
	ObjEffect_SetTexture(spscorearray[i], numimg);
	ObjEffect_SetLayer(spscorearray[i], 8);
	ObjEffect_SetPrimitiveType(spscorearray[i], PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_CreateVertex(spscorearray[i], 4);
	}
	while(Obj_BeDeleted(spscorearray[0])==false)
	{
	yield;
	if(spbonus>0)
	{
		if(GetPlayerLife<playerlife || GetPlayerBomb<playerbomb || GetCommonData("timercount")<=1)
		{
		spbonus=0;
		}
	spbonus-=truncate(5000/6);
	}else{
	spbonus=0;
	}
	ascent(i in 0..8)
	{
	ObjEffect_SetVertexXY(spscorearray[i], 0, GetClipMaxX-100+i*12, GetClipMinY+35);
	ObjEffect_SetVertexXY(spscorearray[i], 1, GetClipMaxX-88+i*12, GetClipMinY+35);
	ObjEffect_SetVertexXY(spscorearray[i], 2, GetClipMaxX-100+i*12, GetClipMinY+55);
	ObjEffect_SetVertexXY(spscorearray[i], 3, GetClipMaxX-88+i*12, GetClipMinY+55);
	}
	updatespscore(spbonus);
	spellbonustotal=spbonus;
		if(GetCommonData("ONSPELL")=="FALSE")
		{
		ascent(i in 0..8)
		{
		Obj_Delete(spscorearray[i]);
		}
		}
	}
}

function updatespscore(bonus)
{
spellbonusarray=ToDigits(bonus, 8, 2);
Set_System_UV(spscore, spellbonusarray[0]);
Set_System_UV(spscore1, spellbonusarray[1]);
Set_System_UV(spscore2, spellbonusarray[2]);
Set_System_UV(spscore3, spellbonusarray[3]);
Set_System_UV(spscore4, spellbonusarray[4]);
Set_System_UV(spscore5, spellbonusarray[5]);
Set_System_UV(spscore6, spellbonusarray[6]);
Set_System_UV(spscore7, spellbonusarray[7]);
}

task timer
{
	yield;
	let secondcount=0;
	let msecondcount=0;
	let timercountb=0;
	timer1=Obj_Create(OBJ_EFFECT);
	timer2=Obj_Create(OBJ_EFFECT);
	timer3=Obj_Create(OBJ_EFFECT);
	timer4=Obj_Create(OBJ_EFFECT);
	timerp=Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(timer1, numimg);
	ObjEffect_SetTexture(timer2, numimg);
	ObjEffect_SetTexture(timer3, numimg);
	ObjEffect_SetTexture(timer4, numimg);
	ObjEffect_SetTexture(timerp, numimg);
	ObjEffect_SetLayer(timer1, 8);
	ObjEffect_SetLayer(timer2, 8);
	ObjEffect_SetLayer(timer3, 8);
	ObjEffect_SetLayer(timer4, 8);
	ObjEffect_SetLayer(timerp, 8);
	ObjEffect_SetPrimitiveType(timer1, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_SetPrimitiveType(timer2, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_SetPrimitiveType(timer3, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_SetPrimitiveType(timer4, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_SetPrimitiveType(timerp, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_CreateVertex(timer1, 4);
	ObjEffect_CreateVertex(timer2, 4);
	ObjEffect_CreateVertex(timer3, 4);
	ObjEffect_CreateVertex(timer4, 4);
	ObjEffect_CreateVertex(timerp, 4);
		ObjEffect_SetVertexXY(timer1, 0, GetClipMaxX-58, GetClipMinY+4);
		ObjEffect_SetVertexXY(timer1, 1, GetClipMaxX-44, GetClipMinY+4);
		ObjEffect_SetVertexXY(timer1, 2, GetClipMaxX-58, GetClipMinY+28);
		ObjEffect_SetVertexXY(timer1, 3, GetClipMaxX-44, GetClipMinY+28);
		ObjEffect_SetVertexXY(timer2, 0, GetClipMaxX-43, GetClipMinY+4);
		ObjEffect_SetVertexXY(timer2, 1, GetClipMaxX-29, GetClipMinY+4);
		ObjEffect_SetVertexXY(timer2, 2, GetClipMaxX-43, GetClipMinY+28);
		ObjEffect_SetVertexXY(timer2, 3, GetClipMaxX-29, GetClipMinY+28);
		ObjEffect_SetVertexXY(timerp, 0, GetClipMaxX-31, GetClipMinY+4);
		ObjEffect_SetVertexXY(timerp, 1, GetClipMaxX-17, GetClipMinY+4);
		ObjEffect_SetVertexXY(timerp, 2, GetClipMaxX-31, GetClipMinY+28);
		ObjEffect_SetVertexXY(timerp, 3, GetClipMaxX-17, GetClipMinY+28);
		ObjEffect_SetVertexXY(timer3, 0, GetClipMaxX-22, GetClipMinY+10);
		ObjEffect_SetVertexXY(timer3, 1, GetClipMaxX-10, GetClipMinY+10);
		ObjEffect_SetVertexXY(timer3, 2, GetClipMaxX-22, GetClipMinY+28);
		ObjEffect_SetVertexXY(timer3, 3, GetClipMaxX-10, GetClipMinY+28);
		ObjEffect_SetVertexXY(timer4, 0, GetClipMaxX-13, GetClipMinY+10);
		ObjEffect_SetVertexXY(timer4, 1, GetClipMaxX-1, GetClipMinY+10);
		ObjEffect_SetVertexXY(timer4, 2, GetClipMaxX-13, GetClipMinY+28);
		ObjEffect_SetVertexXY(timer4, 3, GetClipMaxX-1, GetClipMinY+28);
	while(Obj_BeDeleted(timer1)==false)
	{
	yield;
	if(GetCommonData("timercount")>600)
	{
		ascent(i in 0..4)
		{
		ObjEffect_SetVertexColor(timer1, i, 255, 255, 255, 255);
		ObjEffect_SetVertexColor(timer2, i, 255, 255, 255, 255);
		ObjEffect_SetVertexColor(timer3, i, 255, 255, 255, 255);
		ObjEffect_SetVertexColor(timer4, i, 255, 255, 255, 255);
		ObjEffect_SetVertexColor(timerp, i, 255, 255, 255, 255);
		}
		SetCommonData("timercount", GetCommonData("timercount")-1);
	}else if(GetCommonData("timercount")>0){
		ascent(i in 0..4)
		{
		ObjEffect_SetVertexColor(timer1, i, 255, 255, 64, 64);
		ObjEffect_SetVertexColor(timer2, i, 255, 255, 64, 64);
		ObjEffect_SetVertexColor(timer3, i, 255, 255, 64, 64);
		ObjEffect_SetVertexColor(timer4, i, 255, 255, 64, 64);
		ObjEffect_SetVertexColor(timerp, i, 255, 255, 64, 64);
		}
		SetCommonData("timercount", GetCommonData("timercount")-1);
	}else{
		ascent(i in 0..4)
		{
		ObjEffect_SetVertexColor(timer1, i, 0, 255, 255, 255);
		ObjEffect_SetVertexColor(timer2, i, 0, 255, 255, 255);
		ObjEffect_SetVertexColor(timer3, i, 0, 255, 255, 255);
		ObjEffect_SetVertexColor(timer4, i, 0, 255, 255, 255);
		ObjEffect_SetVertexColor(timerp, i, 0, 255, 255, 255);
		}
	}
	if(GetCommonData("timercount")==0){AddLife(-GetLife);}
	timercountb=GetCommonData("timercount");
	secondcount=truncate(timercountb/60);
	msecondcount=((timercountb-(secondcount*60))/60)*100;
	updatetimer(secondcount, msecondcount);
	if(secondcount<=10 && msecondcount==0){PlaySE(GetCurrentScriptDirectory~"sound\se_timeout.wav");}
	}
}

function updatetimer(sc, msc)
{
timerarray=ToDigits(sc, 2, 2);
timerarrayb=ToDigits(msc, 2, 2);
Set_System_UV(timer1, timerarray[0]);
Set_System_UV(timer2, timerarray[1]);
Set_System_UV(timer3, timerarrayb[0]);
Set_System_UV(timer4, timerarrayb[1]);
Set_System_UV(timerp, 11);
}

function Set_System_UV(id, num)
{
	ObjEffect_SetVertexUV(id, 0, num*16, 0);
	ObjEffect_SetVertexUV(id, 1, num*16+16, 0);
	ObjEffect_SetVertexUV(id, 2, num*16, 19);
	ObjEffect_SetVertexUV(id, 3, num*16+16, 19);
}

	function ToDigits(funcnum, funcdigits, functype)
	{
	let numberarray=[];
	let numberstring=ToString(funcnum);
	ascent(i in 0..length(numberstring))
	{
	if(numberstring[i]=='.')
	{
	break;
	}else{
	numberarray=numberarray~[numberstring[i]+0];
	}
	}
	while(length(numberarray)!=funcdigits)
	{
		if(functype==1)
		{
		numberarray=numberarray~[10];
		}else{
		numberarray=[0]~numberarray;
		}
	}
	return numberarray;
	}