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
|
/* **************************************
* Includes *
* **************************************/
#include "Unit.h"
#include "PeasantSpr.i"
#include "BarracksSpr.i"
#include "TownCentre.i"
/* **************************************
* Defines *
* **************************************/
/* **************************************
* Structs and enums *
* **************************************/
struct t_coordinates
{
int8_t x;
int8_t y;
};
/* **************************************
* Local prototypes *
* **************************************/
/* **************************************
* Local variables *
* **************************************/
/* Tables */
static uint8_t const UnitHPTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 25 ,
[BARRACKS] = 100,
[TOWN_CENTER] = 200 };
static uint8_t const UnitSpeedTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 1,
[BARRACKS] = 0,
[TOWN_CENTER] = 0 };
static const char* const UnitActionsTable_Level[MAX_ACTIONS] = { [ACTION_BUILD_BARRACKS] = "B.BARR",
[ACTION_ATTACK] = "ATTACK",
[ACTION_CREATE_PEASANT] = "C.PEAS."};
static uint8_t const UnitActionsTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = ((1 << ACTION_BUILD_BARRACKS) | (1 << ACTION_ATTACK)),
[BARRACKS] = (1 << ACTION_CREATE_PEASANT),
[TOWN_CENTER] = (1 << ACTION_CREATE_PEASANT) };
// **************
// Sprite tables
// **************
static TYPE_SPRITE UnitSprTable[MAX_UNITS_BUILDINGS];
static TYPE_SPRITE UnitWalkingShadowSprTable[MAX_UNITS_BUILDINGS];
static const struct t_coordinates UnitShadowOffsetTable[MAX_BUILDING_ID - FIRST_BUILDING_ID] = { [BARRACKS - FIRST_BUILDING_ID] = {.x = -6, .y = 0},
[TOWN_CENTER - FIRST_BUILDING_ID] = {.x = -3, .y = 0} };
void UnitInit(void)
{
UnitSprTable[PEASANT].Data = Peasant_SprData;
UnitSprTable[PEASANT].w = GfxGetWidthFromSpriteData(Peasant_SprData);
UnitSprTable[PEASANT].h = GfxGetHeightFromSpriteData(Peasant_SprData);
UnitSprTable[PEASANT].flip = 0;
UnitSprTable[PEASANT].rotation = 0;
UnitSprTable[PEASANT].color = GFX_BLACK;
UnitWalkingShadowSprTable[PEASANT].Data = Peasant_Walking_SprData;
UnitWalkingShadowSprTable[PEASANT].w = GfxGetWidthFromSpriteData(Peasant_Walking_SprData);
UnitWalkingShadowSprTable[PEASANT].h = GfxGetHeightFromSpriteData(Peasant_Walking_SprData);
UnitWalkingShadowSprTable[PEASANT].flip = 0;
UnitWalkingShadowSprTable[PEASANT].rotation = 0;
UnitWalkingShadowSprTable[PEASANT].color = GFX_BLACK;
UnitSprTable[BARRACKS].Data = BarracksSpr_Data;
UnitSprTable[BARRACKS].w = GfxGetWidthFromSpriteData(BarracksSpr_Data);
UnitSprTable[BARRACKS].h = GfxGetHeightFromSpriteData(BarracksSpr_Data);
UnitSprTable[BARRACKS].flip = 0;
UnitSprTable[BARRACKS].rotation = 0;
UnitSprTable[BARRACKS].color = GFX_BLACK;
UnitSprTable[TOWN_CENTER].Data = TownCentreSprData;
UnitSprTable[TOWN_CENTER].w = GfxGetWidthFromSpriteData(TownCentreSprData);
UnitSprTable[TOWN_CENTER].h = GfxGetHeightFromSpriteData(TownCentreSprData);
UnitSprTable[TOWN_CENTER].flip = 0;
UnitSprTable[TOWN_CENTER].rotation = 0;
UnitSprTable[TOWN_CENTER].color = GFX_BLACK;
UnitWalkingShadowSprTable[BARRACKS].Data = BarracksShadowSpr_Data;
UnitWalkingShadowSprTable[BARRACKS].w = GfxGetWidthFromSpriteData(BarracksShadowSpr_Data);
UnitWalkingShadowSprTable[BARRACKS].h = GfxGetHeightFromSpriteData(BarracksShadowSpr_Data);
UnitWalkingShadowSprTable[BARRACKS].flip = 0;
UnitWalkingShadowSprTable[BARRACKS].rotation = 0;
UnitWalkingShadowSprTable[BARRACKS].color = GFX_GRAY;
UnitWalkingShadowSprTable[TOWN_CENTER].Data = TownCentreShadowSprData;
UnitWalkingShadowSprTable[TOWN_CENTER].w = GfxGetWidthFromSpriteData(TownCentreShadowSprData);
UnitWalkingShadowSprTable[TOWN_CENTER].h = GfxGetHeightFromSpriteData(TownCentreShadowSprData);
UnitWalkingShadowSprTable[TOWN_CENTER].flip = 0;
UnitWalkingShadowSprTable[TOWN_CENTER].rotation = 0;
UnitWalkingShadowSprTable[TOWN_CENTER].color = GFX_GRAY;
}
void UnitDraw(TYPE_UNIT* ptrUnit, TYPE_CAMERA* ptrCamera, bool bHighlighted)
{
uint8_t id = ptrUnit->id;
TYPE_SPRITE* ptrSpr;
if (ptrUnit->alive == false)
{
return;
}
if (ptrUnit->building == false)
{
enum
{
WALK_FRAMES = 4
};
// ***************
// Units
// ***************
ptrSpr = ptrUnit->walking ? &UnitWalkingShadowSprTable[id] : &UnitSprTable[id];
ptrSpr->rotation = GFX_NOROT;
ptrSpr->flip = GFX_NOFLIP;
if (ptrUnit->walking == true)
{
if (++ptrUnit->walk_counter > WALK_FRAMES)
{
ptrUnit->walk_counter = 0;
ptrUnit->mirror = ptrUnit->mirror ? false : true;
}
}
switch (ptrUnit->dir)
{
case DIRECTION_UP:
ptrSpr->flip |= GFX_FLIPV;
if (ptrUnit->mirror == true)
{
ptrSpr->flip |= GFX_FLIPH;
}
break;
case DIRECTION_DOWN:
if (ptrUnit->mirror == true)
{
ptrSpr->flip |= GFX_FLIPH;
}
break;
case DIRECTION_LEFT:
ptrSpr->rotation = GFX_ROTCCW;
ptrSpr->flip |= GFX_FLIPH;
if (ptrUnit->mirror == true)
{
ptrSpr->flip |= GFX_FLIPV;
}
break;
case DIRECTION_RIGHT:
ptrSpr->rotation = GFX_ROTCCW;
if (ptrUnit->mirror == true)
{
ptrSpr->flip |= GFX_FLIPV;
}
break;
}
}
else
{
// *******************
// Buildings
// *******************
CameraApplyCoordinatesToSprite( ptrCamera,
&UnitWalkingShadowSprTable[id],
ptrUnit->x + UnitShadowOffsetTable[id - FIRST_BUILDING_ID].x,
ptrUnit->y + UnitShadowOffsetTable[id - FIRST_BUILDING_ID].y );
GfxDrawSprite(&UnitWalkingShadowSprTable[id]);
ptrSpr = &UnitSprTable[id];
}
CameraApplyCoordinatesToSprite( ptrCamera,
ptrSpr,
ptrUnit->x,
ptrUnit->y );
GfxDrawSprite(ptrSpr);
if ( (bHighlighted == true) || (ptrUnit->selected == true) )
{
TYPE_COLLISION_BLOCK cb = CameraApplyCoordinatesToCoordinates(ptrCamera, ptrUnit->x, ptrUnit->y);
int8_t colour = ptrUnit->selected? GFX_BLACK : GFX_GRAY;
uint8_t w = UnitGetWidthFromID(id);
uint8_t h = UnitGetHeightFromID(id);
if (ptrUnit->building == true)
{
GfxDrawRectangle(cb.x - (w >> 3), cb.y - (h >> 3), w + (w >> 2), h + (h >> 2), colour);
}
else
{
GfxDrawCircle(cb.x + (w >> 1), cb.y + (h >> 1), w, colour);
}
}
}
uint8_t UnitGetWidthFromID(uint8_t id)
{
return GfxGetWidthFromSpriteData(UnitSprTable[id].Data);
}
uint8_t UnitGetHeightFromID(uint8_t id)
{
return GfxGetHeightFromSpriteData(UnitSprTable[id].Data);
}
uint8_t UnitGetHpFromID(uint8_t id)
{
return UnitHPTable[id];
}
void UnitMoveTo(TYPE_UNIT* ptrUnit, uint16_t x, uint16_t y)
{
ptrUnit->target_x = x;
ptrUnit->target_y = y;
ptrUnit->walking = true;
}
void UnitAttackAccepted(TYPE_UNIT* ptrUnit)
{
ptrUnit->selecting_attack = true;
}
void UnitHandler(TYPE_UNIT* unitArray, size_t sz)
{
size_t i;
for (i = 0; i < sz; i++)
{
TYPE_UNIT* ptrUnit = &unitArray[i];
if (ptrUnit->alive == false)
{
continue;
}
bool bMoving = true;
if (ptrUnit->walking == true)
{
int8_t x_d = 0;
int8_t y_d = 0;
if ( (ptrUnit->x - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_x)
{
ptrUnit->dir = DIRECTION_LEFT;
x_d = -UnitSpeedTable[ptrUnit->id];
//~ ptrUnit->x -= UnitSpeedTable[ptrUnit->id];
}
else if ( (ptrUnit->x + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_x)
{
ptrUnit->dir = DIRECTION_RIGHT;
x_d = UnitSpeedTable[ptrUnit->id];
//~ ptrUnit->x += UnitSpeedTable[ptrUnit->id];
}
if ( (ptrUnit->y - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_y)
{
ptrUnit->dir = DIRECTION_UP;
y_d = -UnitSpeedTable[ptrUnit->id];
//~ ptrUnit->y -= UnitSpeedTable[ptrUnit->id];
}
else if ( (ptrUnit->y + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_y)
{
ptrUnit->dir = DIRECTION_DOWN;
y_d = UnitSpeedTable[ptrUnit->id];
//~ ptrUnit->y += UnitSpeedTable[ptrUnit->id];
}
else
{
x_d = ptrUnit->x - ptrUnit->target_x;
y_d = ptrUnit->y - ptrUnit->target_y;
bMoving = false;
}
ptrUnit->walking = bMoving;
if (ptrUnit->walking == true)
{
// If player is still walking, check collisions
// against all other active units.
size_t j;
for (j = 0; j < sz; j++)
{
TYPE_UNIT* ptrOtherUnit = &unitArray[j];
TYPE_COLLISION_BLOCK cu = { .x = ptrUnit->x + x_d,
.y = ptrUnit->y + y_d,
.w = UnitGetWidthFromID(ptrUnit->id),
.h = UnitGetHeightFromID(ptrUnit->id) };
TYPE_COLLISION_BLOCK ou;
if (ptrOtherUnit->alive == false)
{
continue;
}
if (j == i)
{
// Do not compare against itself!
continue;
}
ou.x = ptrOtherUnit->x;
ou.y = ptrOtherUnit->x;
ou.w = UnitGetWidthFromID(ptrOtherUnit->id);
ou.h = UnitGetHeightFromID(ptrOtherUnit->id);
if (SystemCollisionCheck(cu, ou) == true)
{
ptrUnit->walking = false;
break;
}
}
}
if (ptrUnit->walking == true)
{
// If no collision is detected, keep moving to the new position
ptrUnit->x += x_d;
ptrUnit->y += y_d;
}
}
}
}
uint8_t UnitGetAvailableActions(TYPE_UNIT* ptrUnit)
{
return UnitActionsTable[ptrUnit->id];
}
const char* UnitGetActionString(UNIT_ACTION action)
{
return UnitActionsTable_Level[action];
}
|