Skip to content

Commit

Permalink
Add method tagAreaAsChanged into Game class.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Oct 9, 2024
1 parent 064f052 commit e3d1db6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ void Game::movement(Tank& tank, Direction direction)
tank.setDirection(direction);

const int tileSize{Config::getInstance().getTileSize()};
map_.tagAreaAsChanged(tank.getLocation(),
{tank.getX() + tileSize, tank.getY() + tileSize});
tagAreaAsChanged(tank, tileSize);

auto [newX, newY]{tank.getNextExpectedPosition()};
if (!point_utils::isValidPoint(newX, newY, tileSize))
Expand All @@ -84,8 +83,7 @@ void Game::movement(Tank& tank, Direction direction)
if (tank.isPlayerControlled())
map_.shift(newPoint, direction);
tank.move(newPoint);
map_.tagAreaAsChanged(tank.getLocation(),
{tank.getX() + tileSize, tank.getY() + tileSize});
tagAreaAsChanged(tank, tileSize);
}
}

Expand All @@ -98,9 +96,7 @@ void Game::movePlayerTank(const std::set<InputAction>& actions)
containsAction(actions, InputAction::FIRE) && tank.canFire(now))
bullets_.emplace_back(tank.fire(now));

const int tileSize{Config::getInstance().getTileSize()};
map_.tagAreaAsChanged(tank.getLocation(),
{tank.getX() + tileSize, tank.getY() + tileSize});
tagAreaAsChanged(tank, Config::getInstance().getTileSize());

const auto [shouldMove, direction]{inputActionsToDirection(actions)};
if (shouldMove)
Expand Down Expand Up @@ -171,13 +167,9 @@ void Game::moveBullets()
const int tileSize{Config::getInstance().getTileSize()};
for (auto bulletIter = bullets_.begin(); bulletIter != bullets_.end();)
{
map_.tagAreaAsChanged(
bulletIter->getLocation(),
{bulletIter->getX() + bulletSize, bulletIter->getY() + bulletSize});
tagAreaAsChanged(*bulletIter, bulletSize);
bool valid{bulletIter->move()};
map_.tagAreaAsChanged(
bulletIter->getLocation(),
{bulletIter->getX() + bulletSize, bulletIter->getY() + bulletSize});
tagAreaAsChanged(*bulletIter, bulletSize);
if (const Point bulletCenter{bulletIter->getCenter()};
valid && (!map_.canFly(bulletCenter)))
{
Expand All @@ -188,10 +180,7 @@ void Game::moveBullets()
if (auto tankIter{hitTank(*bulletIter)};
valid && (tankIter != tanks_.end()))
{
map_.tagAreaAsChanged(
tankIter->getLocation(),
{tankIter->getX() + tileSize, tankIter->getY() + tileSize});

tagAreaAsChanged(*tankIter, tileSize);
if (tankIter->hit(bulletIter->getPower()))
{
if (tankIter->isPlayerControlled())
Expand Down Expand Up @@ -242,3 +231,9 @@ bool Game::containsAction(const std::set<InputAction>& actions,
{
return actions.find(action) != actions.end();
}

void Game::tagAreaAsChanged(Drawable& object, int size)
{
map_.tagAreaAsChanged(object.getLocation(),
{object.getX() + size, object.getY() + size});
}
2 changes: 2 additions & 0 deletions src/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Game
static bool containsAction(const std::set<InputAction>& actions,
InputAction action);

void tagAreaAsChanged(Drawable& object, int size);

Status status_;
std::mt19937 randomGenerator_;
bool playerDestroyed_{false};
Expand Down

0 comments on commit e3d1db6

Please sign in to comment.