summaryrefslogtreecommitdiff
path: root/Player.cpp
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2017-11-05 19:08:38 +0100
committerXaviDCR92 <xavi.dcr@gmail.com>2017-11-05 19:08:38 +0100
commit7b05778e3ef8ef01a4bb15a6ed6fde5b329a8c9b (patch)
tree2629c7ad872b398dc045a863a75879533ce99aa0 /Player.cpp
parent2d04eba931ac84e2f09d3c8391125f8a991ca7d3 (diff)
*Removed decceleration for camera.
Improved pathfinding when a unit collides with another unit. Added generic function to find distances. Other minor changes.
Diffstat (limited to 'Player.cpp')
-rw-r--r--Player.cpp158
1 files changed, 102 insertions, 56 deletions
diff --git a/Player.cpp b/Player.cpp
index db5193d..e02277e 100644
--- a/Player.cpp
+++ b/Player.cpp
@@ -181,7 +181,7 @@ void Player::ShowResources(void)
GfxPrintTextFont(str, font3x3, 42, 1);
- Systemitoa(str, 3, unit_i);
+ Systemitoa(str, 3, getAliveUnits());
for (i = 0; i < 3; i++)
{
@@ -259,6 +259,21 @@ bool Player::checkNewBuildingPosition(TYPE_COLLISION_BLOCK * cb)
return true;
}
+uint8_t Player::getAliveUnits(void)
+{
+ uint8_t ret = 0;
+
+ for (uint8_t i = 0; i < unit_i; i++)
+ {
+ if (units[i].alive != false)
+ {
+ ret++;
+ }
+ }
+
+ return ret;
+}
+
void Player::createUnit(TYPE_UNIT_ID id, TYPE_COLLISION_BLOCK cb)
{
TYPE_RESOURCES res = UnitNeededResourcesFromID(id);
@@ -273,20 +288,27 @@ void Player::createUnit(TYPE_UNIT_ID id, TYPE_COLLISION_BLOCK cb)
return;
}
- if (unit_i < PLAYER_MAX_UNITS_BUILDINGS)
+ if (getAliveUnits() < PLAYER_MAX_UNITS_BUILDINGS)
{
- TYPE_UNIT* ptrNewUnit = &units[unit_i++];
- ptrNewUnit->id = id;
- ptrNewUnit->x = cb.x;
- ptrNewUnit->y = cb.y;
- ptrNewUnit->hp = UnitGetHpFromID(id);
- ptrNewUnit->alive = true;
- ptrNewUnit->building = (id > MAX_UNIT_ID);
-
- /* Substract resources from player */
- Resources.Wood -= res.Wood;
- Resources.Gold -= res.Gold;
- Resources.Food -= res.Food;
+ for (uint8_t i = 0; i < PLAYER_MAX_UNITS_BUILDINGS; i++)
+ {
+ if (units[i].alive == false)
+ {
+ TYPE_UNIT* ptrNewUnit = &units[unit_i++];
+ ptrNewUnit->id = id;
+ ptrNewUnit->x = cb.x;
+ ptrNewUnit->y = cb.y;
+ ptrNewUnit->hp = UnitGetHpFromID(id);
+ ptrNewUnit->alive = true;
+ ptrNewUnit->building = (id > MAX_UNIT_ID);
+
+ /* Substract resources from player */
+ Resources.Wood -= res.Wood;
+ Resources.Gold -= res.Gold;
+ Resources.Food -= res.Food;
+ return;
+ }
+ }
}
else
{
@@ -296,21 +318,32 @@ void Player::createUnit(TYPE_UNIT_ID id, TYPE_COLLISION_BLOCK cb)
TYPE_COLLISION_BLOCK Player::GetCursorPos(void)
{
+ enum
+ {
+ MOUSE_W = 8,
+ MOUSE_H = 8
+ };
+
TYPE_COLLISION_BLOCK cb;
cb.x = (X_SCREEN_RESOLUTION >> 1) - 4 - Camera.X_Offset;
cb.y = (Y_SCREEN_RESOLUTION >> 1) - 4 - Camera.Y_Offset;
- cb.w = 8;
- cb.h = 8;
+ cb.w = MOUSE_W;
+ cb.h = MOUSE_H;
return cb;
}
void Player::UnitBuildingSelection(void)
{
+ enum
+ {
+ MAX_ALLOWED_DISTANCE = (X_SCREEN_RESOLUTION * X_SCREEN_RESOLUTION) + (Y_SCREEN_RESOLUTION) * (Y_SCREEN_RESOLUTION)
+ };
+
uint16_t i;
uint32_t nearest_unit_dist = 0xFFFFFFFF; // Set maximum value
- uint32_t dist;
+ uint32_t dist = nearest_unit_dist;
int8_t nearest_unit = NO_SELECTION;
@@ -318,7 +351,7 @@ void Player::UnitBuildingSelection(void)
{
TYPE_UNIT* u = &units[i];
- if ( (u->alive == false) || (u->selected != false) )
+ if ( (u->alive == false) || (u->selected != false) )
{
continue;
}
@@ -329,7 +362,7 @@ void Player::UnitBuildingSelection(void)
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);
- dist = (dist_x * dist_x) + (dist_y * dist_y);
+ dist = SystemGetHyp(dist_x, dist_y);
if (dist < nearest_unit_dist)
{
@@ -338,7 +371,7 @@ void Player::UnitBuildingSelection(void)
}
}
- selectedUnitCandidate = nearest_unit;
+ selectedUnitCandidate = dist < (uint32_t)MAX_ALLOWED_DISTANCE? nearest_unit: NO_SELECTION;
}
void Player::ActionsMenu(void)
@@ -353,18 +386,21 @@ void Player::ActionsMenu(void)
{
uint8_t availableActions = UnitGetAvailableActions(ptrUnit);
- if (!(availableActions & (1 << showActionsMenu_index) ) )
+ if (availableActions != 0)
{
- IncreaseShowActionsMenuIndex();
- }
+ if (!(availableActions & (1 << showActionsMenu_index) ) )
+ {
+ IncreaseShowActionsMenuIndex();
+ }
- UNIT_ACTION action = (UNIT_ACTION)(showActionsMenu_index);
+ UNIT_ACTION action = (UNIT_ACTION)(showActionsMenu_index);
- const char* str = UnitGetActionString(action);
+ const char* str = UnitGetActionString(action);
- GfxPrintTextFont(str, font3x3, 40, Y_SCREEN_RESOLUTION - 4);
+ GfxPrintTextFont(str, font3x3, 40, Y_SCREEN_RESOLUTION - 4);
- break;
+ break;
+ }
}
}
@@ -428,7 +464,6 @@ void Player::ButtonAPressed(void)
}
}
-
void Player::ButtonAReleased(void)
{
if (showActionsMenu_counter < ACCEPT_UNIT_BUILDING_OPTIONS_FRAMES)
@@ -469,11 +504,11 @@ void Player::ButtonAReleased(void)
break;
case ACTION_BUILD_BARRACKS:
- ActionCreateBuilding(ptrUnit, BARRACKS);
+ ActionCreateBuilding(BARRACKS);
break;
case ACTION_BUILD_TOWER_CENTER:
- ActionCreateBuilding(ptrUnit, TOWN_CENTER);
+ ActionCreateBuilding(TOWN_CENTER);
break;
default:
@@ -490,40 +525,51 @@ void Player::ButtonAReleased(void)
void Player::ActionCreateUnit(TYPE_UNIT* ptrUnit, TYPE_UNIT_ID unit)
{
- uint8_t w = UnitGetWidthFromID(ptrUnit->id);
- uint8_t h = UnitGetHeightFromID(ptrUnit->id);
- uint8_t new_pos_x = ptrUnit->x + SystemRand(0, w + (w >> 1));
- uint8_t new_pos_y = ptrUnit->y + SystemRand(h, h + (h >> 1));
- TYPE_COLLISION_BLOCK cb = {.x = new_pos_x, .y = new_pos_y};
+ enum
+ {
+ MAX_RETRIES = 16
+ };
+
+ uint8_t retries = 0;
+
+ do
+ {
+ uint8_t w = UnitGetWidthFromID(ptrUnit->id);
+ uint8_t h = UnitGetHeightFromID(ptrUnit->id);
+ uint8_t new_pos_x = ptrUnit->x + SystemRand(0, w + (w >> 1));
+ uint8_t new_pos_y = ptrUnit->y + SystemRand(h, h + (h >> 1));
+ TYPE_COLLISION_BLOCK cb = { .x = new_pos_x,
+ .y = new_pos_y,
+ .w = UnitGetWidthFromID(unit),
+ .h = UnitGetHeightFromID(unit)};
+
+ if (UnitCheckCollisionAgainstOtherUnits(&cb, units, NULL) == false)
+ {
+ createUnit(unit, cb);
+ return;
+ }
- createUnit(unit, cb);
+ }while (++retries < MAX_RETRIES);
+
+ /* Will only get here if we could not create the new unit */
+ GfxPrintText_Flash(F("Could not create unit"));
}
-void Player::ActionCreateBuilding(TYPE_UNIT* ptrUnit, TYPE_UNIT_ID bldg)
+void Player::ActionCreateBuilding(TYPE_UNIT_ID bldg)
{
TYPE_COLLISION_BLOCK cb = GetCursorPos();
- for (uint8_t i = 0; i < unit_i; i++)
- {
- TYPE_UNIT* ptrOtherUnit = &units[i];
- TYPE_COLLISION_BLOCK ocb = {.x = ptrOtherUnit->x,
- .y = ptrOtherUnit->y,
- .w = UnitGetWidthFromID(ptrOtherUnit->id),
- .h = UnitGetHeightFromID(ptrOtherUnit->id)};
+ cb.w = UnitGetWidthFromID(bldg);
+ cb.w = UnitGetHeightFromID(bldg);
- if (ptrOtherUnit->alive == false)
- {
- continue;
- }
-
- if (SystemCollisionCheck(&cb, &ocb) != false)
- {
- GfxPrintText_Flash(F("Cannot build here"));
- return;
- }
+ if (UnitCheckCollisionAgainstOtherUnits(&cb, units, NULL) == false)
+ {
+ createUnit(bldg, cb);
+ }
+ else
+ {
+ GfxPrintText_Flash(F("Cannot build here"));
}
-
- createUnit(bldg, cb);
}
void Player::ButtonBPressed(void)