* 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.
This commit is contained in:
XaviDCR92 2018-07-26 21:15:59 +02:00
parent 853c6cddaa
commit bfdc0b9f49
22 changed files with 2183 additions and 1586 deletions

View File

@ -38,6 +38,7 @@
BaseUnit::BaseUnit(void) :
_hp(0),
_alive(false),
_selected(false),
_x(0),
_y(0)
{

View File

@ -31,13 +31,14 @@
class BaseUnit
{
public:
explicit BaseUnit();
explicit BaseUnit(void);
void handler(void);
void create(const uint16_t x, const uint16_t y);
protected:
uint16_t _hp; /**< Health points. */
bool _alive; /**< Alive flag. */
bool _alive; /**< Alive unit flag. */
bool _selected; /**< Selected unit flag. */
uint16_t _x; /**< X coordinate inside map. */
uint16_t _y; /**< Y coordinate inside map. */
};

View File

@ -3,16 +3,20 @@
* ******************************************************************/
#include "Camera.h"
#include "Cursor.h"
#include <stdint.h>
#include <limits.h>
#include <stdbool.h>
/* *******************************************************************
* Defines
* ******************************************************************/
#define MAX_CAMERA_SPEED ((int8_t)3)
#define SPEED_CALCULATION_TIME ((uint8_t)3)
#define CAMERA_ACCELERATION ((int8_t)1)
/*****************************************************************//**
*
* \brief This macro defines camera speed, in pixels per frame.
*
*********************************************************************/
#define CAMERA_SPEED (static_cast<int8_t>(4))
/* *******************************************************************
* Types definition
@ -43,54 +47,10 @@ Camera::Camera(void) :
_bLocked(false),
_xOffset(0),
_yOffset(0),
_xSpeed(0),
_ySpeed(0),
_speedTimer(0)
{
}
void Camera::handler(void)
{
if (((_xOffset + _xSpeed) >= -16)
||
((_xOffset + _xSpeed) <= 0) )
{
_xOffset += _xSpeed;
//~_yOffset += _ySpeed;
}
if (++_speedTimer >= SPEED_CALCULATION_TIME)
{
_speedTimer = 0;
if (_ySpeed < 0)
{
_ySpeed += CAMERA_ACCELERATION;
}
else if (_ySpeed > 0)
{
_ySpeed -= CAMERA_ACCELERATION;
}
else
{
/* Y speed is already still. Continue. */
}
if (_xSpeed < 0)
{
_xSpeed += CAMERA_ACCELERATION;
}
else if (_xSpeed > 0)
{
_xSpeed -= CAMERA_ACCELERATION;
}
else
{
/* x speed is alreadx still. Continue. */
}
}
}
/*****************************************************************//**
*
* \brief This function transforms X coordinates for a given
@ -99,7 +59,7 @@ void Camera::handler(void)
*********************************************************************/
uint8_t Camera::getX(const uint8_t x) const
{
return x + _xOffset;
return x - _xOffset;
}
/*****************************************************************//**
@ -110,7 +70,7 @@ uint8_t Camera::getX(const uint8_t x) const
*********************************************************************/
uint8_t Camera::getY(const uint8_t y) const
{
return y + _yOffset;
return y - _yOffset;
}
/*****************************************************************//**
@ -119,15 +79,23 @@ uint8_t Camera::getY(const uint8_t y) const
* left arrow button.
*
*********************************************************************/
void Camera::onLeftBtnPressed(void)
void Camera::onLeftBtnPressed(Cursor& cursor)
{
if (_xSpeed < 0)
if (not cursor.isXCentered())
{
_xSpeed += CAMERA_ACCELERATION << 1;
/* Move cursor to initial position. */
cursor.move(-CAMERA_SPEED);
}
else if (_xSpeed < MAX_CAMERA_SPEED)
else if (_xOffset >= CAMERA_SPEED)
{
_xSpeed += CAMERA_ACCELERATION;
/* Move camera to the left. */
_xOffset -= CAMERA_SPEED;
}
else
{
/* Left screen margin reached.
* Cursor has to be moved. */
cursor.move(-CAMERA_SPEED);
}
}
@ -137,26 +105,23 @@ void Camera::onLeftBtnPressed(void)
* right arrow button.
*
*********************************************************************/
void Camera::onRightBtnPressed(void)
void Camera::onRightBtnPressed(Cursor& cursor)
{
int8_t acc = 0;
if (_xSpeed > 0)
if (not cursor.isXCentered())
{
acc = CAMERA_ACCELERATION << 1;
/* Move cursor to initial position. */
cursor.move(CAMERA_SPEED);
}
else if (_xSpeed > -MAX_CAMERA_SPEED)
else if (_xOffset < 512)
{
acc = CAMERA_ACCELERATION;
}
if ((_xOffset + (_xSpeed - acc)) >= -16)
{
_xSpeed -= acc;
/* Move camera to the right. */
_xOffset += CAMERA_SPEED;
}
else
{
_xSpeed = 0;
/* Right screen margin reached.
* Cursor has to be moved. */
cursor.move(CAMERA_SPEED);
}
}
@ -166,15 +131,23 @@ void Camera::onRightBtnPressed(void)
* up arrow button.
*
*********************************************************************/
void Camera::onUpBtnPressed(void)
void Camera::onUpBtnPressed(Cursor& cursor)
{
if (_ySpeed < 0)
if (not cursor.isYCentered())
{
_ySpeed += CAMERA_ACCELERATION << 1;
/* Move cursor to initial position. */
cursor.move(0, -CAMERA_SPEED);
}
else if (_ySpeed < MAX_CAMERA_SPEED)
else if (_yOffset > 0)
{
_ySpeed += CAMERA_ACCELERATION;
/* Move camera to the right. */
_yOffset -= CAMERA_SPEED;
}
else
{
/* Upper screen margin reached.
* Cursor has to be moved. */
cursor.move(0, -CAMERA_SPEED);
}
}
@ -184,14 +157,50 @@ void Camera::onUpBtnPressed(void)
* down arrow button.
*
*********************************************************************/
void Camera::onDownBtnPressed(void)
void Camera::onDownBtnPressed(Cursor& cursor)
{
if (_ySpeed > 0)
if (not cursor.isYCentered())
{
_ySpeed -= CAMERA_ACCELERATION << 1;
/* Move cursor to initial position. */
cursor.move(0, CAMERA_SPEED);
}
else if (_ySpeed < MAX_CAMERA_SPEED)
else if (_yOffset < 512)
{
_ySpeed -= CAMERA_ACCELERATION;
/* Move camera to the right. */
_yOffset += CAMERA_SPEED;
}
else
{
/* Lower screen margin reached.
* Cursor has to be moved. */
cursor.move(0, CAMERA_SPEED);
}
}
/*****************************************************************//**
*
* \brief As its name suggests, this function adjusts camera lock.
*
* When camera is locked, it cannot be moved when pressing
* direction keys.
*
* \param bLock
* True if camera must be locked, false otherwise.
*
*********************************************************************/
void Camera::adjustLock(const bool bLock)
{
_bLocked = bLock;
}
/*****************************************************************//**
*
* \brief Returns camera lock state.
*
* \return Returns true if camera is locked, false otherwise.
*
*********************************************************************/
bool Camera::isLocked(void) const
{
return _bLocked;
}

View File

@ -5,7 +5,9 @@
* Includes
* *************************************/
#include "Cursor.h"
#include <stdint.h>
#include <stdbool.h>
/* *************************************
* Defines
@ -23,26 +25,21 @@ class Camera
{
public:
Camera(void);
void handler(void);
void setLock(const bool bLock)
{
_bLocked = bLock;
}
void adjustLock(const bool bLock);
bool isLocked(void) const;
uint8_t getX(const uint8_t x) const;
uint8_t getY(const uint8_t y) const;
/* Event handlers. */
void onLeftBtnPressed(void);
void onRightBtnPressed(void);
void onUpBtnPressed(void);
void onDownBtnPressed(void);
void onLeftBtnPressed(Cursor& cursor);
void onRightBtnPressed(Cursor& cursor);
void onUpBtnPressed(Cursor& cursor);
void onDownBtnPressed(Cursor& cursor);
private:
bool _bLocked;
int16_t _xOffset;
int16_t _yOffset;
int16_t _xSpeed;
int16_t _ySpeed;
uint8_t _speedTimer;
};

126
Cursor.cpp Normal file
View File

@ -0,0 +1,126 @@
/* *******************************************************************
* Includes
* ******************************************************************/
#include "Cursor.h"
#include <stdint.h>
/* *******************************************************************
* Defines
* ******************************************************************/
#define CURSOR_DEFAULT_X (static_cast<uint8_t>(80 >> 1))
#define CURSOR_DEFAULT_Y (static_cast<uint8_t>(44 >> 1))
/* *******************************************************************
* Types definition
* ******************************************************************/
/* *******************************************************************
* Global variables definition
* ******************************************************************/
/* *******************************************************************
* Local variables definition
* ******************************************************************/
/* *******************************************************************
* Local prototypes declaration
* ******************************************************************/
/* *******************************************************************
* Functions definition
* ******************************************************************/
/*****************************************************************//**
*
* \brief Constructor for Cursor class.
*
*********************************************************************/
Cursor::Cursor(void) :
_x(CURSOR_DEFAULT_X),
_y(CURSOR_DEFAULT_Y)
{
}
/*****************************************************************//**
*
* \brief This function moves the cursor to a given position,
* as long as X coordinates are between {0, CURSOR_DEFAULT_X}
* and Y coordinates are between {0, CURSOR_DEFAULT_Y}.
*
* \param x
* X position diff.
*
* \param y
* Y position diff.
*
*********************************************************************/
void Cursor::move(const int8_t x, const int8_t y)
{
if ((static_cast<int8_t>(_x) + x >= 0)
&&
(static_cast<int8_t>(_x) + x <= CURSOR_DEFAULT_X))
{
_x += x;
}
if ((static_cast<int8_t>(_y) + y >= 0)
&&
(static_cast<int8_t>(_y) + y <= CURSOR_DEFAULT_Y))
{
_y += y;
}
}
/*****************************************************************//**
*
* \brief This function simply returns cursor X position.
*
* \return Returns cursor X position.
*
*********************************************************************/
uint8_t Cursor::getX(void)
{
return _x;
}
/*****************************************************************//**
*
* \brief This function simply returns cursor Y position.
*
* \return Returns cursor Y position.
*
*********************************************************************/
uint8_t Cursor::getY(void)
{
return _y;
}
/*****************************************************************//**
*
* \brief This function returns whether cursor is on its initial
* X position.
*
* \return True if \ref Cursor object is on its initial X position,
* false otherwise.
*
*********************************************************************/
bool Cursor::isXCentered(void)
{
return _x == CURSOR_DEFAULT_X;
}
/*****************************************************************//**
*
* \brief This function returns whether cursor is on its initial
* Y position.
*
* \return True if \ref Cursor object is on its initial Y position,
* false otherwise.
*
*********************************************************************/
bool Cursor::isYCentered(void)
{
return _y == CURSOR_DEFAULT_Y;
}

47
Cursor.h Normal file
View File

@ -0,0 +1,47 @@
#ifndef CURSOR_H__
#define CURSOR_H__
/* *******************************************************************
* Includes
* ******************************************************************/
#include <stdbool.h>
#include <stdint.h>
/* *******************************************************************
* Defines
* ******************************************************************/
/* *******************************************************************
* Global types definition
* ******************************************************************/
/* *******************************************************************
* Global variables declaration
* ******************************************************************/
/* *******************************************************************
* Global functions declaration
* ******************************************************************/
/* *******************************************************************
* Class definition
* ******************************************************************/
class Cursor
{
public:
explicit Cursor(void);
void move(const int8_t x = 0, const int8_t y = 0);
uint8_t getX(void);
uint8_t getY(void);
bool isXCentered(void);
bool isYCentered(void);
private:
uint8_t _x;
uint8_t _y;
};
#endif /* CURSOR_H__ */

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -67,7 +67,7 @@ void Game(const struct tGameConfig& sGameConfig)
/* Do not calculate a new frame
* until refresh flag is set. */
while (gb.update() == false);
while (not gb.update());
} while (GamePause() != PAUSE_MENU_CHOICE_QUIT);
}
@ -82,19 +82,20 @@ void Game(const struct tGameConfig& sGameConfig)
*********************************************************************/
static void GameNextFrame(const struct tGameConfig& sGameConfig)
{
for (uint8_t i = 0; i < sGameConfig.u8NHumanPlayers; i++)
for (size_t szHumanPlayer = 0; szHumanPlayer < sGameConfig.u8NHumanPlayers; szHumanPlayer++)
{
HumanPlayer* pHumanPlayerData = &sGameConfig.pHumanPlayerData[i];
HumanPlayer* const pHumanPlayerData = &sGameConfig.pHumanPlayerData[szHumanPlayer];
if (pHumanPlayerData != NULL)
{
/* Execute handler for selected HumanPlayer. */
pHumanPlayerData->handler();
}
else
{
/* HumanPlayer handler callback not available. */
}
}
/* Update camera position according
* to button pressed events. */
sGameConfig.cam.handler();
}
/*****************************************************************//**
@ -118,7 +119,7 @@ static enum tPauseMenuChoice GamePause(void)
[PAUSE_MENU_CHOICE_QUIT] = strPauseMenuOption_1
};
return (enum tPauseMenuChoice)gb.menu(astrPauseMenuOptions, MAX_PAUSE_MENU_CHOICES);
return static_cast<enum tPauseMenuChoice>(gb.menu(astrPauseMenuOptions, MAX_PAUSE_MENU_CHOICES));
}
else
{

View File

@ -4,8 +4,12 @@
#include "HumanPlayer.h"
#include "System.h"
#include "Sprite.h"
#include "Cursor.h"
#include <Buttons.h>
#include <Gamebuino.h>
#include <stdint.h>
#include <string.h>
/* *******************************************************************
* Defines
@ -23,6 +27,25 @@
* Local variables definition
* ******************************************************************/
/*****************************************************************//**
*
* \brief Bitmap data for idle UNIT_ID_PEASANT.
*
*********************************************************************/
static const PROGMEM uint8_t au8MouseSprData[] =
{
8,
8,
0xFC,
0x84,
0x88,
0x84,
0xA2,
0xD1,
0x0A,
0x04
};
/* *******************************************************************
* Local prototypes declaration
* ******************************************************************/
@ -38,7 +61,9 @@
*********************************************************************/
HumanPlayer::HumanPlayer(const char* const strPlayerName, const Camera& cam) :
Player(strPlayerName),
_cam(cam)
_cam(cam),
_ABtnFrames(0),
_eState(PLAYER_STATE_IDLE)
{
_unitsMap[0].create(Unit::UNIT_ID_PEASANT, 16, 16);
}
@ -50,107 +75,13 @@ _cam(cam)
*********************************************************************/
void HumanPlayer::handler(void)
{
this->buttonHandler();
/* Execute HumanPlayerBtn submodule. */
buttonHandler();
for (uint8_t i = 0; i < MAX_UNITS; i++)
{
Unit& unit = _unitsMap[i];
/* Execute parent class unit handler. */
Player::handleUnits();
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] = &Camera::onUpBtnPressed,
[BTN_RIGHT] = &Camera::onRightBtnPressed,
[BTN_DOWN] = &Camera::onDownBtnPressed
};
if (gb.buttons.timeHeld(u8Btn) > 0)
{
/* 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.
* Note: "const" qualifier must be
* removed since camera button event
* handler modifies Camera class members. */
((Camera&)_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)
{
drawHandler();
}
/*****************************************************************//**
@ -159,5 +90,37 @@ void HumanPlayer::onBBtnPressed(void)
*
*********************************************************************/
void HumanPlayer::drawHandler(void)
{
/* Execute base class Unit drawHandler. */
Player::drawUnits();
if (_eState == PLAYER_STATE_UNIT_MENU)
{
gb.display.setColor(BLACK);
gb.display.drawRect(0, 40, 84, 8);
}
/* Configure cursor sprite object. */
Sprite cursorSpr(au8MouseSprData, false, INVERT);
/* Transfer Cursor to Sprite coordinates. */
cursorSpr.setPos(_cursor.getX(), _cursor.getY());
/* Draw cursor sprite. */
cursorSpr.draw();
}
/*****************************************************************//**
*
* \brief This function looks for units nearby and selects them.
* If no units can be selected, player state remains
* unchanged.
*
* \return New player state.
*
* \see \ref tPlayerState.
*
*********************************************************************/
enum tPlayerState HumanPlayer::selectUnit(void)
{
}

View File

@ -7,6 +7,8 @@
#include "Player.h"
#include "Camera.h"
#include "Cursor.h"
#include <string.h>
/* **************************************
* Defines *
@ -27,13 +29,37 @@ class HumanPlayer : public Player
void handler(void);
private:
const Camera& _cam;
void buttonHandler(void);
enum tPlayerState
{
PLAYER_STATE_IDLE, /**< Player has not entered any menu or unit. */
PLAYER_STATE_UNIT_MENU, /**< Player has opened Unit menu. */
PLAYER_STATE_UNIT_SELECTED /**< Player has selected one or more units. */
} _eState; /**< Player action state. */
void drawHandler(void);
/* Event handlers. */
/* Button handlers. */
void buttonHandler(void);
void playerButtonPressedHandler(const size_t szBtn);
void cameraButtonPressedHandler(const size_t szBtn);
void playerButtonReleasedHandler(const size_t szBtn);
/* Pressed button event handlers. */
void onABtnPressed(void);
void onBBtnPressed(void);
/* Released button event handlers. */
void onABtnReleased(void);
/* Unit selection. */
enum tPlayerState selectUnit(void);
/* Private objects. */
const Camera& _cam;
Cursor _cursor;
/* Private variables. */
uint8_t _ABtnFrames;
};
#endif /* HUMAN_PLAYER_H__ */

272
HumanPlayerBtn.cpp Normal file
View File

@ -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;
}

View File

@ -13,8 +13,9 @@ CXX_FLAGS = $(CC_FLAGS) -std=c++11
OBJ_DIR = Obj
SRC_DIR = .
OBJECTS=$(addprefix $(OBJ_DIR)/,main.o Game.o System.o HumanPlayer.o Sprite.o \
Player.o Camera.o BaseUnit.o Unit.o Menu.o )
OBJECTS=$(addprefix $(OBJ_DIR)/,main.o Game.o System.o HumanPlayer.o Sprite.o \
Player.o Camera.o BaseUnit.o Unit.o Menu.o \
Cursor.o HumanPlayerBtn.o)
DEPS = $(OBJECTS:.o=.d)

View File

@ -32,6 +32,8 @@
* Local prototypes declaration
* ******************************************************************/
static void MainMenuSinglePlayer(void);
/* *******************************************************************
* Functions definition
* ******************************************************************/
@ -75,38 +77,7 @@ void MainMenu(void)
switch (gb.menu(astrMainMenuOptions, MAX_CHOICES))
{
case CHOICE_SINGLE_PLAYER_GAME:
{
enum
{
/* Maximum number of characters for
* player name, as specified on
* Gamebuino documentation. */
GAMEBUINO_MAX_PLAYER_NAME = 10
};
Camera cam;
char strName[GAMEBUINO_MAX_PLAYER_NAME] = {0};
/* Fill strName with default user name. */
gb.getDefaultName(strName);
/* Declare 1 human player instance. */
HumanPlayer h(strName, cam);
const struct tGameConfig c =
{
.pHumanPlayerData = &h,
.u8NHumanPlayers = 1,
.cam = cam
};
/* Set global camera for sprites. */
Sprite::setCamera(&c.cam);
/* Initialize game with defined configuration. */
Game(c);
}
MainMenuSinglePlayer();
break;
case CHOICE_MULTI_PLAYER_GAME:
@ -118,3 +89,42 @@ void MainMenu(void)
break;
}
}
/*****************************************************************//**
*
* \brief Executes single player mode.
*
*********************************************************************/
static void MainMenuSinglePlayer(void)
{
enum
{
/* Maximum number of characters for
* player name, as specified on
* Gamebuino documentation. */
GAMEBUINO_MAX_PLAYER_NAME = 10
};
char strName[GAMEBUINO_MAX_PLAYER_NAME] = {0};
/* Fill strName with default user name. */
gb.getDefaultName(strName);
Camera cam;
/* Declare 1 human player instance. */
HumanPlayer h(strName, cam);
const struct tGameConfig c =
{
.pHumanPlayerData = &h,
.u8NHumanPlayers = 1,
.cam = cam
};
/* Set global camera for sprites. */
Sprite::setCamera(&c.cam);
/* Initialize game with defined configuration. */
Game(c);
}

View File

@ -3,6 +3,7 @@
* ******************************************************************/
#include "Player.h"
#include "Unit.h"
#include <string.h>
/* *******************************************************************
@ -54,3 +55,27 @@ _name{'\0'}
/* Set all resources to default value. */
memset(_resourcesMap, DEFAULT_RESOURCES, sizeof(uint8_t) * MAX_RESOURCE_TYPES);
}
void Player::handleUnits(void)
{
for (size_t szUnit = 0; szUnit < MAX_UNITS; szUnit++)
{
/* Select Unit object from internal table. */
Unit& unit = _unitsMap[szUnit];
/* Execute unit handler. */
unit.handler();
}
}
void Player::drawUnits(void)
{
for (size_t szUnit = 0; szUnit < MAX_UNITS; szUnit++)
{
/* Select Unit object from internal table. */
Unit& u = _unitsMap[szUnit];
/* Execute drawHandler for selected Unit. */
u.drawHandler();
}
}

View File

@ -44,6 +44,8 @@ class Player
MAX_UNITS = 32
};
void handleUnits(void);
void drawUnits(void);
uint16_t _resourcesMap[MAX_RESOURCE_TYPES];
char _name[MAX_NAME_LENGTH];
Unit _unitsMap[MAX_UNITS];

View File

@ -28,46 +28,45 @@ long_line_behaviour=1
long_line_column=120
[files]
current_page=17
FILE_NAME_0=0;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FGfx.cpp;0;4
FILE_NAME_1=0;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2Fmain.cpp;0;4
FILE_NAME_2=3528;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FMenu.cpp;0;4
FILE_NAME_3=549;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FPad.cpp;0;4
FILE_NAME_4=987;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FCamera.h;0;4
FILE_NAME_5=665;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FGfx.h;0;4
FILE_NAME_6=314;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FGlobal_Inc.h;0;4
FILE_NAME_7=299;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FMenu.h;0;4
FILE_NAME_8=611;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FPad.h;0;4
FILE_NAME_9=1191;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FPlayer.h;0;4
FILE_NAME_10=412;Make;0;EUTF-8;1;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FMakefile;0;4
FILE_NAME_11=2168;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FSystem.cpp;0;4
FILE_NAME_12=789;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FHumanPlayer.h;0;4
FILE_NAME_13=3100;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FHumanPlayer.cpp;0;4
FILE_NAME_14=1460;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FPlayer.cpp;0;4
FILE_NAME_15=3119;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FGame.cpp;0;4
FILE_NAME_16=571;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FGame.h;0;4
FILE_NAME_17=1886;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FCamera.cpp;0;4
FILE_NAME_18=281;C;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FCameraOld.c;0;4
FILE_NAME_19=768;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FSprite.h;0;4
FILE_NAME_20=2844;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FSprite.cpp;0;4
FILE_NAME_21=818;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FSystem.h;0;4
FILE_NAME_22=5650;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FDisplay.h;0;4
FILE_NAME_23=14993;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FGamebuino.cpp;0;4
FILE_NAME_24=0;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FGamebuino.h;0;4
FILE_NAME_25=3497;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FButtons.cpp;0;4
FILE_NAME_26=1135;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FButtons.h;0;4
FILE_NAME_27=3441;C;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2Fsettings.c;0;4
FILE_NAME_28=19868;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FDisplay.cpp;0;4
FILE_NAME_29=1853;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FBaseUnit.cpp;0;4
FILE_NAME_30=1182;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FBaseUnit.h;0;4
FILE_NAME_31=1373;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FUnit.h;0;4
FILE_NAME_32=2363;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FUnit.cpp;0;4
FILE_NAME_33=0;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FBarracksSpr.i;0;4
FILE_NAME_34=937;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FMouseSpr.i;0;4
FILE_NAME_35=225;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FPeasantSpr.i;0;4
FILE_NAME_36=0;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FSoldierSpr.i;0;4
FILE_NAME_37=0;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FTowerSpr.i;0;4
FILE_NAME_38=0;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FTownCentre.i;0;4
current_page=37
FILE_NAME_0=0;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2Fmain.cpp;0;4
FILE_NAME_1=3821;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FMenu.cpp;0;4
FILE_NAME_2=549;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FPad.cpp;0;4
FILE_NAME_3=136;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FCamera.h;0;4
FILE_NAME_4=299;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FMenu.h;0;4
FILE_NAME_5=611;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FPad.h;0;4
FILE_NAME_6=1102;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FPlayer.h;0;4
FILE_NAME_7=453;Make;0;EUTF-8;1;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FMakefile;0;4
FILE_NAME_8=2168;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FSystem.cpp;0;4
FILE_NAME_9=1739;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FHumanPlayer.h;0;4
FILE_NAME_10=1827;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FPlayer.cpp;0;4
FILE_NAME_11=2960;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FGame.cpp;0;4
FILE_NAME_12=571;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FGame.h;0;4
FILE_NAME_13=2317;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FCamera.cpp;0;4
FILE_NAME_14=1129;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FSprite.h;0;4
FILE_NAME_15=1743;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FSprite.cpp;0;4
FILE_NAME_16=575;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FSystem.h;0;4
FILE_NAME_17=2863;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FDisplay.h;0;4
FILE_NAME_18=7987;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FGamebuino.cpp;0;4
FILE_NAME_19=0;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FGamebuino.h;0;4
FILE_NAME_20=3497;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FButtons.cpp;0;4
FILE_NAME_21=1135;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FButtons.h;0;4
FILE_NAME_22=3441;C;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2Fsettings.c;0;4
FILE_NAME_23=19868;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FLibs%2Flibgamebuino%2FDisplay.cpp;0;4
FILE_NAME_24=1492;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FBaseUnit.cpp;0;4
FILE_NAME_25=1230;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FBaseUnit.h;0;4
FILE_NAME_26=1511;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FUnit.h;0;4
FILE_NAME_27=1197;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FUnit.cpp;0;4
FILE_NAME_28=0;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FBarracksSpr.i;0;4
FILE_NAME_29=937;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FMouseSpr.i;0;4
FILE_NAME_30=225;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FPeasantSpr.i;0;4
FILE_NAME_31=0;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FSoldierSpr.i;0;4
FILE_NAME_32=0;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FTowerSpr.i;0;4
FILE_NAME_33=0;None;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FTownCentre.i;0;4
FILE_NAME_34=2402;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FCursor.cpp;0;4
FILE_NAME_35=1300;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FCursor.h;0;4
FILE_NAME_36=3153;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FHumanPlayer.cpp;0;4
FILE_NAME_37=0;C++;0;EUTF-8;0;1;0;%2Fhome%2Fxavier%2FPocketEmpires%2Fsrc%2FHumanPlayerBtn.cpp;0;4
[VTE]
last_dir=/home/xavier/PocketEmpires/src

View File

@ -58,6 +58,38 @@ _y(0)
{
}
/*****************************************************************//**
*
* \brief This function draws a \ref Sprite object on the screen.
*
* \remarks If \ref Sprite object must be followed by a \ref Camera
* object, X and Y coordinates are automatically adjusted.
*
*********************************************************************/
void Sprite::draw(void)
{
gb.display.setColor(_colour, WHITE);
if (_cam != NULL)
{
const uint8_t x = _followCam ? _cam->getX(_x) : _x;
const uint8_t y = _followCam ? _cam->getY(_y) : _y;
if (_pu8SprData != NULL)
{
gb.display.drawBitmap(x, y, _pu8SprData);
}
else
{
/* Undefined sprite data. */
}
}
else
{
/* Error: uninitialized camera. */
}
}
/*****************************************************************//**
*
* \brief Reportedly, this function updates X/Y coordinates for
@ -76,20 +108,3 @@ void Sprite::setPos(const uint8_t x, const uint8_t y)
_x = x;
_y = y;
}
void Sprite::draw(void)
{
gb.display.setColor(_colour, WHITE);
if (_cam != NULL)
{
const uint8_t x = _followCam ? _cam->getX(_x) : _x;
const uint8_t y = _followCam ? _cam->getY(_y) : _y;
gb.display.drawBitmap(x, y, _pu8SprData);
}
else
{
/* Error: uninitialized camera. */
}
}

View File

@ -19,8 +19,6 @@
* Global Variables *
* **************************************/
/* Gamebuino object is only visible for
* and accessed by C++ source files. */
extern Gamebuino gb;
/* **************************************

View File

@ -99,8 +99,6 @@ void Unit::handler(void)
{
/* Execute base class handler. */
BaseUnit::handler();
drawHandler();
}
/*****************************************************************//**

2
Unit.h
View File

@ -44,9 +44,9 @@ class Unit : public BaseUnit
explicit Unit(const Unit::tUnitID eUnitID = UNIT_ID_NONE);
void create(const enum Unit::tUnitID eUnitID, const uint16_t x, const uint16_t y);
void handler(void);
void drawHandler(void);
private:
void drawHandler(void);
enum tUnitID _eUnitID;
};