
let GCSD = GetCurrentScriptDirectory();

let pathSystem = GCSD ~ "./img/System.png";

@Initialize
{
	InitFrame();
	TScore();
	TGraze();
	TPlayerLife();
	TPlayerSpell();
	TBossLife();
	TBossTimer();
	TCurrentFps();
	TReplayFps();
	AddLives;
	
	InstallFont(GetCurrentScriptDirectory() ~ "DD.ttf");
	
	LoadEnemyShotData(GCSD ~ "./shot/shot_define.dnh");
	StartShotScript(GetCurrentScriptDirectory ~ "MS_Shot.txt");
	LoadItemData(GetCurrentScriptDirectory ~ "./item/Itemdata.txt");
	StartItemScript(GetCurrentScriptDirectory ~ "./item/Itemscript.txt");
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_START_BOSS_SPELL)
	{
		let path = GCSD ~ "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);
	}
}

//----------------------------------------------------
//枠外の背景表示
//----------------------------------------------------
function InitFrame()
{
	let path = GetCurrentScriptDirectory() ~ "img/MeowsticDanmakufu.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 TScore()
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, pathSystem);
	Obj_SetRenderPriority(obj, 0.01);
	ObjSprite2D_SetSourceRect(obj, 380, 538,  380+90, 538+38);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0);
	ObjRender_SetX(obj, 428+30);
	ObjRender_SetY(obj, 118-20);
	
	let objBar = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objBar, pathSystem);
	Obj_SetRenderPriority(objBar, 0.01);
	ObjSprite2D_SetSourceRect(objBar, 0, 0, 256, 8);
	ObjSprite2D_SetDestCenter(objBar);
	ObjRender_SetScaleXYZ(objBar, 0.75, 0.75, 0);
	ObjRender_SetBlendType(objBar, BLEND_ADD_ARGB);
	ObjRender_SetX(objBar, 428+100);
	ObjRender_SetY(objBar, 118-8);
	
	let objScore = ObjText_Create();
	ObjText_SetFontType(objScore, "Modern No. 20");
	ObjText_SetFontSize(objScore, 20);
	ObjText_SetFontBold(objScore, false);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 255, 255);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,0, 0, 0);
	ObjText_SetFontBorderWidth(objScore, 1);
	Obj_SetRenderPriority(objScore, 0.01);
	ObjRender_SetX(objScore, 428+200-300);
	ObjRender_SetY(objScore, 118-32);
	ObjText_SetHorizontalAlignment(objScore, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objScore, 300);
	
	let objH = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objH, pathSystem);
	Obj_SetRenderPriority(objH, 0.01);
	ObjSprite2D_SetSourceRect(objH, 380, 499,  380+122, 499+36);
	ObjSprite2D_SetDestCenter(objH);
	ObjRender_SetScaleXYZ(objH, 0.6, 0.6, 0);
	ObjRender_SetX(objH, 428+36);
	ObjRender_SetY(objH, 90-20);
	
	let objHBar = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objHBar, pathSystem);
	Obj_SetRenderPriority(objHBar, 0.01);
	ObjSprite2D_SetSourceRect(objHBar, 0, 0, 256, 8);
	ObjSprite2D_SetDestCenter(objHBar);
	ObjRender_SetScaleXYZ(objHBar, 0.75, 0.75, 0);
	ObjRender_SetBlendType(objHBar, BLEND_ADD_ARGB);
	ObjRender_SetX(objHBar, 428+100);
	ObjRender_SetY(objHBar, 90-8);
	
	let objHScore = ObjText_Create();
	ObjText_SetFontType(objHScore, "Modern No. 20");
	ObjText_SetFontSize(objHScore, 20);
	ObjText_SetFontBold(objHScore, false);
	ObjText_SetFontColorTop(objHScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objHScore, 255, 255, 255);
	ObjText_SetFontBorderType(objHScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objHScore,0, 0, 0);
	ObjText_SetFontBorderWidth(objHScore, 1);
	Obj_SetRenderPriority(objHScore, 0.01);
	ObjRender_SetX(objHScore, 428+200-300);
	ObjRender_SetY(objHScore, 90-32);
	ObjText_SetHorizontalAlignment(objHScore, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objHScore, 300);
	
	CreateCommonDataArea("HighScore");
	let high = LoadCommonDataAreaA2("HighScore",GetCurrentScriptDirectory ~ "./score.dat");
	
	if(!high){SetAreaCommonData("HighScore","Score",0); }

	
	while(true)
	{
		let score = GetScore();
		let scoreint = IntToString(score);
		
		let scoreH = rtos("0", GetAreaCommonData("HighScore","Score",0));
		
		if(GetScore() > GetAreaCommonData("HighScore","Score",0))  {SetAreaCommonData("HighScore","Score",GetScore()); SetCommonData("GotHighScore", 1);}

		ObjText_SetText(objScore, scoreint);
		ObjText_SetText(objHScore, scoreH);
		yield;
	}
}

//----------------------------------------------------
//Graze表示
//----------------------------------------------------
task TGraze()
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, pathSystem);
	Obj_SetRenderPriority(obj, 0.01);
	ObjSprite2D_SetSourceRect(obj, 380, 451,  380+96, 451+39);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0);
	ObjRender_SetX(obj, 428+30+75);
	ObjRender_SetY(obj, 250-20);
	
	let objBar = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objBar, pathSystem);
	Obj_SetRenderPriority(objBar, 0.01);
	ObjSprite2D_SetSourceRect(objBar, 0, 0, 256, 8);
	ObjSprite2D_SetDestCenter(objBar);
	ObjRender_SetScaleXYZ(objBar, 0.55, 0.75, 0);
	ObjRender_SetBlendType(objBar, BLEND_ADD_ARGB);
	ObjRender_SetColor(objBar, 155, 155, 155);
	ObjRender_SetX(objBar, 428+100+45);
	ObjRender_SetY(objBar, 250-8);
	
	let objScore = ObjText_Create();
	ObjText_SetFontType(objScore, "Modern No. 20");
	ObjText_SetFontSize(objScore, 20);
	ObjText_SetFontBold(objScore, false);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 255, 255);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,0, 0, 0);
	ObjText_SetFontBorderWidth(objScore, 1);
	Obj_SetRenderPriority(objScore, 0.01);
	ObjRender_SetX(objScore, 428+200-300);
	ObjRender_SetY(objScore, 250-32);
	ObjText_SetHorizontalAlignment(objScore, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objScore, 300);
	
	while(true)
	{
		let graze = GetGraze();
		let grazeint = IntToString(graze);

		ObjText_SetText(objScore, grazeint);
		yield;
	}
}

//----------------------------------------------------
//残機表示
//----------------------------------------------------
task TPlayerLife
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, pathSystem);
	Obj_SetRenderPriority(obj, 0.01);
	ObjSprite2D_SetSourceRect(obj, 381, 360,  381+76, 360+38);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0);
	ObjRender_SetX(obj, 428+15);
	ObjRender_SetY(obj, 168-20);
	
	let objBar = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objBar, pathSystem);
	Obj_SetRenderPriority(objBar, 0.01);
	ObjSprite2D_SetSourceRect(objBar, 0, 0, 256, 8);
	ObjSprite2D_SetDestCenter(objBar);
	ObjRender_SetScaleXYZ(objBar, 0.75, 0.75, 0);
	ObjRender_SetBlendType(objBar, BLEND_ADD_ARGB);
	ObjRender_SetColor(objBar, 255, 0, 225);
	ObjRender_SetX(objBar, 428+100);
	ObjRender_SetY(objBar, 168-8);
	
	let LifeCounter = ObjText_Create();
	ObjText_SetFontSize(LifeCounter, 15);
	ObjText_SetFontBold(LifeCounter, true);
	ObjText_SetFontColorTop(LifeCounter, 255, 255, 255);
	ObjText_SetFontColorBottom(LifeCounter, 255, 255, 255);
	ObjText_SetFontBorderType(LifeCounter, BORDER_FULL);
	ObjText_SetFontBorderColor(LifeCounter,0, 0, 0);
	ObjText_SetFontBorderWidth(LifeCounter, 1);
	Obj_SetRenderPriority(LifeCounter, 0.01);
	ObjRender_SetX(LifeCounter, 610);
	ObjRender_SetY(LifeCounter, 120);
	//Obj_SetVisible(LifeCounter, 0);
	
	ascent(i in 1..7){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,pathSystem);
		ObjSprite2D_SetSourceRect(obj,166,197,166+144,197+155);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetScaleXYZ(obj, 0.15, 0.15, 0);
		Obj_SetRenderPriorityI(obj,12);
		ObjRender_SetPosition(obj, 480-15+i*25, 150, 0);
	}

	while(true)
	{
		let lives = GetPlayerLife;
		RenderLives;
			while(true){
			if(lives!=GetPlayerLife()){break;}
			yield;
			}
		let counting = IntToString(-6+GetPlayerLife());
		ObjText_SetText(LifeCounter, "+" ~counting); 
		if(GetPlayerLife>6){Obj_SetVisible(LifeCounter, 1);}
		else{Obj_SetVisible(LifeCounter, 0);}
	}
	
	task RenderLives{    
		ascent(i in 1..GetPlayerLife+1){
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(obj,pathSystem);
			ObjSprite2D_SetSourceRect(obj,0,198,144,198+155);
			ObjSprite2D_SetDestCenter(obj);
			ObjRender_SetScaleXYZ(obj, 0.15, 0.15, 0);
			Obj_SetRenderPriorityI(obj,12);
			ObjRender_SetPosition(obj, 480-15+i*25, 150, 0);
			trigger(obj, GetPlayerLife, i);
		}
	}
	task trigger(obj, lives,i){
	if(i>6){Obj_Delete(obj);}
		while(!Obj_IsDeleted(obj)){
		if(lives!=GetPlayerLife()){
		Obj_Delete(obj);
		break;
		}
		yield;
		}
	}
}

//----------------------------------------------------
//残スペル表示
//----------------------------------------------------
task TPlayerSpell
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, pathSystem);
	Obj_SetRenderPriority(obj, 0.01);
	ObjSprite2D_SetSourceRect(obj, 381, 404,  381+114, 404+38);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0);
	ObjRender_SetX(obj, 428+23);
	ObjRender_SetY(obj, 168-20+25);
	
	let objBar = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objBar, pathSystem);
	Obj_SetRenderPriority(objBar, 0.01);
	ObjSprite2D_SetSourceRect(objBar, 0, 0, 256, 8);
	ObjSprite2D_SetDestCenter(objBar);
	ObjRender_SetScaleXYZ(objBar, 0.75, 0.75, 0);
	ObjRender_SetBlendType(objBar, BLEND_ADD_ARGB);
	ObjRender_SetColor(objBar, 0, 255, 15);
	ObjRender_SetX(objBar, 428+100);
	ObjRender_SetY(objBar, 168-8+25);
	
	ascent(i in 1..7){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,pathSystem);
		ObjSprite2D_SetSourceRect(obj,171,11,171+144,11+155);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetScaleXYZ(obj, 0.15, 0.15, 0);
		Obj_SetRenderPriorityI(obj,12);
		ObjRender_SetPosition(obj, 480-15+i*25, 150+25, 0);
	}

	while(true)
	{
		let bombs = GetPlayerSpell;
		RenderBombs;
			while(true){
			if(bombs!=GetPlayerSpell()){break;}
			yield;
			}
	}
	
	task RenderBombs{    
		ascent(i in 1..GetPlayerSpell+1){
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(obj,pathSystem);
			ObjSprite2D_SetSourceRect(obj,12,11,12+144,11+155);
			ObjSprite2D_SetDestCenter(obj);
			ObjRender_SetScaleXYZ(obj, 0.15, 0.15, 0);
			Obj_SetRenderPriorityI(obj,12);
			ObjRender_SetPosition(obj, 480-15+i*25, 150+25, 0);
			trigger(obj, GetPlayerSpell, i);
		}
	}
	task trigger(obj, bombs,i){
	if(i>6){Obj_Delete(obj);}
		while(!Obj_IsDeleted(obj)){
		if(bombs!=GetPlayerSpell()){
		Obj_Delete(obj);
		break;
		}
		yield;
		}
	}
}

//----------------------------------------------------
//ボスのライフ表示
//----------------------------------------------------
task TBossLife{
	let path = GetCurrentScriptDirectory ~ "img/lifebar.png";
	let countVertex = 128;
	let objScene = ID_INVALID;
	
	let objStar = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objStar, GetCurrentScriptDirectory ~ "img/System.png");
	Obj_SetRenderPriority(objStar, 0.7);
	ObjSpriteList2D_SetSourceRect(objStar, 43, 643, 43+17, 643+17);

	let objOutline = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objOutline, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objOutline, countVertex);
	ObjRender_SetBlendType(objOutline, BLEND_ALPHA);
	Obj_SetRenderPriorityI(objOutline, 45);
	ObjPrim_SetTexture(objOutline, path);
	
	let objLife = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objLife, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objLife, countVertex);
	ObjRender_SetBlendType(objLife, BLEND_ALPHA);
	Obj_SetRenderPriorityI(objLife, 44);
	ObjPrim_SetTexture(objLife, path);
	
	let objLife2 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objLife2, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objLife2, countVertex);
	ObjRender_SetBlendType(objLife2, BLEND_ALPHA);
	Obj_SetRenderPriorityI(objLife2, 44);
	ObjPrim_SetTexture(objLife2, path);
	
	ascent (iVert in 0..countVertex/2){
		let vertex = iVert * 2;
		ObjPrim_SetVertexUVT(objOutline, vertex+0, 0, 0);
		ObjPrim_SetVertexUVT(objOutline, vertex+1, 4, 16);	
		ObjPrim_SetVertexUVT(objLife, vertex+0, 6, 0);
		ObjPrim_SetVertexUVT(objLife, vertex+1, 10, 16);	
		ObjPrim_SetVertexUVT(objLife2, vertex+0, 6, 0);
		ObjPrim_SetVertexUVT(objLife2, vertex+1, 10, 16);		
	}
	
	
	let mx = 0;
	let my = 0;
	let fade = 255;
	let maxRadius = 56;
	
	let lastRemStep = -1;
	let countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
	let lifeRateRender = 0;
	let outlineRender = 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 newStep = 1;
	CreateStepInd;
	
	
	ObjRender_SetScaleXYZ(objOutline, -1, 1, 1);
	ObjRender_SetScaleXYZ(objLife, -1, 1, 1);
	ObjRender_SetScaleXYZ(objLife2, -1, 1, 1);
	
	ObjRender_SetPosition(objStar, 0, 12, 0);
	
	loop{
		ObjSpriteList2D_ClearVertexCount(objStar);
		
		
		if(outlineRender<1){
			ObjRender_SetAlpha(objStar, min(255, 255*(outlineRender*3)-510));
		}else{
			if(GetPlayerY < max(96, 26+(truncate(countRemStep/12)*12))){
				fade = max(fade-10, 85);
			}else{fade = min(fade+10, 255);}
			ObjRender_SetAlpha(objStar, fade);
		}
		
		objScene = GetEnemyBossSceneObjectID;
		if(objScene != ID_INVALID){
			let Boss = GetEnemyBossObjectID;
			Boss = Boss ~ [objOutline];
			mx = ObjRender_GetX(Boss[0]);
			my = ObjRender_GetY(Boss[0]);
		}else{
			mx = -400;
			my = -400;
			outlineRender = 0;
			lifeRateRender = 0;
		}
		RefreshVertex(objOutline);
		
		if(GetCommonData("ForceLifebar", 0) != 0){
			outlineRender = 0;
			lifeRateRender = 0;
		}
		
		
		lastRemStep = countRemStep;
		
		yield;
	}
	

	task RefreshVertex(objid){
		
		countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
		if(lastRemStep != countRemStep){
			newStep = 1;
			CreateStepInd;
			lifeRateRender = 0;
		}
		ascent(iStep in 0 .. countRemStep){
			let y = truncate((iStep)/12)*12;
			let i = iStep-(12*(y/12));
			ObjSpriteList2D_SetDestRect(objStar, 4 + i * 12, 4+y,
				4 + (i + 1) * 12, 16+y);
			ObjSpriteList2D_AddVertex(objStar);
		}
	
		lifeTotalMax = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_MAX_LIFE);
		lifeTotal = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_LIFE);
		lifeRate = min(lifeTotal / lifeTotalMax, lifeRateRender);
		
		
		
		ascent(iVert in 0..countVertex/2){
			let indexVert = iVert * 2;
			let angle = (360 / (countVertex/2 - 1) * iVert);
			angle = min(360*(outlineRender), angle);
			let x = maxRadius*cos(angle-90);
			let y = maxRadius*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife, indexVert + 0 , x, y, 0);
			x = (maxRadius*1.025)*cos(angle-90);
			y = (maxRadius*1.025)*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife, indexVert + 1 , x, y, 0);
			
			x = (maxRadius*1.065)*cos(angle-90);
			y = (maxRadius*1.065)*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife2, indexVert + 0 , x, y, 0);
			x = (maxRadius*1.09)*cos(angle-90);
			y = (maxRadius*1.09)*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife2, indexVert + 1 , x, y, 0);
			
			angle = min(360*(lifeRate), angle);
			x = (maxRadius)*cos(angle-90);
			y = (maxRadius)*sin(angle-90);
			ObjPrim_SetVertexPosition(objOutline, indexVert + 0 , x, y, 0);			
			x = (maxRadius*1.075)*cos(angle-90);
			y = (maxRadius*1.075)*sin(angle-90);
			ObjPrim_SetVertexPosition(objOutline, indexVert + 1 , x, y, 0);
		}
		ObjRender_SetPosition(objOutline, mx, my, 0);
		ObjRender_SetPosition(objLife, mx, my, 0);
		ObjRender_SetPosition(objLife2, mx, my, 0);
		
		lifeRateRender = min(1, lifeRateRender+0.02);
		outlineRender = min(1, outlineRender+0.016);
		
	}
	task CreateStepInd{
		let listLifeDiv = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_LIFE_RATE_LIST);
		ascent(iDiv in 0 .. length(listLifeDiv)){
			let rate = listLifeDiv[iDiv];
			let angle = 360*(rate);
			CreateKnub(angle, rate);
		}
		task CreateKnub(angle, rate){
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjRender_SetBlendType(obj, BLEND_ALPHA);
			Obj_SetRenderPriority(obj, 0.69);
			ObjPrim_SetTexture(obj, path);
			ObjSprite2D_SetSourceRect(obj, 10, 0, 15, 7);
			ObjSprite2D_SetDestCenter(obj);
			ObjRender_SetScaleXYZ(obj, 1.25, 1.25, 1);
			ObjRender_SetPosition(obj, mx, my, 0);
			ObjRender_SetAngleZ(obj, angle);
			yield;
			newStep = 0;
			let life = (lifeTotal / lifeTotalMax);
			while(newStep == 0 && 1-rate<life-0.000001){
				let x = mx+(maxRadius*1.05)*cos(angle-90);
				let y = my+(maxRadius*1.05)*sin(angle-90);
				life = (lifeTotal / lifeTotalMax);
				ObjRender_SetPosition(obj, x, y, 0);
				ObjRender_SetAlpha(obj, min(255, 255*(outlineRender*3)-510));
				yield;
			}
			Obj_Delete(obj);
		}
	}

}


//----------------------------------------------------
//タイマー表示
//----------------------------------------------------
task TBossTimer
{
	InstallFont(GetCurrentScriptDirectory() ~ "DD.ttf");
	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 22);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontType(objText, "DD");
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,25, 25, 25);
	ObjText_SetFontBorderWidth(objText, 1);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objText, GetStgFrameWidth-24);
	Obj_SetRenderPriority(objText, 1.0);
	
	ObjRender_SetX(objText, 44);
	ObjRender_SetY(objText, 16);
	
	let objScene = ID_INVALID;
	loop
	{
		objScene = GetEnemyBossSceneObjectID();
		if(objScene != ID_INVALID)
		{
			RenderTimer();
		}
		yield;
	}

	function RenderTimer()
	{
	let timer = rtos("000.00", ObjEnemyBossScene_GetInfo(objScene, INFO_TIMERF) / 60);
	ObjText_SetText(objText, timer);
	if(GetCommonData("RenderTimer", 1) == 1){Obj_SetVisible(objText, 1);}
	if(GetCommonData("RenderTimer", 1) == 0){Obj_SetVisible(objText, 0);}
	}
}
//----------------------------------------------------
//スペルカード取得表示
//----------------------------------------------------
task TGainSpell(score)
{
	AddScore(score);
	
	let text = ObjPrim_Create(OBJ_SPRITE_2D);

	Obj_SetRenderPriority(text, 0.7);
	ObjPrim_SetTexture(text,pathSystem);
	ObjSprite2D_SetSourceRect(text,374,4,374+346,4+45);
	ObjSprite2D_SetDestCenter(text);
	ObjRender_SetPosition(text, GetStgFrameWidth/2, 125, 0);
	ObjRender_SetAlpha(text, 255);
	ObjRender_SetScaleXYZ(text, 1, 1, 1);

	let strScore = "+" ~ IntToString(score);
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, strScore);
	ObjText_SetFontType(objScore, "Modern No. 20");
	ObjText_SetFontSize(objScore, 38);
	ObjText_SetFontBold(objScore, false);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 255, 255);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,0, 0, 0);
	ObjText_SetFontBorderWidth(objScore, 1);
	Obj_SetRenderPriority(objScore, 0.6);
	ObjRender_SetX(objScore, 180);
	ObjRender_SetY(objScore, 140);

	loop(120)
	{
		yield;
	}
	ascent(i in 0..60){
	ObjRender_SetAlpha(text, 255-i*10);
	ObjRender_SetAlpha(objScore, 255-i*10);
	yield;
	}
	Obj_Delete(text);
	Obj_Delete(objScore);
}

//----------------------------------------------------
//FPS表示
//----------------------------------------------------
task TCurrentFps()
{
	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontType(objText, "DD");
	ObjText_SetFontColorTop(objText, 160, 160, 255);
	ObjText_SetFontColorBottom(objText, 166, 166, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,6, 6, 6);
	ObjText_SetFontBorderWidth(objText, 1);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objText, GetScreenWidth() - 8);
	Obj_SetRenderPriority(objText, 1.0);
	ObjRender_SetX(objText, 0);
	ObjRender_SetY(objText, GetScreenHeight() - 20);

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

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

	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 12);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontType(objText, "DD");
	ObjText_SetFontColorTop(objText, 255, 166, 166);
	ObjText_SetFontColorBottom(objText, 255, 166, 166);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,6, 6, 6);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 1.0);

	let px = GetStgFrameLeft() + GetStgFrameWidth() - 18;
	let py = GetStgFrameTop() + GetScreenHeight() - 14;
	ObjRender_SetX(objText, px);
	ObjRender_SetY(objText, py);

	loop
	{
		let fps = GetReplayFps();
		let text = vtos("02d", 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;
}

task Invincibility{
	AddVirtualKey(VK_USER1,KEY_SPACE,KEY_INVALID);
	
	loop{
		if(GetVirtualKeyState(VK_USER1)==KEY_HOLD){
		SetPlayerInvincibilityFrame(30);}
	yield;}	
}

task AddLives{
	AddVirtualKey(VK_USER2,KEY_L,KEY_INVALID);
	
	loop{
		if(GetVirtualKeyState(VK_USER2)==KEY_PUSH){
		SetPlayerLife(GetPlayerLife+1);}
	yield;}	
}

