PocketEmpires/HumanPlayer.cpp

160 lines
5.0 KiB
C++

/* *******************************************************************
* Includes
* ******************************************************************/
#include "HumanPlayer.h"
#include "System.h"
#include <Buttons.h>
#include <Gamebuino.h>
/* *******************************************************************
* Defines
* ******************************************************************/
/* *******************************************************************
* Types definition
* ******************************************************************/
/* *******************************************************************
* Global variables definition
* ******************************************************************/
/* *******************************************************************
* Local variables definition
* ******************************************************************/
/* *******************************************************************
* Local prototypes declaration
* ******************************************************************/
/* *******************************************************************
* Functions definition
* ******************************************************************/
/*****************************************************************//**
*
* \brief Constructor for HumanPlayer class.
*
*********************************************************************/
HumanPlayer::HumanPlayer(const char* const strPlayerName) :
Player(strPlayerName)
{
_unitsMap[0].create(Unit::UNIT_ID_PEASANT);
}
/*****************************************************************//**
*
* \brief Periodical event handler that calls HumanPlayer subtasks.
*
*********************************************************************/
void HumanPlayer::handler(void)
{
this->buttonHandler();
for (uint8_t i = 0; i < MAX_UNITS; i++)
{
Unit& unit = _unitsMap[i];
unit.handler();
}
}
/*****************************************************************//**
*
* \brief Event handler executed when human player presses
* left arrow button.
*
*********************************************************************/
void HumanPlayer::buttonHandler(void)
{
for (uint8_t u8Btn = 0; u8Btn < NUM_BTN; u8Btn++)
{
/* 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
};
static void (Camera::*const apBtnCameraHandlerTable[NUM_BTN])(void) =
{
[BTN_LEFT] = &Camera::onLeftBtnPressed,
[BTN_UP] = NULL,
[BTN_RIGHT] = &Camera::onRightBtnPressed,
[BTN_DOWN] = NULL
};
if (gb.buttons.pressed(u8Btn))
{
/* 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[u8Btn];
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[u8Btn];
if (pCameraBtnHandler != NULL)
{
/* Camera member function
* pointer is available. Execute. */
(_cam.*pCameraBtnHandler)();
}
else
{
/* Undefined callback for selected button. */
}
}
else
{
/* Key has not been pressed. Exit. */
}
}
}
/*****************************************************************//**
*
* \brief Event handler executed when human player presses
* A button.
*
*********************************************************************/
void HumanPlayer::onABtnPressed(void)
{
}
/*****************************************************************//**
*
* \brief Event handler executed when human player presses
* B button.
*
*********************************************************************/
void HumanPlayer::onBBtnPressed(void)
{
}
/*****************************************************************//**
*
* \brief This function draws all units and player UI.
*
*********************************************************************/
void HumanPlayer::drawHandler(void)
{
}