diff options
Diffstat (limited to 'HumanPlayer.cpp')
| -rw-r--r-- | HumanPlayer.cpp | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/HumanPlayer.cpp b/HumanPlayer.cpp index 02bda69..b432d18 100644 --- a/HumanPlayer.cpp +++ b/HumanPlayer.cpp @@ -61,9 +61,9 @@ static const PROGMEM uint8_t au8MouseSprData[] = *********************************************************************/ HumanPlayer::HumanPlayer(const char* const strPlayerName, const Camera& cam) : Player(strPlayerName), +_eState(PLAYER_STATE_IDLE), _cam(cam), -_ABtnFrames(0), -_eState(PLAYER_STATE_IDLE) +_ABtnFrames(0) { _unitsMap[0].create(Unit::UNIT_ID_PEASANT, 16, 16); } @@ -121,7 +121,7 @@ void HumanPlayer::drawHandler(void) * \see \ref tPlayerState. * *********************************************************************/ -enum tPlayerState HumanPlayer::selectUnit(void) +enum HumanPlayer::tPlayerState HumanPlayer::selectUnit(void) { for (size_t szUnit = 0; szUnit < MAX_UNITS; szUnit++) { @@ -132,10 +132,42 @@ enum tPlayerState HumanPlayer::selectUnit(void) { if (not u.isSelected()) { + enum + { + /* Maximum distance, in pixels, + * between unit and cursor + * in order to select a unit. */ + MAX_SELECTION_DISTANCE = 8 + }; + /* Extract Unit object X position. */ const uint16_t x = u.getX(); -#error ("TODO") + /* Extract Unit object Y position. */ + const uint16_t y = u.getY(); + + /* Extract cursor X. */ + const uint16_t cx = _cursor.getX(); + + /* Extract cursor Y. */ + const uint16_t cy = _cursor.getY(); + + /* Calculate X distance between cursor and unit. */ + const uint16_t x_dist = (uint16_t)abs((int)x - (int)cx); + + /* Calculate Y distance between cursor and unit. */ + const uint16_t y_dist = (uint16_t)abs((int)y - (int)cy); + + if ((x_dist < MAX_SELECTION_DISTANCE) + && + (y_dist < MAX_SELECTION_DISTANCE)) + { + /* Select unit. */ + u.setSelected(true); + + /* Player has entered unit selection mode. */ + return PLAYER_STATE_UNIT_SELECTED; + } } else { @@ -147,4 +179,6 @@ enum tPlayerState HumanPlayer::selectUnit(void) /* Unit is not alive. Continue. */ } } + + return PLAYER_STATE_IDLE; } |
