/*!

	鮫亀スコアボード部分

*/
#if defined SCOREBOARD_INCLUDED___
  #endinput
#endif
#define SCOREBOARD_INCLUDED___

#include "types.inc"


//////////////////////////////////////////////////////////////////////////////////

static obj:g_ScoreBoard;			//背景
static obj:g_ScoreObject;			//ボードの書き文字
static obj:g_GetObject;				//
static obj:g_LeftObject;			//
static obj:g_ScoreStringObject[6];	//スコア関係
static obj:g_GetStringObject[6];
static obj:g_LeftStringObject[3];

static texture:g_TextureScore[TEXTURE_SCOREBOARD_NUM];

static g_ScoreValue;				//得点
static g_GetScoreValue;				//消したときにとれる得点
static g_LeftValue;					//残り数

//////////////////////////////////////////////////////////////////////////////////
/*!
	背景の作成をする
*/
stock ScoreBoardCreate(obj:parent) {
	g_ScoreBoard = obj:CreateCharacterObject(
							0.0, 0.0,
							640.0, 448.0,
							TextureLoad("./texture/win_field.png", false), parent,
							ZSORT_NORMAL, 2.0);

	imgDecStart("./texture/moji01.png", _, _, _, _, IMGDEC_FLAG_INDIRECT);
	imgDecWait();
	TrimingTextureFromXml(domRoot(), "TEXTURE", "score", g_TextureScore);

	g_ScoreObject = obj:CreateCharacterObject(
							241.0, -150.0,
							80.0, 16.0,
							g_TextureScore[TEXTURE_SCORE], parent,
							ZSORT_NORMAL, 3.0);
	g_GetObject = obj:CreateCharacterObject(
							225.0, -86.0,
							48.0, 16.0,
							g_TextureScore[TEXTURE_GET], parent,
							ZSORT_NORMAL, 3.0);
	g_LeftObject = obj:CreateCharacterObject(
							234.0, -22.0,
							64.0, 16.0,
							g_TextureScore[TEXTURE_LEFT], parent,
							ZSORT_NORMAL, 3.0);

	////////スコアとかの得点表示用

	for (new i = 0; i < sizeof g_ScoreStringObject;i++) {
		g_ScoreStringObject[i] = obj:CreateCharacterObject(
							210.0 + i * 14.0, -118.0,
							12.0, 16.0,
							g_TextureScore[TEXTURE_ASCII_0], parent,
							ZSORT_NORMAL, 3.0);
	}
	for (new i = 0; i < sizeof g_GetStringObject;i++) {
		g_GetStringObject[i] = obj:CreateCharacterObject(
							210.0 + i * 14.0, -53.0,
							12.0, 16.0,
							g_TextureScore[TEXTURE_ASCII_0], parent,
							ZSORT_NORMAL, 3.0);
	}
	for (new i = 0; i < sizeof g_LeftStringObject;i++) {
		g_LeftStringObject[i] = obj:CreateCharacterObject(
							252.0 + i * 14.0, 10.0,
							12.0, 16.0,
							g_TextureScore[TEXTURE_ASCII_0], parent,
							ZSORT_NORMAL, 3.0);
	}


}

stock ScoreBoardHide() {
	if (IsValidObject(g_ScoreBoard)) {
		Hide(g_ScoreBoard);
		HideChildren(g_ScoreBoard);
	}
}

stock ScoreBoardShow() {
	if (IsValidObject(g_ScoreBoard)) {
		Show(g_ScoreBoard);
		ShowChildren(g_ScoreBoard);
	}
}

stock ScoreBoardDestroy() {
	SafeDestroyObject(g_ScoreBoard);
	SafeDestroyObject(g_ScoreObject);
	SafeDestroyObject(g_GetObject);
	SafeDestroyObject(g_LeftObject);

	ArrayReleaseTexture(g_TextureScore);

}
//////////////////////////////////////////////////////////////////////////////////
/*!
	スコアテクスチャの設定
*/
stock SetNumericTexture(obj:target[], score, length = sizeof target) {

	new score_string[128];
	sprintf(score_string, "%d", score);

	new string_length = strlen(score_string);

//	printf("%d : %d, %s\n", score, length, score_string);

	//とりあえず全部隠す
	for (new i = 0; i < length;i++) {
		Hide(target[i]);
	}

	//そして、設定
	length--;
	for (new i = (string_length - 1); i > -1;i--) {

//		printf("%d\n",i);

		new tex_id = score_string{i} - '0';
		SetTexture(target[length], g_TextureScore[tex_id]);
		Show(target[length]);		//必要な分だけ表示する
		length--;
	}
}

//////////////////////////////////////////////////////////////////////////////////
/*!
	スコア表示
*/
stock GetScore() {
	return g_ScoreValue;
}
//stock ScoreAdd(const value) {
//	g_ScoreValue = g_ScoreValue + value;
//	SetNumericTexture(g_ScoreStringObject, g_ScoreValue);
//}
stock ScoreAdd() {
	g_ScoreValue = g_ScoreValue + g_GetScoreValue;
	SetNumericTexture(g_ScoreStringObject, g_ScoreValue);
}
stock ScoreImmediateAdd(value) {
	g_ScoreValue = g_ScoreValue + value;
	SetNumericTexture(g_ScoreStringObject, g_ScoreValue);
}
stock ScoreClear() {
	g_ScoreValue = 0;
	SetNumericTexture(g_ScoreStringObject, g_ScoreValue);
}
//////////////////////////////////////////////////////////////////////////////////
/*!
	取得できる得点表示
*/
stock SetGetScore(value) {
	SetNumericTexture(g_GetStringObject, value);
	g_GetScoreValue = value;
}

//////////////////////////////////////////////////////////////////////////////////
/*!
	残り数設定
*/
stock SetLeft(value) {
	g_LeftValue = value;
	SetNumericTexture(g_LeftStringObject, g_LeftValue);
}
stock LeftSub(value) {
	g_LeftValue -= value;
	SetNumericTexture(g_LeftStringObject, g_LeftValue);
}
stock GetLeft() {
	return g_LeftValue;
}
