summaryrefslogtreecommitdiff
path: root/Player.cpp
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2017-11-10 00:04:35 +0100
committerXaviDCR92 <xavi.dcr@gmail.com>2017-11-10 00:04:35 +0100
commitd85464781580796bbcc744ae732e56d1920e3b0f (patch)
treeac0939ebae1e1fe9afb8060ad2329c52bcab00ad /Player.cpp
parent7b05778e3ef8ef01a4bb15a6ed6fde5b329a8c9b (diff)
Kinda improved pathfinding algorithm. Still some work TODO.
Fixed some errors in DEBUG_VAR macro. Shadows are now drawn before any other object. Different strings are now shown depending on the number of selected units. Some work on calculating actions mask when different types of units are selected. Still some work TODO.
Diffstat (limited to 'Player.cpp')
-rw-r--r--Player.cpp173
1 files changed, 108 insertions, 65 deletions
diff --git a/Player.cpp b/Player.cpp
index e02277e..4358d0c 100644
--- a/Player.cpp
+++ b/Player.cpp
@@ -98,20 +98,25 @@ void Player::showHealth(uint8_t hp)
void Player::DrawHandler(void)
{
- enum
- {
- PROGRESS_BAR_X = X_SCREEN_RESOLUTION - 16,
- PROGRESS_BAR_Y = Y_SCREEN_RESOLUTION - 4,
- PROGRESS_BAR_W = ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES << 1,
- PROGRESS_BAR_H = 3,
- };
-
uint8_t i;
bool bAnyoneSelected = false;
+ for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
+ {
+ TYPE_UNIT *u = &units[i];
+
+ if ( (u->alive == false) || (u->building == false) )
+ {
+ continue;
+ }
+
+ UnitDrawShadow(u, &Camera);
+ }
+
+
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
- TYPE_UNIT* u = &units[i];
+ TYPE_UNIT *u = &units[i];
if (u->alive == false)
{
@@ -130,33 +135,26 @@ void Player::DrawHandler(void)
if ( (u->selected != false) && (bAnyoneSelected == false) )
{
bAnyoneSelected = true;
-
- showHealth(u->hp);
}
}
if (human != false)
{
- if (bAnyoneSelected != false)
- {
- GfxDrawRectangle(PROGRESS_BAR_X, PROGRESS_BAR_Y, PROGRESS_BAR_W, PROGRESS_BAR_H, GFX_BLACK);
-
- if (showActionsMenu_counter != 0)
- {
- GfxFillRectangle(PROGRESS_BAR_X, PROGRESS_BAR_Y, showActionsMenu_counter << 1, PROGRESS_BAR_H, GFX_BLACK);
- }
- }
+ MenuDrawHandler();
+ }
+}
- ActionsMenu();
+void Player::MenuDrawHandler(void)
+{
+ ActionsMenu();
- ShowResources();
- }
+ ShowResources();
}
void Player::ShowResources(void)
{
char str[8];
- uint8_t i;
+ size_t i;
str[1] = '=';
@@ -181,22 +179,12 @@ void Player::ShowResources(void)
GfxPrintTextFont(str, font3x3, 42, 1);
- Systemitoa(str, 3, getAliveUnits());
-
- for (i = 0; i < 3; i++)
- {
- if (str[i] == '\0')
- {
- break;
- }
- }
+ i = Systemitoa(str, 3, getAliveUnits());
str[i++] = '/';
Systemitoa(&str[i], sizeof(str) - i, PLAYER_MAX_UNITS_BUILDINGS);
- //~ snprintf(str, sizeof(str), "%d/%d", unit_i, PLAYER_MAX_UNITS_BUILDINGS);
-
GfxPrintTextFont(str, font3x3, 42, 5);
}
@@ -210,7 +198,7 @@ bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
- TYPE_UNIT* ptrUnit = &units[i];
+ TYPE_UNIT *ptrUnit = &units[i];
success = false;
if (ptrUnit->building == false)
@@ -338,7 +326,8 @@ void Player::UnitBuildingSelection(void)
{
enum
{
- MAX_ALLOWED_DISTANCE = (X_SCREEN_RESOLUTION * X_SCREEN_RESOLUTION) + (Y_SCREEN_RESOLUTION) * (Y_SCREEN_RESOLUTION)
+ X_MAX_ALLOWED_DISTANCE = X_SCREEN_RESOLUTION >> 1,
+ Y_MAX_ALLOWED_DISTANCE = Y_SCREEN_RESOLUTION >> 1
};
uint16_t i;
@@ -349,7 +338,7 @@ void Player::UnitBuildingSelection(void)
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
- TYPE_UNIT* u = &units[i];
+ TYPE_UNIT *u = &units[i];
if ( (u->alive == false) || (u->selected != false) )
{
@@ -359,8 +348,13 @@ void Player::UnitBuildingSelection(void)
TYPE_COLLISION_BLOCK cursor_cb = GetCursorPos();
TYPE_COLLISION_BLOCK u_cb = {u->x, u->y, UnitGetWidthFromID(u->id), UnitGetHeightFromID(u->id) };
- uint16_t dist_x = (u_cb.x + (u_cb.w >> 1) - cursor_cb.x);
- uint16_t dist_y = (u_cb.y + (u_cb.h >> 1) - cursor_cb.y);
+ int16_t dist_x = (u_cb.x + (u_cb.w >> 1) - cursor_cb.x);
+ int16_t dist_y = (u_cb.y + (u_cb.h >> 1) - cursor_cb.y);
+
+ if ( (abs(dist_x) > X_MAX_ALLOWED_DISTANCE) || (abs(dist_y) > Y_MAX_ALLOWED_DISTANCE) )
+ {
+ continue;
+ }
dist = SystemGetHyp(dist_x, dist_y);
@@ -371,41 +365,90 @@ void Player::UnitBuildingSelection(void)
}
}
- selectedUnitCandidate = dist < (uint32_t)MAX_ALLOWED_DISTANCE? nearest_unit: NO_SELECTION;
+ selectedUnitCandidate = nearest_unit;
}
void Player::ActionsMenu(void)
{
- if (showActionsMenu != false)
+ enum
{
- for (uint8_t i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
+ ALL_ACTIONS_MASK = 0xFF
+ };
+
+ uint8_t selectedUnits = 0;
+ uint8_t availableActions = ALL_ACTIONS_MASK;
+
+ TYPE_UNIT *ptrSelectedUnit = NULL;
+
+ for (uint8_t i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
+ {
+ TYPE_UNIT *ptrUnit = &units[i];
+
+ if (ptrUnit->selected != false)
{
- TYPE_UNIT* ptrUnit = &units[i];
+ ptrSelectedUnit = ptrUnit;
+ selectedUnits++;
+ availableActions &= UnitGetAvailableActions(ptrUnit);
+ }
+ }
- if (ptrUnit->selected != false)
+ if (selectedUnits != 0)
+ {
+ if (selectedUnits == 1)
+ {
+ showHealth(ptrSelectedUnit->hp);
+ }
+ else
+ {
+ enum
{
- uint8_t availableActions = UnitGetAvailableActions(ptrUnit);
+ UNITS_SELECTED_NUMBER_STR_SIZE = 3 /* "000" */,
+ UNITS_SELECTED_STR = UNITS_SELECTED_NUMBER_STR_SIZE + 6 /* strlen("units\0") */,
- if (availableActions != 0)
- {
- if (!(availableActions & (1 << showActionsMenu_index) ) )
- {
- IncreaseShowActionsMenuIndex();
- }
+ UNITS_SELECTED_TEXT_X = 4,
+ UNITS_SELECTED_TEXT_Y = Y_SCREEN_RESOLUTION - 4,
+ };
- UNIT_ACTION action = (UNIT_ACTION)(showActionsMenu_index);
+ char unitsSelectedStr[UNITS_SELECTED_STR];
+ size_t i = Systemitoa(unitsSelectedStr, UNITS_SELECTED_NUMBER_STR_SIZE, selectedUnits);
- const char* str = UnitGetActionString(action);
+ //~ strcpy(&unitsSelectedStr[i], "units");
+ memcpy(&unitsSelectedStr[i], "units\0", 6 /* strlen("units")*/);
- GfxPrintTextFont(str, font3x3, 40, Y_SCREEN_RESOLUTION - 4);
+ GfxPrintTextFont(unitsSelectedStr, font3x3, UNITS_SELECTED_TEXT_X, UNITS_SELECTED_TEXT_Y);
+ }
- break;
- }
+ if ( (availableActions != 0)
+ &&
+ (availableActions != ALL_ACTIONS_MASK) )
+ {
+ enum
+ {
+ PROGRESS_BAR_X = X_SCREEN_RESOLUTION - 16,
+ PROGRESS_BAR_Y = Y_SCREEN_RESOLUTION - 4,
+ PROGRESS_BAR_W = ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES << 1,
+ PROGRESS_BAR_H = 3,
+ };
+
+ if (!(availableActions & (1 << showActionsMenu_index) ) )
+ {
+ IncreaseShowActionsMenuIndex();
}
+ GfxDrawRectangle(PROGRESS_BAR_X, PROGRESS_BAR_Y, PROGRESS_BAR_W, PROGRESS_BAR_H, GFX_BLACK);
+
+ GfxFillRectangle(PROGRESS_BAR_X, PROGRESS_BAR_Y, showActionsMenu_counter << 1, PROGRESS_BAR_H, GFX_BLACK);
+
+ if (showActionsMenu != false)
+ {
+ UNIT_ACTION action = (UNIT_ACTION)(showActionsMenu_index);
+
+ const char* str = UnitGetActionString(action);
+
+ GfxPrintTextFont(str, font3x3, 40, Y_SCREEN_RESOLUTION - 4);
+ }
}
}
-
}
void Player::Handler(void)
@@ -472,7 +515,7 @@ void Player::ButtonAReleased(void)
{
// When actions menu is not active, select unit if
// a candidate is present
- TYPE_UNIT* ptrUnit = &units[selectedUnitCandidate];
+ TYPE_UNIT *ptrUnit = &units[selectedUnitCandidate];
ptrUnit->selected = true;
anyUnitSelected = true;
@@ -487,7 +530,7 @@ void Player::ButtonAReleased(void)
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
- TYPE_UNIT* ptrUnit = &units[i];
+ TYPE_UNIT *ptrUnit = &units[i];
if (ptrUnit->selected != false)
{
@@ -523,7 +566,7 @@ void Player::ButtonAReleased(void)
showActionsMenu = (showActionsMenu_counter < ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES)? false: true;
}
-void Player::ActionCreateUnit(TYPE_UNIT* ptrUnit, TYPE_UNIT_ID unit)
+void Player::ActionCreateUnit(TYPE_UNIT *ptrUnit, TYPE_UNIT_ID unit)
{
enum
{
@@ -594,7 +637,7 @@ void Player::ButtonBPressed(void)
/* Cancel selection of all units */
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
- TYPE_UNIT* ptrUnit = &units[i];
+ TYPE_UNIT *ptrUnit = &units[i];
if (ptrUnit->selected != false)
{
@@ -621,7 +664,7 @@ void Player::ButtonBReleased(void)
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
- TYPE_UNIT* ptrUnit = &units[i];
+ TYPE_UNIT *ptrUnit = &units[i];
if (ptrUnit->selected != false)
{
@@ -651,7 +694,7 @@ void Player::ButtonLeftReleased(void)
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
- TYPE_UNIT* ptrUnit = &units[i];
+ TYPE_UNIT *ptrUnit = &units[i];
if (ptrUnit->selected != false)
{
@@ -692,7 +735,7 @@ void Player::IncreaseShowActionsMenuIndex(void)
for (i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
{
- TYPE_UNIT* ptrUnit = &units[i];
+ TYPE_UNIT *ptrUnit = &units[i];
if (ptrUnit->selected != false)
{