/*------------------------Kirbio's Talking Dialogue System-----------------------
	Based on Miransu Uwabami's dialogue system in Lost the Leader.
	
	Functions
	
	IsOnEvent - returns boolean,checks if the talking dialogue is active or not
	IntiallizeEvent(leftchar,rightchar) - start dialogue system,define number of character for both side here
	FinallizeEvent - End dialogue system
	ObjEvent_SetGraphic(side,number,texture) - Set Graphic for Talker
	SetPortraitsOffset(side,num,offsetx,offsety) - Set graphic offset for Talker(optional) Default 0,0
	SetText(side,type,text) - Talk
	GetEventStepK1 - Same as .12m GetEventStep
	SetEventStepK1(num) - Same as .12m SetEventStep
	
	WaitForDialogueComplete - Wait till dialogue complete
	-------------------------------------------------------------------------------*/
	
#include"./DialogueFunctionsA1.txt"
//---------------------Important Dialogue-controlling Variable---------------------------------
let EventPic_Left = [];
let EventAlpha_Left = [];
let EventActive_Left = [];
let EventOffset_Left = [];

let EventPic_Right = [];
let EventAlpha_Right = [];
let EventActive_Right = [];
let EventOffset_Right = [];

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

let EventRT = "Event Render Target";

//---------------------Code Part-----------------
function IntiallizeEvent(leftchar,rightchar){
	SetForbidPlayerShot(true); SetForbidPlayerSpell(true);
	SetCommonData("OnEvent",true);
	SetCommonData("EventStep",0);
	loop(leftchar){
		let pic = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriority(pic, 0.99);
		EventPic_Left = EventPic_Left ~ [pic];
		EventActive_Left = EventActive_Left ~ [false];
		EventOffset_Left = EventOffset_Left ~ [[0,0]];
		
	}
	loop(rightchar){
		let pic = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriority(pic, 0.99);
		EventPic_Right = EventPic_Right ~ [pic];
		EventActive_Right = EventActive_Right ~ [false];
		EventOffset_Right = EventOffset_Right ~ [[0,0]];
	}
	EventWorking;
}

task SetPortraitsOffset(side,num,offsetx,offsety){
	if(side==LEFT){
	EventOffset_Left[num] = [offsetx,offsety];
	}else if(side==RIGHT){
		EventOffset_Right[num] = [offsetx,offsety];
	}
}

task EventWorking{
	while(IsOnEvent){
		ascent(i in 0..length(EventPic_Left)){
			let obj = EventPic_Left[i];
			let isactive = EventActive_Left[i];
			let offset = [EventOffset_Left[i][0],EventOffset_Left[i][1]];
			ObjRender_SetAlpha(obj,[255,255][isactive]);
			ObjRender_SetColor(obj,[120,255][isactive],[120,255][isactive],[120,255][isactive]);
			Obj_SetRenderPriority(obj, [0.91,0.92][isactive]);
			ObjRender_SetPosition(obj,GetStgFrameLeft+130-60*i+16*isactive+offset[0],640-340-16*isactive+offset[1],0);
		}
		ascent(i in 0..length(EventPic_Right)){
			let obj = EventPic_Right[i];
			let isactive = EventActive_Right[i];
			let offset = [EventOffset_Right[i][0],EventOffset_Right[i][1]];
			ObjRender_SetAlpha(obj,[255,255][isactive]);
			ObjRender_SetColor(obj,[120,255][isactive],[120,255][isactive],[120,255][isactive]);
			Obj_SetRenderPriority(obj, [0.91,0.92][isactive]);
			ObjRender_SetPosition(obj,GetStgFrameLeft+GetStgFrameWidth-120+60*i-16*isactive+offset[0],640-330-16*isactive+offset[1],0);
		}
		yield;
	}
}

function FinallizeEvent{
ascent(i in 0..length(EventPic_Left)){
Obj_Delete(EventPic_Left[i]);
}
EventPic_Left = [];
EventAlpha_Left = [];
EventActive_Left = [];
ascent(i in 0..length(EventPic_Right)){
Obj_Delete(EventPic_Right[i]);
}
EventPic_Right = [];
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,scale){
	let obj = [EventPic_Left,EventPic_Right][side][number];
	ObjPrim_SetTexture(obj,texture);
	//Obj_SetRenderPriority(obj, 90);
	ObjSprite2D_SetSourceRect(obj,1,1,GetTextureWidth(texture)+1,GetTextureHeight(texture)+1);
	ObjRender_SetScaleXYZ(obj,scale,scale,scale);
	ObjSprite2D_SetDestCenter(obj);
}



function SetText(side,num,type,text){
let linelength = 0;
let lineheight = 0;

let objText = ObjText_Create();
ObjText_SetText(objText, text);
ObjText_SetFontType(objText,"Connecting Chain Handserif");
ObjText_SetFontSize(objText, 20);
ObjText_SetFontColorTop(objText,0,0,0);
ObjText_SetFontColorBottom(objText,0,0,0);
ObjText_SetFontBorderType(objText,BORDER_NONE);

ObjText_SetLinePitch(objText, 0);
Obj_SetRenderPriorityI(objText, 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,ALIGNMENT_RIGHT][side]);



let objBalloon = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
Obj_SetRenderPriorityI(objBalloon, 90);
ObjPrim_SetTexture(objBalloon, imgBalloon);
ObjSpriteList2D_SetSourceRect(objBalloon,0,0,8,8);
ObjSpriteList2D_SetDestRect(objBalloon,32-8,20-8,32,20);
ObjSpriteList2D_AddVertex(objBalloon);
ObjSpriteList2D_SetSourceRect(objBalloon,8,0,24,8);
ObjSpriteList2D_SetDestRect(objBalloon,32,20-8,32+(linelength),20);
ObjSpriteList2D_AddVertex(objBalloon);
ObjSpriteList2D_SetSourceRect(objBalloon,24,0,32,8);
ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength),20-8,32+(linelength)+8,20);
ObjSpriteList2D_AddVertex(objBalloon);
ObjSpriteList2D_SetSourceRect(objBalloon,0,8,8,24);
ObjSpriteList2D_SetDestRect(objBalloon,32-8,20,32,20+(lineheight));
ObjSpriteList2D_AddVertex(objBalloon);
ObjSpriteList2D_SetSourceRect(objBalloon,8,8,24,24);
ObjSpriteList2D_SetDestRect(objBalloon,32,20,32+(linelength),20+(lineheight));
ObjSpriteList2D_AddVertex(objBalloon);
ObjSpriteList2D_SetSourceRect(objBalloon,24,8,32,24);
ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength),20,32+(linelength)+8,20+(lineheight));
ObjSpriteList2D_AddVertex(objBalloon);
ObjSpriteList2D_SetSourceRect(objBalloon,0,24,8,32);
ObjSpriteList2D_SetDestRect(objBalloon,32-8,20+(lineheight),32,20+(lineheight)+8);
ObjSpriteList2D_AddVertex(objBalloon);
ObjSpriteList2D_SetSourceRect(objBalloon,8,24,24,32);
ObjSpriteList2D_SetDestRect(objBalloon,32,20+(lineheight),32+(linelength),20+(lineheight)+8);
ObjSpriteList2D_AddVertex(objBalloon);
ObjSpriteList2D_SetSourceRect(objBalloon,24,24,32,32);
ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength),20+(lineheight),32+(linelength)+8,20+(lineheight)+8);
ObjSpriteList2D_AddVertex(objBalloon);
if(side==0){
ObjSpriteList2D_SetSourceRect(objBalloon,48,16,64,32);
ObjSpriteList2D_SetDestRect(objBalloon,32,-2,48,14);
ObjSpriteList2D_AddVertex(objBalloon);
}else{
ObjSpriteList2D_SetSourceRect(objBalloon,64,16,48,32);
ObjSpriteList2D_SetDestRect(objBalloon,32+(linelength)-16,-2,32+(linelength),14);
ObjSpriteList2D_AddVertex(objBalloon);
}
ObjSpriteList2D_CloseVertex(objBalloon);
ObjRender_SetPosition(objText,  280-((120+60*num)*((-1)^side))-(linelength-80)*side, GetStgFrameHeight-160, 0);
ObjRender_SetPosition(objBalloon, 280-((120+60*num)*((-1)^side))-32-(linelength-80)*side, GetStgFrameHeight-160-20, 0);
ascent(i in 0..9){
ObjRender_SetScaleY(objBalloon,i/8);
ObjRender_SetScaleY(objText,i/8);
wait(1);
}
while(!(GetVirtualKeyState(VK_SHOT)==KEY_PUSH)){
yield;
}
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==0);
		}else{
			SetTalking(LEFT,i,false);
		}
	}
	ascent(i in 0..length(EventPic_Right)){
		if(i == num){
		SetTalking(RIGHT,num,side==1);
		}else{
			SetTalking(RIGHT,i,false);
		}
	}
}

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

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

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

function CheckPress{
loop(10){
yield;
}
while(!(GetVirtualKeyState(VK_SHOT)==KEY_PUSH)){
yield;
}
}