summaryrefslogtreecommitdiff
path: root/Unit.cpp
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2018-08-05 18:06:49 +0200
committerXaviDCR92 <xavi.dcr@gmail.com>2018-08-05 18:06:49 +0200
commitefc7020defa40c74a467442e6763df71c0783fd0 (patch)
treefa3f7cf7648e7e05ed0483cf25ead7712f2fc0da /Unit.cpp
parent6ecb59761ac940e66c9670d23dd65696df3df7cf (diff)
* Increased MAX_SELECTION_DISTANCE, but this should be done according to BaseUnit instead!HEADmaster
+ BaseUnit now takes a pointer to a Camera object. + Implemented Unit basic movement. + Initial stub for Coordinates class, meant to replace all manual X/Y coordinates handling between classes.
Diffstat (limited to 'Unit.cpp')
-rw-r--r--Unit.cpp47
1 files changed, 45 insertions, 2 deletions
diff --git a/Unit.cpp b/Unit.cpp
index f06fd5c..f739d36 100644
--- a/Unit.cpp
+++ b/Unit.cpp
@@ -59,7 +59,10 @@ static const PROGMEM uint8_t au8PeasantSprData[] =
*********************************************************************/
Unit::Unit(const enum Unit::tUnitID eUnitID) :
BaseUnit(),
-_eUnitID(eUnitID)
+_eUnitID(eUnitID),
+_eState(UNIT_STATE_IDLE),
+_target_x(0),
+_target_y(0)
{
}
@@ -92,6 +95,19 @@ void Unit::create(const enum Unit::tUnitID eUnitID, const uint16_t x, const uint
/*****************************************************************//**
*
+ * \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.
*
*********************************************************************/
@@ -99,6 +115,33 @@ 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;
+ }
}
/*****************************************************************//**
@@ -125,7 +168,7 @@ void Unit::drawHandler(void)
Sprite spr(pu8UnitSpriteData);
- spr.setPos(_x, _y);
+ spr.setPos(_x - (Sprite::getWidth(pu8UnitSpriteData) >> 1), _y - (Sprite::getHeight(pu8UnitSpriteData) >> 1));
spr.draw();