/*!
 * <File comment goes here!!>
 * 
 * Copyright (c) 200x by <your name/ organization here>
 */
#include "BreakoutBox.h"

#include "BreakoutApp.h"
#include "Visualisation.h"

#include "Vector2.h"
#include "AABB.h"

#include "GameConfig.h"
#include "SDL.h"

BreakoutBox::BreakoutBox()
: m_bIsEnabled(true), m_health(0), m_bIsAlive(true), m_lastHitTime(0)
{

}

BreakoutBox::BreakoutBox(const Vector2i &pos, const Vector2i &size, const unsigned int &health)
: m_bIsEnabled(true), m_health(health), m_position(pos), m_size(size), m_bIsAlive(true), m_lastHitTime(0)
{
	updateBBox();
}

BreakoutBox::~BreakoutBox()
{
}

void BreakoutBox::Draw()
{
	if(m_bIsEnabled)
	{
		SpriteID sprID = BreakoutApp::getSingleton()->GetBoxSpriteFromHealth(m_health);
		Visualisation::getSingleton()->DrawSprite(sprID, m_position.x, m_position.y);
	}
}

void BreakoutBox::updateBBox()
{
// 	m_boundingBox.Left = m_position.x - (m_size.x / 2);
// 	m_boundingBox.Right = m_boundingBox.Left + m_size.x;
// 	m_boundingBox.Bottom = m_position.y - (m_size.y / 2);
// 	m_boundingBox.Top = m_boundingBox.Bottom + m_size.y;

	m_boundingBox.Left = m_position.x;
	m_boundingBox.Right = m_boundingBox.Left + m_size.x;
	m_boundingBox.Bottom = m_position.y;
	m_boundingBox.Top = m_boundingBox.Bottom + m_size.y;
}

void BreakoutBox::TakeHit()
{
	//Protection against issue where block was hit twice by the same collision.
	if(m_lastHitTime + BOX_MIN_TIME_BETWEEN_HITS > SDL_GetTicks())
		return;

	m_health--;
	m_lastHitTime = SDL_GetTicks();
	if(m_health <= 0)
		m_bIsAlive = false; 
}
