From 7447ed097d11091102a423e835ed8584718e1f9b Mon Sep 17 00:00:00 2001 From: przemek83 <4788832+przemek83@users.noreply.github.com> Date: Fri, 13 Sep 2024 11:10:37 +0200 Subject: [PATCH] Rework casts and types in Map class. --- src/Map.cpp | 56 +++++++++++++++++++++++++++++------------------------ src/Map.h | 27 ++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/src/Map.cpp b/src/Map.cpp index 2c4edaf..9602cb6 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -63,10 +63,11 @@ std::list Map::loadMap(std::iostream& stream) break; } - const Point point{x * Config::getInstance().getTileSize(), - y * Config::getInstance().getTileSize()}; + const Point point{ + static_cast(x) * Config::getInstance().getTileSize(), + static_cast(y) * Config::getInstance().getTileSize()}; - auto& tile{getTile({x, y})}; + auto& tile{getTileUsingPosition({x, y})}; switch (sign) { case '1': @@ -117,12 +118,12 @@ std::list Map::loadMap(std::iostream& stream) bool Map::canDrive(Point point) const { - return getTile(screenPointToTile(point))->canDrive(); + return getTileUsingPosition(screenPointToTilePosition(point))->canDrive(); } std::pair Map::takePowerUp(Point point) { - auto& tile{getTile(screenPointToTile(point))}; + auto& tile{getTileUsingPosition(screenPointToTilePosition(point))}; if (!tile->isPowerUp()) return {false, ResourceType::PLAIN}; const ResourceType type{tile->getResourceType()}; @@ -132,13 +133,13 @@ std::pair Map::takePowerUp(Point point) bool Map::canFly(Point point) const { - return getTile(screenPointToTile(point))->canFly(); + return getTileUsingPosition(screenPointToTilePosition(point))->canFly(); } void Map::hit(Point point, int power) { - auto [x, y]{screenPointToTile(point)}; - auto& tile{getTile({x, y})}; + auto [x, y]{screenPointToTilePosition(point)}; + auto& tile{getTileUsingPosition({x, y})}; if ((!canFly(point)) && tile->hit(power)) { const bool baseDestroyed{tile->getResourceType() == ResourceType::BASE}; @@ -186,33 +187,38 @@ void Map::shift(Point& pointToShift, Direction direction) const void Map::tagAreaAsChanged(Point leftUpper, Point rightLower) { const int tileSize{Config::getInstance().getTileSize()}; - Point point{screenPointToTile(leftUpper)}; + TilePosition position{screenPointToTilePosition(leftUpper)}; if (point_utils::isValidPoint(leftUpper)) - changedTiles_[point.x_][point.y_] = true; + changedTiles_[position.x_][position.y_] = true; - point = screenPointToTile({leftUpper.x_, leftUpper.y_ + tileSize}); + position = + screenPointToTilePosition({leftUpper.x_, leftUpper.y_ + tileSize}); if (point_utils::isValidPoint({leftUpper.x_, leftUpper.y_ + tileSize})) - changedTiles_[point.x_][point.y_] = true; + changedTiles_[position.x_][position.y_] = true; - point = screenPointToTile(rightLower); + position = screenPointToTilePosition(rightLower); if (point_utils::isValidPoint(rightLower)) - changedTiles_[point.x_][point.y_] = true; + changedTiles_[position.x_][position.y_] = true; - point = screenPointToTile({rightLower.x_, rightLower.y_ - tileSize}); + position = screenPointToTilePosition( + Point{rightLower.x_, rightLower.y_ - tileSize}); if (point_utils::isValidPoint({rightLower.x_, rightLower.y_ - tileSize})) - changedTiles_[point.x_][point.y_] = true; + changedTiles_[position.x_][position.y_] = true; } -Point Map::screenPointToTile(Point location) +Map::TilePosition Map::screenPointToTilePosition(Point point) { - return {location.x_ / Config::getInstance().getTileSize(), - location.y_ / Config::getInstance().getTileSize()}; + const int tileSize{Config::getInstance().getTileSize()}; + std::size_t x{static_cast(point.x_ / tileSize)}; + std::size_t y{static_cast(point.y_ / tileSize)}; + return {x, y}; } -Point Map::tileToScreenPoint(Point point) +Point Map::tilePositionToScreenPoint(TilePosition position) { - return {point.x_ * Config::getInstance().getTileSize(), - point.y_ * Config::getInstance().getTileSize()}; + const int tileSize{Config::getInstance().getTileSize()}; + return {static_cast(position.x_) * tileSize, + static_cast(position.y_) * tileSize}; } void Map::shiftRight(Point& point, int tileSize) @@ -243,7 +249,7 @@ void Map::drawBackground(const Screen& screen) if (!changedTiles_[x][y]) continue; - const auto& tile{getTile({x, y})}; + const auto& tile{getTileUsingPosition({x, y})}; if (tile->isPartOfBackground()) { tile->draw(screen); @@ -252,7 +258,7 @@ void Map::drawBackground(const Screen& screen) else { - plainTile_->setLocation(tileToScreenPoint({x, y})); + plainTile_->setLocation(tilePositionToScreenPoint({x, y})); plainTile_->draw(screen); } } @@ -266,7 +272,7 @@ void Map::drawForeground(const Screen& screen) if (!changedTiles_[x][y]) continue; - const auto& tile{getTile({x, y})}; + const auto& tile{getTileUsingPosition({x, y})}; if (!tile->isPartOfBackground()) { tile->draw(screen); diff --git a/src/Map.h b/src/Map.h index 07f7770..4bee7ed 100644 --- a/src/Map.h +++ b/src/Map.h @@ -35,18 +35,37 @@ class Map void tagAreaAsChanged(Point leftUpper, Point rightLower); private: + struct TilePosition + { + std::size_t x_; + std::size_t y_; + }; + const std::unique_ptr& getTile(Point point) const { - return board_[point.y_][point.x_]; + return board_[static_cast(point.y_)] + [static_cast(point.x_)]; } std::unique_ptr& getTile(Point point) { - return board_[point.y_][point.x_]; + return board_[static_cast(point.y_)] + [static_cast(point.x_)]; + } + + const std::unique_ptr& getTileUsingPosition( + TilePosition position) const + { + return board_[position.y_][position.x_]; + } + + std::unique_ptr& getTileUsingPosition(TilePosition position) + { + return board_[position.y_][position.x_]; } - static Point screenPointToTile(Point location); - static Point tileToScreenPoint(Point point); + static TilePosition screenPointToTilePosition(Point point); + static Point tilePositionToScreenPoint(TilePosition position); static void shiftRight(Point& point, int tileSize); static void shiftLeft(Point& point, int tileSize);