let INFO_YELLOW = 1.0;
let INFO_WHITE = 1.1;
let INFO_RED = 1.2;
let SAY_YELLOW = 2.0;
let SAY_WHITE = 2.1;
let SAY_RED = 2.2;
let THINK_YELLOW = 3.0;
let THINK_WHITE = 3.1;
let THINK_RED = 3.2;
let CALL_YELLOW = 4.0;
let CALL_WHITE = 4.1;
let CALL_RED = 4.2;
let PLAYER_SPEAKS = 0;
let ENEMY_SPEAKS = 1;
let MULTIPLY_TALK_Y = 0;
let MULTIPLY_TALK_X = 0;
let SPEECH_SIZE = 0;
let TEXT_ALPHA = -100;
let playerPortPath = GetMainStgScriptDirectory ~ "blank.png";
let enemyPortPath = GetMainStgScriptDirectory ~ "blank.png";
let IsTalking = false;

let stop = false;
let start_player = true;
let start_enemy = true;
let end_player = false;
let end_enemy = false;
let playerPort = ID_INVALID;
let enemyPort = ID_INVALID;
let whotalk = 2;
let text = "";
let mode = INFO_WHITE;
let x_player = GetStgFrameWidth/2-130-300;
let y_player = GetStgFrameHeight/2+10;
let x_enemy = GetStgFrameWidth/2+130+300;
let y_enemy = GetStgFrameHeight/2+10;
let alpha_player = 0;
let alpha_enemy = 0;
let alpha_bubble = 255;
let stop_player  = true; 
let stop_enemy = true;

function SetEventTextA(type,dialogue) {
let count = 0;
	SPEECH_SIZE = 0;
	TEXT_ALPHA = -100;
	text = dialogue;
	mode = type;
	loop(5) { yield; }
	while(GetVirtualKeyState(VK_OK) != KEY_PUSH && GetVirtualKeyState(VK_USER1) != KEY_HOLD) {
		if(count == 300) { break; }
		count++;
		yield;
	}
}
function SetEventTextB(type,dialogue,frame) {
let count = 0;
	SPEECH_SIZE = 0;
	TEXT_ALPHA = -100;
	text = dialogue;
	mode = type;
	loop(frame) {
		yield;
	}
}
function SetEventTextC(type,dialogue,frame) {
let count = 0;
	SPEECH_SIZE = 0;
	TEXT_ALPHA = -100;
	text = dialogue;
	mode = type;
	loop(5) { yield; }
	loop(frame) { yield; }
	while(GetVirtualKeyState(VK_OK) != KEY_PUSH && GetVirtualKeyState(VK_USER1) != KEY_HOLD) {
		if(count == 300) { break; }
		count++;
		yield;
	}
}
task SetEventSpeaker(char) {
	whotalk = char;
	stop = true;
	yield;
	stop = false;
	if(char == PLAYER_SPEAKS) {
		SetMovePortraitPosition01(playerPort,GetStgFrameWidth/2-130,GetStgFrameHeight/2,10,8,"PLAYER");
		SetMovePortraitPosition01(enemyPort,GetStgFrameWidth/2+130+30,GetStgFrameHeight/2+15,10,8,"ENEMY");
	}
	if(char == ENEMY_SPEAKS) {
		SetMovePortraitPosition01(playerPort,GetStgFrameWidth/2-130-30,GetStgFrameHeight/2+15,8,8,"PLAYER");
		SetMovePortraitPosition01(enemyPort,GetStgFrameWidth/2+130,GetStgFrameHeight/2,10,8,"ENEMY");
	}
}
function SetPlayerPort(path) {
	playerPortPath = path;
}
function SetEnemyPort(path) {
	enemyPortPath = path;
}
task RemovePlayerPort {
	stop = true;
	yield;
	stop = false;
	SetMovePortraitPosition(playerPort,GetStgFrameWidth/2-130-300,GetStgFrameHeight/2+15,10,12,"PLAYER");
}
task RemoveEnemyPort {
	stop = true;
	yield;
	stop = false;
	SetMovePortraitPosition(enemyPort,GetStgFrameWidth/2+130+300,GetStgFrameHeight/2+15,10,12,"ENEMY");
}
task SetNewPlayerPort(path) {
	playerPortPath = path;
	stop = true;
	yield;
	stop = false;
	if(whotalk == PLAYER_SPEAKS) {
		SetMovePortraitPosition(playerPort,GetStgFrameWidth/2-130,GetStgFrameHeight/2,10,12,"PLAYER");
	}
	else {
		SetMovePortraitPosition(playerPort,GetStgFrameWidth/2-130-30,GetStgFrameHeight/2+15,10,12,"PLAYER");
	}
}
task SetNewEnemyPort(path) {
	enemyPortPath = path;
	stop = true;
	yield;
	stop = false;
	if(whotalk == ENEMY_SPEAKS) {
		SetMovePortraitPosition(enemyPort,GetStgFrameWidth/2+130,GetStgFrameHeight/2,10,12,"ENEMY");
	}
	else {
		SetMovePortraitPosition(enemyPort,GetStgFrameWidth/2+130+30,GetStgFrameHeight/2+15,10,12,"ENEMY");
	}
}
function EndEvent {
	stop = true;
	yield;
	stop = false;
	SetMovePortraitPosition01(playerPort,GetStgFrameWidth/2-130-300,GetStgFrameHeight/2+15,10,12,"PLAYER");
	SetMovePortraitPosition01(enemyPort,GetStgFrameWidth/2+130+300,GetStgFrameHeight/2+15,10,12,"ENEMY");
	while(alpha_bubble > 0) { alpha_bubble-=255/20; TEXT_ALPHA-=50/3.5; yield; }
	loop(20){ TEXT_ALPHA-=50/3.5; yield; }
	IsTalking = false;
}
task StartEvent {
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	IsTalking = true;
	
	yield;
	
	InstallFont(GetCurrentScriptDirectory ~ "FONT/LiberationMono-Bold.ttf");

	let WANTED_Y = (mode%1*10)*32;
	let WANTED_X = floor(mode-1)*32;
	
	let speechImage = GetCurrentScriptDirectory ~ "IMG/speech.png";

	let SPEECH_TOP = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let SPEECH_MID = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let SPEECH_BOT = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(SPEECH_TOP, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetPrimitiveType(SPEECH_MID, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetPrimitiveType(SPEECH_BOT, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(SPEECH_TOP, 8);
	ObjPrim_SetVertexCount(SPEECH_MID, 8);
	ObjPrim_SetVertexCount(SPEECH_BOT, 8);
	ObjPrim_SetTexture(SPEECH_TOP, speechImage);
	ObjPrim_SetTexture(SPEECH_MID, speechImage);
	ObjPrim_SetTexture(SPEECH_BOT, speechImage);
	Obj_SetRenderPriorityI(SPEECH_TOP, 70);
	Obj_SetRenderPriorityI(SPEECH_MID, 70);
	Obj_SetRenderPriorityI(SPEECH_BOT, 70);

	let speechText = ObjText_Create;
	
	ObjText_SetMaxWidth(speechText, 265);
	ObjText_SetFontSize(speechText, 16);
	ObjText_SetFontType(speechText, "Liberation Mono");
	ObjText_SetFontBold(speechText, false);
	ObjText_SetFontColorTop(speechText, 0, 0, 0);
	ObjText_SetFontColorBottom(speechText, 0, 0, 0);
	Obj_SetRenderPriorityI(speechText, 75);

	let speechArrow = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(speechArrow, speechImage);
	Obj_SetRenderPriorityI(speechArrow, 75);

	playerPort = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(playerPort, playerPortPath);
	Obj_SetRenderPriorityI(playerPort, 65);
	ObjRender_SetPosition(playerPort, x_player, y_player, 0);

	enemyPort = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(enemyPort, enemyPortPath);
	Obj_SetRenderPriorityI(enemyPort, 65);
	ObjRender_SetPosition(enemyPort, x_enemy, y_enemy, 0);

	while(IsTalking) {
		ObjText_SetText(speechText, text);
		ObjPrim_SetTexture(playerPort, playerPortPath);
		ObjSprite2D_SetSourceRect(playerPort,0,0,GetTextureWidth(playerPortPath),GetTextureHeight(playerPortPath));
		ObjSprite2D_SetDestCenter(playerPort);
		ObjRender_SetScaleXYZ(playerPort,0.8,0.8,1);
		ObjRender_SetAlpha(playerPort,alpha_player);
		ObjPrim_SetTexture(enemyPort, enemyPortPath);
		ObjSprite2D_SetSourceRect(enemyPort,0,0,GetTextureWidth(enemyPortPath),GetTextureHeight(enemyPortPath));
		ObjSprite2D_SetDestCenter(enemyPort);
		ObjRender_SetScaleXYZ(enemyPort,0.8,0.8,1);
		ObjRender_SetAlpha(enemyPort,alpha_enemy);
		
		ObjRender_SetAlpha(SPEECH_TOP,alpha_bubble);
		ObjRender_SetAlpha(SPEECH_MID,alpha_bubble);
		ObjRender_SetAlpha(SPEECH_BOT,alpha_bubble);
		ObjRender_SetAlpha(speechText,alpha_bubble);
		
		WANTED_Y = (mode%1*10)*32;
		WANTED_X = floor(mode-1)*32;
		
		ObjSprite2D_SetSourceRect(speechArrow, 0 + WANTED_X, 0 + WANTED_Y, 32 + WANTED_X, 32 + WANTED_Y);
		
		if(floor(mode)==1){	ObjRender_SetAlpha(speechArrow, 0); }
		else { ObjRender_SetAlpha(speechArrow, alpha_bubble); }
	
		MULTIPLY_TALK_Y = 0;

		MULTIPLY_TALK_X = ObjText_GetTextLength(speechText)*8;

		if(SPEECH_SIZE < 1) {
			SPEECH_SIZE+=1/12;
		}

		if(MULTIPLY_TALK_X > 30*10+0) {
			MULTIPLY_TALK_Y += 22;
		}
		if(MULTIPLY_TALK_X > 30*20+0) {
			MULTIPLY_TALK_Y += 22;
		}
		if(MULTIPLY_TALK_X > 30*30+0) {
			MULTIPLY_TALK_Y += 22;
		}
		if(MULTIPLY_TALK_X > 30*40+0) {
			MULTIPLY_TALK_Y += 22;
		}

		MULTIPLY_TALK_X = min(MULTIPLY_TALK_X,256);
		
		if(TEXT_ALPHA < 50)
		{
			TEXT_ALPHA+=50/7;
		}
		
		ObjPrim_SetVertexUVT(SPEECH_TOP, 0, 00, 00+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_TOP, 1, 00, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_TOP, 2, 16, 00+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_TOP, 3, 16, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_TOP, 4, 16, 00+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_TOP, 5, 16, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_TOP, 6, 32, 00+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_TOP, 7, 32, 16+WANTED_Y);

		ObjPrim_SetVertexUVT(SPEECH_MID, 0, 00, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_MID, 1, 00, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_MID, 2, 16, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_MID, 3, 16, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_MID, 4, 16, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_MID, 5, 16, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_MID, 6, 32, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_MID, 7, 32, 16+WANTED_Y);

		ObjPrim_SetVertexUVT(SPEECH_BOT, 0, 00, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_BOT, 1, 00, 32+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_BOT, 2, 16, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_BOT, 3, 16, 32+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_BOT, 4, 16, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_BOT, 5, 16, 32+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_BOT, 6, 32, 16+WANTED_Y);
		ObjPrim_SetVertexUVT(SPEECH_BOT, 7, 32, 32+WANTED_Y);

		ObjPrim_SetVertexPosition(SPEECH_TOP, 0, 00, 00, 0);
		ObjPrim_SetVertexPosition(SPEECH_TOP, 1, 00, 16, 0);
		ObjPrim_SetVertexPosition(SPEECH_TOP, 2, 16, 00, 0);
		ObjPrim_SetVertexPosition(SPEECH_TOP, 3, 16, 16, 0);
		ObjPrim_SetVertexPosition(SPEECH_TOP, 4, 16+(SPEECH_SIZE*MULTIPLY_TALK_X), 00, 0);
		ObjPrim_SetVertexPosition(SPEECH_TOP, 5, 16+(SPEECH_SIZE*MULTIPLY_TALK_X), 16, 0);
		ObjPrim_SetVertexPosition(SPEECH_TOP, 6, 32+(SPEECH_SIZE*MULTIPLY_TALK_X), 00, 0);
		ObjPrim_SetVertexPosition(SPEECH_TOP, 7, 32+(SPEECH_SIZE*MULTIPLY_TALK_X), 16, 0);

		ObjPrim_SetVertexPosition(SPEECH_MID, 0, 00, 00, 0);
		ObjPrim_SetVertexPosition(SPEECH_MID, 1, 00, 00+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);
		ObjPrim_SetVertexPosition(SPEECH_MID, 2, 16, 00, 0);
		ObjPrim_SetVertexPosition(SPEECH_MID, 3, 16, 00+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);
		ObjPrim_SetVertexPosition(SPEECH_MID, 4, 16+(SPEECH_SIZE*MULTIPLY_TALK_X), 00, 0);
		ObjPrim_SetVertexPosition(SPEECH_MID, 5, 16+(SPEECH_SIZE*MULTIPLY_TALK_X), 00+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);
		ObjPrim_SetVertexPosition(SPEECH_MID, 6, 32+(SPEECH_SIZE*MULTIPLY_TALK_X), 00, 0);
		ObjPrim_SetVertexPosition(SPEECH_MID, 7, 32+(SPEECH_SIZE*MULTIPLY_TALK_X), 00+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);

		ObjPrim_SetVertexPosition(SPEECH_BOT, 0, 00, 00+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);
		ObjPrim_SetVertexPosition(SPEECH_BOT, 1, 00, 16+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);
		ObjPrim_SetVertexPosition(SPEECH_BOT, 2, 16, 00+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);
		ObjPrim_SetVertexPosition(SPEECH_BOT, 3, 16, 16+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);
		ObjPrim_SetVertexPosition(SPEECH_BOT, 4, 16+(SPEECH_SIZE*MULTIPLY_TALK_X), 00+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);
		ObjPrim_SetVertexPosition(SPEECH_BOT, 5, 16+(SPEECH_SIZE*MULTIPLY_TALK_X), 16+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);
		ObjPrim_SetVertexPosition(SPEECH_BOT, 6, 32+(SPEECH_SIZE*MULTIPLY_TALK_X), 00+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);
		ObjPrim_SetVertexPosition(SPEECH_BOT, 7, 32+(SPEECH_SIZE*MULTIPLY_TALK_X), 16+SPEECH_SIZE*MULTIPLY_TALK_Y, 0);
		
		alternative(whotalk)
		case(PLAYER_SPEAKS)	{
			ObjSprite2D_SetDestRect(speechArrow, GetStgFrameWidth/2-110, GetStgFrameHeight-(148+32), GetStgFrameWidth/2-110+32, GetStgFrameHeight-(116+32));
			ObjRender_SetPosition(SPEECH_TOP, GetStgFrameWidth/2-110, GetStgFrameHeight-150, 0);
			ObjRender_SetPosition(SPEECH_MID, GetStgFrameWidth/2-110, GetStgFrameHeight-150+16, 0);
			ObjRender_SetPosition(SPEECH_BOT, GetStgFrameWidth/2-110, GetStgFrameHeight-150+16, 0);
			ObjRender_SetScaleXYZ(SPEECH_TOP, 1, 1, 1);
			ObjRender_SetScaleXYZ(SPEECH_MID, 1, 1, 1);
			ObjRender_SetScaleXYZ(SPEECH_BOT, 1, 1, 1);
			
			
			ObjRender_SetPosition(speechText, GetStgFrameWidth/2-113+16, (GetStgFrameHeight)-140, 0);
			ObjRender_SetAlpha(speechText, TEXT_ALPHA/50*255);
			ObjText_SetHorizontalAlignment(speechText,ALIGNMENT_LEFT);
			
			
			if(alpha_player<255) { alpha_player+=25; }
			if(alpha_enemy>155) { alpha_enemy-=25; }
			if(alpha_enemy<155) { alpha_enemy+=25; }
			
			// if(start_player == false) {
				// if(x_player < GetStgFrameWidth/2-130) {
				// x_player += 20/6;
				// }
				// ObjRender_SetPosition(playerPort, x_player, y_player, 0);
			// }
			// if(start_enemy == false) {
				// if(x_enemy < GetStgFrameWidth/2+130+20) {
					// x_enemy += 20/6;
				// }
				// ObjRender_SetPosition(enemyPort, x_enemy, y_enemy, 0);
			// }
			
		}
		case(ENEMY_SPEAKS) {
			ObjSprite2D_SetDestRect(speechArrow, GetStgFrameWidth/2+110, GetStgFrameHeight-(148+32), GetStgFrameWidth/2+110-32, GetStgFrameHeight-(116+32));
			ObjRender_SetPosition(SPEECH_TOP, GetStgFrameWidth/2+110, GetStgFrameHeight-150, 0);
			ObjRender_SetPosition(SPEECH_MID, GetStgFrameWidth/2+110, GetStgFrameHeight-150+16, 0);
			ObjRender_SetPosition(SPEECH_BOT, GetStgFrameWidth/2+110, GetStgFrameHeight-150+16, 0);
			ObjRender_SetScaleXYZ(SPEECH_TOP, -1, 1, 1);
			ObjRender_SetScaleXYZ(SPEECH_MID, -1, 1, 1);
			ObjRender_SetScaleXYZ(SPEECH_BOT, -1, 1, 1);
			ObjRender_SetPosition(speechText, GetStgFrameWidth/2-183+16, (GetStgFrameHeight)-140, 0);
			ObjRender_SetAlpha(speechText, TEXT_ALPHA/50*255);
			ObjText_SetHorizontalAlignment(speechText,ALIGNMENT_RIGHT);
			
			if(alpha_enemy<255) { alpha_enemy+=25; }
			if(alpha_player>155) { alpha_player-=25; }
			if(alpha_player<155) { alpha_player+=25; }
			
			// if(start_enemy == false) {
				// if(x_enemy > GetStgFrameWidth/2+130) {
					// x_enemy -= 20/6;
				// }
				// ObjRender_SetPosition(enemyPort, x_enemy, y_enemy, 0);
			// }
			// if(start_player == false) {
				// if(x_player > GetStgFrameWidth/2-130-20) {
					// x_player -= 20/6;
				// }
				// ObjRender_SetPosition(playerPort, x_player, y_player, 0);
			// }
		}
		// if(start_player == true && stop_player == true) {
			// SetMovePortraitPosition(playerPort,GetStgFrameWidth/2-130-20,GetStgFrameHeight/2+10,1,6,"PLAYER");
			// stop_player = false;
		// }
		// if(start_enemy == true && stop_enemy == true) {
			// SetMovePortraitPosition(enemyPort,GetStgFrameWidth/2+130+20,GetStgFrameHeight/2+10,1,6,"ENEMY");
			// stop_enemy = false;
		// }
		
		yield;
	}
	
	Obj_Delete(SPEECH_TOP);
	Obj_Delete(SPEECH_MID);
	Obj_Delete(SPEECH_BOT);
	Obj_Delete(speechText);
	Obj_Delete(speechArrow);
	Obj_Delete(playerPort);
	Obj_Delete(enemyPort);

	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	
	loop(10) { yield; }
	
	SetCommonData("OnEvent",false);
}
function SetMovePortraitPosition(obj,x,y,weight,max_speed,type) {
	let distance = GetDistance(ObjRender_GetX(obj),ObjRender_GetY(obj),x,y);
	let angle = atan2(y-ObjRender_GetY(obj), x-ObjRender_GetX(obj));
	while(distance > 0.4){
		if(stop == true) { break; }
		let speed = distance/weight;
		if(speed > max_speed) { speed = max_speed; }
		ObjRender_SetX(obj, ObjRender_GetX(obj) + cos(angle)*speed);
		ObjRender_SetY(obj, ObjRender_GetY(obj) + sin(angle)*speed);
		distance = GetDistance(ObjRender_GetX(obj),ObjRender_GetY(obj),x,y);
		yield;
	}
	ObjRender_SetPosition(obj,x,y,0);
	if(type == "PLAYER") {
		x_player = GetStgFrameWidth/2-130-20;
	}
	if(type == "ENEMY") {
		x_enemy = GetStgFrameWidth/2+130+20;
	}
}
task SetMovePortraitPosition01(obj,x,y,weight,max_speed,type) {
	let distance = GetDistance(ObjRender_GetX(obj),ObjRender_GetY(obj),x,y);
	let angle = atan2(y-ObjRender_GetY(obj), x-ObjRender_GetX(obj));
	while(distance > 0.4){
		if(stop == true) { break; }
		let speed = distance/weight;
		if(speed > max_speed) { speed = max_speed; }
		ObjRender_SetX(obj, ObjRender_GetX(obj) + cos(angle)*speed);
		ObjRender_SetY(obj, ObjRender_GetY(obj) + sin(angle)*speed);
		distance = GetDistance(ObjRender_GetX(obj),ObjRender_GetY(obj),x,y);
		yield;
	}
	ObjRender_SetPosition(obj,x,y,0);
	if(type == "PLAYER") {
		x_player = GetStgFrameWidth/2-130-20;
	}
	if(type == "ENEMY") {
		x_enemy = GetStgFrameWidth/2+130+20;
	}
}
function GetDistance(x1,y1,x2,y2) {
	return ((x1-x2)^2+(y1-y2)^2)^(1/2);
}