#include_function ".\lib\dialogue_functions.txt"

let strings=[];
let stringcolors=[];
let imageleft=[];
let imageright=[];
let steps=[];
let dialogueobj;
let imageleftstance=[];
let imagerightstance=[];
let movestepside=[];
let movesteptype=[];
let charadd=[];
let charaddvertex=[];
let movestepnum=[];
let changestepside=[];
let changestepnum=[];
let changestepimg=[];
let skipcount=0;

task InitializeDialogue(texture, left, top, right, bottom)
{
	ForbidShot(true);
	let stepnum=0;
	dialogueobj=CreateObjEffect(texture, 7);
	ObjEffect_SetColor(dialogueobj, 4, 255, 255, 255, 255);
	ascent(i in 0..26)
	{
	ObjEffect_SetXYRect(dialogueobj, GetClipMinX+25, GetCenterY+125, GetClipMaxX-25, GetCenterY+125+50*i/25);
	ObjEffect_SetUVRect(dialogueobj, left, top, right, (bottom-top)*i/25);
	yield;
	}
	while(Obj_BeDeleted(dialogueobj)==false)
	{
	yield;
		if(stepnum<length(steps))
		{
		if(stepnum%1==0)
		{
			if(steps[stepnum]=="STRING")
			{
			DrawDialogueText(strings[stepnum], GetClipMinX+25, GetCenterY+135, GetClipMaxX-25,
			stringcolors[stepnum][0], stringcolors[stepnum][1], stringcolors[stepnum][2]);
			stepnum+=0.5;
			}
			else if(steps[stepnum]=="MOVE")
			{
				if(movestepside[stepnum]=="LEFT")
				{
				let movearray=movestepnum[stepnum];
				imageleftstance[movearray]=movesteptype[stepnum];
				}
				if(movestepside[stepnum]=="RIGHT")
				{
				let movearray=movestepnum[stepnum];
				imagerightstance[movearray]=movesteptype[stepnum];
				}
			stepnum++;
			}
			else if(steps[stepnum]=="CHARADD")
			{
			AddCharImage(charadd[stepnum][0], charaddvertex[stepnum][0], charaddvertex[stepnum][1], 
			charaddvertex[stepnum][2], charaddvertex[stepnum][3], charadd[stepnum][1]);
			stepnum++;
			}
			else if(steps[stepnum]=="CHANGE")
			{
			let changearray=changestepnum[stepnum];
			if(changestepside[stepnum]=="LEFT"){ObjEffect_SetTexture(imageleft[changearray], changestepimg[stepnum]);}
			if(changestepside[stepnum]=="RIGHT"){ObjEffect_SetTexture(imageright[changearray], changestepimg[stepnum]);}
			stepnum++;
			}
		}else{
			if(steps[stepnum-0.5]=="STRING")
			{
			if(GetKeyState(VK_SHOT)==KEY_PUSH){stepnum+=0.5; skipcount=0;}
			if(GetKeyState(VK_SKIP)==KEY_PUSH || GetKeyState(VK_SKIP)==KEY_HOLD){skipcount++;}
			if(skipcount>=10){stepnum+=0.5; skipcount=0;}
			}
		}
		}
		if(stepnum==length(steps))
		{
		AddLife(-GetLife);
		Obj_Delete(dialogueobj);
		ForbidShot(false);
		}
	}
}

function AddDialogueText(string, r, g, b)
{
	steps=steps~["STRING"];
	strings=strings~[string];
	stringcolors=stringcolors~[ [r,g,b] ];
	movestepside=movestepside~[ "" ];
	movestepnum=movestepnum~[ 0 ];
	movesteptype=movesteptype~[ 0 ];
	charadd=charadd~[ ["", ""] ];
	charaddvertex=charaddvertex~[ [0, 0, 0, 0] ];
	changestepside=changestepside~[""];
	changestepnum=changestepnum~[0];
	changestepimg=changestepimg~[""];
}

function AddDialogueChar(texture, left, top, right, bottom, side)
{
	steps=steps~["CHARADD"];
	strings=strings~[""];
	stringcolors=stringcolors~[ [0,0,0] ];
	movestepside=movestepside~[ "" ];
	movestepnum=movestepnum~[ 0 ];
	movesteptype=movesteptype~[ 0 ];
	charadd=charadd~[ [texture, side] ];
	charaddvertex=charaddvertex~[ [left, top, right, bottom] ];
	changestepside=changestepside~[""];
	changestepnum=changestepnum~[0];
	changestepimg=changestepimg~[""];
}

task AddCharImage(texture, left, top, right, bottom, side)
{
	let xdist=0;
	let ydist=0;
	let color=150;
	let imagenum=0;
	if(side=="LEFT")
	{
	imagenum=length(imageleft);
	imageleft=imageleft~[CreateObjEffect(texture, 6)];
	imageleftstance=imageleftstance~[0];
	ObjEffect_SetColor(imageleft[imagenum], 4, 255, 255, 255, 255);
	ObjEffect_SetUVRect(imageleft[imagenum], left, top, right, bottom);
		while(Obj_BeDeleted(imageleft[imagenum])==false)
		{
		yield;
		if(imageleftstance[imagenum]==0)
		{
		if(xdist>-5){xdist-=2;}
		if(ydist<5){ydist+=2;}
		if(color>150){color-=10.5;}
		ObjEffect_SetLayer(imageleft[imagenum], 5);
		}else{
		if(xdist<5){xdist+=2;}
		if(ydist>-5){ydist-=2;}
		if(color<255){color+=10.5;}
		ObjEffect_SetLayer(imageleft[imagenum], 6);
		}
		Obj_SetPosition(imageleft[imagenum], GetClipMinX+90+xdist, GetCenterY+25+ydist);
		ObjEffect_SetColor(imageleft[imagenum], 4, 255, color, color, color);
		ObjEffect_SetAngle(imageleft[imagenum], 0, 0, 0);
		ObjEffect_SetXYRect(imageleft[imagenum], -(right-left)/2, -(bottom-top)/2, (right-left)/2, (bottom-top)/2);
		}
	}else{
	imagenum=length(imageright);
	imageright=imageright~[CreateObjEffect(texture, 6)];
	imagerightstance=imagerightstance~[0];
	ObjEffect_SetColor(imageright[imagenum], 4, 255, 255, 255, 255);
	ObjEffect_SetUVRect(imageright[imagenum], left, top, right, bottom);
		while(Obj_BeDeleted(imageright[imagenum])==false)
		{
		yield;
		if(imagerightstance[imagenum]==0)
		{
		if(xdist<5){xdist+=2;}
		if(ydist<5){ydist+=2;}
		if(color>150){color-=10.5;}
		ObjEffect_SetLayer(imageright[imagenum], 5);
		}else{
		if(xdist>-5){xdist-=2;}
		if(ydist>-5){ydist-=2;}
		if(color<255){color+=10.5;}
		ObjEffect_SetLayer(imageright[imagenum], 6);
		}
		ObjEffect_SetColor(imageright[imagenum], 4, 255, color, color, color);
		Obj_SetPosition(imageright[imagenum], GetClipMaxX-90+xdist, GetCenterY+25+ydist);
		ObjEffect_SetAngle(imageright[imagenum], 0, 0, 0);
		ObjEffect_SetXYRect(imageright[imagenum], -(right-left)/2, -(bottom-top)/2, (right-left)/2, (bottom-top)/2);
		}
	}
}

function MoveDialogueChar(side, num, stance)
{
	steps=steps~["MOVE"];
	strings=strings~[""];
	stringcolors=stringcolors~[ [NULL,NULL,NULL] ];
	movestepside=movestepside~[ side ];
	movestepnum=movestepnum~[ num ];
	movesteptype=movesteptype~[ stance ];
	charadd=charadd~[ ["", ""] ];
	charaddvertex=charaddvertex~[ [0, 0, 0, 0] ];
	changestepside=changestepside~[""];
	changestepnum=changestepnum~[0];
	changestepimg=changestepimg~[""];
}

function ChangeCharImg(side, num, img)
{
	steps=steps~["CHANGE"];
	strings=strings~[""];
	stringcolors=stringcolors~[ [NULL,NULL,NULL] ];
	movestepside=movestepside~[ "" ];
	movestepnum=movestepnum~[ 0 ];
	movesteptype=movesteptype~[ 0 ];
	charadd=charadd~[ ["", ""] ];
	charaddvertex=charaddvertex~[ [0, 0, 0, 0] ];
	changestepside=changestepside~[side];
	changestepnum=changestepnum~[num];
	changestepimg=changestepimg~[img];
}