summaryrefslogtreecommitdiff
path: root/Unit.c
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2017-11-05 05:38:31 +0100
committerXaviDCR92 <xavi.dcr@gmail.com>2017-11-05 05:38:31 +0100
commit8fba2176bc34aa7e507f0b9d983427bb5e522e17 (patch)
treed5e1c7d5560f30581fc5a1957121fc0a198b62f7 /Unit.c
parent2cf2d608af862e812e7fd3ac580f869141a96fa7 (diff)
downloadpocketempires-8fba2176bc34aa7e507f0b9d983427bb5e522e17.tar.gz
* Modified shadows for Town centre.
* Bugfixes in unit movement and collision detection. Now it should work fine. * Added include guards to all *.i files. * Other minor changes
Diffstat (limited to 'Unit.c')
-rw-r--r--Unit.c97
1 files changed, 66 insertions, 31 deletions
diff --git a/Unit.c b/Unit.c
index 26beae0..9147306 100644
--- a/Unit.c
+++ b/Unit.c
@@ -3,7 +3,12 @@
* **************************************/
#include "Unit.h"
+
+/* Units sprite data */
#include "PeasantSpr.i"
+#include "SoldierSpr.i"
+
+/* Buildings sprite data */
#include "BarracksSpr.i"
#include "TownCentre.i"
@@ -30,21 +35,31 @@ struct t_coordinates
* **************************************/
/* Tables */
-static uint8_t const UnitHPTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 25 ,
- [BARRACKS] = 100,
- [TOWN_CENTER] = 200 };
+static uint8_t const UnitHPTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 25,
+ [SOLDIER] = 80,
+ [BARRACKS] = 100,
+ [TOWN_CENTER] = 200 };
+
+static TYPE_RESOURCES const UnitResourcesTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = {.Wood = 0, .Gold = 0, .Food = 50},
+ [SOLDIER] = {.Wood = 25, .Gold = 10, .Food = 50},
+ [BARRACKS] = {.Wood = 100, .Gold = 0, .Food = 0},
+ [TOWN_CENTER] = {.Wood = 200, .Gold = 0, .Food = 0} };
static uint8_t const UnitSpeedTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 1,
- [BARRACKS] = 0,
- [TOWN_CENTER] = 0 };
+ [SOLDIER] = 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 const char* const UnitActionsTable_Level[MAX_ACTIONS] = { [ACTION_BUILD_BARRACKS] = "B.BARR",
+ [ACTION_ATTACK] = "ATTACK",
+ [ACTION_CREATE_PEASANT] = "C.PEAS.",
+ [ACTION_CREATE_SOLDIER] = "C.SLDR.",
+ [ACTION_BUILD_TOWER_CENTER] = "C.TWNC."};
-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) };
+static uint8_t const UnitActionsTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = ((1 << ACTION_BUILD_BARRACKS) | (1 << ACTION_BUILD_TOWER_CENTER) | (1 << ACTION_ATTACK)),
+ [SOLDIER] = (1 << ACTION_ATTACK),
+ [BARRACKS] = (1 << ACTION_CREATE_SOLDIER),
+ [TOWN_CENTER] = (1 << ACTION_CREATE_PEASANT) };
// **************
// Sprite tables
@@ -56,6 +71,10 @@ static const struct t_coordinates UnitShadowOffsetTable[MAX_BUILDING_ID - FIRST_
void UnitInit(void)
{
+ /* ***********************************
+ * Unit sprite data init
+ * ***********************************/
+
UnitSprTable[PEASANT].Data = Peasant_SprData;
UnitSprTable[PEASANT].w = GfxGetWidthFromSpriteData(Peasant_SprData);
UnitSprTable[PEASANT].h = GfxGetHeightFromSpriteData(Peasant_SprData);
@@ -63,6 +82,13 @@ void UnitInit(void)
UnitSprTable[PEASANT].rotation = 0;
UnitSprTable[PEASANT].color = GFX_BLACK;
+ UnitSprTable[SOLDIER].Data = SoldierSprData;
+ UnitSprTable[SOLDIER].w = GfxGetWidthFromSpriteData(SoldierSprData);
+ UnitSprTable[SOLDIER].h = GfxGetHeightFromSpriteData(SoldierSprData);
+ UnitSprTable[SOLDIER].flip = 0;
+ UnitSprTable[SOLDIER].rotation = 0;
+ UnitSprTable[SOLDIER].color = GFX_BLACK;
+
UnitWalkingShadowSprTable[PEASANT].Data = Peasant_Walking_SprData;
UnitWalkingShadowSprTable[PEASANT].w = GfxGetWidthFromSpriteData(Peasant_Walking_SprData);
UnitWalkingShadowSprTable[PEASANT].h = GfxGetHeightFromSpriteData(Peasant_Walking_SprData);
@@ -70,6 +96,17 @@ void UnitInit(void)
UnitWalkingShadowSprTable[PEASANT].rotation = 0;
UnitWalkingShadowSprTable[PEASANT].color = GFX_BLACK;
+ UnitWalkingShadowSprTable[SOLDIER].Data = SoldierSprData_Walking;
+ UnitWalkingShadowSprTable[SOLDIER].w = GfxGetWidthFromSpriteData(SoldierSprData_Walking);
+ UnitWalkingShadowSprTable[SOLDIER].h = GfxGetHeightFromSpriteData(SoldierSprData_Walking);
+ UnitWalkingShadowSprTable[SOLDIER].flip = 0;
+ UnitWalkingShadowSprTable[SOLDIER].rotation = 0;
+ UnitWalkingShadowSprTable[SOLDIER].color = GFX_BLACK;
+
+ /* ***********************************
+ * Buildings sprite data init
+ * ***********************************/
+
UnitSprTable[BARRACKS].Data = BarracksSpr_Data;
UnitSprTable[BARRACKS].w = GfxGetWidthFromSpriteData(BarracksSpr_Data);
UnitSprTable[BARRACKS].h = GfxGetHeightFromSpriteData(BarracksSpr_Data);
@@ -213,21 +250,26 @@ void UnitDraw(TYPE_UNIT* ptrUnit, TYPE_CAMERA* ptrCamera, bool bHighlighted)
}
}
-uint8_t UnitGetWidthFromID(uint8_t id)
+uint8_t UnitGetWidthFromID(TYPE_UNIT_ID id)
{
return GfxGetWidthFromSpriteData(UnitSprTable[id].Data);
}
-uint8_t UnitGetHeightFromID(uint8_t id)
+uint8_t UnitGetHeightFromID(TYPE_UNIT_ID id)
{
return GfxGetHeightFromSpriteData(UnitSprTable[id].Data);
}
-uint8_t UnitGetHpFromID(uint8_t id)
+uint8_t UnitGetHpFromID(TYPE_UNIT_ID id)
{
return UnitHPTable[id];
}
+TYPE_RESOURCES UnitNeededResourcesFromID(TYPE_UNIT_ID id)
+{
+ return UnitResourcesTable[id];
+}
+
void UnitMoveTo(TYPE_UNIT* ptrUnit, uint16_t x, uint16_t y)
{
ptrUnit->target_x = x;
@@ -264,37 +306,28 @@ void UnitHandler(TYPE_UNIT* unitArray, size_t sz)
if ( (ptrUnit->x - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_x)
{
ptrUnit->dir = DIRECTION_LEFT;
- x_d = -UnitSpeedTable[ptrUnit->id];
- //~ ptrUnit->x -= UnitSpeedTable[ptrUnit->id];
+ x_d = (int8_t)-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];
+ x_d = (int8_t)UnitSpeedTable[ptrUnit->id];
}
-
- if ( (ptrUnit->y - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_y)
+ else if ( (ptrUnit->y - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_y)
{
ptrUnit->dir = DIRECTION_UP;
- y_d = -UnitSpeedTable[ptrUnit->id];
- //~ ptrUnit->y -= UnitSpeedTable[ptrUnit->id];
+ y_d = (int8_t)-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];
+ y_d = (int8_t)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
@@ -324,18 +357,20 @@ void UnitHandler(TYPE_UNIT* unitArray, size_t sz)
ou.x = ptrOtherUnit->x;
- ou.y = ptrOtherUnit->x;
+ ou.y = ptrOtherUnit->y;
ou.w = UnitGetWidthFromID(ptrOtherUnit->id);
ou.h = UnitGetHeightFromID(ptrOtherUnit->id);
- if (SystemCollisionCheck(cu, ou) == true)
+ if (SystemCollisionCheck(&cu, &ou) == true)
{
- ptrUnit->walking = false;
+ bMoving = false;
break;
}
}
}
+ ptrUnit->walking = bMoving;
+
if (ptrUnit->walking == true)
{
// If no collision is detected, keep moving to the new position