summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2017-08-26 11:13:20 +0200
committerXaviDCR92 <xavi.dcr@gmail.com>2017-08-26 11:13:20 +0200
commit54ea724c5d2f3a2c5bdf3cfe6f6807d43a5ae9d7 (patch)
tree0cc7c34c790799cd1c29a186f7ab031b9465b970
parent6628e7de58425b3e93da9ae6fcb7347137d96096 (diff)
downloadpocketempires-54ea724c5d2f3a2c5bdf3cfe6f6807d43a5ae9d7.tar.gz
+ Added MouseSpr and Unit modules.
* Unit actions, building actions.
-rw-r--r--GameStructures.h1
-rw-r--r--MouseSpr.c13
-rw-r--r--Player.cpp118
-rw-r--r--Player.h5
-rw-r--r--Unit.c285
-rw-r--r--Unit.h50
6 files changed, 453 insertions, 19 deletions
diff --git a/GameStructures.h b/GameStructures.h
index 058d1d6..72958a3 100644
--- a/GameStructures.h
+++ b/GameStructures.h
@@ -41,6 +41,7 @@ typedef struct t_Unit
uint8_t hp;
uint8_t id;
bool dir; // False = up-down; true = left-right
+ bool mirror; // True = down or left; true = up or right
bool walking;
bool alive;
bool selected;
diff --git a/MouseSpr.c b/MouseSpr.c
new file mode 100644
index 0000000..ac40958
--- /dev/null
+++ b/MouseSpr.c
@@ -0,0 +1,13 @@
+const uint8_t PROGMEM MouseSprData[] =
+{
+ 8,8, //width and height
+ B11111100,
+ B10000100,
+ B10001000,
+ B10000100,
+ B10100010,
+ B11010001,
+ B00001010,
+ B00000100,
+
+};
diff --git a/Player.cpp b/Player.cpp
index 95f23e7..39886a9 100644
--- a/Player.cpp
+++ b/Player.cpp
@@ -9,6 +9,8 @@
* **************************************/
#define CANCEL_SELECTION_NO_FRAMES 5
+#define ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES 5
+#define MAX_SELECTION_DIST 400
/* **************************************
* Local variables *
@@ -133,6 +135,11 @@ void Player::DrawHandler(void)
UnitSelectedOptions(u);
}
}
+
+ if(progress_bar != 0)
+ {
+ GfxDrawRectangle(X_SCREEN_RESOLUTION - 16, Y_SCREEN_RESOLUTION - 4, progress_bar, 2, GFX_BLACK);
+ }
}
bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
@@ -264,7 +271,7 @@ void Player::UnitBuildingSelection(void)
{
TYPE_UNIT * u = &units[i];
- if(u->alive == false)
+ if( (u->alive == false) || (u->selected == true) )
{
continue;
}
@@ -291,7 +298,7 @@ void Player::UnitBuildingSelection(void)
{
TYPE_BUILDING * b = &buildings[i];
- if(b->built == false)
+ if( (b->built == false) || (b->selected == true) )
{
continue;
}
@@ -322,9 +329,9 @@ void Player::UnitBuildingSelection(void)
selectedBuilding = NULL;
}
- if( (nearest_unit_dist > 400)
+ if( (nearest_unit_dist > MAX_SELECTION_DIST)
&&
- (nearest_building_dist > 400) )
+ (nearest_building_dist > MAX_SELECTION_DIST) )
{
selectedUnit = NULL;
selectedBuilding = NULL;
@@ -341,20 +348,68 @@ void Player::UnitBuildingSelection(void)
GfxPrintText(buf, X_SCREEN_RESOLUTION - 32, 24);*/
}
-void Player::Handler(void)
+void Player::BuildingUnitActions(void)
{
- static bool bCancelSelection = false;
+ const char* pActionStr = NULL;
+
+ for(uint8_t i = 0; i < PLAYER_MAX_BUILDINGS; i++)
+ {
+ if(buildings[i].selected == true)
+ {
+ BuildingSelectedOptions(selectedBuilding);
+ break;
+ }
+ }
+
+ if(pActionStr == NULL)
+ {
+ for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++)
+ {
+ if(units[i].selected == true)
+ {
+ pActionStr = UnitSelectedOptions(selectedUnit);
+ break;
+ }
+ }
+ }
+ if(pActionStr != NULL)
+ {
+ GfxPrintTextFont(pActionStr, font3x3, 48, Y_SCREEN_RESOLUTION - 4);
+ }
+}
+
+void Player::Handler(void)
+{
CameraHandler(&Camera);
UnitBuildingSelection();
- for(int i = 0; i < PLAYER_MAX_UNITS; i++)
+ for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++)
{
TYPE_UNIT * ptrUnit = &units[i];
UnitHandler(ptrUnit);
}
+
+ ButtonHandler();
+
+ BuildingUnitActions();
+
+ GfxShowResources(&Resources);
+}
+
+void Player::ButtonHandler(void)
+{
+ static bool bCancelSelection = false;
+
+ if(PadButtonPressed(PAD_A) == true)
+ {
+ if(progress_bar < ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES)
+ {
+ progress_bar++;
+ }
+ }
if(PadButtonReleased(PAD_A) == true)
{
@@ -374,7 +429,7 @@ void Player::Handler(void)
GfxPrintText_Flash(F("Building built!\0"));
}*/
- for(int i = 0; i < PLAYER_MAX_UNITS; i++)
+ for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++)
{
TYPE_UNIT * u = &units[i];
@@ -384,7 +439,7 @@ void Player::Handler(void)
}
}
- for(int i = 0; i < PLAYER_MAX_BUILDINGS; i++)
+ for(uint8_t i = 0; i < PLAYER_MAX_BUILDINGS; i++)
{
TYPE_BUILDING * b = &buildings[i];
@@ -394,18 +449,31 @@ void Player::Handler(void)
}
}
}
+ else if(PadButtonPressedFrames(PAD_A, ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES) == true)
+ {
+ for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++)
+ {
+ TYPE_UNIT* u = &units[i];
+
+ if(u->selected == true)
+ {
+ UnitAcceptAction(u);
+ break;
+ }
+ }
+ }
else if( (PadButtonPressedFrames(PAD_B, CANCEL_SELECTION_NO_FRAMES) == true)
&&
(bCancelSelection == false) )
{
- for(int i = 0; i < PLAYER_MAX_UNITS; i++)
+ for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++)
{
TYPE_UNIT * u = &units[i];
u->selected = false;
}
- for(int i = 0; i < PLAYER_MAX_BUILDINGS; i++)
+ for(uint8_t i = 0; i < PLAYER_MAX_BUILDINGS; i++)
{
TYPE_BUILDING * b = &buildings[i];
@@ -416,30 +484,42 @@ void Player::Handler(void)
selectedBuilding = NULL;
bCancelSelection = true;
+ showUnitBuildingOptions = true;
+ }
+ else
+ {
+ // Button A is not pressed. Reset progress bar.
+ progress_bar = 0;
}
if( (PadButtonReleased(PAD_B) == true)
&&
(bCancelSelection == false) )
{
- TYPE_COLLISION_BLOCK cursor = GetCursorPos();
-
- for(int i = 0; i < PLAYER_MAX_UNITS; i++)
+ if(showUnitBuildingOptions == false)
{
- TYPE_UNIT * u = &units[i];
+ TYPE_COLLISION_BLOCK cursor = GetCursorPos();
- if(u->selected == true)
+ for(uint8_t i = 0; i < PLAYER_MAX_UNITS; i++)
{
- UnitMoveTo(u, cursor.x, cursor.y);
+ TYPE_UNIT * u = &units[i];
+
+ if(u->selected == true)
+ {
+ UnitMoveTo(u, cursor.x, cursor.y);
+ }
}
}
+ else
+ {
+ showUnitBuildingOptions = false;
+ }
}
else if( (PadButtonReleased(PAD_B) == true)
&&
(bCancelSelection == true) )
{
bCancelSelection = false;
+ UnitResetMenuLevel();
}
-
- GfxShowResources(&Resources);
}
diff --git a/Player.h b/Player.h
index 6a4782c..bfc3d68 100644
--- a/Player.h
+++ b/Player.h
@@ -47,7 +47,10 @@ class Player
bool checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb);
void UnitBuildingSelection(void);
void showHealth(uint8_t hp);
+ void ButtonHandler(void);
+ void BuildingUnitActions(void);
TYPE_COLLISION_BLOCK GetCursorPos(void);
+
char name[PLAYER_NAME_LENGTH];
TYPE_UNIT units[PLAYER_MAX_UNITS];
TYPE_BUILDING buildings[PLAYER_MAX_BUILDINGS];
@@ -57,8 +60,10 @@ class Player
uint8_t unit_i;
uint8_t bldg_i;
bool human;
+ bool showUnitBuildingOptions;
TYPE_CAMERA Camera;
TYPE_RESOURCES Resources;
+ uint8_t progress_bar;
//Print _serial;
};
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;
+}
diff --git a/Unit.h b/Unit.h
new file mode 100644
index 0000000..f8ad6ad
--- /dev/null
+++ b/Unit.h
@@ -0,0 +1,50 @@
+#ifndef __UNIT_HEADER__
+#define __UNIT_HEADER__
+
+/* **************************************
+ * Includes *
+ * **************************************/
+
+#include "Global_Inc.h"
+#include "Gfx.h"
+#include "GameStructures.h"
+#include "Camera.h"
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif //__cplusplus
+
+/* **************************************
+ * Defines *
+ * **************************************/
+
+ /* **************************************
+ * Structs and enums *
+ * **************************************/
+
+enum
+{
+ PEASANT = 0
+};
+
+/* **************************************
+ * Global prototypes *
+ * **************************************/
+
+void UnitInit(void);
+uint8_t UnitGetHpFromID(uint8_t id);
+uint8_t UnitGetWidthFromID(uint8_t id);
+uint8_t UnitGetHeightFromID(uint8_t id);
+void UnitDraw(TYPE_CAMERA * ptrCamera, TYPE_UNIT * ptrUnit, bool bSelected);
+const char* UnitSelectedOptions(TYPE_UNIT* ptrUnit);
+void UnitMoveTo(TYPE_UNIT * ptrUnit, uint16_t x, uint16_t y);
+void UnitHandler(TYPE_UNIT * ptrUnit);
+void UnitAcceptAction(TYPE_UNIT* ptrUnit);
+void UnitResetMenuLevel(void);
+
+#ifdef __cplusplus
+}
+#endif //__cplusplus
+
+#endif //__UNIT_HEADER__