#include "cAnimatedSprite.hpp"

using VideoEngine::cAnimatedSprite;

cAnimatedSprite::cAnimatedSprite( const unsigned long int x /*= 0*/, const unsigned long int y /*= 0*/,
								 const unsigned long int fps /*= 16*/ )
: m_nextTime(0), m_RATE(0)
{
	setFPS(fps);
	setIncrement(x, y);
}

cAnimatedSprite::cAnimatedSprite( const cAnimatedSprite& copy )
: m_nextTime(0), m_RATE(0)
{
	setFPS(copy.getFPS());
	setIncrement(copy.getXIncrement(), copy.getYIncrement());
}

cAnimatedSprite::~cAnimatedSprite()
{
}

///Functions
void cAnimatedSprite::UptoDown()
{
	//if (TimeLeft() == 0)
	//{
		signed long int x = 0;
		signed long int y = 0;
		getStartImageArea(x, y);
		y += m_yIncrement;
		if (y >= signed(getImage()->getHeight()))
			y = 1;
		setStartImageArea(x, y);
	//}	
	Draw();
}

void cAnimatedSprite::DowntoUp()
{
	//if (TimeLeft() == 0)
	//{
		signed long int x = 0;
		signed long int y = 0;
		
		getStartImageArea(x, y);
		y -= m_yIncrement;
		if (y < 1)
			y = getImage()->getHeight() - m_yIncrement;
		setStartImageArea(x, y);
	//}
	Draw();
}

void cAnimatedSprite::LefttoRight()
{
	signed long int x = 0;
	signed long int y = 0;

	getStartImageArea(x, y);
	if (x >= signed(getImage()->getWidth()))
		setStartImageArea(1,y);
	if (TimeLeft() == 0)
	{
		x = 0;
		y = 0;
		
		getStartImageArea(x, y);
		x += m_xIncrement;
		if (x >= signed(getImage()->getWidth()))
			x = 1;
		setStartImageArea(x, y);
	}
	Draw();     
}

void cAnimatedSprite::RighttoLeft()
{
	if (TimeLeft() == 0)
	{
		signed long int x = 0;
		signed long int y = 0;
		
		getStartImageArea(x, y);
		x -= m_xIncrement;
		if (x <= 1)
			x = getImage()->getWidth() - m_xIncrement;
		setStartImageArea(x, y);
	}
	Draw();
}

///Sets
void cAnimatedSprite::setXIncrement( const unsigned long int x )
{
	m_xIncrement = x;
}

void cAnimatedSprite::setYIncrement( const unsigned long int y )
{
	m_yIncrement = y;
}

void cAnimatedSprite::setIncrement( const unsigned long int y, const unsigned long int x )
{
	m_xIncrement = x;
	m_yIncrement = y;
}

void cAnimatedSprite::setFPS( const unsigned long int fps /*= 16*/ )
{
	m_fps = fps;
	m_RATE = (1000 / m_fps);
	m_nextTime = ((unsigned long int)SDL_GetTicks()) + m_RATE;
}

///Gets
const unsigned long int cAnimatedSprite::getXIncrement() const
{
	return m_xIncrement;
}

const unsigned long int cAnimatedSprite::getYIncrement() const
{
	return m_yIncrement;
}

void cAnimatedSprite::getIncrement( unsigned long int& x, unsigned long int& y ) const
{
	x = m_xIncrement;
	y = m_yIncrement;
}

const unsigned long int cAnimatedSprite::getFPS() const
{
	return m_fps;
}

const unsigned long int cAnimatedSprite::TimeLeft()
{
	unsigned long int now = (unsigned long int)SDL_GetTicks();

	if(m_nextTime <= now)
	{
		AddTimeandRate();
		return 0;
	}
	else
		return 1;
}

void cAnimatedSprite::AddTimeandRate()
{
	m_nextTime += m_RATE;
}


