summaryrefslogtreecommitdiff
path: root/Player.cpp
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2017-03-12 21:09:29 +0100
committerXaviDCR92 <xavi.dcr@gmail.com>2017-03-12 21:09:29 +0100
commit6628e7de58425b3e93da9ae6fcb7347137d96096 (patch)
tree336d1f74961187aad7c85ba06dcdd0684c003282 /Player.cpp
parentf416816883545433ea33ca410983371e657dba18 (diff)
* Player can now select nearest unit or building. Multiple buildings and units can be selected at the same time.
* Unit module moved from C++ to C (only extension change - and compiler - was needed). * GfxGetHeightFromSpriteData and GfxGetWidthFromSpriteData were getting incorrect data! Incorrect memory address was being read when calling pgm_read_byte, and caused unexpected behaviour under real hw.
Diffstat (limited to 'Player.cpp')
-rw-r--r--Player.cpp110
1 files changed, 107 insertions, 3 deletions
diff --git a/Player.cpp b/Player.cpp
index 4e00951..95f23e7 100644
--- a/Player.cpp
+++ b/Player.cpp
@@ -8,6 +8,8 @@
* Defines *
* **************************************/
+#define CANCEL_SELECTION_NO_FRAMES 5
+
/* **************************************
* Local variables *
* **************************************/
@@ -72,6 +74,17 @@ void Player::Init(void)
}
}
+void Player::showHealth(uint8_t hp)
+{
+ char str[8];
+
+ GfxFillRectangle(0, Y_SCREEN_RESOLUTION - 5, X_SCREEN_RESOLUTION, 8, GFX_WHITE);
+
+ snprintf(str, 8, "HP=%u", hp);
+
+ GfxPrintTextFont(str, font3x3, 4, Y_SCREEN_RESOLUTION - 4);
+}
+
void Player::DrawHandler(void)
{
uint8_t i;
@@ -82,12 +95,20 @@ void Player::DrawHandler(void)
for(i = 0; i < PLAYER_MAX_BUILDINGS; i++)
{
TYPE_BUILDING * b = &buildings[i];
+
+ if(b->built == false)
+ {
+ continue;
+ }
+
bool selected = (b == selectedBuilding);
BuildingDraw(&Camera, b, selected);
if( (b->selected == true) && (bAnyoneSelected == false) )
{
bAnyoneSelected = true;
+
+ showHealth(b->hp);
BuildingSelectedOptions(b);
}
}
@@ -95,12 +116,20 @@ void Player::DrawHandler(void)
for(i = 0; i < PLAYER_MAX_UNITS; i++)
{
TYPE_UNIT * u = &units[i];
+
+ if(u->alive == false)
+ {
+ continue;
+ }
+
bool selected = (u == selectedUnit);
UnitDraw(&Camera, u, selected);
if( (u->selected == true) && (bAnyoneSelected == false) )
{
bAnyoneSelected = true;
+
+ showHealth(u->hp);
UnitSelectedOptions(u);
}
}
@@ -234,6 +263,12 @@ void Player::UnitBuildingSelection(void)
for(i = 0; i < PLAYER_MAX_UNITS; i++)
{
TYPE_UNIT * u = &units[i];
+
+ if(u->alive == false)
+ {
+ continue;
+ }
+
TYPE_COLLISION_BLOCK cursor_cb = GetCursorPos();
TYPE_COLLISION_BLOCK u_cb = {u->x, u->y, UnitGetWidthFromID(u->id), UnitGetHeightFromID(u->id) };
@@ -255,6 +290,12 @@ void Player::UnitBuildingSelection(void)
for(i = 0; i < PLAYER_MAX_BUILDINGS; i++)
{
TYPE_BUILDING * b = &buildings[i];
+
+ if(b->built == false)
+ {
+ continue;
+ }
+
TYPE_COLLISION_BLOCK cursor_cb = GetCursorPos();
TYPE_COLLISION_BLOCK u_cb = {b->x, b->y, BuildingGetWidthFromID(b->id), BuildingGetHeightFromID(b->id) };
@@ -281,7 +322,15 @@ void Player::UnitBuildingSelection(void)
selectedBuilding = NULL;
}
- char buf[8];
+ if( (nearest_unit_dist > 400)
+ &&
+ (nearest_building_dist > 400) )
+ {
+ selectedUnit = NULL;
+ selectedBuilding = NULL;
+ }
+
+ /*char buf[8];
snprintf(buf, 8, "%lu", nearest_building_dist);
@@ -289,15 +338,24 @@ void Player::UnitBuildingSelection(void)
snprintf(buf, 8, "%lu", nearest_unit_dist);
- GfxPrintText(buf, X_SCREEN_RESOLUTION - 32, 24);
+ GfxPrintText(buf, X_SCREEN_RESOLUTION - 32, 24);*/
}
void Player::Handler(void)
-{
+{
+ static bool bCancelSelection = false;
+
CameraHandler(&Camera);
UnitBuildingSelection();
+ for(int i = 0; i < PLAYER_MAX_UNITS; i++)
+ {
+ TYPE_UNIT * ptrUnit = &units[i];
+
+ UnitHandler(ptrUnit);
+ }
+
if(PadButtonReleased(PAD_A) == true)
{
/*TYPE_COLLISION_BLOCK cl;
@@ -336,6 +394,52 @@ void Player::Handler(void)
}
}
}
+ else if( (PadButtonPressedFrames(PAD_B, CANCEL_SELECTION_NO_FRAMES) == true)
+ &&
+ (bCancelSelection == false) )
+ {
+ for(int i = 0; i < PLAYER_MAX_UNITS; i++)
+ {
+ TYPE_UNIT * u = &units[i];
+
+ u->selected = false;
+ }
+
+ for(int i = 0; i < PLAYER_MAX_BUILDINGS; i++)
+ {
+ TYPE_BUILDING * b = &buildings[i];
+
+ b->selected = false;
+ }
+
+ selectedUnit = NULL;
+ selectedBuilding = NULL;
+
+ bCancelSelection = true;
+ }
+
+ if( (PadButtonReleased(PAD_B) == true)
+ &&
+ (bCancelSelection == false) )
+ {
+ TYPE_COLLISION_BLOCK cursor = GetCursorPos();
+
+ for(int i = 0; i < PLAYER_MAX_UNITS; i++)
+ {
+ TYPE_UNIT * u = &units[i];
+
+ if(u->selected == true)
+ {
+ UnitMoveTo(u, cursor.x, cursor.y);
+ }
+ }
+ }
+ else if( (PadButtonReleased(PAD_B) == true)
+ &&
+ (bCancelSelection == true) )
+ {
+ bCancelSelection = false;
+ }
GfxShowResources(&Resources);
}