#include "Rabbit.h"
#include "Globals.h"
#include "Wumbo-InputManager.h"
#include "Wumbo-Quicky.h"
#include "Level.h"
#include "Credits.h"
#include <cmath>
Rabbit::Rabbit(Level *lvl) : Entity(lvl)
{

}
void Rabbit::init()
{
	degreeshift = 0;
	rotation = 0;
	targetRotation = 0;
	moving = false;
	dontdoanything = false;
	targetX = x = 15;
	y = 0;
	targetZ = z = 15;
}
void Rabbit::update()
{
	if (dontdoanything)
		return;
	if (moving)
	{
		if (targetRotation != degreemod(rotation))
		{
			rotation += degreeshift;
			y = 5*abs(sin((targetRotation-degreemod(rotation))/90.f * glm::pi<float>()));
		}
		else if (targetX != x || targetZ != z)
		{
			if (firstHopStep)
			{
				ss_playsound_distort_vector(&snd_hop);
				firstHopStep = false;
			}
			bool isx = (targetX != x);
			if (targetX < x)
				x -= 1;
			if (targetX > x)
				x += 1;
			if (targetZ < z)
				z -= 1;
			if (targetZ > z)
				z += 1;
			if (isx)
				y = 10*abs(sin((targetX-x)/10.f * glm::pi<float>()));
			else
				y = 10*abs(sin((targetZ-z)/10.f * glm::pi<float>()));
			if (targetX == x && targetZ == z)
			{
				moving = false;

				if (level->get((x-5)/10,(z-5)/10) != TILE_HOLE)
				{
					for(int i=0;i<level->transports.size();i++)
					{
						if (level->transports.at(i).isAtGridPosition((x-5)/10,(z-5)/10))
						{
							if (level->get((x-5)/10,(z-5)/10) == TILE_WHOOSHDOOR)
							{
								if (strcmp(level->transports.at(i).name,"GAMEEND") == 0)
								{
									level->getProgram()->setScene(new Credits(level->getProgram()));
									return;
								}
								dontdoanything = true;
								int dx = (x-5)/10-level->transports.at(i).x;
								int dy = (z-5)/10-level->transports.at(i).y;

								level->PREPARETRANSFER_name = std::string(level->transports.at(i).name);
								level->PREPARETRANSFER_x = level->transports.at(i).destX+dx;
								level->PREPARETRANSFER_y = level->transports.at(i).destY+dy;
								if (level->transports.at(i).x == 0)
									level->PREPARETRANSFER_animate = 2;
								if (level->transports.at(i).x == 14)
									level->PREPARETRANSFER_animate = 0;
								if (level->transports.at(i).y == 0)
									level->PREPARETRANSFER_animate = 1;
								if (level->transports.at(i).y == 14)
									level->PREPARETRANSFER_animate = 3;
								ss_playsound_distort_vector(&snd_whoosh);
							}
							else
							{
								level->getProgram()->setScene(new Level(level->getProgram(),std::string(level->transports.at(i).name),level->transports.at(i).destX,level->transports.at(i).destY,false,degreemod(rotation)));
							}
						}
					}
				}
			}
		}
		else
			moving = false;
	}
	else
	{
		if (y > 0)
		{
			y -= 10*abs(sin(glm::pi<float>()/5));
			if (y <= 0)
			{
				y = 0;
				ss_playsound_distort_vector(&snd_hardland);
			}
		}
		if (level->get((x-5)/10,(z-5)/10) == TILE_HOLE)
		{
			if (y == 0)
				ss_playsound_distort_vector(&snd_fall);
			y -= 10*abs(sin(glm::pi<float>()/5));
			if (y < -60)
			{
				for(int i=0;i<level->transports.size();i++)
				{
					if (level->transports.at(i).isAtGridPosition((x-5)/10,(z-5)/10))
					{
						level->getProgram()->setScene(new Level(level->getProgram(),level->transports.at(i).name,level->transports.at(i).destX,level->transports.at(i).destY,true,degreemod(rotation)));
					}
				}
			}
		}
		if (y == 0)
		{
			if (Wumbo::__quickyKeyboard->isKeyDown(Wumbo::Key::Left) && !level->displayingDialog)
			{
				moving = true;
				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;
				}
				if (!tileIsSolid(level->get((x-5)/10-1,(z-5)/10)))
				{
					targetX = x-10;
					firstHopStep = true;
				}
				else if (tileIsTalker(level->get((x-5)/10-1,(z-5)/10)))
					handleTalk((x-5)/10-1,(z-5)/10);
			}
			else if (Wumbo::__quickyKeyboard->isKeyDown(Wumbo::Key::Right) && !level->displayingDialog)
			{
				moving = true;
				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;
				}
				if (!tileIsSolid(level->get((x-5)/10+1,(z-5)/10)))
				{
					targetX = x+10;
					firstHopStep = true;
				}
				else if (tileIsTalker(level->get((x-5)/10+1,(z-5)/10)))
					handleTalk((x-5)/10+1,(z-5)/10);
			}
			else if (Wumbo::__quickyKeyboard->isKeyDown(Wumbo::Key::Up) && !level->displayingDialog)
			{
				moving = true;
				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);
				}
				if (!tileIsSolid(level->get((x-5)/10,(z-5)/10-1)))
				{
					targetZ = z-10;
					firstHopStep = true;
				}
				else if (tileIsTalker(level->get((x-5)/10,(z-5)/10-1)))
					handleTalk((x-5)/10,(z-5)/10-1);
			}
			else if (Wumbo::__quickyKeyboard->isKeyDown(Wumbo::Key::Down) && !level->displayingDialog)
			{
				moving = true;
				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;
				}
				if (!tileIsSolid(level->get((x-5)/10,(z-5)/10+1)))
				{
					targetZ = z+10;
					firstHopStep = true;
				}
				else if (tileIsTalker(level->get((x-5)/10,(z-5)/10+1)))
					handleTalk((x-5)/10,(z-5)/10+1);
			}
		}
	}
}
void Rabbit::render()
{
	matrixBegin();
	matrixTranslate(x,y,z);
	matrixRotate(rotation,0,1,0);
	matrixUpload();
	mdl_rabbit->draw();

	if (y > 0)
	{
		matrixBegin();
		matrixTranslate(x,.5f,z);
		matrixRotate(rotation,0,1,0);
		matrixUpload();
		mdl_rabbit_shadow->draw();
	}
}
void Rabbit::handleTalk(int _x, int _y)
{
	for(int i=0;i<level->talkers.size();i++)
	{
		if (level->talkers.at(i).isAtGridPosition(_x,_y))
		{
			level->talkers.at(i).activate(degreemod(targetRotation));
		}
	}
}