diff --git a/src/client/Client.cc b/src/client/Client.cc index 66fb14d..2039aa0 100644 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -219,6 +219,15 @@ void Client::update(double elapsed_time) } } + /* decrease player hover when not over a tile */ + if (m_map.get_tile_at(m_players[current_player]->x, + m_players[current_player]->y).isNull()) + { + m_players[current_player]->hover -= elapsed_time / 10; + if (m_players[current_player]->hover < 0) + m_players[current_player]->hover = 0; + } + // Send an update to the server if something has changed if((m_players[current_player]->w_pressed != w_pressed) || (m_players[current_player]->a_pressed != a_pressed) || diff --git a/src/common/Map.cc b/src/common/Map.cc index 4953752..dd2238d 100644 --- a/src/common/Map.cc +++ b/src/common/Map.cc @@ -3,6 +3,15 @@ using namespace std; +/* + * The center of the tile at m_grid[i][j] is (x, y) where + * x = j * m_span_x + m_offset_x + * y = (i + ((j & 1) ? 0.5 : 0.0) * m_tile_size + m_offset_y + * + * The rectangular bounding box for a hex tile centered at (x, y) + * are ((x - HEX_WIDTH_TO_HEIGHT * m_tile_size / 2, y - m_tile_size / 2), + * (x + HEX_WIDTH_TO_HEIGHT * m_tile_size / 2, y + m_tile_size / 2)) + */ Map::Map(int width, int height, float tile_size) { m_width = width; @@ -10,13 +19,13 @@ Map::Map(int width, int height, float tile_size) m_tile_size = tile_size; /* construction of default map */ - float span_x = HEX_WIDTH_TO_HEIGHT * 0.75 * tile_size; - float offset_x = -span_x * (width / 2.0); - float offset_y = -tile_size * (height / 2.0); + m_span_x = HEX_WIDTH_TO_HEIGHT * 0.75 * tile_size; + m_offset_x = -m_span_x * (width / 2.0); + m_offset_y = -tile_size * (height / 2.0); - HexTile outer_out(0, 0, 0.85 * span_x * width); - HexTile outer_in(0, 0, 0.5 * span_x * width); - HexTile inner_out(0, 0, 0.30 * span_x * width); + HexTile outer_out(0, 0, 0.85 * m_span_x * width); + HexTile outer_in(0, 0, 0.5 * m_span_x * width); + HexTile inner_out(0, 0, 0.30 * m_span_x * width); for (int i = 0; i < height; i++) { @@ -24,13 +33,14 @@ Map::Map(int width, int height, float tile_size) for (int j = 0; j < width; j++) { refptr ht; - float x = j * span_x + offset_x; - float y = (i + ((j & 1) ? 0.5 : 0.0)) * tile_size + offset_y; + float x = j * m_span_x + m_offset_x; + float y = (i + ((j & 1) ? 0.5 : 0.0)) * tile_size + m_offset_y; if (inner_out.point_within(y, x) || (outer_out.point_within(y, x) && !outer_in.point_within(y, x))) { + refptr get_tile_at(float x, float y); ht = new HexTile(x, y, tile_size); } @@ -38,3 +48,28 @@ Map::Map(int width, int height, float tile_size) } } } + +/* + * Given a point (x, y) return the HexTile, if any, that the point is on. + * A NULL refptr is returned if the point is not on any tile. + */ +refptr Map::get_tile_at(float x, float y) +{ + int i_base = (int) ((y - m_offset_y) / m_tile_size); + int j_base = (int) ((x - m_offset_x) / m_span_x); + for (int i_offset = 0; i_offset <= 1; i_offset++) + { + for (int j_offset = 0; j_offset <= 1; j_offset++) + { + int i = i_base + i_offset; + int j = j_base + j_offset; + if (i >= 0 && i < m_height && j >= 0 && j < m_width) + { + if (!m_grid[i][j].isNull()) + if (m_grid[i][j]->point_within(x, y)) + return m_grid[i][j]; + } + } + } + return NULL; +} diff --git a/src/common/Map.h b/src/common/Map.h index b3050ba..52c6d16 100644 --- a/src/common/Map.h +++ b/src/common/Map.h @@ -11,12 +11,16 @@ class Map Map(int width=21, int height=21, float tile_size=50); bool tile_present(int x, int y) { return !m_grid[y][x].isNull(); } refptr get_tile(int x, int y) { return m_grid[y][x]; } + refptr get_tile_at(float x, float y); int get_width() { return m_width; } int get_height() { return m_height; } protected: int m_width; int m_height; float m_tile_size; + float m_span_x; + float m_offset_x; + float m_offset_y; std::vector< std::vector< refptr< HexTile > > > m_grid; };