#include "player.h"

Player::Player(int id, int x, int y, QCanvasSprite *sprite)
	: Sprite(id, x, y, sprite)
{
	m_lives = 1;
	m_bombs = 1;
	m_power = 1;
	m_steps = 0;
	m_totalsteps = 1;
	m_stepcounter = 0;
	m_visibility = 1;
	m_flash = 0;

	m_seat = -1;

	m_leftDirection = m_rightDirection = m_upDirection = m_downDirection = 0;
}

void Player::numLives(int lives)
{
	m_lives = lives;
}

int Player::numLives(void)
{
	return m_lives;
}

void Player::numBombs(int bombs)
{
	m_bombs = bombs;
}

int Player::numBombs(void)
{
	return m_bombs;
}

void Player::power(int power)
{
	m_power = power;
}

int Player::power(void)
{
	return m_power;
}

void Player::setFd(int fd)
{
	m_fd = fd;
}

int Player::fd()
{
	return m_fd;
}

void Player::steps(int steps)
{
	m_steps = steps;
}

int Player::steps(void)
{
	return m_steps;
}

void Player::totalsteps(int steps)
{
	m_totalsteps = steps;
}

int Player::totalsteps(void)
{
	return m_totalsteps;
}

void Player::stepcounter(int counter)
{
	m_stepcounter = counter;
}

int Player::stepcounter(void)
{
	return m_stepcounter;
}

// Movement models:
// - 1 frame: static
// - 2 frames: static (2nd is dummy)
// - 4 frames: static left/right, toggling for up/down
// - 12 frames: 3 frames (animated) per direction
void Player::move(int x, int y)
{
	if(sprite())
	{
		if (x < this->x()) { /* moving left */
			switch (m_leftDirection) {
				case 0:
					if(sprite()->frameCount() > 4) sprite()->setFrame(6);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(1);
					m_leftDirection++;
					break;
				case 1:
					if(sprite()->frameCount() > 4) sprite()->setFrame(7);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(1);
					m_leftDirection++;
					break;
				case 2:
					if(sprite()->frameCount() > 4) sprite()->setFrame(6);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(1);
					m_leftDirection++;
					break;
				case 3:
					if(sprite()->frameCount() > 4) sprite()->setFrame(8);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(1);
					m_leftDirection = 0;
					break;
			}
		}

		if (x > this->x()) { /* moving right */
			switch (m_rightDirection) {
				case 0:
					if(sprite()->frameCount() > 4) sprite()->setFrame(3);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(0);
					m_rightDirection++;
					break;
				case 1:
					if(sprite()->frameCount() > 4) sprite()->setFrame(4);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(0);
					m_rightDirection++;
					break;
				case 2:
					if(sprite()->frameCount() > 4) sprite()->setFrame(3);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(0);
					m_rightDirection++;
					break;
				case 3:
					if(sprite()->frameCount() > 4) sprite()->setFrame(5);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(0);
					m_rightDirection = 0;
					break;
			}
		}
	
		if (y < this->y()) { /* moving up */
			switch (m_upDirection) {
				case 0:
					if(sprite()->frameCount() > 4) sprite()->setFrame(9);
					else if(sprite()->frameCount() > 3) sprite()->setFrame(2);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(0);
					m_upDirection++;
					break;
				case 1:
					if(sprite()->frameCount() > 4) sprite()->setFrame(10);
					else if(sprite()->frameCount() > 3) sprite()->setFrame(3);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(1);
					m_upDirection++;
					break;
				case 2:
					if(sprite()->frameCount() > 4) sprite()->setFrame(9);
					else if(sprite()->frameCount() > 3) sprite()->setFrame(2);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(0);
					m_upDirection++;
					break;
				case 3:
					if(sprite()->frameCount() > 4) sprite()->setFrame(11);
					else if(sprite()->frameCount() > 3) sprite()->setFrame(3);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(1);
					m_upDirection = 0;
					break;
			}
		}

		if (y > this->y()) { /* moving down */
			switch (m_downDirection) {
				case 0:
					if(sprite()->frameCount() > 4) sprite()->setFrame(0);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(0);
					m_downDirection++;
					break;
				case 1:
					if(sprite()->frameCount() > 4) sprite()->setFrame(1);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(1);
					m_downDirection++;
					break;
				case 2:
					if(sprite()->frameCount() > 4) sprite()->setFrame(0);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(0);
					m_downDirection++;
					break;
				case 3:
					if(sprite()->frameCount() > 4) sprite()->setFrame(2);
					else if(sprite()->frameCount() > 1) sprite()->setFrame(1);
					m_downDirection = 0;
					break;
			}
		}
	}

	Sprite::move(x, y);
}

void Player::visibility(int v)
{
	m_visibility = v;
}

int Player::visibility()
{
	return m_visibility;
}

void Player::numFlash(int f)
{
	m_flash = f;
}

int Player::numFlash()
{
	return m_flash;
}

void Player::setSeat(int seat)
{
	m_seat = seat;
}

int Player::seat()
{
	return m_seat;
}

