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

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

//---------------------------
// Defines
//---------------------------
#define GAME_ENGINE (GameEngine::GetSingleton())
#define LEVEL		(Level::GetSingleton())
//---------------------------
// Constructor & Destructor
//---------------------------
Flower::Flower(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 = int(m_Position.y) - m_Height;

	// aabb
	m_AABB.top = m_Position.y;
	m_AABB.bottom = m_AABB.top + m_Height;
	m_AABB.left = m_Position.x;
	m_AABB.right = m_AABB.left + m_Width;
}

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

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

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

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

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

	case Flower::STAGE::PICKUP:
		// aabb update 
		m_AABB.top = m_Position.y;
		m_AABB.bottom = m_AABB.top + m_Height;
		m_AABB.left = m_Position.x;
		m_AABB.right = m_AABB.left + m_Width;
		break;

	case Flower::STAGE::DESTROYED:
		{
			RECT2 zero;
			m_AABB = zero;
			break;
		}

	default:
		break;
	}

	// animation tick
	AnimationTick();

}

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

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

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

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

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

	default:
		break;
	}
}

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

	// aabb update 
	m_AABB.top = m_Position.y;
	m_AABB.bottom = m_AABB.top + m_Height;
	m_AABB.left = m_Position.x;
	m_AABB.right = m_AABB.left + m_Width;
}

void Flower::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 Flower::GetAABBRect()
{
	RECT2 zero;

	if (m_Stage == STAGE::PICKUP)
		return m_AABB;
	else 
		return zero;
}

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

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

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

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

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

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

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

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

int Flower::GetType()
{
	return 2;
}

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

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

