PocketEmpires/BaseUnit.cpp

178 lines
4.9 KiB
C++

/* *******************************************************************
* Includes
* ******************************************************************/
#include "BaseUnit.h"
#include "Camera.h"
#include "System.h"
#include "Sprite.h"
#include <stdbool.h>
#include <stdint.h>
#include <Gamebuino.h>
/* *******************************************************************
* Defines
* ******************************************************************/
/* *******************************************************************
* Types definition
* ******************************************************************/
/* *******************************************************************
* Global variables definition
* ******************************************************************/
/* *******************************************************************
* Local variables definition
* ******************************************************************/
static const Camera* cam;
/* *******************************************************************
* Local prototypes declaration
* ******************************************************************/
/* *******************************************************************
* Functions definition
* ******************************************************************/
/*****************************************************************//**
*
* \brief Constructor for BaseUnit class.
*
*********************************************************************/
BaseUnit::BaseUnit(void) :
_hp(0),
_alive(false),
_selected(false),
_x(0),
_y(0)
{
}
/*****************************************************************//**
*
* \brief This function is executed when user wants to create
* a new object derived from BaseUnit, and this function
* sets default parameters for a BaseUnit abstract object.
*
*********************************************************************/
void BaseUnit::create(const uint16_t x, const uint16_t y)
{
_alive = true;
_x = x;
_y = y;
}
/*****************************************************************//**
*
* \brief Periodical handler for BaseUnit class.
*
*********************************************************************/
void BaseUnit::handler(void)
{
}
/*****************************************************************//**
*
* \brief This function sets global camera for all BaseUnit
* instances.
*
*********************************************************************/
void BaseUnit::setCamera(const Camera* const c)
{
cam = c;
}
/*****************************************************************//**
*
* \brief Periodical draw handler for BaseUnit class.
*
*********************************************************************/
void BaseUnit::drawHandler(const uint8_t* const pu8SprData)
{
if (isSelected())
{
/* Retrieve unit width from sprite data. */
const uint8_t w = Sprite::getWidth(pu8SprData);
/* Retrieve unit height from sprite data. */
const uint8_t h = Sprite::getHeight(pu8SprData);
if (cam != NULL)
{
const uint8_t x = cam->getX(_x - w);
const uint8_t y = cam->getY(_y - h);
gb.display.drawRoundRect(x, y, w << 1, h << 1, 2);
}
}
else
{
/* BaseUnit object is not selected. Exit. */
}
}
/*****************************************************************//**
*
* \brief Returns Unit alive flag.
*
* \return Returns true if Unit object is alive, false otherwise.
*
*********************************************************************/
bool BaseUnit::isAlive(void)
{
return _alive;
}
/*****************************************************************//**
*
* \brief Returns Unit selected flag.
*
* \return Returns true if Unit object is selected, false otherwise.
*
*********************************************************************/
bool BaseUnit::isSelected(void)
{
return _selected;
}
/*****************************************************************//**
*
* \brief This function selects/deselects a \ref BaseUnit object
* according to requested state.
*
* \param bSelect
* If true, unit must be selected. Otherwise, unit
* must be deselected.
*
*********************************************************************/
void BaseUnit::setSelected(const bool bSelect)
{
_selected = bSelect;
}
/*****************************************************************//**
*
* \brief Returns Unit X coordinates.
*
* \return Returns Unit X coordinates.
*
*********************************************************************/
uint16_t BaseUnit::getX(void)
{
return _x;
}
/*****************************************************************//**
*
* \brief Returns Unit Y coordinates.
*
* \return Returns Unit Y coordinates.
*
*********************************************************************/
uint16_t BaseUnit::getY(void)
{
return _y;
}