/**
 * フレーム内の要素の描画
 */
task FrameDrawTask(){
	let showscore1 = false;
	let showscore2 = false;
	let showscore3 = false;
	let showscore4 = false;
	let showscore5 = false;
	let showscore6 = false;
	
	let showscorecomma1 = false;
	let showscorecomma2 = false;
	let showscorecomma3 = false;
	let showscorecomma4 = false;
	let showscorecomma5 = false;
	let showscorecomma6 = false;
	
	let tmp_score1 = 0;
	let tmp_score2 = 0;
	let tmp_score3 = 0;
	let tmp_score4 = 0;
	let tmp_score5 = 0;
	let tmp_score6 = 0;
	
	let showpoint1 = false;
	let showpoint2 = false;
	
	let showpointcomma1 = false;
	let showpointcomma2 = false;
	
	let tmp_point = 0;
	let tmp_point1 = 0;
	let tmp_point2 = 0;
	
	DrawGauge();
	DrawSystemText();
	
	loop{
		//スコア計算
		tmp_score1 = truncate( score1 / 1000000 );
		tmp_score2 = truncate( score1 / 100 ) % 10000;
		tmp_score3 = ( score1 % 100 ) * 100 + truncate( score2 / 100000 );
		tmp_score4 = truncate( score2 / 10 ) % 10000;
		tmp_score5 = ( score2 % 10 ) * 1000 + truncate( score3 / 10000 );
		tmp_score6 = score3 % 10000;
		
		showscore1 = false;
		showscore2 = showscore1 || ( tmp_score1 > 0 );
		showscore3 = showscore2 || ( tmp_score2 > 0 );
		showscore4 = showscore3 || ( tmp_score3 > 0 );
		showscore5 = showscore4 || ( tmp_score4 > 0 );
		showscore6 = showscore5 || ( tmp_score5 > 0 );
		
		showscorecomma1 = ( tmp_score1 > 0 );
		showscorecomma2 = showscorecomma1 || ( tmp_score2 > 0 );
		showscorecomma3 = showscorecomma2 || ( tmp_score3 > 0 );
		showscorecomma4 = showscorecomma3 || ( tmp_score4 > 0 );
		showscorecomma5 = showscorecomma4 || ( tmp_score5 > 0 );
		showscorecomma6 = showscorecomma5 || ( tmp_score6 > 0 );
		
		//スコア描画
		ScoreDraw( tmp_score1, 16, -54,  20, showscore1, false,  true );
		if( showscorecomma1 ){
			CommaDraw( 24,  22, 12 );
		}
		ScoreDraw( tmp_score2, 16,  36,  20, showscore2, false,  true );
		if( showscorecomma2 ){
			CommaDraw( 24, 112, 12 );
		}
		ScoreDraw( tmp_score3, 16, 126,  20, showscore3, false,  true );
		if( showscorecomma3 ){
			CommaDraw( 24, 202, 12 );
		}
		ScoreDraw( tmp_score4, 16, 216,  20, showscore4, false,  true );
		if( showscorecomma4 ){
			CommaDraw( 24, 292, 12 );
		}
		ScoreDraw( tmp_score5, 16, 306,  20, showscore5, false,  true );
		if( showscorecomma5 ){
			CommaDraw( 24, 382, 12 );
		}
		ScoreDraw( tmp_score6, 16, 396,  20, showscore6,  true,  true );
		
		//スコア値計算
		tmp_point = GetScorePoint();
		tmp_point1 = truncate( tmp_point / 10000 );
		tmp_point2 = tmp_point % 10000;
		
		showpoint1 = false;
		showpoint2 = showpoint1 || ( tmp_point1 > 0 );
		
		showpointcomma1 = ( tmp_point1 > 0 );
		showpointcomma2 = showpointcomma1 || ( tmp_point2 > 0 );
		
		//スコア値描画
		ScoreDraw( tmp_point1, 508, 30, 16, showpoint1, false, false );
		if( showpointcomma1 ){
			CommaDraw( 568, 40, 10 );
		}
		ScoreDraw( tmp_point2, 576, 30, 16, showpoint2,  true, false );
		
		yield;
	}
}

/**
 * 4桁の数値の描画
 */
task ScoreDraw( draw, x, y, size, zerodraw, isminscore, isvertical ){
	let sw = true;
	let xsize = 0;
	let ysize = 0;
	
	if( isvertical ){
		ysize = size;
	}else{
		xsize = size;
	}
	
	draw = draw % 10000;
	let d1 = truncate( draw / 1000 );
	if( d1 == 0 && !zerodraw && sw ){
		d1 = -1;
	}else{
		sw = false;
	}
	draw = draw % 1000;
	let d2 = truncate( draw / 100 );
	if( d2 == 0 && !zerodraw && sw ){
		d2 = -1;
	}else{
		sw = false;
	}
	draw = draw % 100;
	let d3 = truncate( draw / 10 );
	if( d3 == 0 && !zerodraw && sw ){
		d3 = -1;
	}else{
		sw = false;
	}
	draw = draw % 10;
	let d4 = truncate( draw / 1 );
	if( d4 == 0 && !zerodraw && sw && !isminscore ){
		d4 = -1;
	}else{
		sw = false;
	}
	
	NumberDraw( d1, x + xsize * 0, y + ysize * 0, size );
	NumberDraw( d2, x + xsize * 1, y + ysize * 1, size );
	NumberDraw( d3, x + xsize * 2, y + ysize * 2, size );
	NumberDraw( d4, x + xsize * 3, y + ysize * 3, size );
}

/**
 * 任意の一数値を描画する
 */
task NumberDraw( num, x, y, size ){
	if( num == -1 ){
		return;
	}
	
	let imgNumber = "img\Font.png";
	let imgX = 36;
	let imgY = 32;
	let imgXHalf = imgX / 2;
	let imgYHalf = imgY / 2;
	let offsetX = imgX * num;
	let offsetY = 0;
	let scale = size / imgY;
	
	let obj = Obj_Create( OBJ_EFFECT );
	ObjEffect_SetTexture( obj, imgNumber );
	ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
	ObjEffect_CreateVertex( obj, 4 );
	
	ObjEffect_SetVertexXY( obj, 0, -imgXHalf, -imgYHalf );
	ObjEffect_SetVertexXY( obj, 1, -imgXHalf,  imgYHalf );
	ObjEffect_SetVertexXY( obj, 2,  imgXHalf, -imgYHalf );
	ObjEffect_SetVertexXY( obj, 3,  imgXHalf,  imgYHalf );
	
	ObjEffect_SetVertexUV( obj, 0,        offsetX,        offsetY );
	ObjEffect_SetVertexUV( obj, 1,        offsetX, offsetY + imgY );
	ObjEffect_SetVertexUV( obj, 2, offsetX + imgX,        offsetY );
	ObjEffect_SetVertexUV( obj, 3, offsetX + imgX, offsetY + imgY );
	
	ObjEffect_SetVertexColor( obj, 0, 255, 255, 255, 255 );
	ObjEffect_SetVertexColor( obj, 1, 255, 255, 255, 255 );
	ObjEffect_SetVertexColor( obj, 2, 255, 255, 255, 255 );
	ObjEffect_SetVertexColor( obj, 3, 255, 255, 255, 255 );
	
	ObjEffect_SetLayer( obj, 8 );
	
	ObjEffect_SetScale( obj, scale, scale );
	
	Obj_SetPosition( obj, x, y );
	
	yield;
	
	if( !Obj_BeDeleted( obj ) ){
		Obj_Delete( obj );
	}
}

/**
 * カンマを描画する
 */
task CommaDraw( x, y, size ){
	let imgX = 10;
	let imgY = 10;
	let imgXHalf = imgX / 2;
	let imgYHalf = imgY / 2;
	let offsetX = 0;
	let offsetY = 20;
	let scale = size / imgY;
	
	let obj = Obj_Create( OBJ_EFFECT );
	ObjEffect_SetTexture( obj, imgText );
	ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
	ObjEffect_CreateVertex( obj, 4 );
	
	ObjEffect_SetVertexXY( obj, 0, -imgXHalf, -imgYHalf );
	ObjEffect_SetVertexXY( obj, 1, -imgXHalf,  imgYHalf );
	ObjEffect_SetVertexXY( obj, 2,  imgXHalf, -imgYHalf );
	ObjEffect_SetVertexXY( obj, 3,  imgXHalf,  imgYHalf );
	
	ObjEffect_SetVertexUV( obj, 0,        offsetX,        offsetY );
	ObjEffect_SetVertexUV( obj, 1,        offsetX, offsetY + imgY );
	ObjEffect_SetVertexUV( obj, 2, offsetX + imgX,        offsetY );
	ObjEffect_SetVertexUV( obj, 3, offsetX + imgX, offsetY + imgY );
	
	ObjEffect_SetVertexColor( obj, 0, 255, 255, 255, 255 );
	ObjEffect_SetVertexColor( obj, 1, 255, 255, 255, 255 );
	ObjEffect_SetVertexColor( obj, 2, 255, 255, 255, 255 );
	ObjEffect_SetVertexColor( obj, 3, 255, 255, 255, 255 );
	
	ObjEffect_SetLayer( obj, 8 );
	
	ObjEffect_SetScale( obj, scale, scale );
	
	Obj_SetPosition( obj, x, y );
	
	yield;
	
	if( !Obj_BeDeleted( obj ) ){
		Obj_Delete( obj );
	}
}

/**
 * ゲージ描画
 */
task DrawGauge(){
	//念のためIDバグ回避
	//5分ごとに再起動
	let rebootcount = 60 * 60 * 5;
	
	let Gauge_Position = 0;
	let Gauge_X        = 432;
	let Gauge_Y        = 150;
	let Gauge_Size     = 1;
	let Gauge_Angle    = 1;
	
	let count = 0;
	let gauge = 0;
	let pct = 0;
	let x1 = Gauge_X;
	let y1 = Gauge_Y;
	let x2 = Gauge_X + 40;
	let y2 = Gauge_Y;
	let x3 = Gauge_X + 80;
	let y3 = Gauge_Y;
	let x4 = Gauge_X + 120;
	let y4 = Gauge_Y;
	let x5 = Gauge_X + 160;
	let y5 = Gauge_Y;
	let imgx = 300;
	let imgy = 32;
	let layer = 8;
	let offsetx = 0;
	let offsety = 0;
	
	loop{
		let objbase1        = CreateObjGauge( imgGauge, x1, y1, 0, 84, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 255, 255 );
		let objshieldgauge  = CreateObjGauge( imgGauge, x1, y1, 0, 52, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 255, 128 );
		let objframe1       = CreateObjGauge( imgGauge, x1, y1, 0, 20, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 255, 255 );
		
		let objbase2        = CreateObjGauge( imgGauge, x2, y2, 0, 84, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 255, 255 );
		let objchargegauge  = CreateObjGauge( imgGauge, x2, y2, 0, 52, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 128, 128, 255 );
		let objframe2       = CreateObjGauge( imgGauge, x2, y2, 0, 20, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 255, 255 );
		
		let objbase3        = CreateObjGauge( imgGauge, x3, y3, 0, 84, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 255, 255 );
		let objbombextgauge = CreateObjGauge( imgGauge, x3, y3, 0, 52, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 128, 255 );
		let objframe3       = CreateObjGauge( imgGauge, x3, y3, 0, 20, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 255, 255 );
		
		let objbase4        = CreateObjGauge( imgGauge, x4, y4, 0, 84, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 255, 255 );
		let objplayergauge  = CreateObjGauge( imgGauge, x4, y4, 0, 52, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 128, 128 );
		let objframe4       = CreateObjGauge( imgGauge, x4, y4, 0, 20, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 255, 255 );
		
		let objbase5        = CreateObjGauge( imgGauge, x5, y5, 0, 84, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 255, 255 );
		let objbombgauge    = CreateObjGauge( imgGauge, x5, y5, 0, 52, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 128, 255, 128 );
		let objframe5       = CreateObjGauge( imgGauge, x5, y5, 0, 20, imgx, imgy, layer, Gauge_Size, Gauge_Angle, 255, 255, 255, 255 );
		
		while( !Obj_BeDeleted( objbase1 ) ){
			//ゲージのサイズ指定
			
			//シールドゲージ
			gauge = GetShieldGauge();
			pct = gauge / ShieldGauge_Max;
			if( pct > 1 ){
				pct = 1;
			}
			ObjEffect_SetScale( objshieldgauge, Gauge_Size * pct, Gauge_Size );
			if( Gauge_Angle == 1 ){
				offsetx = 0;
				offsety = imgx * (1 - pct) * Gauge_Size;
				Obj_SetPosition( objshieldgauge, x1 + offsetx, y1 + offsety );
			}
			
			//チャージゲージ
			gauge = GetChargeGauge() / ChargeGauge_Max + GetNowChargeCount();
			pct = gauge / 10;
			if( pct > 1 ){
				pct = 1;
			}
			ObjEffect_SetScale( objchargegauge, Gauge_Size * pct, Gauge_Size );
			if( Gauge_Angle == 1 ){
				offsetx = 0;
				offsety = imgx * (1 - pct) * Gauge_Size;
				Obj_SetPosition( objchargegauge, x2 + offsetx, y2 + offsety );
			}
			
			//ボムエクステンドゲージ
			gauge = GetBombExtendPoint();
			pct = 0;
			if( GetBombExtendNormaPoint() != 0 ){
				pct = gauge / GetBombExtendNormaPoint();
			}
			if( pct > 1 ){
				pct = 1;
			}
			ObjEffect_SetScale( objbombextgauge, Gauge_Size * pct, Gauge_Size );
			if( Gauge_Angle == 1 ){
				offsetx = 0;
				offsety = imgx * (1 - pct) * Gauge_Size;
				Obj_SetPosition( objbombextgauge, x3 + offsetx, y3 + offsety );
			}
			
			//プレイヤー数
			gauge = GetPlayerLife();
			pct = gauge / 10;
			if( pct > 1 ){
				pct = 1;
			}
			ObjEffect_SetScale( objplayergauge, Gauge_Size * pct, Gauge_Size );
			if( Gauge_Angle == 1 ){
				offsetx = 0;
				offsety = imgx * (1 - pct) * Gauge_Size;
				Obj_SetPosition( objplayergauge, x4 + offsetx, y4 + offsety );
			}
			
			//ボム数
			gauge = GetPlayerBomb();
			pct = gauge / 10;
			if( pct > 1 ){
				pct = 1;
			}
			ObjEffect_SetScale( objbombgauge, Gauge_Size * pct, Gauge_Size );
			if( Gauge_Angle == 1 ){
				offsetx = 0;
				offsety = imgx * (1 - pct) * Gauge_Size;
				Obj_SetPosition( objbombgauge, x5 + offsetx, y5 + offsety );
			}
			
			count += 1;
			yield;
			
			//再起動
			if( count >= rebootcount ){
				Obj_Delete( objbase1 );
				Obj_Delete( objshieldgauge );
				Obj_Delete( objframe1 );
				
				Obj_Delete( objbase2 );
				Obj_Delete( objchargegauge );
				Obj_Delete( objframe2 );
				
				Obj_Delete( objbase3 );
				Obj_Delete( objbombextgauge );
				Obj_Delete( objframe3 );
				
				Obj_Delete( objbase4 );
				Obj_Delete( objplayergauge );
				Obj_Delete( objframe4 );
				
				Obj_Delete( objbase5 );
				Obj_Delete( objbombgauge );
				Obj_Delete( objframe5 );
				
				count = 0;
			}
		}
	}
}

/**
 * 文字画像の描画
 */
task DrawSystemText(){
	//念のためIDバグ回避
	//5分ごとに再起動
	let rebootcount = 60 * 60 * 5;
	let count = 0;
	
	loop{
		//Score
		let objText_score  = CreateObjEffect( imgText,  16,  48,   0, 256,  24, 64, 8, 255, 255, 255, 255 );
		//Score Point
		let objText_sp     = CreateObjEffect( imgText, 484,  28,   0,   0, 120, 20, 8, 255, 255, 255, 255 );
		//Shield
		let objText_shield = CreateObjEffect( imgText, 448, 102,  24, 256,  24, 72, 8, 255, 255, 255, 255 );
		//Charge
		let objText_charge = CreateObjEffect( imgText, 488, 102,  48, 256,  30, 82, 8, 255, 255, 255, 255 );
		//Extend
		let objText_extend = CreateObjEffect( imgText, 528, 102,  78, 256,  24, 80, 8, 255, 255, 255, 255 );
		//Player
		let objText_player = CreateObjEffect( imgText, 568, 102, 102, 256,  30, 76, 8, 255, 255, 255, 255 );
		//Bomb
		let objText_bomb   = CreateObjEffect( imgText, 608, 102, 132, 256,  24, 66, 8, 255, 255, 255, 255 );
		
		ObjEffect_SetRenderState( objText_score, ADD );
		ObjEffect_SetRenderState( objText_sp, ADD );
		ObjEffect_SetRenderState( objText_shield, ADD );
		ObjEffect_SetRenderState( objText_charge, ADD );
		ObjEffect_SetRenderState( objText_extend, ADD );
		ObjEffect_SetRenderState( objText_player, ADD );
		ObjEffect_SetRenderState( objText_bomb, ADD );
		
		while( !Obj_BeDeleted( objText_score ) ){
			count += 1;
			yield;
			
			//再起動
			if( count >= rebootcount ){
				Obj_Delete( objText_score );
				Obj_Delete( objText_sp );
				Obj_Delete( objText_shield );
				Obj_Delete( objText_charge );
				Obj_Delete( objText_extend );
				Obj_Delete( objText_player );
				Obj_Delete( objText_bomb );
				
				count = 0;
			}
		}
	}
}

/**
 * OBJ_EFFECT生成関数2
 * こちらは左上が描画基準となります
 * 
 * @param {String}  effectimg 画像
 * @param {Integer} x         中心x座標
 * @param {Integer} y         中心y座標
 * @param {Integer} cutx      切り出す基点x値
 * @param {Integer} cuty      切り出す基点y値
 * @param {Integer} imgx      画像のX幅
 * @param {Integer} imgy      画像のY幅
 * @param {Integer} layer     表示レイヤー
 * @param {Integer} size      表示サイズ
 * @param {Integer} rotate    描画方向
 * @param {Integer} alpha     α値
 * @param {Integer} r         R値
 * @param {Integer} g         G値
 * @param {Integer} b         B値
 * @return {Object} 作成したオブジェクトのid
 */
function CreateObjGauge( effectimg, x, y, cutx, cuty, imgx, imgy, layer, size, rotate, alpha, r, g, b ){
	//オブジェクト作成
	let obj = Obj_Create( OBJ_EFFECT );
	//テクスチャ貼り付け
	ObjEffect_SetTexture( obj, effectimg );
	//頂点の取り方を設定
	ObjEffect_SetPrimitiveType( obj, PRIMITIVE_TRIANGLESTRIP );
	//頂点数を4に設定
	ObjEffect_CreateVertex( obj, 4 );
	//頂点位置設定
	if( rotate == 0 ){
		//横
		ObjEffect_SetVertexXY(obj, 0,    0,    0 );
		ObjEffect_SetVertexXY(obj, 1,    0, imgy );
		ObjEffect_SetVertexXY(obj, 2, imgx,    0 );
		ObjEffect_SetVertexXY(obj, 3, imgx, imgy );
	}else{
		//縦
		ObjEffect_SetVertexXY(obj, 0,     0,    0 );
		ObjEffect_SetVertexXY(obj, 1,     0, imgy );
		ObjEffect_SetVertexXY(obj, 2, -imgx,    0 );
		ObjEffect_SetVertexXY(obj, 3, -imgx, imgy );
		
		ObjEffect_SetAngle( obj, 0, 0, -90 );
	}
	//画像切り出し設定
	ObjEffect_SetVertexUV( obj, 0,        cutx,        cuty );
	ObjEffect_SetVertexUV( obj, 1,        cutx, cuty + imgy );
	ObjEffect_SetVertexUV( obj, 2, cutx + imgx,        cuty );
	ObjEffect_SetVertexUV( obj, 3, cutx + imgx, cuty + imgy );
	//α値、RGB値設定
	ObjEffect_SetVertexColor( obj, 0, alpha, r, g, b );
	ObjEffect_SetVertexColor( obj, 1, alpha, r, g, b );
	ObjEffect_SetVertexColor( obj, 2, alpha, r, g, b );
	ObjEffect_SetVertexColor( obj, 3, alpha, r, g, b );
	//レイヤー設定
	ObjEffect_SetLayer( obj, layer );
	//サイズ設定
	ObjEffect_SetScale( obj, size, size );
	//位置設定
	Obj_SetPosition( obj, x, y );
	
	return obj;
}