function My_Obj_GetValueDefault(obj, key, default_val)
{
	let tmp = default_val;
	if (Obj_IsValueExists(obj, key)) { tmp = Obj_GetValue(obj, key); }
	return (tmp);
}
let objPlayer = GetPlayerObjectID;

let objTalk_Text = [];	//文字
let objTalk_Base = 0;	//背景
let objTalk_Face = [0, 0];	//顔画像

//吹き出し部分へのめり込み範囲
let MerikomiX = 0;
let MerikomiY = 6;

//色ナンバー
let talk_cr = [255, 255, 255, 192, 192, 192, 255, 255, 192];
let talk_cg = [192, 220, 255, 255, 255, 192, 192, 255, 192];
let talk_cb = [192, 192, 192, 192, 255, 255, 255, 255, 192];

let FUKIDASHI_RED    = 0;
let FUKIDASHI_ORANGE = 1;
let FUKIDASHI_YELLOW = 2;
let FUKIDASHI_GREEN  = 3;
let FUKIDASHI_SKY    = 4;
let FUKIDASHI_BLUE   = 5;
let FUKIDASHI_PURLE  = 6;
let FUKIDASHI_WHITE  = 7;
let FUKIDASHI_GRAY   = 8;

let saveIsTalk = "IS_TALK";			//会話中フラグ


//会話開始
function Talk_RefleshObject
{
	if (objTalk_Base) { Obj_Delete(objTalk_Base); }
	objTalk_Base = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objTalk_Base, PRIMITIVE_TRIANGLELIST);
	ObjPrim_SetVertexCount(objTalk_Base, 6*9);
	ObjPrim_SetTexture(objTalk_Base, imgTextBox);
	Obj_SetRenderPriorityI(objTalk_Base, 83);

	ascent(i in 0 .. length(objTalk_Face))
	{
		if (objTalk_Face[i]) { Obj_Delete(objTalk_Face[i]); }
		objTalk_Face[i] = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriorityI(objTalk_Face[i], 81);
	}
	while(length(objTalk_Text) > 0)
	{
		if (objTalk_Text[0]) { Obj_Delete(objTalk_Text[0]); }
		objTalk_Text = erase(objTalk_Text, 0);
	}
	
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);

	Obj_SetValue(objPlayer, saveIsTalk, true);	//フラグON
}

//会話終了
function Talk_DeleteObject
{
	let base_alpha = [255, 255];
	ascent(j in 0 .. 2)
	{
		if (!Obj_IsDeleted(objTalk_Face[j]))
		{
			let hidden_flag = My_Obj_GetValueDefault(objTalk_Face[j], "HIDDEN", 0);
			if (hidden_flag) { base_alpha[j] = 0; }
		}
	}

	descent(i in 0 .. 15)
	{
		ascent(j in 0 .. 2)
		{
			if (!Obj_IsDeleted(objTalk_Face[j]))
			{
				let muki = (j-0.5)*2;
				ObjRender_SetAlpha(objTalk_Face[j], base_alpha[j]*(i+1)/15);
				ObjRender_SetPosition(objTalk_Face[j], ObjRender_GetX(objTalk_Face[j])-3*muki, ObjRender_GetY(objTalk_Face[j])+3, 0);
			}
		}
		yield;
	}
	Obj_Delete(objTalk_Base);
	while(length(objTalk_Text) > 0)
	{
		if (objTalk_Text[0]) { Obj_Delete(objTalk_Text[0]); }
		objTalk_Text = erase(objTalk_Text, 0);
	}
	ascent(i in 0 .. length(objTalk_Face))
	{
		if (objTalk_Face[i]) { Obj_Delete(objTalk_Face[i]); }
	}
	
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);

	Obj_SetValue(objPlayer, saveIsTalk, true);	//フラグOFF
}

//吹き出し呼び出し
function Talk_SetText_Flame(text, font_size, type, color, x, y)
{
	//テキストリセット
	while(length(objTalk_Text) > 0)
	{
		if (objTalk_Text[0]) { Obj_Delete(objTalk_Text[0]); }
		objTalk_Text = erase(objTalk_Text, 0);
	}

	let c = 0;
	if (type == 0) { c = 1; }
	
	//空白行を取り除く
	descent(i in 0 .. length(text))
	{
		if (length(text[i]) <= 0) { text = erase(text, i); }
	}

	//文字の最大幅取得
	let max_text_len = 0;
	let text_lines = length(text);	//行数
	ascent(i in 0 .. text_lines)
	{
		max_text_len = max(max_text_len, length(text[i]));
	}

	let string_size = font_size/12*16;	//文字サイズ 12pt=16px
	let flame_w = string_size * max_text_len/2 * 0.8;
	let flame_h = string_size * text_lines * 0.8;

	//フレームセット
	let flame_size = SetFukidashiSize(flame_w, flame_h, 1);
	ObjRender_SetPosition(objTalk_Base, x, y, 0);
	ObjRender_SetColor(objTalk_Base, talk_cr[color], talk_cg[color], talk_cb[color]);
	if (c)
	{
		ObjRender_SetAngleZ(objTalk_Base, 0);
		ObjRender_SetAngleX(objTalk_Base, 0);
	}
	else
	{
		ObjRender_SetAngleZ(objTalk_Base, 180);
		ObjRender_SetAngleX(objTalk_Base, 180);
	}

	//テキストセット
	ascent(i in 0 .. length(text))
	{
		let tmp_text = ObjText_Create();
		Obj_SetRenderPriorityI(tmp_text, 83);
		ObjText_SetText(tmp_text, text[i]);
		ObjText_SetFontType(tmp_text, "ＭＳ ゴシック");
		ObjText_SetFontSize(tmp_text, font_size);
		ObjText_SetFontBold(tmp_text, true);
		ObjText_SetAutoTransCenter(tmp_text, false);
		ObjRender_SetColor(tmp_text, 32, 32, 32);
		if (c) {
		//	ObjRender_SetPosition(tmp_text, x+24, y-flame_size[1]+24-MerikomiY + i*string_size, 0);
			ObjRender_SetPosition(tmp_text, x+24, y+48-MerikomiY + i*string_size, 0);
		} else {
			ObjRender_SetPosition(tmp_text, x-flame_size[0]+24, y+48-MerikomiY + i*string_size, 0);
		}
		
		//配列に追加
		objTalk_Text = objTalk_Text ~ [tmp_text];
	}

	MovePicture(0, 1-c);
	MovePicture(1, c);
	wait(4);
}

//画像移動
task MovePicture(pic_index, to_front)
{
	let now_front = My_Obj_GetValueDefault(objTalk_Face[pic_index], "STATE", -1);
	Obj_SetValue(objTalk_Face[pic_index], "STATE", to_front);

	let hidden_flag = My_Obj_GetValueDefault(objTalk_Face[pic_index], "HIDDEN", 0);
	let before_hidden_flag = My_Obj_GetValueDefault(objTalk_Face[pic_index], "BEFORE_HIDDEN", 0);
	Obj_SetValue(objTalk_Face[pic_index], "BEFORE_HIDDEN", hidden_flag);

	let muki = (pic_index-0.5)*2;
	let vx = 6*muki;
	let vy = -2;
	let timer = 8;

	if (now_front == -1)
	{
		if (to_front) { timer *= 2; }
		PopImage(pic_index);
	}
	else if (hidden_flag)
	{
		if (hidden_flag == before_hidden_flag) { return; }
		if (!to_front) { timer *= 2; }
		if (!to_front) { vx = -vx; vy = -vy; }
	}
	else
	{
		if (to_front == now_front) { return; }
		if (!to_front) { vx = -vx; vy = -vy; }
		if (hidden_flag != before_hidden_flag)
		{
			if (to_front) { timer *= 2; }
			PopImage(pic_index);
		}
	}

	if (to_front)
		{ Obj_SetRenderPriorityI(objTalk_Face[pic_index], 82); }
	else
		{ Obj_SetRenderPriorityI(objTalk_Face[pic_index], 81); }

	ascent(i in 0 .. timer)
	{
		if (!Obj_IsDeleted(objTalk_Face[pic_index]))
		{
			let tmp = (i+1)/timer;
			
			ObjRender_SetPosition(objTalk_Face[pic_index],
				ObjRender_GetX(objTalk_Face[pic_index])+vx, ObjRender_GetY(objTalk_Face[pic_index])+vy, 0);

			if (to_front) {
				let tmp_c = tmp*155+100;
				ObjRender_SetColor(objTalk_Face[pic_index], tmp_c, tmp_c, tmp_c);
			} else {
				let tmp_c = 255-tmp*156;
				ObjRender_SetColor(objTalk_Face[pic_index], tmp_c, tmp_c, tmp_c);
			}
		}
		yield;
	}
}

//画像セット
function Talk_SetFace(type, filename, x, y, w, h, scale)
{
	let c = 0;
	if (type == 0) { c = 1; }

	if (filename == "")
	{
		Obj_SetValue(objTalk_Face[c], "HIDDEN", 1);
		ClearImage(c);
	}
	else
	{
		Obj_SetValue(objTalk_Face[c], "HIDDEN", 0);
		ObjPrim_SetTexture(objTalk_Face[c], filename);
		Obj_SetVisible(objTalk_Face[c], true);
	}
	
	ObjSprite2D_SetSourceRect(objTalk_Face[c], 0, 0, w, h);
	ObjSprite2D_SetDestRect(objTalk_Face[c], 0, 0, w*scale, h*scale);
	if (!Obj_IsValueExists(objTalk_Face[c], "NEW"))
	{
		Obj_SetValue(objTalk_Face[c], "NEW", 1);
		Obj_SetValue(objTalk_Face[c], "STATE", -1);
		ObjRender_SetPosition(objTalk_Face[c], x, y, 0);
		ObjRender_SetAlpha(objTalk_Face[c], 0);
	}
}

//普通にメッセージ
function Talk_SetText(x, y, type, color, text1, text2)
{
	let font_size = 17;
	Talk_SetText_Flame([text1, text2], font_size, type, color, x, y);
	Talk_WaitKey;
}

//でっかいの
function Talk_SetTextBig(x, y, type, color, text1, text2)
{
	let font_size = 24;
	Talk_SetText_Flame([text1, text2], font_size, type, color, x, y);
	Talk_WaitKey;
}

//一定時間で表示
function Talk_SetText_Timer(x, y, type, color, text1, text2, timer)
{
	let font_size = 17;
	Talk_SetText_Flame([text1, text2], font_size, type, color, x, y);

	loop(timer) { yield; }
}

//ぶるぶるメッセージ
function Talk_SetText_ShakeTimer(x, y, type, color, text1, text2, timer)
{
	let font_size = 24;
	Talk_SetText_Flame([text1, text2], font_size, type, color, x, y);

	let tmp_timer = trunc(timer/3);
	let bx = ObjRender_GetX(objTalk_Base);
	let by = ObjRender_GetY(objTalk_Base);
	let tx = [];
	let ty = [];
	ascent(i in 0 .. length(objTalk_Text))
	{
		tx = tx ~ [ObjRender_GetX(objTalk_Text[i])];
		ty = ty ~ [ObjRender_GetY(objTalk_Text[i])];
	}

	ascent(i in 0 .. tmp_timer)
	{
		let r = sin(i/tmp_timer*180)*12;
		ObjRender_SetPosition(objTalk_Base, bx+rand(-r,r), by+rand(-r,r), 0);
		ascent(i in 0 .. length(objTalk_Text))
		{
			ObjRender_SetPosition(objTalk_Text[i], tx[i]+rand(-r,r), ty[i]+rand(-r,r), 0);
		}
		yield;
	}
	ObjRender_SetPosition(objTalk_Base, bx, by, 0);
	ascent(i in 0 .. length(objTalk_Text))
	{
		ObjRender_SetPosition(objTalk_Text[i], tx[i], ty[i], 0);
	}
	loop(timer-tmp_timer) { yield; }
}

task ClearImage(j)
{
	let muki = (j-0.5)*2;
	descent(i in 0 .. 15)
	{
		ObjRender_SetAlpha(objTalk_Face[j], 255*(i+1)/15);
		yield;
	}
}

task PopImage(j)
{
	let muki = (j-0.5)*2;
	ascent(i in 0 .. 15)
	{
		ObjRender_SetAlpha(objTalk_Face[j], 255*(i+1)/15);
		yield;
	}
}

//吹き出しサイズは内部サイズ指定。これに横72-24縦72が追加される
function SetFukidashiSize(f_width, f_height, scale)
{
	f_width = max(0, f_width-24-MerikomiX*2);
	f_height = max(0, f_height-MerikomiY*2);
	
	let base_vc = 0;
	let fuki_x = 0;
	let fuki_y = 0;
	//左上
	AddImg_Triangle01(objTalk_Base, base_vc,
			0, 0, 48, 48,  fuki_x, fuki_y, fuki_x+48, fuki_y+48);
	//上
	base_vc += 6; fuki_x += 48;
	AddImg_Triangle01(objTalk_Base, base_vc,
			48, 0, 96, 48,  fuki_x, fuki_y, fuki_x+f_width, fuki_y+48);
	//右上
	base_vc += 6; fuki_x += f_width;
	AddImg_Triangle01(objTalk_Base, base_vc,
			96, 0, 120, 48,  fuki_x, fuki_y, fuki_x+24, fuki_y+48);

	fuki_x = 0; fuki_y += 48;
	base_vc += 6;

	//左中
	AddImg_Triangle01(objTalk_Base, base_vc,
			0, 48, 48, 96,  fuki_x, fuki_y, fuki_x+48, fuki_y+f_height);
	//中
	base_vc += 6; fuki_x += 48;
	AddImg_Triangle01(objTalk_Base, base_vc,
			48, 48, 96, 96,  fuki_x, fuki_y, fuki_x+f_width, fuki_y+f_height);
	//右中
	base_vc += 6; fuki_x += f_width;
	AddImg_Triangle01(objTalk_Base, base_vc,
			96, 48, 120, 96,  fuki_x, fuki_y, fuki_x+24, fuki_y+f_height);

	fuki_x = 0; fuki_y += f_height;
	base_vc += 6;

	//左下
	AddImg_Triangle01(objTalk_Base, base_vc,
			0, 96, 48, 120,  fuki_x, fuki_y, fuki_x+48, fuki_y+24);
	//下
	base_vc += 6; fuki_x += 48;
	AddImg_Triangle01(objTalk_Base, base_vc,
			48, 96, 96, 120,  fuki_x, fuki_y, fuki_x+f_width, fuki_y+24);
	//右下
	base_vc += 6; fuki_x += f_width;
	AddImg_Triangle01(objTalk_Base, base_vc,
			96, 96, 120, 120,  fuki_x, fuki_y, fuki_x+24, fuki_y+24);
	
	return ([f_width+72, f_height+72]);
}

//6頂点の座標を追加
function AddImg_Triangle01(obj, base_vc,
		x1, y1, x2, y2,		//切り取り範囲
		to_x1, to_y1, to_x2, to_y2	//貼り付け範囲
	)
{
//	let base_vc = ObjPrim_GetVertexCount(obj);
//	ObjPrim_SetVertexCount(obj, base_vc+6);		//頂点を追加
	
	ObjPrim_SetVertexUVT(obj, base_vc+0, x1, y1);
	ObjPrim_SetVertexUVT(obj, base_vc+1, x1, y2);
	ObjPrim_SetVertexUVT(obj, base_vc+2, x2, y1);
	ObjPrim_SetVertexUVT(obj, base_vc+3, x2, y1);
	ObjPrim_SetVertexUVT(obj, base_vc+4, x1, y2);
	ObjPrim_SetVertexUVT(obj, base_vc+5, x2, y2);

	ObjPrim_SetVertexPosition(obj, base_vc+0, to_x1, to_y1, 0);
	ObjPrim_SetVertexPosition(obj, base_vc+1, to_x1, to_y2, 0);
	ObjPrim_SetVertexPosition(obj, base_vc+2, to_x2, to_y1, 0);
	ObjPrim_SetVertexPosition(obj, base_vc+3, to_x2, to_y1, 0);
	ObjPrim_SetVertexPosition(obj, base_vc+4, to_x1, to_y2, 0);
	ObjPrim_SetVertexPosition(obj, base_vc+5, to_x2, to_y2, 0);
}

//キー待ち
sub Talk_WaitKey
{
	while(!(GetVirtualKeyState(VK_SHOT) == KEY_PUSH ||
		(GetVirtualKeyState(VK_SHOT) == KEY_HOLD && (GetVirtualKeyState(VK_SLOWMOVE) == KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE) == KEY_HOLD)))
		)
			{ yield; }
}