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
|
/* *******************************************************************
* Includes
* ******************************************************************/
#include "BaseUnit.h"
#include "Unit.h"
#include "Sprite.h"
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
/* *******************************************************************
* Defines
* ******************************************************************/
/* *******************************************************************
* Types definition
* ******************************************************************/
/* *******************************************************************
* Global variables definition
* ******************************************************************/
/* *******************************************************************
* Local variables definition
* ******************************************************************/
/*****************************************************************//**
*
* \brief Bitmap data for idle UNIT_ID_PEASANT.
*
*********************************************************************/
static const PROGMEM uint8_t au8PeasantSprData[] =
{
8,
8,
0x00,
0x3C,
0x42,
0x99,
0xA5,
0x66,
0x18,
0x00
};
/* *******************************************************************
* Local prototypes declaration
* ******************************************************************/
/* *******************************************************************
* Functions definition
* ******************************************************************/
/*****************************************************************//**
*
* \brief Constructor for Unit class.
*
*********************************************************************/
Unit::Unit(const enum Unit::tUnitID eUnitID) :
BaseUnit(),
_eUnitID(eUnitID),
_eState(UNIT_STATE_IDLE),
_target_x(0),
_target_y(0)
{
}
/*****************************************************************//**
*
* \brief Creates a Unit instance by setting default parameters
* and X/Y coordinates.
*
*********************************************************************/
void Unit::create(const enum Unit::tUnitID eUnitID, const uint16_t x, const uint16_t y)
{
/* Execute base class function first. */
BaseUnit::create(x, y);
/* Assign new ID to selected Unit. */
_eUnitID = eUnitID;
/* This table relates all available
* unit IDs against a bitmap data table. */
const uint8_t au8HpData[MAX_UNIT_ID] PROGMEM =
{
[UNIT_ID_NONE] = 0,
[UNIT_ID_PEASANT] = 25,
[UNIT_ID_SWORDMAN] = 35
};
/* Assign health according to unit ID. */
_hp = au8HpData[eUnitID];
}
/*****************************************************************//**
*
* \brief This function sets a position target for a \ref Unit
* object.
*
*********************************************************************/
void Unit::moveTo(const uint16_t x, const uint16_t y)
{
_target_x = x;
_target_y = y;
_eState = UNIT_STATE_MOVING;
}
/*****************************************************************//**
*
* \brief Periodical handler for Unit class.
*
*********************************************************************/
void Unit::handler(void)
{
/* Execute base class handler. */
BaseUnit::handler();
switch (_eState)
{
case UNIT_STATE_MOVING:
if (_x < _target_x)
{
_x += 1;
}
else if (_x > _target_x)
{
_x -= 1;
}
if (_y < _target_y)
{
_y += 1;
}
else if (_y > _target_y)
{
_y -= 1;
}
break;
case UNIT_STATE_IDLE:
default:
break;
}
}
/*****************************************************************//**
*
* \brief This function checks whether unit is inside screen
* boundaries and draws its associated bitmap.
*
*********************************************************************/
void Unit::drawHandler(void)
{
if (_eUnitID < MAX_UNIT_ID)
{
/* This table relates all available
* unit IDs against a bitmap data table. */
const uint8_t* const apu8UnitSpriteDataTable[MAX_UNIT_ID] PROGMEM =
{
[UNIT_ID_NONE] = NULL,
[UNIT_ID_PEASANT] = au8PeasantSprData,
[UNIT_ID_SWORDMAN] = NULL
};
/* Point to appropiate bitmap data given unit ID. */
const uint8_t* const pu8UnitSpriteData = apu8UnitSpriteDataTable[_eUnitID];
Sprite spr(pu8UnitSpriteData);
spr.setPos(_x - (Sprite::getWidth(pu8UnitSpriteData) >> 1), _y - (Sprite::getHeight(pu8UnitSpriteData) >> 1));
spr.draw();
BaseUnit::drawHandler(pu8UnitSpriteData);
}
else
{
/* Unit has an invalid ID. Exit. */
}
}
|