//-----------------------------------------------------
// Name, first name: Hoet, Guntran
// Group: 1DAE3
//-----------------------------------------------------

//---------------------------
// Includes
//---------------------------
#include "Coin.h"

//---------------------------
// Defines
//---------------------------
#define GAME_ENGINE (GameEngine::GetSingleton())

//---------------------------
// Constructor & Destructor
//---------------------------
Coin::Coin(int x = 0, int y = 0, Bitmap* bmpBitmapPtr)
	:EntityMaster(x, y, bmpBitmapPtr)
	//bool
	,m_IsDeleted(false)
	//int
	,m_Width()
	,m_Height()
	,m_TickCount()
	,m_AnimationStep()
	//bitmap
	,m_BmpBitmapPtr(bmpBitmapPtr)
	//others
{
	// data memebers
	m_Position = DOUBLE2( x, y );
	m_Width = m_BmpBitmapPtr->GetWidth() / STEPS_TOTAL;
	m_Height = m_BmpBitmapPtr->GetHeight();
}

Coin::~Coin()
{
	//delete m_BmpBitmapPtr;		// gets deleted in the EnityList
	//m_BmpBitmapPtr = nullptr;
}

//---------------------------
// Methods - Member functions
//---------------------------

void Coin::Tick(double deltaTime)
{
	if (!m_IsDeleted)
	{
		++m_TickCount;
		if (m_TickCount == INTERVAL)
		{
			m_TickCount = 0;
			++m_AnimationStep;
			m_AnimationStep %= STEPS_TOTAL;
		}

		// det the clip rect
		m_Rect.top = 0;
		m_Rect.bottom = m_Rect.top + m_Height;
		m_Rect.left = m_Width * m_AnimationStep;
		m_Rect.right = m_Rect.left + m_Width;

	}
}

void Coin::Paint(MATRIX3X2 matView)
{
	if (!m_IsDeleted)
	{
		MATRIX3X2 matTranslate, matTransform;
		matTranslate.SetAsTranslate(m_Position);
		matTransform = matTranslate * matView;
		GAME_ENGINE->SetTransformMatrix(matTransform);

		DOUBLE2 zero (0,0);		// a position is req to be able to add the rect into DrawBitmap
		GAME_ENGINE->DrawBitmap(m_BmpBitmapPtr, zero, m_Rect);
	}
}

bool Coin::IsHit()
{
	return false;
}

DOUBLE2 Coin::GetPos()
{
	return m_Position;
}

void Coin::DrawDebug(MATRIX3X2 matView)
{
	GAME_ENGINE->SetTransformMatrix(matView);
	GAME_ENGINE->SetColor(COLOR(255,255,0,150));
	GAME_ENGINE->FillRect(GetAABBRect());
}

RECT2 Coin::GetAABBRect()
{
	RECT2 aabb;
	if (!m_IsDeleted)
	{
		int leftMarge = 3;
		int rightMarge = 6;		// because bitmap has some blank space at the sides of the coin

		aabb.top = m_Position.y;
		aabb.bottom = aabb.top + m_Height;
		aabb.left = m_Position.x + leftMarge;
		aabb.right = aabb.left - rightMarge + m_Width;
	}
	else 
	{
		aabb.top = 0;
		aabb.bottom = 0;
		aabb.left = 0;
		aabb.right = 0;
	}

	return aabb;
}

int Coin::ReturnReward()
{
	return PICK_UP_REWARD;
}

void Coin::Delete()
{
	m_IsDeleted = true;
	//delete m_BmpBitmapPtr;
	//m_BmpBitmapPtr = nullptr;
}

void Coin::Move(DOUBLE2 dir)
{
	m_Position += dir;
}

void Coin::ZeroVelocity(bool xVelocity, bool yVelocity)
{
	//if (xVelocity)
	//	m_Velocity.x = 0;
								// do nothing here, Coin has no velocity.
	//if (yVelocity)
	//	m_Velocity.y = 0;
}

int Coin::GetType()
{
	return EntityMaster::TYPE_COIN;
}

void Coin::SetStage(int stage)
{
	// nothing
}

int Coin::GetMushroomType()
{
	return 0;
}

