summaryrefslogtreecommitdiff
path: root/Unit.c
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2017-09-08 18:39:22 +0200
committerXaviDCR92 <xavi.dcr@gmail.com>2017-09-08 18:39:22 +0200
commit786dccd2bc0946d48b8a2758ef2c607678bc8dd9 (patch)
tree47952d77e5646a993671134b50c16f32ebe35b1c /Unit.c
parent72d350a37aa67936361ac8a374472b7e0227da61 (diff)
downloadpocketempires-786dccd2bc0946d48b8a2758ef2c607678bc8dd9.tar.gz
Removed Building and GameStructures modules. Restructured SW for Unit and Player, still a lot TODO
Diffstat (limited to 'Unit.c')
-rw-r--r--Unit.c401
1 files changed, 199 insertions, 202 deletions
diff --git a/Unit.c b/Unit.c
index e0da0e6..84b7931 100644
--- a/Unit.c
+++ b/Unit.c
@@ -1,174 +1,221 @@
/* **************************************
* Includes *
* **************************************/
-
+
#include "Unit.h"
#include "PeasantSpr.c"
-
+#include "BarracksSpr.c"
+
/* **************************************
* Defines *
* **************************************/
-
-#define MAX_ACTION_LEVELS 2
-
+
+#define MAX_ACTIONS 3
+
+/* **************************************
+* Structs and enums *
+* **************************************/
+
+struct t_coordinates
+{
+ int8_t x;
+ int8_t y;
+};
+
/* **************************************
* Local prototypes *
* **************************************/
-static void UnitIncreaseMenuLevel(void);
-static void UnitBuildAccept(TYPE_UNIT* ptrUnit);
-
+static void UnitBuildAccepted(TYPE_UNIT* ptrUnit);
+static void UnitAttackAccepted(TYPE_UNIT* ptrUnit);
+
/* **************************************
* Local variables *
* **************************************/
-static uint8_t menuLevel;
+/* Tables */
+static uint8_t const UnitHPTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 25 ,
+ [BARRACKS] = 100 };
+static uint8_t const UnitSpeedTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = 1 ,
+ [BARRACKS] = 0 };
-typedef struct
-{
- const char* str;
- void (*pAction)(TYPE_UNIT*);
-}TYPE_UNIT_ACTION;
+static TYPE_UNIT_ACTION const UnitActionsTable_Level0[MAX_ACTIONS] = { [ACTION_BUILD] = {"BUILD", &UnitBuildAccepted} ,
+ [ACTION_ATTACK] = {"ATTACK", &UnitAttackAccepted} ,
+ [ACTION_CREATE_UNIT] = {"CREATE", NULL} };
-typedef enum t_availableactions
-{
- ACTION_BUILD = 0,
- ACTION_ATTACK,
- MAX_ACTIONS
-}UNIT_AVAILABLE_ACTIONS;
+static uint8_t const UnitActionsTable[MAX_UNITS_BUILDINGS] = { [PEASANT] = ((1 << ACTION_BUILD) | (1 << ACTION_ATTACK)),
+ [BARRACKS] = (1 << ACTION_CREATE_UNIT) };
-/* Sprites */
-static TYPE_SPRITE PeasantSpr;
-static TYPE_SPRITE PeasantWalkingSpr;
+// **************
+// 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} };
-/* Tables */
-static uint8_t const UnitHPTable[] = { [PEASANT] = 25 };
-static uint8_t const UnitSpeedTable[] = { [PEASANT] = 1 };
+void UnitInit(void)
+{
+ enum
+ {
+ BARRACKS_SHADOW_OFFSET_X = -8,
+ BARRACKS_SHADOW_OFFSET_Y = 0
+ };
-static TYPE_UNIT_ACTION const UnitActionsTable_Level0[MAX_ACTIONS] = { [ACTION_BUILD] = {"Build", NULL},
- [ACTION_ATTACK] = {"Attack", NULL} };
-static TYPE_UNIT_ACTION const UnitActionsTable_Level1[MAX_ACTIONS] = { {"Barracks", &UnitBuildAccept} };
+ 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;
-static TYPE_UNIT_ACTION* const UnitActionsTable_LevelX[MAX_ACTION_LEVELS] = { &UnitActionsTable_Level0 ,
- &UnitActionsTable_Level1 };
+ 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;
-static TYPE_SPRITE * const UnitSprTable[] = {&PeasantSpr};
-static TYPE_SPRITE * const UnitWalkingSprTable[] = {&PeasantWalkingSpr};
+ 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;
-void UnitInit(void)
-{
- PeasantSpr.Data = Peasant_SprData;
- PeasantSpr.w = GfxGetWidthFromSpriteData(Peasant_SprData);
- PeasantSpr.h = GfxGetHeightFromSpriteData(Peasant_SprData);
- PeasantSpr.flip = 0;
- PeasantSpr.rotation = 0;
- PeasantSpr.color = GFX_BLACK;
-
- PeasantWalkingSpr.Data = Peasant_Walking_SprData;
- PeasantWalkingSpr.w = GfxGetWidthFromSpriteData(Peasant_Walking_SprData);
- PeasantWalkingSpr.h = GfxGetHeightFromSpriteData(Peasant_Walking_SprData);
- PeasantWalkingSpr.flip = 0;
- PeasantWalkingSpr.rotation = 0;
- PeasantWalkingSpr.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;
}
-void UnitDraw(TYPE_CAMERA * ptrCamera, TYPE_UNIT * ptrUnit, bool bSelected)
+void UnitDraw(TYPE_UNIT* ptrUnit, TYPE_CAMERA* ptrCamera, bool bHighlighted)
{
uint8_t id = ptrUnit->id;
TYPE_SPRITE * ptrSpr;
- static uint8_t walk_counter = 0;
- static bool mirror = false;
-
- if(ptrUnit->alive == false)
+
+ if (ptrUnit->alive == false)
{
return;
}
-
- ptrSpr = ptrUnit->walking ? UnitWalkingSprTable[id] : UnitSprTable[id];
-
- ptrSpr->rotation = ptrUnit->dir ? ROTCCW : NOROT;
- ptrSpr->flip = GFX_NOFLIP;
-
- if(ptrUnit->walking == true)
- {
- if(mirror == true)
- {
- if(ptrUnit->dir == false)
- {
- ptrSpr->flip = GFX_FLIPH;
- }
- else
- {
- ptrSpr->flip = GFX_FLIPV;
- }
- }
- else
- {
- ptrSpr->flip = NOROT;
- }
- }
-
- if(ptrUnit->mirror == false)
- {
- if(ptrUnit->dir == false)
- {
- ptrSpr->flip |= GFX_FLIPV;
- }
- else
- {
- ptrSpr->flip |= GFX_FLIPH;
- }
- }
-
+
+ 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;
+
+ default:
+ // Invalid direction
+ 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( (bSelected == true) && (ptrUnit->selected == false) )
- {
- TYPE_COLLISION_BLOCK cb;
-
- cb = CameraApplyCoordinatesToCoordinates(ptrCamera, ptrUnit->x, ptrUnit->y);
-
- GfxDrawCircle(cb.x + 3, cb.y + 3, UnitGetWidthFromID(ptrUnit->id), GFX_GRAY);
- }
- else if(ptrUnit->selected == true)
- {
- TYPE_COLLISION_BLOCK cb;
-
- cb = CameraApplyCoordinatesToCoordinates(ptrCamera, ptrUnit->x, ptrUnit->y);
-
- GfxDrawCircle(cb.x + 3, cb.y + 3, UnitGetWidthFromID(ptrUnit->id), GFX_BLACK);
- }
-
- /*char str[2];
-
- snprintf(str, 2, "%u", walk_counter);
-
- GfxPrintText(str, 60, 20);
-
- snprintf(str, 2, "%u", mirror);
-
- GfxPrintText(str, 60, 30);*/
-
- if(++walk_counter > 4)
- {
- walk_counter = 0;
- mirror = mirror ? false : true;
- }
+
+ 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);
+ return GfxGetWidthFromSpriteData(UnitSprTable[id].Data);
}
uint8_t UnitGetHeightFromID(uint8_t id)
{
- return GfxGetHeightFromSpriteData(UnitSprTable[id]->Data);
+ return GfxGetHeightFromSpriteData(UnitSprTable[id].Data);
}
uint8_t UnitGetHpFromID(uint8_t id)
@@ -176,120 +223,70 @@ uint8_t UnitGetHpFromID(uint8_t id)
return UnitHPTable[id];
}
-void UnitMoveTo(TYPE_UNIT * ptrUnit, uint16_t x, uint16_t y)
+void UnitMoveTo(TYPE_UNIT* ptrUnit, uint16_t x, uint16_t y)
{
ptrUnit->target_x = x;
ptrUnit->target_y = y;
ptrUnit->walking = true;
}
-void UnitHandler(TYPE_UNIT * ptrUnit)
+void UnitAttackAccepted(TYPE_UNIT* ptrUnit)
+{
+ ptrUnit->selecting_attack = true;
+}
+
+void UnitHandler(TYPE_UNIT* ptrUnit)
{
bool bMoving = false;
-
- if(ptrUnit->walking == true)
+
+ if (ptrUnit->walking == true)
{
- if( (ptrUnit->x - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_x)
+ if ( (ptrUnit->x - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_x)
{
+ ptrUnit->dir = DIRECTION_LEFT;
ptrUnit->x -= UnitSpeedTable[ptrUnit->id];
- //ptrUnit->rotation = GFX_ROTCCW;
bMoving = true;
- ptrUnit->dir = true;
- ptrUnit->mirror = false;
}
- else if( (ptrUnit->x + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_x)
+ else if ( (ptrUnit->x + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_x)
{
+ ptrUnit->dir = DIRECTION_RIGHT;
ptrUnit->x += UnitSpeedTable[ptrUnit->id];
- //ptrUnit->rotation = GFX_ROTCCW;
bMoving = true;
- ptrUnit->dir = true;
- ptrUnit->mirror = true;
}
-
- if( (ptrUnit->y - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_y)
+
+ if ( (ptrUnit->y - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_y)
{
+ ptrUnit->dir = DIRECTION_UP;
ptrUnit->y -= UnitSpeedTable[ptrUnit->id];
- //ptrUnit->rotation = 0;
bMoving = true;
- ptrUnit->dir = false;
- ptrUnit->mirror = false;
}
- else if( (ptrUnit->y + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_y)
+ else if ( (ptrUnit->y + UnitSpeedTable[ptrUnit->id]) < ptrUnit->target_y)
{
+ ptrUnit->dir = DIRECTION_DOWN;
ptrUnit->y += UnitSpeedTable[ptrUnit->id];
- //ptrUnit->rotation = 0;
bMoving = true;
- ptrUnit->dir = false;
- ptrUnit->mirror = true;
}
-
- ptrUnit->walking = bMoving;
- }
-}
-const char* UnitSelectedOptions(TYPE_UNIT* ptrUnit)
-{
- const TYPE_UNIT_ACTION* tUnitAction = NULL;
-
- switch(menuLevel)
- {
- case 0:
- tUnitAction = &UnitActionsTable_Level0[ptrUnit->id];
- break;
- case 1:
- tUnitAction = &UnitActionsTable_Level1[ptrUnit->id];
- break;
-
- default:
- return NULL;
- break;
+ ptrUnit->walking = bMoving;
}
-
- return tUnitAction->str;
}
void UnitAcceptAction(TYPE_UNIT* ptrUnit)
{
- const TYPE_UNIT_ACTION* tUnitAction = NULL;
-
- switch(menuLevel)
- {
- case 0:
- tUnitAction = &UnitActionsTable_Level0[ptrUnit->id];
- break;
- case 1:
- tUnitAction = &UnitActionsTable_Level1[ptrUnit->id];
- break;
-
- default:
- return;
- break;
- }
-
- if(tUnitAction->pAction == NULL)
- {
- UnitIncreaseMenuLevel();
- }
- else
- {
- tUnitAction->pAction(ptrUnit);
- }
+
}
-void UnitIncreaseMenuLevel(void)
+uint8_t UnitGetAvailableActions(TYPE_UNIT* ptrUnit)
{
- if(menuLevel < 1)
- {
- menuLevel++;
- }
+ return UnitActionsTable[ptrUnit->id];
}
-void UnitBuildAccept(TYPE_UNIT* ptrUnit)
+void UnitBuildAccepted(TYPE_UNIT* ptrUnit)
{
-
+
}
-void UnitResetMenuLevel(void)
+const char* UnitGetActionString(UNIT_ACTION action)
{
- menuLevel = 0;
+ return UnitActionsTable_Level0[action].str;
}