summaryrefslogtreecommitdiff
path: root/Unit.c
diff options
context:
space:
mode:
Diffstat (limited to 'Unit.c')
-rw-r--r--Unit.c285
1 files changed, 285 insertions, 0 deletions
diff --git a/Unit.c b/Unit.c
new file mode 100644
index 0000000..b9c3040
--- /dev/null
+++ b/Unit.c
@@ -0,0 +1,285 @@
+/* **************************************
+ * Includes *
+ * **************************************/
+
+#include "Unit.h"
+#include "PeasantSpr.c"
+
+/* **************************************
+ * Defines *
+ * **************************************/
+
+/* **************************************
+* Local prototypes *
+* **************************************/
+
+static void UnitIncreaseMenuLevel(void);
+static void UnitBuildAccept(TYPE_UNIT* ptrUnit);
+
+/* **************************************
+ * Local variables *
+ * **************************************/
+
+static uint8_t menuLevel;
+
+typedef struct
+{
+ const char* str;
+ void (*pAction)(TYPE_UNIT*);
+}TYPE_UNIT_ACTION;
+
+/* Sprites */
+static TYPE_SPRITE PeasantSpr;
+static TYPE_SPRITE PeasantWalkingSpr;
+
+/* Tables */
+static uint8_t const UnitHPTable[] = { 25 };
+static uint8_t const UnitSpeedTable[] = { 1 };
+
+static TYPE_UNIT_ACTION const UnitActionsTable_Level0[] = { {"Build", NULL} };
+static TYPE_UNIT_ACTION const UnitActionsTable_Level1[] = { {"Barracks", &UnitBuildAccept} };
+
+static TYPE_SPRITE * const UnitSprTable[] = {&PeasantSpr};
+static TYPE_SPRITE * const UnitWalkingSprTable[] = {&PeasantWalkingSpr};
+
+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;
+}
+
+void UnitDraw(TYPE_CAMERA * ptrCamera, TYPE_UNIT * ptrUnit, bool bSelected)
+{
+ uint8_t id = ptrUnit->id;
+ TYPE_SPRITE * ptrSpr;
+ static uint8_t walk_counter = 0;
+ static bool mirror = 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;
+ }
+ }
+
+ 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;
+ }
+}
+
+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 UnitHandler(TYPE_UNIT * ptrUnit)
+{
+ bool bMoving = false;
+
+ if(ptrUnit->walking == true)
+ {
+ if( (ptrUnit->x - UnitSpeedTable[ptrUnit->id]) > ptrUnit->target_x)
+ {
+ 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)
+ {
+ 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)
+ {
+ 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)
+ {
+ ptrUnit->y += UnitSpeedTable[ptrUnit->id];
+ //ptrUnit->rotation = 0;
+ bMoving = true;
+ ptrUnit->dir = false;
+ ptrUnit->mirror = true;
+ }
+
+ if(bMoving == false)
+ {
+ ptrUnit->walking = false;
+ }
+ }
+}
+
+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;
+ }
+
+ 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 NULL;
+ break;
+ }
+
+ if(tUnitAction->pAction == NULL)
+ {
+ UnitIncreaseMenuLevel();
+ }
+ else
+ {
+ tUnitAction->pAction(ptrUnit);
+ }
+}
+
+void UnitIncreaseMenuLevel(void)
+{
+ if(menuLevel < 1)
+ {
+ menuLevel++;
+ }
+}
+
+void UnitBuildAccept(TYPE_UNIT* ptrUnit)
+{
+
+}
+
+void UnitResetMenuLevel(void)
+{
+ menuLevel = 0;
+}