From dff685ca8cf41bb62bc0f25900d0e9061eb3e383 Mon Sep 17 00:00:00 2001 From: przemek83 <4788832+przemek83@users.noreply.github.com> Date: Wed, 9 Oct 2024 10:15:34 +0200 Subject: [PATCH] Simplify a little checking user actions in Game class. --- src/Game.cpp | 16 +++++++++++----- src/Game.h | 3 +++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index b0da11f..76ae7fb 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -28,25 +28,25 @@ std::pair 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; @@ -95,7 +95,7 @@ void Game::movePlayerTank(const std::set& 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()}; @@ -236,3 +236,9 @@ void Game::setPower(Tank& tank) takenPowerUp) tank.applyPowerUp(powerUp); } + +bool Game::containsAction(const std::set& actions, + InputAction action) +{ + return actions.find(action) != actions.end(); +} diff --git a/src/Game.h b/src/Game.h index 03605ad..b977789 100644 --- a/src/Game.h +++ b/src/Game.h @@ -45,6 +45,9 @@ class Game Tank& getPlayerTank(); + static bool containsAction(const std::set& actions, + InputAction action); + Status status_; std::mt19937 randomGenerator_; bool playerDestroyed_{false};