task ClearScreen{
	ascent(i in 0..100){
		if(InField(GetX, GetY)==false){
			DeleteEnemyShotInCircle(ALL, GetCenterX, GetCenterY, i*4.5);
		}else{
			DeleteEnemyShotInCircle(ALL, GetX, GetY, i*4.5);
		}
		yield;
	}
}

function Atktimer(time){
	Lifebar;
	timer;
	SetCommonData("timercount", time);
}

function SpellCard(time){
	Lifebar;
	SetCommonData("ONSPELL", "TRUE");
	spellbonus;
	timer;
	SetCommonData("timercount", time);
}

function SpellEnd{
	SetCommonData("ONSPELL", "FALSE");
	if(spbonus==1){return true;}else{return false;}
}

task Lifebar{
	let bar="script\img\Cbar.png"; LoadGraphic(bar);
	let barb="script\img\Cbarb.png"; LoadGraphic(barb);
	let maxlife=GetLife;
	let blink=0;
	let obj=Obj_Create(OBJ_EFFECT);
	let objb=Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, bar);
	ObjEffect_SetLayer(obj, 1);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_CreateVertex(obj, 4);
	ObjEffect_SetTexture(objb, barb);
	ObjEffect_SetLayer(objb, 1);
	ObjEffect_SetPrimitiveType(objb, PRIMITIVE_TRIANGLESTRIP);
	ObjEffect_CreateVertex(objb, 4);
	ObjEffect_SetVertexUV(obj, 0, 0, 0);
	ObjEffect_SetVertexUV(obj, 1, 200, 0);
	ObjEffect_SetVertexUV(obj, 2, 0, 30);
	ObjEffect_SetVertexUV(obj, 3, 200, 30);
	ObjEffect_SetVertexUV(objb, 0, 0, 0);
	ObjEffect_SetVertexUV(objb, 1, 200, 0);
	ObjEffect_SetVertexUV(objb, 2, 0, 30);
	ObjEffect_SetVertexUV(objb, 3, 200, 30);
	ObjEffect_SetVertexXY(obj, 0, -100, -15);
	ObjEffect_SetVertexXY(obj, 1, 100, -15);
	ObjEffect_SetVertexXY(obj, 2, -100, 15);
	ObjEffect_SetVertexXY(obj, 3, 100, 15);
	ObjEffect_SetVertexXY(objb, 0, -100, -15);
	ObjEffect_SetVertexXY(objb, 1, -100, -15);
	ObjEffect_SetVertexXY(objb, 2, -100, 15);
	ObjEffect_SetVertexXY(objb, 3, -100, 15);
	ObjEffect_SetScale(obj, 0.75, 0.5);
	ObjEffect_SetScale(objb, 0.75, 0.5);
	while(Obj_BeDeleted(obj)==false){
		ObjEffect_SetVertexXY(objb, 1, -100+200*(GetLife/maxlife), -15);
		ObjEffect_SetVertexXY(objb, 3, -100+200*(GetLife/maxlife), 15);
		if(GetLife/maxlife>0.3){
			ascent(i in 0..4){
				ObjEffect_SetVertexColor(obj, i, 255, 64, 255, 64);
				ObjEffect_SetVertexColor(objb, i, 255, 64, 255, 64);
			}
		}else if(GetLife/maxlife>0.1){
			ascent(i in 0..4){
				ObjEffect_SetVertexColor(obj, i, 255, 255, 64, 64);
				ObjEffect_SetVertexColor(objb, i, 255, 255, 64, 64);
			}
		}else{
			blink++;
			if(blink<=8){
				ascent(i in 0..4){
					ObjEffect_SetVertexColor(obj, i, 255, 255, 64, 64);
					ObjEffect_SetVertexColor(objb, i, 255, 255, 64, 64);
				}
			}else{
				ascent(i in 0..4){
					ObjEffect_SetVertexColor(obj, i, 255, 255, 255, 255);
					ObjEffect_SetVertexColor(objb, i, 255, 255, 255, 255);
				}
			}
			if(blink==10){blink=0;}
		}
		Obj_SetPosition(obj, GetX, GetY+75);
		Obj_SetPosition(objb, GetX, GetY+75);
		yield;
	}
}

let numimg=GetCurrentScriptDirectory~"img\Numbers.png";
LoadGraphic(numimg);
let spbonus=1;
let timer1;
let timer2;
let timer3;
let timer4;
let timerp;
let timerarray=[];
let timerarrayb=[];
let survival=0;

task spellbonus
{
	SetCommonData("MISSED", "FALSE");
	yield;
	while(spbonus>0){
		if(GetCommonData("timercount")<=1 && survival!=1)
		{
		spbonus=0;
		}
		if(GetCommonDataDefault("MISSED", "FALSE")=="TRUE")
		{
		spbonus=0;
		SetCommonData("MISSED", "FALSE");
		}
		yield;
	}
}
sub setsurvival{
	survival=1;
}

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");
	if(GetCommonData("timercount")>5999){timercountb=5999;}
	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;
	}