//----------------Based on Kirbio's Talking Dialogue System (from Contradictory Catastrophe)----------------------
let KoishiPath = GetCurrentScriptDirectory ~ "../../Player/pl0/Face/";
let KoishiPlain = KoishiPath ~ "Plain.png";
let KoishiJoy = KoishiPath ~ "Joy.png";
let KoishiSmug = KoishiPath ~ "Smug.png";
let KoishiFear = KoishiPath ~ "Fear.png";
let KoishiShock = KoishiPath ~ "Shock.png";

let BossPath = GetCurrentScriptDirectory ~ "./Boss/img/Face/";
let ReisenPlain = BossPath ~ "Plain.png";
let ReisenJoy = BossPath ~ "Joy.png";
let ReisenFear = BossPath ~ "Fear.png";
let ReisenDPlain = BossPath ~ "Plain_Defeat.png";
let ReisenDJoy = BossPath ~ "Joy_Defeat.png";
let ReisenDSad = BossPath ~ "Sad_Defeat.png";
let Tewi = BossPath ~ "Tewi.png";

let BOTH = 10;

let EventPic_Left = [];
let EventAlpha_Left = [];
let EventActive_Left = [];
let EventCreated_Left = [];
let EventPos_Left = [];

let EventPic_Right = [];
let EventAlpha_Right = [];
let EventActive_Right = [];
let EventCreated_Right = [];
let EventPos_Right = [];

let talkHold = false;

let imgBalloon = GetCurrentScriptDirectory ~ "../../system/img/Balloon.png";

let EventRY = "Event Render Target";

function InitializeEvent(leftchar,rightchar)
{
	TCheckPress;
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	SetCommonData("OnEvent",true);
	SetCommonData("EventStep",0);
	loop(leftchar)
		{
		let pic = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriority(pic, 0.89);
		EventPic_Left = EventPic_Left ~ [pic];
		EventActive_Left = EventActive_Left ~ [false];	
		EventAlpha_Left = EventAlpha_Left ~ [0];
		EventCreated_Left = EventCreated_Left ~ [false];
		EventPos_Left = EventPos_Left ~ [0];
		}
	loop(rightchar)
		{
		let pic = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriority(pic,0.89);
		ObjRender_SetScaleX(pic,-1);
		EventPic_Right = EventPic_Right ~ [pic];
		EventActive_Right = EventActive_Right ~ [false];
		EventAlpha_Right = EventAlpha_Right ~ [0];
		EventCreated_Right = EventCreated_Right ~ [false];
		EventPos_Right = EventPos_Right ~ [0];
		}
	EventWorking;
}

task EventWorking
	{
	while(IsOnEvent)
		{
		ascent(i in 0..length(EventPic_Left))
			{
			let obj = EventPic_Left[i];
			let isactive = EventActive_Left[i];
			Obj_SetRenderPriority(obj,[0.89,0.90][isactive]);
			if(EventAlpha_Left[i] < 255 && EventCreated_Left[i] == true)		
				{
				EventAlpha_Left[i] = EventAlpha_Left[i]+255/7;
				}
			if(EventPos_Left[i] < 1 && isactive == true)
				{
				EventPos_Left[i] = EventPos_Left[i]+1/7;
				}
			else if(EventPos_Left[i] > 0 && isactive == false)
				{
				EventPos_Left[i] = EventPos_Left[i]-1/7;
				}
			if(EventCreated_Left[i] == false)
				{
				isactive = true;
				if(EventAlpha_Left[i] > 0)
					{
					EventAlpha_Left[i] = EventAlpha_Left[i]-255/7;
					}
				else
					{
					EventAlpha_Left[i] = 0;
					}
				}
			ObjRender_SetAlpha(obj,[80,EventAlpha_Left[i]][isactive]);
			ObjRender_SetPosition(obj,240-150+16*EventPos_Left[i],600-256-16*EventPos_Left[i],0);
			}
		ascent(i in 0..length(EventPic_Right))
			{
			let obj = EventPic_Right[i];
			let isactive = EventActive_Right[i];
			Obj_SetRenderPriority(obj,[0.89,0.90][isactive]);
			if(EventAlpha_Right[i] < 255 && EventCreated_Right[i] == true)		
				{
				EventAlpha_Right[i] = EventAlpha_Right[i]+255/7;
				}
			if(EventPos_Right[i] < 1 && isactive == true)
				{
				EventPos_Right[i] = EventPos_Right[i]+1/7;
				}
			else if(EventPos_Right[i] > 0 && isactive == false)
				{
				EventPos_Right[i] = EventPos_Right[i]-1/7;
				}
			if(EventCreated_Right[i] == false)
				{
				isactive = true;
				if(EventAlpha_Right[i] > 0)
					{
					EventAlpha_Right[i] = EventAlpha_Right[i]-255/7;
					}
				else
					{
					EventAlpha_Right[i] = 0;
					}
				}
			ObjRender_SetAlpha(obj,[80,EventAlpha_Right[i]][isactive]);
			ObjRender_SetPosition(obj,320+180-16*EventPos_Right[i],600-256-16*EventPos_Right[i],0);
			}
		yield;
		}
	}

function FinalizeEvent
	{
	let alphaM_Left = [];
	let alphaM_Right = [];
	ascent(i in 0..length(EventPic_Left))
		{
		alphaM_Left = alphaM_Left ~ [EventAlpha_Left[i]];
		}
	ascent(i in 0..length(EventPic_Right))
		{
		alphaM_Right = alphaM_Right ~ [EventAlpha_Right[i]];
		}
	loop(15)
		{
		ascent(i in 0..length(EventPic_Left))
			{
			EventAlpha_Left[i] = EventAlpha_Left[i]-alphaM_Left[i]/5;
			}
		ascent(i in 0..length(EventPic_Right))
			{
			EventAlpha_Left[i] = EventAlpha_Right[i]-alphaM_Right[i]/5;
			}
		yield;
		}
	ascent(i in 0..length(EventPic_Left))
		{
		Obj_Delete(EventPic_Left[i]);
		}
	EventAlpha_Left = [];
	EventActive_Left = [];
	ascent(i in 0..length(EventPic_Right))
		{
		Obj_Delete(EventPic_Right[i]);
		}
	EventAlpha_Right = [];
	EventActive_Right = [];
	SetCommonData("OnEvent",false);	
	SetCommonData("EventStep",0);
	SetForbidPlayerShot(false); 
	SetForbidPlayerSpell(false);
	}

let LEFT = 0;
let RIGHT = 1;
let NONE = 2;

task ObjEvent_SetGraphic(side,number,texture)
	{
	let obj = [EventPic_Left,EventPic_Right][side][number];
	if(side == LEFT)
		{
		if(texture != "")
			{
			EventCreated_Left[number] = true;
			}
		else
			{
			EventCreated_Left[number] = false;
			}
		}
	else if(side == RIGHT)
		{
		if(texture != "")
			{
			EventCreated_Right[number] = true;
			}
		else
			{
			EventCreated_Right[number] = false;
			}
		}
	if(texture != "")
		{
		ObjPrim_SetTexture(obj,texture);
		ObjSprite2D_SetSourceRect(obj,1,1,GetTextureWidth(texture)+1,GetTextureHeight(texture)+1);
		ObjSprite2D_SetDestCenter(obj);
		}
	}	

function SetText(side,num,type,text)
	{
	let typeA = floor(type/3);
	let typeB = [48,64,typeA];	
	if(type >= 9)
		{
		typeA = min(3,type-9);
		typeB = [0,1,3];
		}
	else if(type%3 == 1)
		{
		typeB = [70,96];
		}
	else if(type%3 == 2)
		{
		typeB = [108,128,typeA];
		}

	let linelength = 0;
	let lineheight = 0;

	let objText = ObjText_Create();
	ObjText_SetText(objText, text);
	ObjText_SetFontType(objText,"Century Gothic");
	ObjText_SetFontSize(objText, 21);
	ObjText_SetFontColorTop(objText,0,0,0);
	ObjText_SetFontColorBottom(objText,0,0,0);
	ObjText_SetFontBorderType(objText,BORDER_NONE);
	ObjText_SetFontBold(objText,true);
		
	ObjText_SetLinePitch(objText, 0);
	Obj_SetRenderPriority(objText, 0.91);
	ObjText_SetAutoTransCenter(objText, false);
	linelength = ObjText_GetTotalWidth(objText);
	lineheight = ObjText_GetTotalHeight(objText);
	ObjText_SetMaxWidth(objText, linelength+1);
	//ObjRender_SetPosition(objText, 32,20,0);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_LEFT);

	let objBalloon = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	Obj_SetRenderPriority(objBalloon,0.90);
	ObjPrim_SetTexture(objBalloon,imgBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,0,0+32*typeA,8,8+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32-8,20-8,32,20);
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,8,0+32*typeA,24,8+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32,20-8,32+(linelength),20);
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,24,0+32*typeA,32,8+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength),20-8,32+(linelength)+8,20);
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,0,8+32*typeA,8,24+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32-8,20,32,20+(lineheight));
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,8,8+32*typeA,24,24+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32,20,32+(linelength),20+(lineheight));
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,24,8+32*typeA,32,24+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength),20,32+(linelength)+8,20+(lineheight));
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,0,24+32*typeA,8,32+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32-8,20+(lineheight),32,20+(lineheight)+8);
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,8,24+32*typeA,24,32+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32,20+(lineheight),32+(linelength),20+(lineheight)+8);
	ObjSpriteList2D_AddVertex(objBalloon);
	ObjSpriteList2D_SetSourceRect(objBalloon,24,24+32*typeA,32,32+32*typeA);
	ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength),20+(lineheight),32+(linelength)+8,20+(lineheight)+8);
	ObjSpriteList2D_AddVertex(objBalloon);
	
	if(side==0)
		{
		ObjSpriteList2D_SetSourceRect(objBalloon,typeB[0],6+typeB[2]*32,typeB[1],32+typeB[2]*32);
		ObjSpriteList2D_SetDestRect(objBalloon,32,-2,48,14);
		ObjSpriteList2D_AddVertex(objBalloon);
		}
	else
		{
		ObjSpriteList2D_SetSourceRect(objBalloon,typeB[1],6+typeB[2]*32,typeB[0],32+typeB[2]*32);
		ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength)-16,-2,32+(linelength),14);
		ObjSpriteList2D_AddVertex(objBalloon);
		}

	ObjSpriteList2D_CloseVertex(objBalloon);
	ObjRender_SetPosition(objText,260-((120-30*num)*((-1)^side))-(linelength-120)*side, GetStgFrameHeight-160+side*45, 0);
	ObjRender_SetPosition(objBalloon,260-((120-30*num)*((-1)^side))-32-(linelength-120)*side, GetStgFrameHeight-160+side*45-20, 0);
	ascent(i in 0..9)
		{
		ObjRender_SetScaleY(objBalloon,i/8);
		ObjRender_SetScaleY(objText,i/8);
		if(talkHold == false || i%2 == 0)
			{
			yield;
			}
		}
	ObjRender_SetScaleY(objBalloon,1);
	ObjRender_SetScaleY(objText,1);
	while(GetVirtualKeyState(VK_SHOT)==KEY_PUSH && talkHold == false){yield;}
	while(GetVirtualKeyState(VK_SHOT)!=KEY_PUSH && talkHold == false){yield;}
	PlaySound("se_PlayerShot",120);
	Obj_Delete(objBalloon);
	Obj_Delete(objText);
	}

task SetTalking(side,num,status)
	{
	if(side==LEFT)
		{
		EventActive_Left[num] = status;
		}
	else
		{
		EventActive_Right[num] = status;
		}
	}

task Talking(num,side)
	{
	ascent(i in 0..length(EventPic_Left))
		{
		if(i == num)
			{
			SetTalking(LEFT,num,(side==LEFT||side == BOTH));
			}
		else
			{
			SetTalking(LEFT,num,false);
			}
		}
	ascent(i in 0..length(EventPic_Right))
		{
		if(i == num)
			{
			SetTalking(RIGHT,num,(side==RIGHT||side == BOTH));
			}
		else
			{
			SetTalking(RIGHT,num,false);
			}
		}
	}

function IsOnEvent
	{
	return GetCommonData("OnEvent",false);
	}

function GetEvenetStepK1
	{
	return GetCommonData("EventStep",0);
	}

task SetEvenetStepK1(num)
	{
	SetCommonData("EventStep",num);
	}

task TCheckPress
	{
	let counter = 0;
	loop
		{
		if(GetVirtualKeyState(VK_USER1) == KEY_PUSH || GetVirtualKeyState(VK_USER1) == KEY_HOLD)
			{
			counter+=1;
			}
		else
			{
			counter = 0;
			}
		if(counter >= 5)
			{
			talkHold = true;
			}
		else
			{
			talkHold = false;
			}
		yield;
		}
	}