summaryrefslogtreecommitdiff
path: root/Player.cpp
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2017-03-09 23:59:53 +0100
committerXaviDCR92 <xavi.dcr@gmail.com>2017-03-09 23:59:53 +0100
commitf416816883545433ea33ca410983371e657dba18 (patch)
tree1c8682679b1c67261447570c4f66d0d8919bdc00 /Player.cpp
parent8ec41b4410aba535008daf991ea59a8740951d44 (diff)
* Nearest unit and/or building is found (no sqrt method used).
* Preliminar unit/building selection algorithm. * Added cursor onscreen. - Removed old Peasant sprites. Only 2 sprites needed!
Diffstat (limited to 'Player.cpp')
-rw-r--r--Player.cpp189
1 files changed, 169 insertions, 20 deletions
diff --git a/Player.cpp b/Player.cpp
index 59391db..4e00951 100644
--- a/Player.cpp
+++ b/Player.cpp
@@ -14,7 +14,6 @@
Player::Player(void)
{
-
}
Player::~Player(void)
@@ -29,14 +28,23 @@ void Player::Init(void)
unit_i = 0;
bldg_i = 0;
+ selectedUnit = NULL;
+ selectedBuilding = NULL;
+
CameraInit(&Camera);
BuildingInit();
+ UnitInit();
for(i = 0; i < PLAYER_MAX_BUILDINGS; i++)
{
memset(&buildings[i], 0, sizeof(TYPE_BUILDING));
}
+ for(i = 0; i < PLAYER_MAX_UNITS; i++)
+ {
+ memset(&units[i], 0, sizeof(TYPE_UNIT));
+ }
+
TYPE_COLLISION_BLOCK cl;
Resources.Wood = 25;
@@ -45,39 +53,56 @@ void Player::Init(void)
cl.x = SystemRand(0, 20);
cl.y = SystemRand(0, 20);
- cl.w = BuildingGetWidthFromID(0);
- cl.h = BuildingGetHeightFromID(0);
+ cl.w = BuildingGetWidthFromID(BARRACKS);
+ cl.h = BuildingGetHeightFromID(BARRACKS);
- if(createBuilding(0, cl) == false)
+ if(createBuilding(BARRACKS, cl) == false)
{
GfxPrintText_Flash(F("Failed to create building!"));
}
-}
-
-bool Player::createUnit(uint8_t id)
-{
- if(unit_i < PLAYER_MAX_UNITS)
- {
- units[unit_i++] = id;
- return true;
- }
- else
+
+ cl.x = SystemRand(48, 56);
+ cl.y = SystemRand(48, 56);
+ cl.w = UnitGetWidthFromID(PEASANT);
+ cl.h = UnitGetHeightFromID(PEASANT);
+
+ if(createUnit(PEASANT, cl) == false)
{
- return false;
+ GfxPrintText_Flash(F("Failed to create unit!"));
}
-
- return false;
}
void Player::DrawHandler(void)
{
uint8_t i;
+ bool bAnyoneSelected = false;
//GfxRenderTiles(&Camera);
for(i = 0; i < PLAYER_MAX_BUILDINGS; i++)
{
- BuildingDraw(&Camera, &buildings[i]);
+ TYPE_BUILDING * b = &buildings[i];
+ bool selected = (b == selectedBuilding);
+ BuildingDraw(&Camera, b, selected);
+
+ if( (b->selected == true) && (bAnyoneSelected == false) )
+ {
+ bAnyoneSelected = true;
+ BuildingSelectedOptions(b);
+ }
+ }
+
+ for(i = 0; i < PLAYER_MAX_UNITS; i++)
+ {
+ TYPE_UNIT * u = &units[i];
+ bool selected = (u == selectedUnit);
+ UnitDraw(&Camera, u, selected);
+
+ if( (u->selected == true) && (bAnyoneSelected == false) )
+ {
+ bAnyoneSelected = true;
+ UnitSelectedOptions(u);
+ }
}
}
@@ -165,13 +190,117 @@ bool Player::createBuilding(uint8_t id, TYPE_COLLISION_BLOCK cb)
return false;
}
-void Player::Handler(void)
+bool Player::createUnit(uint8_t id, TYPE_COLLISION_BLOCK cb)
+{
+ if(unit_i < PLAYER_MAX_UNITS)
+ {
+ units[unit_i].id = id;
+ units[unit_i].x = cb.x;
+ units[unit_i].y = cb.y;
+ units[unit_i].hp = UnitGetHpFromID(id);
+ units[unit_i].alive = true;
+
+ unit_i++;
+
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+
+ return false;
+}
+
+TYPE_COLLISION_BLOCK Player::GetCursorPos(void)
+{
+ 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;
+
+ return cb;
+}
+
+void Player::UnitBuildingSelection(void)
{
+ uint16_t i;
+ TYPE_UNIT * nearest_unit = NULL;
+ uint32_t nearest_unit_dist = 0xFFFFFFFF; // Set maximum value
+ uint32_t dist;
+
+ for(i = 0; i < PLAYER_MAX_UNITS; i++)
+ {
+ TYPE_UNIT * u = &units[i];
+ 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 - cursor_cb.x);
+ uint16_t dist_y = (u_cb.y - cursor_cb.y);
+
+ dist = (dist_x * dist_x) + (dist_y * dist_y);
+
+ if(dist < nearest_unit_dist)
+ {
+ nearest_unit_dist = dist;
+ nearest_unit = u;
+ }
+ }
+
+ TYPE_BUILDING * nearest_building = NULL;
+ uint32_t nearest_building_dist = 0xFFFFFFFF; // Set maximum value
+
+ for(i = 0; i < PLAYER_MAX_BUILDINGS; i++)
+ {
+ TYPE_BUILDING * b = &buildings[i];
+ TYPE_COLLISION_BLOCK cursor_cb = GetCursorPos();
+ TYPE_COLLISION_BLOCK u_cb = {b->x, b->y, BuildingGetWidthFromID(b->id), BuildingGetHeightFromID(b->id) };
+
+ uint16_t dist_x = (u_cb.x + (u_cb.w >> 1) - cursor_cb.x + 4);
+ uint16_t dist_y = (u_cb.y + (u_cb.h >> 1) - cursor_cb.y + 4);
+
+ dist = (dist_x * dist_x) + (dist_y * dist_y);
+
+ if(dist < nearest_building_dist)
+ {
+ nearest_building_dist = dist;
+ nearest_building = b;
+ }
+ }
+
+ if(nearest_building_dist <= nearest_unit_dist)
+ {
+ selectedUnit = NULL;
+ selectedBuilding = nearest_building;
+ }
+ else
+ {
+ selectedUnit = nearest_unit;
+ selectedBuilding = NULL;
+ }
+
+ char buf[8];
+
+ snprintf(buf, 8, "%lu", nearest_building_dist);
+
+ GfxPrintText(buf, X_SCREEN_RESOLUTION - 32, 16);
+
+ snprintf(buf, 8, "%lu", nearest_unit_dist);
+
+ GfxPrintText(buf, X_SCREEN_RESOLUTION - 32, 24);
+}
+
+void Player::Handler(void)
+{
CameraHandler(&Camera);
+ UnitBuildingSelection();
+
if(PadButtonReleased(PAD_A) == true)
{
- TYPE_COLLISION_BLOCK cl;
+ /*TYPE_COLLISION_BLOCK cl;
cl.x = SystemRand(0, 32);
cl.y = SystemRand(0, 32);
@@ -185,6 +314,26 @@ void Player::Handler(void)
else
{
GfxPrintText_Flash(F("Building built!\0"));
+ }*/
+
+ for(int i = 0; i < PLAYER_MAX_UNITS; i++)
+ {
+ TYPE_UNIT * u = &units[i];
+
+ if(selectedUnit == u)
+ {
+ u->selected = true;
+ }
+ }
+
+ for(int i = 0; i < PLAYER_MAX_BUILDINGS; i++)
+ {
+ TYPE_BUILDING * b = &buildings[i];
+
+ if(selectedBuilding == b)
+ {
+ b->selected = true;
+ }
}
}