Skip to content

Commit

Permalink
Simplify a little checking user actions in Game class.
Browse files Browse the repository at this point in the history
  • Loading branch information
przemek83 committed Oct 9, 2024
1 parent d34361d commit dff685c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ std::pair<bool, Direction> Game::inputActionsToDirection(
bool found{false};
Direction direction{Direction::UP};

if (actions.find(InputAction::UP) != actions.end())
if (containsAction(actions, InputAction::UP))
{
found = true;
direction = Direction::UP;
}

if ((!found) && (actions.find(InputAction::DOWN) != actions.end()))
if ((!found) && containsAction(actions, InputAction::DOWN))
{
found = true;
direction = Direction::DOWN;
}

if ((!found) && (actions.find(InputAction::LEFT) != actions.end()))
if ((!found) && containsAction(actions, InputAction::LEFT))
{
found = true;
direction = Direction::LEFT;
}

if ((!found) && (actions.find(InputAction::RIGHT) != actions.end()))
if ((!found) && containsAction(actions, InputAction::RIGHT))
{
found = true;
direction = Direction::RIGHT;
Expand Down Expand Up @@ -95,7 +95,7 @@ void Game::movePlayerTank(const std::set<InputAction>& actions)

setPower(tank);
const auto now{std::chrono::system_clock::now()};
if ((actions.find(InputAction::FIRE) != actions.end()) && tank.canFire(now))
if (containsAction(actions, InputAction::FIRE) && tank.canFire(now))
bullets_.emplace_back(tank.fire(now));

const int tileSize{Config::getInstance().getTileSize()};
Expand Down Expand Up @@ -236,3 +236,9 @@ void Game::setPower(Tank& tank)
takenPowerUp)
tank.applyPowerUp(powerUp);
}

bool Game::containsAction(const std::set<InputAction>& actions,
InputAction action)
{
return actions.find(action) != actions.end();
}
3 changes: 3 additions & 0 deletions src/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class Game

Tank& getPlayerTank();

static bool containsAction(const std::set<InputAction>& actions,
InputAction action);

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

0 comments on commit dff685c

Please sign in to comment.