diff options
| author | XaviDCR92 <xavi.dcr@gmail.com> | 2018-07-26 21:15:59 +0200 |
|---|---|---|
| committer | XaviDCR92 <xavi.dcr@gmail.com> | 2018-07-26 21:15:59 +0200 |
| commit | bfdc0b9f497ef10f6687abcc55d93405c611af11 (patch) | |
| tree | 43914b2eada0bf5fb09093d61a250d4930b57a5d /HumanPlayerBtn.cpp | |
| parent | 853c6cddaa2713a9eb0c1f1c55e3f61592f04a46 (diff) | |
* Menu.cpp: actions for CHOICE_SINGLE_PLAYER_GAME have been moved to a new function called MainMenuSinglePlayer().
+ BaseUnit.cpp, BaseUnit.h: new _selected flag.
+ Camera.cpp, Camera.h: linear movement has been taken over quadratic movement. Also, cursor now moves if dealing with screen borders.
+ Cursor.cpp, Cursor.h: new Cursor class holds cursor X/Y information. It is meant to be contained inside a HumanPlayer object.
* Game.cpp: minor changes in casts and comments.
+ HumanPlayer.cpp: added callbacks for button release events.
* HumanPlayer.cpp, HumanPlayerBtn.cpp: button handling has been transferred from HumanPlayer.cpp to HumanPlayerBtn.cpp in order to improve modularity.
+ Sprite.cpp: sprite data was not being checked against NULL.
- Unit.cpp: drawHandler() is now executed by Player object.
Diffstat (limited to 'HumanPlayerBtn.cpp')
| -rw-r--r-- | HumanPlayerBtn.cpp | 272 |
1 files changed, 272 insertions, 0 deletions
diff --git a/HumanPlayerBtn.cpp b/HumanPlayerBtn.cpp new file mode 100644 index 0000000..a21962e --- /dev/null +++ b/HumanPlayerBtn.cpp @@ -0,0 +1,272 @@ +/* ******************************************************************* + * Includes + * ******************************************************************/ + +#include "HumanPlayer.h" +#include "System.h" +#include "Cursor.h" +#include <Buttons.h> +#include <Gamebuino.h> +#include <stdint.h> +#include <string.h> +#include <limits.h> + +/* ******************************************************************* + * Defines + * ******************************************************************/ + +/* ******************************************************************* + * Types definition + * ******************************************************************/ + +/* ******************************************************************* + * Global variables definition + * ******************************************************************/ + +/* ******************************************************************* + * Local variables definition + * ******************************************************************/ + +/* ******************************************************************* + * Local prototypes declaration + * ******************************************************************/ + +/* ******************************************************************* + * Functions definition + * ******************************************************************/ + +/*****************************************************************//** + * + * \brief Event handler executed when human player presses + * left arrow button. + * + *********************************************************************/ +void HumanPlayer::buttonHandler(void) +{ + for (size_t szBtn = 0; szBtn < NUM_BTN; szBtn++) + { + if (gb.buttons.timeHeld(static_cast<uint8_t>(szBtn)) > 0) + { + /* Update player attributes + * according to pressed button. */ + playerButtonPressedHandler(szBtn); + + /* Update camera attributes + * according to pressed button. */ + cameraButtonPressedHandler(szBtn); + } + else + { + /* Key has not been pressed. */ + + /* Update player attributes + * according to released button. */ + playerButtonReleasedHandler(szBtn); + } + } +} + +/*****************************************************************//** + * + * \brief This function executes button pressed handler events + * used on a \ref HumanPlayer object. + * + * \param szBtn + * Pressed button ID. + * + *********************************************************************/ +void HumanPlayer::playerButtonPressedHandler(const size_t szBtn) +{ + /* This array of member functions lists + * button pressed event handlers for each button. */ + static void (HumanPlayer::*const apBtnHandlerTable[NUM_BTN])(void) = + { + [BTN_LEFT] = NULL, + [BTN_UP] = NULL, + [BTN_RIGHT] = NULL, + [BTN_DOWN] = NULL, + [BTN_A] = &HumanPlayer::onABtnPressed, + [BTN_B] = &HumanPlayer::onBBtnPressed + }; + + /* Key has been pressed. Execute both + * HumanPlayer and Camera handlers, if available. */ + + /* Get pointer to HumanPlayer member function for selected button. */ + void (HumanPlayer::*const pBtnHandler)(void) = apBtnHandlerTable[szBtn]; + + if (pBtnHandler != NULL) + { + /* HumanPlayer member function + * pointer is available. Execute. */ + (this->*pBtnHandler)(); + } + else + { + /* Undefined callback for selected button. */ + } +} + +/*****************************************************************//** + * + * \brief This function executes button pressed handler events + * used on a \ref Camera object. + * + * \param szBtn + * Pressed button ID. + * + *********************************************************************/ +void HumanPlayer::cameraButtonPressedHandler(const size_t szBtn) +{ + if (not _cam.isLocked()) + { + static void (Camera::*const apBtnCameraHandlerTable[NUM_BTN])(Cursor&) = + { + [BTN_LEFT] = &Camera::onLeftBtnPressed, + [BTN_UP] = &Camera::onUpBtnPressed, + [BTN_RIGHT] = &Camera::onRightBtnPressed, + [BTN_DOWN] = &Camera::onDownBtnPressed + }; + + /* Get pointer to Camera member function for selected button. */ + void (Camera::*const pCameraBtnHandler)(Cursor&) = apBtnCameraHandlerTable[szBtn]; + + if (pCameraBtnHandler != NULL) + { + /* Camera member function pointer + * is available. + * Note: "const" qualifier must be + * removed since camera button event + * handler modifies Camera class members. */ + ((Camera&)_cam.*pCameraBtnHandler)(_cursor); + } + else + { + /* Undefined callback for selected button. */ + } + } +} + +/*****************************************************************//** + * + * \brief This function executes button released handler events + * used on a \ref HumanPlayer object. + * + * \param szBtn + * Released button ID. + * + *********************************************************************/ +void HumanPlayer::playerButtonReleasedHandler(const size_t szBtn) +{ + /* This array of member functions lists + * button pressed event handlers for each button. */ + static void (HumanPlayer::*const apBtnHandlerTable[NUM_BTN])(void) = + { + [BTN_LEFT] = NULL, + [BTN_UP] = NULL, + [BTN_RIGHT] = NULL, + [BTN_DOWN] = NULL, + [BTN_A] = &HumanPlayer::onABtnReleased + }; + + /* Key has been pressed. Execute both + * HumanPlayer and Camera handlers, if available. */ + + /* Get pointer to HumanPlayer member function for selected button. */ + void (HumanPlayer::*const pBtnHandler)(void) = apBtnHandlerTable[szBtn]; + + if (pBtnHandler != NULL) + { + /* HumanPlayer member function + * pointer is available. Execute. */ + (this->*pBtnHandler)(); + } + else + { + /* Undefined callback for selected button. */ + } +} + +/*****************************************************************//** + * + * \brief Event handler executed when human player presses + * A button. + * + *********************************************************************/ +void HumanPlayer::onABtnPressed(void) +{ + switch (_eState) + { + case PLAYER_STATE_IDLE: + + /* Select nearest unit, if possible. */ + _eState = selectUnit(); + break; + + case PLAYER_STATE_UNIT_SELECTED: + + if (_ABtnFrames < UCHAR_MAX) + { + /* Increase number of frames + * A button has been pressed. */ + _ABtnFrames++; + } + + break; + + default: + /* Undefined player state. Exit. */ + break; + } +} + +/*****************************************************************//** + * + * \brief Event handler executed when human player presses + * B button. + * + *********************************************************************/ +void HumanPlayer::onBBtnPressed(void) +{ +} + +/*****************************************************************//** + * + * \brief Event handler executed when human player releases + * A button. + * + *********************************************************************/ +void HumanPlayer::onABtnReleased(void) +{ + switch (_eState) + { + case PLAYER_STATE_UNIT_SELECTED: + { + enum + { + /* Number of frames where A button + * must be pressed in order to enter + * unit menu. */ + ENTER_MENU_FRAMES = 5 + }; + + if (_ABtnFrames >= ENTER_MENU_FRAMES) + { + /* Enable unit menu. */ + _eState = PLAYER_STATE_UNIT_MENU; + } + else + { + /* Short button press. Exit. */ + } + } + break; + + default: + /* Undefined state. Exit. */ + break; + } + + /* Reset pressed A button frames counter. */ + _ABtnFrames = 0; +} |
