diff options
Diffstat (limited to 'HumanPlayer.cpp')
| -rw-r--r-- | HumanPlayer.cpp | 107 |
1 files changed, 95 insertions, 12 deletions
diff --git a/HumanPlayer.cpp b/HumanPlayer.cpp index eadebcf..766bf7d 100644 --- a/HumanPlayer.cpp +++ b/HumanPlayer.cpp @@ -3,23 +3,35 @@ * **************************************/ #include "HumanPlayer.h" +#include "System.h" +#include <Buttons.h> +#include <Gamebuino.h> -/* ************************************** - * Defines * - * **************************************/ +/* ******************************************************************* + * Defines + * ******************************************************************/ -/* ************************************** - * Structs and enums * - * **************************************/ +/* ******************************************************************* + * Types definition + * ******************************************************************/ -/* ************************************** - * Local variables * - * **************************************/ +/* ******************************************************************* + * Global variables + * ******************************************************************/ -/* ************************************** - * Functions definition * - * **************************************/ +/* ******************************************************************* + * Local variables + * ******************************************************************/ +/* ******************************************************************* + * Functions definition + * ******************************************************************/ + +/*****************************************************************//** + * + * \brief Constructor for HumanPlayer class. + * + *********************************************************************/ HumanPlayer::HumanPlayer(const char* const strPlayerName) : Player(strPlayerName) { @@ -31,12 +43,83 @@ Player(strPlayerName) * left arrow button. * *********************************************************************/ +void HumanPlayer::buttonHandler(void) +{ + for (uint8_t btn = 0; btn < NUM_BTN; btn++) + { + /* This array of member functions lists + * button pressed event handlers for each button. */ + 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 + }; + + void (Camera::*const apBtnCameraHandlerTable[NUM_BTN])(void) = + { + [BTN_LEFT] = &Camera::onLeftBtnPressed, + [BTN_UP] = NULL, + [BTN_RIGHT] = &Camera::onRightBtnPressed, + [BTN_DOWN] = NULL + }; + + /* Member function pointer is valid. */ + if (gb.buttons.pressed(btn)) + { + /* 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[btn]; + + if (pBtnHandler != NULL) + { + /* HumanPlayer member function + * pointer is available. Execute. */ + (this->*pBtnHandler)(); + } + else + { + /* Undefined callback for selected button. */ + } + + /* Get pointer to Camera member function for selected button. */ + void (Camera::*const pCameraBtnHandler)(void) = apBtnCameraHandlerTable[btn]; + + if (pCameraBtnHandler != NULL) + { + /* Camera member function + * pointer is available. Execute. */ + (_cam.*pCameraBtnHandler)(); + } + else + { + /* Undefined callback for selected button. */ + } + } + } +} + +/*****************************************************************//** + * + * \brief Event handler executed when human player presses + * left arrow button. + * + *********************************************************************/ void HumanPlayer::onLeftBtnPressed(void) { /* Also, send the event to Camera object. */ _cam.onLeftBtnPressed(); } +void HumanPlayer::onABtnPressed(void) +{ +} + void HumanPlayer::drawHandler(void) { |
