Skip to content

Commit

Permalink
Extract some code from Game::moveBullets method into new one.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Oct 11, 2024
1 parent 72faf9c commit c8be78f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
26 changes: 16 additions & 10 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,37 +156,31 @@ void Game::drawTanks(const Display& display) const
void Game::moveBullets()
{
const int bulletSize{Config::getInstance().getBulletSize()};
const int tileSize{Config::getInstance().getTileSize()};
auto bulletIter{bullets_.begin()};
while (bulletIter != bullets_.end())
{
tagAreaAsChanged(*bulletIter, bulletSize);
bool valid{bulletIter->move()};
tagAreaAsChanged(*bulletIter, bulletSize);

if (const Point bulletCenter{bulletIter->getCenter()};
valid && (!map_.canFly(bulletCenter)))
{
map_.hit(bulletCenter, bulletIter->getPower());
valid = false;
}

if (auto tankIter{hitTank(*bulletIter)};
if (auto tankIter{getImpactedTank(*bulletIter)};
valid && (tankIter != tanks_.end()))
{
tagAreaAsChanged(*tankIter, tileSize);
if (tankIter->hit(bulletIter->getPower()))
{
if (tankIter->isPlayerControlled())
playerDestroyed_ = true;
tanks_.erase(tankIter);
}
hitTank(tankIter, bulletIter->getPower());
valid = false;
}
bulletIter = (valid ? ++bulletIter : bullets_.erase(bulletIter));
}
}

std::list<Tank>::iterator Game::hitTank(const Bullet& bullet)
std::list<Tank>::iterator Game::getImpactedTank(const Bullet& bullet)
{
return std::find_if(
tanks_.begin(), tanks_.end(),
Expand Down Expand Up @@ -247,3 +241,15 @@ Direction Game::getEnemyTankDirection(Tank& tank)

return direction;
}

void Game::hitTank(std::list<Tank>::iterator& tankIter, int power)
{
const int tileSize{Config::getInstance().getTileSize()};
tagAreaAsChanged(*tankIter, tileSize);
if (tankIter->hit(power))
{
if (tankIter->isPlayerControlled())
playerDestroyed_ = true;
tanks_.erase(tankIter);
}
}
4 changes: 3 additions & 1 deletion src/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Game

private:
void movement(Tank& tank, Direction direction);
std::list<Tank>::iterator hitTank(const Bullet& bullet);
std::list<Tank>::iterator getImpactedTank(const Bullet& bullet);

void drawTanks(const Display& display) const;
void drawEndOfGame(Display& display, const std::string& text) const;
Expand All @@ -53,6 +53,8 @@ class Game

Direction getEnemyTankDirection(Tank& tank);

void hitTank(std::list<Tank>::iterator& tankIter, int power);

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

0 comments on commit c8be78f

Please sign in to comment.