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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/* *******************************************************************
* 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 if (gb.buttons.released(static_cast<uint8_t>(szBtn)))
{
/* 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,
[BTN_B] = &HumanPlayer::onBBtnReleased
};
/* 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)
{
enum
{
/* Number of frames that B button must
* be pressed in order to cancel unit selection. */
CANCEL_SELECTION_FRAMES = 10
};
if (++_BBtnFrames >= CANCEL_SELECTION_FRAMES)
{
switch (_eState)
{
case PLAYER_STATE_UNIT_SELECTED:
{
/* Iterator that will be used
* to move along _unitsMap. */
size_t i;
for (i = 0; i < (sizeof (_unitsMap) / sizeof (_unitsMap[0])); i++)
{
Unit& u = _unitsMap[i];
/* Reset selected flags. */
u.setSelected(false);
}
/* Reset to default state. */
_eState = PLAYER_STATE_IDLE;
}
/* Fall through. */
case PLAYER_STATE_IDLE:
/* Fall through. */
default:
/* Reset pressed B button frames counter. */
_BBtnFrames = 0;
/* Undefined player state. Exit. */
break;
}
}
}
/*****************************************************************//**
*
* \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. */
}
}
/* Fall through. */
case PLAYER_STATE_IDLE:
/* Fall through. */
default:
/* Undefined player state. Exit. */
/* Reset pressed A button frames counter. */
_ABtnFrames = 0;
break;
}
}
/*****************************************************************//**
*
* \brief Event handler executed when human player releases
* B button.
*
*********************************************************************/
void HumanPlayer::onBBtnReleased(void)
{
switch (_eState)
{
case PLAYER_STATE_UNIT_SELECTED:
{
/* Iterator that will be used
* to move along _unitsMap. */
size_t i;
for (i = 0; i < (sizeof (_unitsMap) / sizeof (_unitsMap[0])); i++)
{
Unit& u = _unitsMap[i];
if (u.isSelected())
{
/* Get cursor coordinates. */
const uint16_t x = _cursor.getX();
const uint16_t y = _cursor.getY();
/* Move unit to cursor position. */
u.moveTo(x, y);
}
else
{
/* Unit is not selected by the player. Continue. */
}
}
}
/* Fall through.*/
case PLAYER_STATE_IDLE:
/* Fall through. */
default:
/* Undefined player state. Exit. */
/* Reset pressed B button frames counter. */
_BBtnFrames = 0;
break;
}
}
|