182 lines
4.9 KiB
C++
182 lines
4.9 KiB
C++
/* *******************************************************************
|
|
* Includes
|
|
* ******************************************************************/
|
|
|
|
#include "BaseUnit.h"
|
|
#include "Unit.h"
|
|
#include "Sprite.h"
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
|
|
/* *******************************************************************
|
|
* Defines
|
|
* ******************************************************************/
|
|
|
|
/* *******************************************************************
|
|
* Types definition
|
|
* ******************************************************************/
|
|
|
|
/* *******************************************************************
|
|
* Global variables definition
|
|
* ******************************************************************/
|
|
|
|
/* *******************************************************************
|
|
* Local variables definition
|
|
* ******************************************************************/
|
|
|
|
/*****************************************************************//**
|
|
*
|
|
* \brief Bitmap data for idle UNIT_ID_PEASANT.
|
|
*
|
|
*********************************************************************/
|
|
static const PROGMEM uint8_t au8PeasantSprData[] =
|
|
{
|
|
8,
|
|
8,
|
|
0x00,
|
|
0x3C,
|
|
0x42,
|
|
0x99,
|
|
0xA5,
|
|
0x66,
|
|
0x18,
|
|
0x00
|
|
};
|
|
|
|
/* *******************************************************************
|
|
* Local prototypes declaration
|
|
* ******************************************************************/
|
|
|
|
/* *******************************************************************
|
|
* Functions definition
|
|
* ******************************************************************/
|
|
|
|
/*****************************************************************//**
|
|
*
|
|
* \brief Constructor for Unit class.
|
|
*
|
|
*********************************************************************/
|
|
Unit::Unit(const enum Unit::tUnitID eUnitID) :
|
|
BaseUnit(),
|
|
_eUnitID(eUnitID),
|
|
_eState(UNIT_STATE_IDLE),
|
|
_target_x(0),
|
|
_target_y(0)
|
|
{
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
*
|
|
* \brief Creates a Unit instance by setting default parameters
|
|
* and X/Y coordinates.
|
|
*
|
|
*********************************************************************/
|
|
void Unit::create(const enum Unit::tUnitID eUnitID, const uint16_t x, const uint16_t y)
|
|
{
|
|
/* Execute base class function first. */
|
|
BaseUnit::create(x, y);
|
|
|
|
/* Assign new ID to selected Unit. */
|
|
_eUnitID = eUnitID;
|
|
|
|
/* This table relates all available
|
|
* unit IDs against a bitmap data table. */
|
|
const uint8_t au8HpData[MAX_UNIT_ID] PROGMEM =
|
|
{
|
|
[UNIT_ID_NONE] = 0,
|
|
[UNIT_ID_PEASANT] = 25,
|
|
[UNIT_ID_SWORDMAN] = 35
|
|
};
|
|
|
|
/* Assign health according to unit ID. */
|
|
_hp = au8HpData[eUnitID];
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
*
|
|
* \brief This function sets a position target for a \ref Unit
|
|
* object.
|
|
*
|
|
*********************************************************************/
|
|
void Unit::moveTo(const uint16_t x, const uint16_t y)
|
|
{
|
|
_target_x = x;
|
|
_target_y = y;
|
|
_eState = UNIT_STATE_MOVING;
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
*
|
|
* \brief Periodical handler for Unit class.
|
|
*
|
|
*********************************************************************/
|
|
void Unit::handler(void)
|
|
{
|
|
/* Execute base class handler. */
|
|
BaseUnit::handler();
|
|
|
|
switch (_eState)
|
|
{
|
|
case UNIT_STATE_MOVING:
|
|
if (_x < _target_x)
|
|
{
|
|
_x += 1;
|
|
}
|
|
else if (_x > _target_x)
|
|
{
|
|
_x -= 1;
|
|
}
|
|
|
|
if (_y < _target_y)
|
|
{
|
|
_y += 1;
|
|
}
|
|
else if (_y > _target_y)
|
|
{
|
|
_y -= 1;
|
|
}
|
|
break;
|
|
|
|
case UNIT_STATE_IDLE:
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
/*****************************************************************//**
|
|
*
|
|
* \brief This function checks whether unit is inside screen
|
|
* boundaries and draws its associated bitmap.
|
|
*
|
|
*********************************************************************/
|
|
void Unit::drawHandler(void)
|
|
{
|
|
if (_eUnitID < MAX_UNIT_ID)
|
|
{
|
|
/* This table relates all available
|
|
* unit IDs against a bitmap data table. */
|
|
const uint8_t* const apu8UnitSpriteDataTable[MAX_UNIT_ID] PROGMEM =
|
|
{
|
|
[UNIT_ID_NONE] = NULL,
|
|
[UNIT_ID_PEASANT] = au8PeasantSprData,
|
|
[UNIT_ID_SWORDMAN] = NULL
|
|
};
|
|
|
|
/* Point to appropiate bitmap data given unit ID. */
|
|
const uint8_t* const pu8UnitSpriteData = apu8UnitSpriteDataTable[_eUnitID];
|
|
|
|
Sprite spr(pu8UnitSpriteData);
|
|
|
|
spr.setPos(_x - (Sprite::getWidth(pu8UnitSpriteData) >> 1), _y - (Sprite::getHeight(pu8UnitSpriteData) >> 1));
|
|
|
|
spr.draw();
|
|
|
|
BaseUnit::drawHandler(pu8UnitSpriteData);
|
|
}
|
|
else
|
|
{
|
|
/* Unit has an invalid ID. Exit. */
|
|
}
|
|
}
|