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

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

//---------------------------
// Defines
//---------------------------
#define GAME_ENGINE (GameEngine::GetSingleton())
#define HUD			(HUD::GetSingleton())
#define SOUND		(Sound::GetSingleton())
//---------------------------
// Constructor & Destructor
//---------------------------
CoinBlock::CoinBlock(int x, int y, Bitmap *bmpBitmapPtr)
	:EntityMaster(x,y,bmpBitmapPtr)
	// bools
	,m_IsDestroyed(false)
	//int 'n doubles
	,m_Width()
	,m_Height()
	,m_EndHeight()
	,m_TickCount()
	,m_AnimationStep()
	// others
	,m_BmpBitmapPtr(bmpBitmapPtr)
	,m_Stage(STAGE::POPOUT)
{
	// pos
	m_Position = DOUBLE2 (x, y);

	// bmp and related
	m_Width = m_BmpBitmapPtr->GetWidth()/ANIM_STEPS;
	m_Height = m_BmpBitmapPtr->GetHeight();
	m_EndHeight = 0; //int(m_Position.y) - m_Height;

	// play sound when created (when the coin is found)
	SOUND->SFX(4,1,50);
	// add score when found
	HUD->AddCoin();
}

CoinBlock::~CoinBlock()
{
	// nothing to destroy
}

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

void CoinBlock::Tick(double deltaTime)
{
	++m_TickCount;

	switch (m_Stage)
	{
	case CoinBlock::STAGE::LOCKED:
		// nothing
		break;

	case CoinBlock::STAGE::POPOUT:
		PopOutTick(deltaTime);
		break;

	case CoinBlock::STAGE::PICKUP:
		// wait to be picked up
		break;

	case CoinBlock::STAGE::DESTROYED:
		break;

	default:
		break;
	}

	// destroy after time
	const int destroyTime = 20;
	if (m_TickCount >= destroyTime)
		m_Stage = STAGE::DESTROYED;

	// animation tick
	AnimationTick();
}

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

	switch (m_Stage)
	{
	case CoinBlock::STAGE::LOCKED:
		// dont draw
		break;

	case CoinBlock::STAGE::POPOUT:
		GAME_ENGINE->DrawBitmap(m_BmpBitmapPtr,0,0,m_Clip);
		break;

	case CoinBlock::STAGE::PICKUP:
		GAME_ENGINE->DrawBitmap(m_BmpBitmapPtr,0,0,m_Clip);
		break;

	case CoinBlock::STAGE::DESTROYED:
		// dont draw
		break;

	default:
		break;
	}
}

void CoinBlock::PopOutTick(double deltaTime)
{
	if (m_Position.y > m_EndHeight)
	{
		m_Position.y -= POP_SPEED_GENERAL * 2 * deltaTime;
	}
	else
	{
		m_Stage = STAGE::PICKUP;
	}
}

void CoinBlock::AnimationTick()
{
	if (m_TickCount % ANIM_INTERVAL == 0)
	{
		++m_AnimationStep;
		m_AnimationStep %= ANIM_STEPS;
	}

	m_Clip.top = 0;
	m_Clip.bottom = m_Clip.top + m_Height;
	m_Clip.left = m_AnimationStep * m_Width;
	m_Clip.right = m_Clip.left + m_Width;
}

RECT2 CoinBlock::GetAABBRect()
{
	RECT2 zero;
		return zero;
}

int CoinBlock::GetStage()
{
	switch (m_Stage)
	{
	case CoinBlock::STAGE::LOCKED:
		return 0;
	case CoinBlock::STAGE::POPOUT:
		return 1;
	case CoinBlock::STAGE::PICKUP:
		return 2;
	case CoinBlock::STAGE::DESTROYED:
		return 3;
	default:
		return-1;
	}
}

void CoinBlock::SetStage(int stage)
{
	m_Stage = (STAGE)stage;
}

void CoinBlock::DrawDebug(MATRIX3X2 matView)
{
	//GAME_ENGINE->SetColor(COLOR(80,200,50,80));
	//GAME_ENGINE->FillRect(m_AABB);
}

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

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

int CoinBlock::ReturnReward()
{
	return 0;
}

void CoinBlock::Delete()
{
	// nothing
}

void CoinBlock::Move(DOUBLE2 dir)
{
	// dont move (can't move)
}

void CoinBlock::ZeroVelocity(bool xVelocity, bool yVelocity)
{
	// nothing
}

int CoinBlock::GetType()
{
	return 0;
}

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


