#include "./LibEffects.dnh"
#include "./LibFonts.dnh"

let bool_timer = true;
let dirCurrent = GetCurrentScriptDirectory();

let constObj = ObjSound_Create();
ObjSound_SetSoundDivision(constObj, SOUND_SE);
ObjSound_SetResumeEnable(constObj, false);

let stgx = GetStgFrameWidth()/2;
let stgy = GetStgFrameHeight()/2;

let objScene = ID_INVALID;
let pathHealth = dirCurrent ~ "img/lifebar.png";

let EV_RANGE_START = 11115;
let EV_SPRITES = EV_USER + EV_RANGE_START + 0;
let EV_CONCETRATION = EV_USER + EV_RANGE_START + 1;
let EV_EXPLOSION = EV_USER + EV_RANGE_START + 2;

let EV_PLAYSFX = EV_USER + EV_RANGE_START + 3;

@Initialize{
	InitFrame(); LoadItemData(GetCurrentScriptDirectory ~ "./../lib/ItemLib.dnh");
	SetPauseScriptPath(GetCurrentScriptDirectory() ~ "./Default_Pause.txt");
	SetEndSceneScriptPath(GetCurrentScriptDirectory() ~ "./Default_EndScene.txt");
	SetReplaySaveSceneScriptPath(GetCurrentScriptDirectory() ~ "./Default_ReplaySaveScene.txt");

	SetStgFrame(0, 0, GetScreenWidth(), GetScreenHeight(), 1, 80);
	stgx = GetStgFrameWidth()/2; stgy = GetStgFrameHeight()/2;

	SetPlayerIntersectionEraseShot(false);

	TScore();
	THiScore();
	TGraze();
	TPlayerLife();
	TPlayerSpell();
	TBossLife();
	TDBossTimer();
	TCurrentFps();
	TReplayFps();
}

@MainLoop{
	objScene = GetEnemyBossSceneObjectID;
	yield;
}

@Event{
	alternative(GetEventType())
	case(EV_START_BOSS_SPELL){
		let path = dirCurrent ~ "Default_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);
	}
	case(EV_USER_SYSTEM + 1){
		if(GetEventArgument(0)){
			if(GetEventArgument(1) == 0){SetPlayerLife(GetPlayerLife() + 1);}
			if(GetEventArgument(1) == 1){SetPlayerSpell(GetPlayerSpell() + 1);}
			extend(GetEventArgument(1));
		}
		else{
			TLoseSpell();
		}
	}
	case(EV_USER_SYSTEM + 2){
		let path = dirCurrent ~ "../lib/SFX/Final_00.wav";
		NotifyEventAll(EV_PLAYSFX, constObj, path, 100);

		let allB = GetShotIdInCircleA2(stgx, stgy, 800, TARGET_ENEMY); let n = 1;
		for each(let never : allB){
			fuckoff(never, ObjMove_GetX(never), ObjMove_GetY(never), n);
			BulletDelete(never, ObjMove_GetX(never), ObjMove_GetY(never)); n *= -1;
		}
	}

	//Effects
	case(EV_USER_SYSTEM + 3){
		Survival(GetEventArgument(0));
	}

	case(EV_USER_SYSTEM + 4){
		bool_timer = GetEventArgument(0);
	}

	case(EV_EXPLOSION){
		let pos = GetEventArgument(0); //[x, y]
		let dr = GetEventArgument(1); //[duration, radius]
		let dt = GetEventArgument(2); //[density, turn, scale]
		let bl = GetEventArgument(3); //blend
		let cg = GetEventArgument(4); //[R, G, B, graphic]

		_ConcentrationExplosion(pos[0], pos[1], dr[0], dr[1], dt[0], dt[1], dt[2], bl, cg[0], cg[1], cg[2], cg[3]);
	}

	case(EV_CONCETRATION){
		let pos = GetEventArgument(0); //[x, y]
		let dr = GetEventArgument(1); //[duration, radius]
		let dta = GetEventArgument(2); //[density, turn, ap]
		let bl = GetEventArgument(3); //blend
		let cg = GetEventArgument(4); //[R, G, B, graphic]

		_Concentration(pos[0], pos[1], dr[0], dr[1], dta[0], dta[1], dta[2], bl, cg[0], cg[1], cg[2], cg[3]);
	}

	//Sounds
	case(EV_PLAYSFX){
		ObjSound_Load(GetEventArgument(0), GetEventArgument(1));
		ObjSound_SetVolumeRate(GetEventArgument(0), GetEventArgument(2));
    ObjSound_Play(GetEventArgument(0));
	}
}

task BulletDelete(b, x, y){
	let item = CreateItemU1(1, x, y, 5); 
	let color = GetShotDataInfoA1(ObjShot_GetImageID(b), TARGET_ENEMY, INFO_DELAY_COLOR);
	ObjRender_SetColor(item, color[0], color[1], color[2]); ObjRender_SetScaleXYZ(item, 1.35, 1.35, 0);
	ObjItem_SetRenderScoreEnable(item, false); ObjMove_SetPosition(item, x, y);
	wait(45);
	ObjItem_SetDefinedMovePatternA1(item, ITEM_MOVE_TOPLAYER);
}

task fuckoff(b, x, y, s){ //thanks Lusus
	let color = GetShotDataInfoA1(ObjShot_GetImageID(b), TARGET_ENEMY, INFO_DELAY_COLOR);
	let path = dirCurrent ~ "img/light.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 63, 63);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.95); 
	ObjRender_SetPosition(obj, x, y, 0); ObjRender_SetColor(obj, color[0], color[1], color[2]);
	ObjRender_SetAngleZ(obj, rand(0,360)); let az = ObjRender_GetAngleZ(obj);
	
	let z = 0; let rate;
	ascent(i in 0..36+1){
		rate = Interpolate_Accelerate(0.6, 0, i/36);
		ObjRender_SetAlpha(obj, Interpolate_Smoother(100, 0, i/36));
		ObjRender_SetScaleXYZ(obj, rate, rate, 0);
		
		ObjRender_SetAngleZ(obj, az + z);
		z += 36*s;
		yield;
	}
	
	Obj_Delete(obj);
}

//----------------------------------------------------
//枠外の背景表示
//----------------------------------------------------
function InitFrame(){
	let path = GetCurrentScriptDirectory() ~ "img/fwuck.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 1);
	ObjSprite2D_SetSourceRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
	ObjSprite2D_SetDestRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}

//----------------------------------------------------
//スコア表示
//----------------------------------------------------
task TScore(){
	let x = GetScreenWidth()-53.12; let y = 140;

	//Score文字列
	let text = ObjPrim_Create(OBJ_SPRITE_2D);
	let img = GetCurrentScriptDirectory() ~ "img/fiesta.png";
	ObjPrim_SetTexture(text, img);
	ObjSprite2D_SetSourceRect(text, 3, 41, 77, 68);
	ObjSprite2D_SetDestCenter(text);
	ObjRender_SetBlendType(text, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(text, 0.55, 0.55, 0);
	Obj_SetRenderPriority(text, 1);
	ObjRender_SetPosition(text, x+5, y-1.55, 0);

	//LineScore
	let o = 0;
	let line = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(line, img);
	ObjSprite2D_SetSourceRect(line, 1, 180, 148, 187);
	ObjSprite2D_SetDestCenter(line);
	yesColor(line, 0, 0, 70);
	ObjRender_SetBlendType(line, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(line, 0.67, 1, 0);
	Obj_SetRenderPriority(line, 1);
	ObjRender_SetPosition(line, x+4, y+11, 0);

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

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(obj, 0.75, 0.75, 0);
	Obj_SetRenderPriority(obj, 1);
	ObjRender_SetY(obj, y+16);

	let score = 0;
	while(true){
		score = score * 0.89 + GetScore() * 0.11; let co = 0;
		score = min(ceil(score), 999999999);
		let listNum = DigitToArray(score, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(obj, (x-43) + iObj * 10 + co);
			ObjSpriteList2D_SetSourceRect(obj, 544+num * 16, 161, 544+(num + 1) * 16, 179);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 14, 14*1.4);
			ObjSpriteList2D_AddVertex(obj);

			if((iObj+1) % 3 == 0 && iObj != 0){co += 3.5;}
		}

		ObjRender_SetScaleY(line, 1.5 + sin(o)*0.15); o += 4;
		yield;
	}
}
//----------------------------------------------------
//HiScore
//----------------------------------------------------
task THiScore(){
	let x = GetScreenWidth()-53.12; let y = 80;
	
	let text = ObjPrim_Create(OBJ_SPRITE_2D);
	let img = GetCurrentScriptDirectory() ~ "img/fiesta.png";
	ObjPrim_SetTexture(text, img);
	ObjSprite2D_SetSourceRect(text, 3, 4, 151, 38);
	ObjSprite2D_SetDestCenter(text);
	ObjRender_SetBlendType(text, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(text, 0.55, 0.55, 0);
	Obj_SetRenderPriority(text, 1);
	ObjRender_SetPosition(text, x+5, y, 0);

	//LineScore
	let o = 0;
	let line = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(line, img);
	ObjSprite2D_SetSourceRect(line, 1, 180, 148, 187);
	ObjSprite2D_SetDestCenter(line);
	yesColor(line, 0, 0, 42);
	ObjRender_SetBlendType(line, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(line, 0.67, 1, 0);
	Obj_SetRenderPriority(line, 1);
	ObjRender_SetPosition(line, x+4, y+11, 0);

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

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(obj, 0.75, 0.75, 0);
	ObjRender_SetAlpha(obj, 255);
	Obj_SetRenderPriority(obj, 1);
	ObjRender_SetY(obj, y+16);

	while(true){					 //
		let score = GetAreaCommonData("HiScore", "HISCORE", 1000000);
		score = min(score, 999999999); let co = 0;
		let listNum = DigitToArray(score, count);
		
		if(GetScore>GetAreaCommonData("HiScore", "HISCORE", 1000000)){
			SetAreaCommonData("HiScore", "HISCORE", GetScore());
			SaveCommonDataAreaA1("HiScore");
		}
		
		if(!IsCommonDataAreaExists("HiScore")){
			CreateCommonDataArea("HiScore");
		}
		LoadCommonDataAreaA1("HiScore");
		
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(obj, (x-43) + iObj * 10 + co);
			ObjSpriteList2D_SetSourceRect(obj, 544+num * 16, 161, 544+(num + 1) * 16, 179);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 14, 14*1.4);
			ObjSpriteList2D_AddVertex(obj);

			if((iObj+1) % 3 == 0 && iObj != 0){co += 3.5;}
		}
		
		ObjRender_SetScaleY(line, 1.5 + sin(o)*0.15); o += 4;
		yield;
	}
}
//----------------------------------------------------
//Graze表示
//----------------------------------------------------
task TGraze(){
	let x = GetScreenWidth()-53.12; let y = 200; 

	let text = ObjPrim_Create(OBJ_SPRITE_2D);
	let img = GetCurrentScriptDirectory() ~ "img/fiesta.png";
	ObjPrim_SetTexture(text, img);
	ObjSprite2D_SetSourceRect(text, 2, 133, 85, 162);
	ObjSprite2D_SetDestCenter(text);
	ObjRender_SetBlendType(text, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(text, 0.55, 0.55, 0);
	Obj_SetRenderPriority(text, 1);
	ObjRender_SetPosition(text, x+5, y, 0);

	//LineScore
	let o = 0;
	let line = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(line, img);
	ObjSprite2D_SetSourceRect(line, 1, 180, 148, 187);
	ObjSprite2D_SetDestCenter(line);
	yesColor(line, 0, 0, 88);
	ObjRender_SetBlendType(line, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(line, 0.67, 1, 0);
	Obj_SetRenderPriority(line, 1);
	ObjRender_SetPosition(line, x+4, y+11, 0);

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

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 1);
	ObjRender_SetY(obj, y+16);
	ObjRender_SetScaleXYZ(obj, 0.85, 0.85, 0);
	while(true){
		let graze = GetGraze(); let co = 0;
		graze = min(graze, 9999);
		let listNum = DigitToArray(graze, count);
		count = length(listNum);

		let v = 1; let i = 2;
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(obj, (x-25) + iObj * 11 + co);
			ObjSpriteList2D_SetSourceRect(obj, 544+num * 16, 161, 544+(num + 1) * 16, 179);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 14, 14*1.5);
			ObjSpriteList2D_AddVertex(obj);

			//v = iObj+1;
			if(v % i == 0 && (v-1) != 0){i++; v = 1; co += 4;}
			else{v++;}
		}

		ObjRender_SetScaleY(line, 1.5 + sin(o)*0.15); o += 4;
		yield;
	}
}

//----------------------------------------------------
//残機表示
//----------------------------------------------------
task TPlayerLife{
	let y = GetStgFrameHeight-10; let img = GetCurrentScriptDirectory() ~ "img/bitmap.png";

	let sp = 13; let n = 34; let scale = [1.2, 0.8];
	ascent(i in 0..11){
		EmptyLife(n, i);
		RealLife(n, i);
	}
	TText;
	
	task EmptyLife(px, i){
		let x = px;

		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 6, 6, 21, 21);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj, scale[0], scale[1], 0);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, x, y - i*sp, 0);
	}
	
	task RealLife(px, i){
		let x = px;

		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 26, 6, 41, 21);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj, scale[0], scale[1], 0);
		Obj_SetRenderPriority(obj, 1);  
		ObjRender_SetPosition(obj, x, y - i*sp, 0);
		while(true){
			ObjSprite2D_SetDestCenter(obj);

			let point = GetPlayerLife();
			if(point > i){ObjRender_SetAlpha(obj, 255);}
			else{ObjRender_SetAlpha(obj, 70);}
			yield;
		}
	}
	
	task TText{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D); img = GetCurrentScriptDirectory() ~ "img/fiesta.png";
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 2, 70, 75, 99);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj, 0.55, 0.55, 0);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, n-20, y - 14, 0);
		ObjRender_SetAngleZ(obj, -90);
	}
	
}

task TPlayerSpell{
	let y = GetStgFrameHeight-10; let img = GetCurrentScriptDirectory() ~ "img/bitmap.png";

	let sp = 13; let n = 80; let scale = [1.2, 0.8];
	ascent(i in 0..11){
		EmptyBomb(n, i);
		RealBomb(n, i);
	}
	TText;
	
	task EmptyBomb(px, i){
		let x = px;

		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 6, 26, 21, 41);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj, scale[0], scale[1], 0);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, x, y - i*sp, 0);
	}
	
	task RealBomb(px, i){
		let x = px;

		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 26, 26, 41, 41);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj, scale[0], scale[1], 0);
		Obj_SetRenderPriority(obj, 1);  
		ObjRender_SetPosition(obj, x, y - i*sp, 0);
		while(true){
			ObjSprite2D_SetDestCenter(obj);

			let point = GetPlayerSpell();
			if(point > i){ObjRender_SetAlpha(obj, 255);}
			else{ObjRender_SetAlpha(obj, 70);}
			yield;
		}
	}
	
	task TText{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D); img = GetCurrentScriptDirectory() ~ "img/fiesta.png";
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 2, 100, 97, 130);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj, 0.55, 0.55, 0);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, n-18, y - 20, 0);
		ObjRender_SetAngleZ(obj, -90);
	}

}

//----------------------------------------------------
//ボスのライフ表示
//----------------------------------------------------
task TBossLife{
	let path = GetCurrentScriptDirectory() ~ "img/Default_System.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 0.75);
	ObjSpriteList2D_SetSourceRect(obj, 1, 1, 127, 11);

	let objST = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objST, path);
	Obj_SetRenderPriority(objST, 0.75);
	ObjRender_SetScaleXYZ(objST, 0.78, 0.78, 0);
	ObjSpriteList2D_SetSourceRect(objST, 173, 3, 206, 36);

	let lastRemStep = -1;
	let lifeRateRender = 0;

	let objScene = ID_INVALID;
	loop{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		ObjSpriteList2D_ClearVertexCount(objST);
		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);
		let listLifeDiv = [0] ~ ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_LIFE_RATE_LIST);
		
		//{
			let org = 20; let ox = 109; let fx = 422;
			let oy = 6; let xx = 115;

			let nx = 4; let ny = -0.25;
		//}

		ObjSpriteList2D_SetSourceRect(obj, 1, 1, 127, 11);
		ObjSpriteList2D_SetDestRect(obj, ox, 1 + oy, ox + fx * lifeRate, 8 + oy);
		ObjRender_SetColor(obj, 191, 191, 191);
		ObjSpriteList2D_AddVertex(obj);
		
		oy -= 1;
		ObjSpriteList2D_SetSourceRect(obj, 130, 1, 139, 12);
		ascent(iDiv in 0 .. length(listLifeDiv)){
			let rate = listLifeDiv[iDiv];
			let x = ox + fx * (1-rate);

			ObjSpriteList2D_SetDestRect(obj, x - nx, -2 + oy, x + nx, 12 + oy);
			ObjSpriteList2D_AddVertex(obj);
		}

		ObjSpriteList2D_ClearVertexCount(objST);
		ascent(iStep in 0 .. countRemStep){
			let y = truncate((iStep)/34)*34;
			let i = iStep - (34*(y/34));
			//22 x 22

			ObjSpriteList2D_SetDestRect(objST, 21 + i*20 + xx, 20 + y + org, 21 + (i+1)*20 + xx, 1 + y + org);
			ObjRender_SetBlendType(objST, BLEND_ALPHA);
			ObjSpriteList2D_AddVertex(objST);
		}

		lifeRateRender += 0.05;
		lifeRateRender = min(lifeRateRender, 1);
		lastRemStep = countRemStep;
	}
}
//----------------------------------------------------
//タイマー表示
//----------------------------------------------------
task TDBossTimer{
	let pathDigit = GetCurrentScriptDirectory() ~ "img/SystemDigit.png";

	let objMain = ObjText_Create();
	ObjText_SetFontType(objMain, "osaka_unicode");
	ObjText_SetFontCharacterSet(objMain, 0x00000001);
	ObjText_SetFontSize(objMain, 24);
	ObjText_SetFontBold(objMain, true);
	ObjText_SetFontColorTop(objMain, 255, 255, 255);
	ObjText_SetFontColorBottom(objMain, 77, 77, 77);
	ObjText_SetFontBorderType(objMain, BORDER_FULL);
	ObjText_SetFontBorderColor(objMain, 0, 0, 0);
	ObjText_SetFontBorderWidth(objMain, 3);
	ObjText_SetMaxWidth(objMain, GetScreenWidth() - 8);
	ObjRender_SetX(objMain, stgx-36);
	ObjRender_SetBlendType(objMain, BLEND_ALPHA);
	Obj_SetRenderPriority(objMain, 0.75);
	ObjRender_SetY(objMain, 18);
	ObjRender_SetAlpha(objMain, 0);

	let objSub = ObjText_Create();
	ObjText_SetFontType(objSub, "osaka_unicode");
	ObjText_SetFontCharacterSet(objSub, 0x00000001);
	ObjText_SetFontSize(objSub, 16);
	ObjText_SetFontBold(objSub, true);
	ObjText_SetFontColorTop(objSub, 255, 255, 255);
	ObjText_SetFontColorBottom(objSub, 77, 77, 77);
	ObjText_SetFontBorderType(objSub, BORDER_FULL);
	ObjText_SetFontBorderColor(objSub, 0, 0, 0);
	ObjText_SetFontBorderWidth(objSub, 2.5);
	ObjText_SetMaxWidth(objSub, GetScreenWidth() - 8);
	ObjRender_SetX(objSub, stgx + 13);
	ObjRender_SetBlendType(objSub, BLEND_ALPHA);
	Obj_SetRenderPriority(objSub, 0.75);
	ObjRender_SetY(objSub, 18+0.5);
	ObjRender_SetAlpha(objSub, 0);

	let objScene = ID_INVALID;
	let timer; let con;
	loop{
		if(!bool_timer){
			ObjRender_SetAlpha(objMain, 0); ObjRender_SetAlpha(objSub, 0);
		}
		else{
			objScene = GetEnemyBossSceneObjectID();
			if(objScene != ID_INVALID){
				ObjRender_SetAlpha(objMain, 255); ObjRender_SetAlpha(objSub, 255);
			}
			else{
				ObjRender_SetAlpha(objMain, 0); ObjRender_SetAlpha(objSub, 0);
			}
		}

		timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMERF); 
		ObjText_SetText(objMain, rtos("000", floor(timer/60)));
		ObjText_SetText(objSub, rtos("#00", ((timer%60)/60)*100));
		yield;
	}

}

//----------------------------------------------------
//スペルカード取得表示
//----------------------------------------------------

task extend(settings){
	let y = 210;
	let path = (settings == 0 ? "se/seExtend.wav" : (settings == 1 ? "se/seSpellExtend.wav" : ""));
	let posx = (settings == 0 ? 34 : (settings == 1 ? 80 : 57));

	let extend = ObjSound_Create();
		ObjSound_Load(extend, GetCurrentScriptDirectory() ~ path);
		ObjSound_SetSoundDivision(extend, SOUND_SE);

	let gg = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(gg, GetCurrentScriptDirectory() ~ "img/messages/boi.png");
	ObjSprite2D_SetSourceRect(gg, 0, 127, 236, 157);
	ObjSprite2D_SetDestCenter(gg);
	ObjRender_SetBlendType(gg, BLEND_ALPHA);
	Obj_SetRenderPriority(gg, 1);
	
	ObjRender_SetPosition(gg, posx, y, 0);
	ObjRender_SetAngleZ(gg, -90);
	ObjSound_Play(extend);

	ascent(i in 0..15+1){
		ObjRender_SetScaleY(gg, Interpolate_Linear(0, 1, i/15));
		yield;
	}

	wait(60*1);

	ascent(i in 0..30+1){
		ObjRender_SetAngleY(gg, Interpolate_Linear(0, 90, i/30));
		ObjRender_SetY(gg, y - Interpolate_Linear(0, 15, i/30));
		ObjRender_SetAlpha(gg, Interpolate_Linear(255, 0, i/30));
		yield;
	}

	Obj_Delete(gg); Obj_Delete(extend);
}

task TGainSpell(score){
	//Setting the Score{
		let scored = DigitToCommaArray(score);
		
	//}

	let pic = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(pic, GetCurrentScriptDirectory() ~ "./img/frame2.png");
	ObjRender_SetAlpha(pic, 255);
	ObjSprite2D_SetSourceRect(pic, 17, 98, 238, 126);
	ObjRender_SetX(pic, 325);
	ObjRender_SetY(pic, 108);
	ObjRender_SetBlendType(pic, BLEND_ALPHA);
	ObjSprite2D_SetDestCenter(pic);

	let objMain = ObjText_Create();
	ObjText_SetText(objMain, scored);
	ObjText_SetFontType(objMain, "osaka_unicode");
	ObjText_SetFontCharacterSet(objMain, 0x00000001);
	ObjText_SetFontSize(objMain, 24);
	ObjText_SetFontBold(objMain, true);
	ObjText_SetFontColorTop(objMain, 255, 255, 255);
	ObjText_SetFontColorBottom(objMain, 77, 77, 77);
	ObjText_SetFontBorderType(objMain, BORDER_FULL);
	ObjText_SetFontBorderColor(objMain, 0, 0, 0);
	ObjText_SetFontBorderWidth(objMain, 2);
	ObjText_SetMaxWidth(objMain, GetScreenWidth() - 8);
	ObjRender_SetX(objMain, 260); ObjRender_SetY(objMain, 126);
	Obj_SetRenderPriority(objMain, 0.75);
	
	ascent(i in 0..15+1){
		ObjRender_SetScaleY(pic, Interpolate_Linear(0, 1, i/15));
		ObjRender_SetScaleY(objMain, Interpolate_Linear(0, 1, i/15));
		yield;
	}
	
	wait(60*1);

	ascent(i in 0..30+1){
		ObjRender_SetAlpha(pic, Interpolate_Linear(255, 0, i/30));
		ObjRender_SetAlpha(objMain, Interpolate_Linear(255, 0, i/30));
		yield;
	}

	Obj_Delete(pic); Obj_Delete(objMain);
}

task TLoseSpell(){
	let lose = ObjSound_Create();
		ObjSound_Load(lose, GetCurrentScriptDirectory() ~ "se/seBonusFailed.wav");
		ObjSound_SetSoundDivision(lose, SOUND_SE);

	let pic = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(pic, GetCurrentScriptDirectory() ~ "./img/frame2.png");
	ObjRender_SetAlpha(pic, 255);
	ObjSprite2D_SetSourceRect(pic, 56, 386, 200, 412);
	ObjRender_SetX(pic, 325);
	ObjRender_SetY(pic, 108);
	ObjRender_SetBlendType(pic, BLEND_ALPHA);
	ObjSprite2D_SetDestCenter(pic);

	ObjSound_Play(lose);
	ascent(i in 0..15+1){
		ObjRender_SetScaleY(pic, Interpolate_Linear(0, 1, i/15));
		yield;
	}

	wait(60*1);

	ascent(i in 0..30+1){
		ObjRender_SetAlpha(pic, Interpolate_Linear(255, 0, i/30));
		ObjRender_SetY(pic, 108 + Interpolate_Linear(0, 15, i/30));
		yield;
	}

	Obj_Delete(pic); Obj_Delete(lose);
}

//----------------------------------------------------
//FPS表示
//----------------------------------------------------
task TCurrentFps(){
	if(IsReplay()){return;}

	let objText = ObjText_Create();
	ObjText_SetFontType(objText, "THSpatial");
	ObjText_SetFontCharacterSet(objText, 0x00000001);
	ObjText_SetFontSize(objText, 17);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 242, 242, 242);
	ObjText_SetFontColorBottom(objText, 140, 140, 140);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 1.8);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objText, GetScreenWidth() - 8);
	Obj_SetRenderPriority(objText, 1.0);
	ObjRender_SetX(objText, 5.5);
	ObjRender_SetY(objText, GetScreenHeight() - 20);

	loop{
		let fps = GetCurrentFps();
		let text = vtos("2.2f", fps) ~ " fps";
		ObjText_SetText(objText, text);
		yield;
	}
}

task TReplayFps(){
	if(!IsReplay()){return;}

	let objText = ObjText_Create();
	ObjText_SetFontType(objText, "THSpatial");
	ObjText_SetFontCharacterSet(objText, 0x00000001);
	ObjText_SetFontSize(objText, 17);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 242, 242, 242);
	ObjText_SetFontColorBottom(objText, 140, 140, 140);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 1.8);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objText, GetScreenWidth() - 8);
	Obj_SetRenderPriority(objText, 1.0);
	ObjRender_SetX(objText, 5.5);
	ObjRender_SetY(objText, GetScreenHeight() - 20);

	loop{
		let fps = GetReplayFps();
		let text = vtos("2.2f", fps) ~ " fps";
		ObjText_SetText(objText, text);
		yield;
	}
}

//----------------------------------------------------
//ユーティリティ
//----------------------------------------------------
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;
}

function DigitToCommaArray(num) {
    let srcStr = IntToString(num);
    let res = [];
    let nChar = 0;
    for (let i = length(srcStr) - 1; i >= 0; i--) {
        res = [srcStr[i]] ~ res;
        nChar++;
        if (nChar % 3 == 0 && i > 0)
            res = "," ~ res;
    }
    return res;
}


//function wait(n){loop(n){yield;}}
function GetDistance(x1,y1,x2,y2){return(((x2-x1)^2+(y2-y1)^2)^(1/2));}
function GetGapX(let x, let gapLength, let gapAngle){ return x+gapLength*cos(gapAngle);}
function GetGapY(let y, let gapLength, let gapAngle){ return y+gapLength*sin(gapAngle);}
task yesColor(me, H, x, y){ObjRender_SetColorHSV(me, H, 51/20*x, 51/20*y);}