Skip to content

Commit

Permalink
Some simplifications in Tank class.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Sep 13, 2024
1 parent ffe1844 commit e5bf897
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/Tank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,32 @@ bool Tank::canFire(TimePoint currentTime) const
Config::getInstance().getFireDelay();
}

bool Tank::destroy()
{
if (stats_.lives_ <= 1)
{
stats_.lives_ = 0;
return true;
}

const int livesLeft = stats_.lives_ - 1;
respawn();
stats_.lives_ = livesLeft;

return false;
}

bool Tank::hit(int power)
{
if (power > stats_.shield_)
stats_.shield_ = 0;
else
stats_.shield_ -= power;

if (stats_.shield_ == 0)
{
if (stats_.lives_ <= 1)
{
stats_.lives_ = 0;
return true;
}
const int livesLeft = stats_.lives_ - 1;
respawn();
stats_.lives_ = livesLeft;
}
return false;
if (stats_.shield_ != 0)
return false;

return destroy();
}

void Tank::setSpeedUp() { ++stats_.speed_; }
Expand Down Expand Up @@ -182,12 +189,17 @@ void Tank::respawn()
direction_ = (isPlayerControlled() ? Direction::UP : Direction::DOWN);
}

int Tank::getCalculatedSpeed(float speedFactor) const
void Tank::adjustEnemySpeed(float& speed) const
{
const int tileSize{Config::getInstance().getTileSize()};
while ((speed >= 1) && ((tileSize % static_cast<int>(speed)) != 0))
speed -= 1;
}

int Tank::getCalculatedSpeed(float speedFactor) const
{
float speed{std::round(static_cast<float>(stats_.speed_) * speedFactor)};
if (!isPlayerControlled())
while ((speed >= 1) && ((tileSize % static_cast<int>(speed)) != 0))
speed -= 1;
adjustEnemySpeed(speed);
return std::max(1, static_cast<int>(speed));
}
3 changes: 3 additions & 0 deletions src/Tank.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ class Tank : public Drawable

void tierUp();

void adjustEnemySpeed(float& speed) const;
int getCalculatedSpeed(float speedFactor) const;

bool destroy();

static const int BASIC_ATTACK{1};
static const int BASIC_HEALTH{1};
static const int BASIC_SPEED{2};
Expand Down

0 comments on commit e5bf897

Please sign in to comment.