Skip to content

Commit

Permalink
Rework casts and types in Map class.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Sep 13, 2024
1 parent 34967a9 commit 7447ed0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
56 changes: 31 additions & 25 deletions src/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ std::list<Tank> Map::loadMap(std::iostream& stream)
break;
}

const Point point{x * Config::getInstance().getTileSize(),
y * Config::getInstance().getTileSize()};
const Point point{
static_cast<int>(x) * Config::getInstance().getTileSize(),
static_cast<int>(y) * Config::getInstance().getTileSize()};

auto& tile{getTile({x, y})};
auto& tile{getTileUsingPosition({x, y})};
switch (sign)
{
case '1':
Expand Down Expand Up @@ -117,12 +118,12 @@ std::list<Tank> Map::loadMap(std::iostream& stream)

bool Map::canDrive(Point point) const
{
return getTile(screenPointToTile(point))->canDrive();
return getTileUsingPosition(screenPointToTilePosition(point))->canDrive();
}

std::pair<bool, ResourceType> 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()};
Expand All @@ -132,13 +133,13 @@ std::pair<bool, ResourceType> 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};
Expand Down Expand Up @@ -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<std::size_t>(point.x_ / tileSize)};
std::size_t y{static_cast<std::size_t>(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<int>(position.x_) * tileSize,
static_cast<int>(position.y_) * tileSize};
}

void Map::shiftRight(Point& point, int tileSize)
Expand Down Expand Up @@ -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);
Expand All @@ -252,7 +258,7 @@ void Map::drawBackground(const Screen& screen)

else
{
plainTile_->setLocation(tileToScreenPoint({x, y}));
plainTile_->setLocation(tilePositionToScreenPoint({x, y}));
plainTile_->draw(screen);
}
}
Expand All @@ -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);
Expand Down
27 changes: 23 additions & 4 deletions src/Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tile>& getTile(Point point) const
{
return board_[point.y_][point.x_];
return board_[static_cast<std::size_t>(point.y_)]
[static_cast<std::size_t>(point.x_)];
}

std::unique_ptr<Tile>& getTile(Point point)
{
return board_[point.y_][point.x_];
return board_[static_cast<std::size_t>(point.y_)]
[static_cast<std::size_t>(point.x_)];
}

const std::unique_ptr<Tile>& getTileUsingPosition(
TilePosition position) const
{
return board_[position.y_][position.x_];
}

std::unique_ptr<Tile>& 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);
Expand Down

0 comments on commit 7447ed0

Please sign in to comment.