summaryrefslogtreecommitdiff
path: root/HumanPlayer.cpp
blob: e83d4e646cc981eec58d0bb5982b3bf2ea24ec56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* *******************************************************************
 *  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, const Camera& cam) :
Player(strPlayerName),
_cam(cam)
{
    _unitsMap[0].create(Unit::UNIT_ID_PEASANT, 16, 16);
}

/*****************************************************************//**
 *
 * \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]    = &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)
{
}

/*****************************************************************//**
 *
 * \brief   This function draws all units and player UI.
 *
 *********************************************************************/
void HumanPlayer::drawHandler(void)
{
}