#include "Talker.h"
#include "Globals.h"
#include "Level.h"
Talker::Talker(Level *lvl, int _x, int _y, int _voice, int _count, int _type, std::string text1, std::string text2, std::string text3)
{
	temp = new Batch();
	rotation = 0;
	targetRotation = 0;
	active = false;
	doneMoving = false;
	level = lvl;
	type = _type;
	voice = _voice;
	count = _count;
	text[0] = text1;
	text[1] = text2;
	text[2] = text3;
	x = _x;
	y = 0;
	z = _y;
	/*w = 1;
	h = 1;*/
}
bool Talker::isAtGridPosition(int _x, int _y)
{
	return _x == x && _y == z;
}
void Talker::activate(int direction)
{
	if (count == 0)
		return;
	level->displayingDialog = true;
	active = true;
	if (immobile)
	{
		doneMoving = true;
		return;
	}
	doneMoving = false;
	if (direction == 0)
	{
		if (degreemod(rotation) != 180)
		{
			targetRotation = 180;
			int r = degreemod(rotation);
			if (r == 0)
				degreeshift = 9*(rand()%2?-1:1);
			if (r == 90)
				degreeshift = 9;
			if (r == 180)
				degreeshift = 0;
			if (r == 270)
				degreeshift = -9;
		}
		else
			doneMoving = true;
	}
	if (direction == 180)
	{
		if (degreemod(rotation) != 0)
		{
			targetRotation = 0;
			int r = degreemod(rotation);
			if (r == 0)
				degreeshift = 0;
			if (r == 90)
				degreeshift = -9;
			if (r == 180)
				degreeshift = 9*(rand()%2?-1:1);
			if (r == 270)
				degreeshift = 9;
		}
		else
			doneMoving = true;
	}
	if (direction == 270)
	{
		if (degreemod(rotation) != 90)
		{
			targetRotation = 90;
			int r = degreemod(rotation);
			if (r == 0)
				degreeshift = 9;
			if (r == 90)
				degreeshift = 0;
			if (r == 180)
				degreeshift = -9;
			if (r == 270)
				degreeshift = 9*(rand()%2?-1:1);
		}
		else
			doneMoving = true;
	}
	if (direction == 90)
	{
		if (degreemod(rotation) != 270)
		{
			targetRotation = 270;
			int r = degreemod(rotation);
			if (r == 0)
				degreeshift = -9;
			if (r == 90)
				degreeshift = 9*(rand()%2?-1:1);
			if (r == 180)
				degreeshift = 9;
			if (r == 270)
				degreeshift = 0;
		}
		else
			doneMoving = true;
	}
}
void Talker::update()
{
	if (active)
	{
		if (doneMoving)
		{
			active = false;
			doneMoving = false;
			for(int j=0;j<count;j++)
				level->addPhrase(text[j],voice);
		}
		else if (!immobile)
		{
			if (targetRotation != degreemod(rotation))
			{
				rotation += degreeshift;
				y = 5*abs(sin((targetRotation-degreemod(rotation))/90.f * glm::pi<float>()));
			}
			else
				doneMoving = true;
		}
	}
}
void Talker::render()
{
	if (immobile)
		return;
	if (type == TILE_BIRD)
	{
		matrixBegin();
		matrixTranslate(10*x+5,y,10*z+5);
		matrixRotate(rotation,0,1,0);
		matrixUpload();
		mdl_bird->draw();

		if (y > 0)
		{
			matrixBegin();
			matrixTranslate(10*x+5,.5f,10*z+5);
			matrixRotate(rotation,0,1,0);
			matrixUpload();
			mdl_bird_shadow->draw();
		}
	}
	if (type == TILE_SCRIBBLE)
	{
		temp->begin();
			int n = 1 + rand()%5;
			for(int i=0;i<n;i++)
			{
				int s = rand()%4;
				int xxx = 5-rand()%11;
				int yyy = 5-rand()%11;
				int zzz = 5-rand()%11;
				temp->addCube(xxx-s,yyy-s,zzz-s,xxx+s,yyy+s,zzz+s,SHARECART_COLORS[rand()%16],1,1,1,1,1,1);
			}
		temp->finish();

		matrixBegin();
		matrixTranslate(10*x+5,y,10*z+5);
		matrixRotate(rotation,0,1,0);
		matrixUpload();
		temp->draw();
	}
}