/*------------------------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
SetText(side,type,text) - Talk
GetEventStepK1 - Same as .12m GetEventStep
SetEventStepK1(num) - Same as .12m SetEventStep

WaitForDialogueComplete - Wait till dialogue complete
-------------------------------------------------------------------------------*/
//-------------------------Setting Area----------------------------------
//Set a path to speech ballon graphic below
let imgBalloon = GetCurrentScriptDirectory ~ "../Functions/speech.png";
//---------------------------------------------------------------------------------
//---------------------Important Dialogue-controlling Variable---------------------------------
let EventPic_Left = [];
let EventAlpha_Left = [];
let EventActive_Left = [];

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

let EventRT = "Event Render Target";

//---------------------Code Part-----------------
function IntiallizeEvent(leftchar,rightchar, x, y, z){
SetForbidPlayerShot(true); SetForbidPlayerSpell(true);
SetCommonData("OnEvent",true);
SetCommonData("EventStep",0);
loop(leftchar){
let pic = ObjPrim_Create(OBJ_SPRITE_2D);
Obj_SetRenderPriorityI(pic, 0);
EventPic_Left = EventPic_Left ~ [pic];
EventActive_Left = EventActive_Left ~ [false];

}
loop(rightchar){
let pic = ObjPrim_Create(OBJ_SPRITE_2D);
Obj_SetRenderPriorityI(pic, 0);
EventPic_Right = EventPic_Right ~ [pic];
EventActive_Right = EventActive_Right ~ [false];
}
EventWorking( x, y, z );
}

task EventWorking( x, y, z ){
while(IsOnEvent){
ascent(i in 0..length(EventPic_Left)){
let obj = EventPic_Left[i];
let isactive =  EventActive_Left[i];
ObjRender_SetAlpha(obj,[80,255][isactive]);
Obj_SetRenderPriorityI(obj, [59,60][isactive]);
ObjRender_SetScaleXYZ( obj, x, y, z );
ObjRender_SetPosition(obj,GetStgFrameLeft+250-60*(i*1.5)+10*isactive,280-200*(-isactive*-0.001),0);
}
ascent(i in 0..length(EventPic_Right)){
let obj = EventPic_Right[i];
let isactive = EventActive_Right[i];
ObjRender_SetAlpha(obj,[125,255][isactive]);
Obj_SetRenderPriorityI(obj, [59,60][isactive]);
ObjRender_SetScaleXYZ( obj, x-0.03, y-0.03, z-0.03 );
ObjRender_SetPosition(obj,GetStgFrameWidth-150+60*i-16*isactive,660-342-16*isactive,0);
}
yield;
}
}

function FinallizeEvent{
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];
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 linelength = 0;
let lineheight = 0;

let objText = ObjText_Create();
ObjText_SetText(objText, text);
ObjText_SetFontType(objText,"Arial Rounded MT Bold");
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,  140-((120+40*num)*((-1)^side))-(linelength-80)*side, GetStgFrameHeight-250, 0);
ObjRender_SetPosition(objBalloon, 140-((120+40*num)*((-1)^side))-32-(linelength-80)*side, GetStgFrameHeight-250-20, 0);
ascent(i in 0..9){
ObjRender_SetScaleY(objBalloon,i/8);
ObjRender_SetScaleY(objText,i/8);
yield;
}
while(!(GetVirtualKeyState(VK_SHOT)==KEY_PUSH)){
yield;
}
PlaySound("talk",100,0);
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;
}
PlaySound("talk",100,0);
}