From 5ee9bd8efa05870a9d5d58c8daf30c1de0c150cd Mon Sep 17 00:00:00 2001 From: Wojciech Razik Date: Mon, 16 Oct 2023 13:12:57 +0200 Subject: [PATCH] Rename Tower to Turret, fix Sound class (#59) * Rename Tower to Turret, fix Sound class * Install openal on MacOS * Add nproc, add another mac dependency * Add OpenAL --- .github/workflows/macos_clang.yaml | 8 +++- .github/workflows/ubuntu_gcc.yaml | 2 +- CMakeLists.txt | 7 ++- Readme.md | 2 - src/Board.cpp | 5 +- src/Controllers/DummyController.cpp | 4 +- src/Controllers/KeyboardController.cpp | 12 ++--- src/Sound.cpp | 22 +++------ src/Sound.hpp | 14 +++--- src/Tank/Tank.cpp | 55 +++++----------------- src/Tank/Tank.hpp | 15 +++--- src/Tank/TankFactory.cpp | 10 ++-- src/Tank/TankFactory.hpp | 2 +- src/Tank/{TankTower.cpp => TankTurret.cpp} | 48 +++++++++---------- src/Tank/{TankTower.hpp => TankTurret.hpp} | 12 ++--- test/CMakeLists.txt | 2 +- test/TankTests.cpp | 18 ++++--- test/TankTowerTests.cpp | 31 ------------ test/TankTurretTests.cpp | 30 ++++++++++++ 19 files changed, 130 insertions(+), 169 deletions(-) rename src/Tank/{TankTower.cpp => TankTurret.cpp} (59%) rename src/Tank/{TankTower.hpp => TankTurret.hpp} (78%) delete mode 100644 test/TankTowerTests.cpp create mode 100644 test/TankTurretTests.cpp diff --git a/.github/workflows/macos_clang.yaml b/.github/workflows/macos_clang.yaml index 2c1529c..1999441 100644 --- a/.github/workflows/macos_clang.yaml +++ b/.github/workflows/macos_clang.yaml @@ -24,11 +24,15 @@ jobs: - name: Checkout LFS objects run: git lfs checkout + - name: Install OpenAl audio + run: | + brew install openal-soft + - name: Install vcpkg packages # vcpkg binary can only be compiled using gcc for now (bootstrap step) run: | ./vcpkg/bootstrap-vcpkg.sh - + - name: Create Build Environment run: | cmake -E make_directory ${{runner.workspace}}/build @@ -46,4 +50,4 @@ jobs: working-directory: ${{runner.workspace}}/build shell: bash run: | - cmake --build . --config $BUILD_TYPE + cmake --build . --config $BUILD_TYPE -- -j $(sysctl -n hw.logicalcpu) diff --git a/.github/workflows/ubuntu_gcc.yaml b/.github/workflows/ubuntu_gcc.yaml index 928202a..5822faf 100644 --- a/.github/workflows/ubuntu_gcc.yaml +++ b/.github/workflows/ubuntu_gcc.yaml @@ -70,7 +70,7 @@ jobs: working-directory: ${{runner.workspace}}/build shell: bash # Execute the build. You can specify a specific target with "--target " - run: cmake --build . --config $BUILD_TYPE + run: cmake --build . --config $BUILD_TYPE -- -j $(nproc) - name: Test working-directory: ${{runner.workspace}}/build diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d876d5..f2ba953 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,7 +42,7 @@ add_library( src/Tank/Tank.cpp src/Tank/TankFactory.cpp src/Tank/TankPart.cpp - src/Tank/TankTower.cpp + src/Tank/TankTurret.cpp src/Tank/TankHealthBar.cpp src/TextureStore.cpp src/Random.cpp @@ -73,9 +73,14 @@ find_package( REQUIRED) find_package(range-v3 REQUIRED) find_package(Microsoft.GSL CONFIG REQUIRED) +if (APPLE) + find_package(OpenAL REQUIRED) + target_link_libraries(tank_bot_fight PRIVATE OpenAL::OpenAL) +endif() target_link_libraries(tank_bot_fight_lib PUBLIC sfml-graphics ${EXTRA_LIBS} Microsoft.GSL::GSL) target_link_libraries(tank_bot_fight PRIVATE tank_bot_fight_lib sfml-graphics sfml-audio) + add_subdirectory(test) diff --git a/Readme.md b/Readme.md index 698dc49..d1f6335 100644 --- a/Readme.md +++ b/Readme.md @@ -29,8 +29,6 @@ You have to remember about two things: - Dependencies are installed via vcpkg - - ## Downloading the repo So, before you start, you must first download checkout git-lfs (https://git-lfs.github.com/) diff --git a/src/Board.cpp b/src/Board.cpp index 69d1537..089cb2f 100644 --- a/src/Board.cpp +++ b/src/Board.cpp @@ -19,12 +19,11 @@ Board::Board() constexpr float TANK_Y = 50.f; constexpr float TANK2_X = WIDTH / 2.0f; constexpr float TANK2_Y = 400.0f; - Sound tank_shot_sound("tank_shot.flac"); mWindow.setFramerateLimit(30); mKeyboardPlayer = std::make_unique( - *this, TankFactory::Random(mStore, TANK_X, TANK_Y, tank_shot_sound)); + *this, TankFactory::Random(mStore, TANK_X, TANK_Y, Sound("tank_shot.flac"))); mDummyPlayer = std::make_unique( - *this, TankFactory::Random(mStore, TANK2_X, TANK2_Y, tank_shot_sound)); + *this, TankFactory::Random(mStore, TANK2_X, TANK2_Y, Sound("tank_shot.flac"))); mFont.loadFromFile(files::asset_path() + "DejaVuSans.ttf"); mText.setFont(mFont); } diff --git a/src/Controllers/DummyController.cpp b/src/Controllers/DummyController.cpp index c36ca4d..a055c57 100644 --- a/src/Controllers/DummyController.cpp +++ b/src/Controllers/DummyController.cpp @@ -31,11 +31,11 @@ void DummyController::update() { break; case DummyMove::TurnLeft: mTank.rotate_body(Rotation::Clockwise); - mTank.rotate_tower(Rotation::Clockwise); + mTank.rotate_turret(Rotation::Clockwise); break; case DummyMove::TurnRight: mTank.rotate_body(Rotation::Counterclockwise); - mTank.rotate_tower(Rotation::Counterclockwise); + mTank.rotate_turret(Rotation::Counterclockwise); break; case DummyMove::Idle: mTank.set_gear(Gear::Neutral); diff --git a/src/Controllers/KeyboardController.cpp b/src/Controllers/KeyboardController.cpp index 8d54bdb..e222963 100644 --- a/src/Controllers/KeyboardController.cpp +++ b/src/Controllers/KeyboardController.cpp @@ -12,17 +12,17 @@ void KeyboardController::update(const sf::Event& event) { switch (event.key.code) { case sf::Keyboard::A: mTank.rotate_body(Rotation::Counterclockwise); - mTank.rotate_tower(Rotation::Counterclockwise); + mTank.rotate_turret(Rotation::Counterclockwise); break; case sf::Keyboard::D: mTank.rotate_body(Rotation::Clockwise); - mTank.rotate_tower(Rotation::Clockwise); + mTank.rotate_turret(Rotation::Clockwise); break; case sf::Keyboard::Left: - mTank.rotate_tower(Rotation::Counterclockwise); + mTank.rotate_turret(Rotation::Counterclockwise); break; case sf::Keyboard::Right: - mTank.rotate_tower(Rotation::Clockwise); + mTank.rotate_turret(Rotation::Clockwise); break; case sf::Keyboard::W: mTank.set_gear(Gear::Drive); @@ -44,11 +44,11 @@ void KeyboardController::update(const sf::Event& event) { case sf::Keyboard::A: case sf::Keyboard::D: mTank.rotate_body(Rotation::None); - mTank.rotate_tower(Rotation::None); + mTank.rotate_turret(Rotation::None); break; case sf::Keyboard::Left: case sf::Keyboard::Right: - mTank.rotate_tower(Rotation::None); + mTank.rotate_turret(Rotation::None); break; case sf::Keyboard::W: case sf::Keyboard::S: diff --git a/src/Sound.cpp b/src/Sound.cpp index 617ac13..f694682 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -1,23 +1,15 @@ #include "Sound.hpp" -Sound::Sound(const Sound& sound) { - mBuffer = sound.mBuffer; - mSound = sound.mSound; - mSound.setBuffer(mBuffer); -} +#include Sound::Sound(const std::string& file_name) { - if (load_from_file(file_name)) { - mSound.setBuffer(mBuffer); - } else { + std::string path_to_file = files::asset_path() + "Sounds/" + file_name; + if (!mBuffer.loadFromFile(path_to_file)) { throw std::runtime_error("couldn't load sound"); } } -void Sound::play() { mSound.play(); } - -bool Sound::load_from_file(std::string file_name) { - std::string path_to_file = files::asset_path() + "Sounds/" + file_name; - - return mBuffer.loadFromFile(path_to_file); -} +void Sound::play() { + mSound.setBuffer(mBuffer); + mSound.play(); +} \ No newline at end of file diff --git a/src/Sound.hpp b/src/Sound.hpp index df26749..bb088fe 100644 --- a/src/Sound.hpp +++ b/src/Sound.hpp @@ -6,18 +6,16 @@ class Sound { private: - // sound data is not stored directly in sf::Sound, it is stored in sf::SoundBuffer sf::SoundBuffer mBuffer; - - // to play a sound, sf::Sound instance is needed. sf::Sound mSound; public: - // default constructor is needed for testing - Sound() = default; - Sound(const Sound& sound); + Sound(Sound&&) noexcept = default; + Sound& operator=(Sound&&) noexcept = default; + + Sound(const Sound& sound) = delete; + Sound& operator=(const Sound& sound) = delete; - Sound(const std::string& file_name); + explicit Sound(const std::string& file_name); void play(); - bool load_from_file(std::string file_name); }; diff --git a/src/Tank/Tank.cpp b/src/Tank/Tank.cpp index c8c24cd..b243837 100644 --- a/src/Tank/Tank.cpp +++ b/src/Tank/Tank.cpp @@ -9,39 +9,27 @@ constexpr int TANK_INITIAL_ROTATION = 180; Tank::Tank(float x, float y, const TankTextures &textures, std::unique_ptr &&engine, - const Sound &shot_sound, const TracesHandlerConfig &traces_handler_config, + Sound&& shot_sound, const TracesHandlerConfig &traces_handler_config, const std::chrono::milliseconds &shot_cooldown) : mPos({x, y}), mBody(textures.mBody), - mTower(TankTowerTextures{.mTower = textures.mTower, + mTurret(TankTurretTextures{.mTurret = textures.mTurret, .mShotAnimation = textures.mShot, .mMissile = textures.mMissile}, - shot_cooldown, shot_sound), + shot_cooldown, std::move(shot_sound)), mEngine(std::move(engine)), mTracesHandler(std::make_unique(textures.mTracks, mBody.get_sprite(), mPos, traces_handler_config)) { set_rotation(TANK_INITIAL_ROTATION); mBody.get_sprite().setPosition(mPos); - mTower.set_position(mPos); + mTurret.set_position(mPos); } -Tank::Tank(const Tank &rhs) - : mPos(rhs.mPos), - mCurrentSpeed(rhs.mCurrentSpeed), - mBody(rhs.mBody), - mTower(rhs.mTower), - mEngine(rhs.mEngine->copy()), - mTracesHandler(std::make_unique(rhs.mTracesHandler->get_trace_texture(), - mBody.get_sprite(), mPos, - rhs.mTracesHandler->get_config())), - mHealth(rhs.mHealth), - mHealthBar(rhs.mHealthBar) {} - Tank::Tank(Tank &&rhs) noexcept : mPos(rhs.mPos), mCurrentSpeed(rhs.mCurrentSpeed), mBody(std::move(rhs.mBody)), - mTower(std::move(rhs.mTower)), + mTurret(std::move(rhs.mTurret)), mEngine(std::move(rhs.mEngine)), mTracesHandler(std::make_unique(rhs.mTracesHandler->get_trace_texture(), mBody.get_sprite(), mPos, @@ -49,23 +37,6 @@ Tank::Tank(Tank &&rhs) noexcept mHealth(rhs.mHealth), mHealthBar(std::move(rhs.mHealthBar)) {} -Tank &Tank::operator=(const Tank &rhs) { - if (this == &rhs) { - return *this; - } - mPos = rhs.mPos; - mCurrentSpeed = rhs.mCurrentSpeed; - mBody = rhs.mBody; - mTower = rhs.mTower; - mEngine = rhs.mEngine->copy(); - mTracesHandler = - std::make_unique(rhs.mTracesHandler->get_trace_texture(), mBody.get_sprite(), - mPos, rhs.mTracesHandler->get_config()); - mHealth = rhs.mHealth; - mHealthBar = rhs.mHealthBar; - return *this; -} - Tank &Tank::operator=(Tank &&rhs) noexcept { if (this == &rhs) { return *this; @@ -73,7 +44,7 @@ Tank &Tank::operator=(Tank &&rhs) noexcept { mPos = rhs.mPos; mCurrentSpeed = rhs.mCurrentSpeed; mBody = std::move(rhs.mBody); - mTower = std::move(rhs.mTower); + mTurret = std::move(rhs.mTurret); mEngine = std::move(rhs.mEngine); mTracesHandler = std::make_unique(rhs.mTracesHandler->get_trace_texture(), mBody.get_sprite(), @@ -87,10 +58,10 @@ void Tank::set_gear(Gear gear) { mEngine->set_gear(gear); } void Tank::rotate_body(Rotation r) { mBody.rotate(r); } -void Tank::rotate_tower(Rotation r) { mTower.rotate(r); } +void Tank::rotate_turret(Rotation r) { mTurret.rotate(r); } void Tank::set_rotation(const float angle) { - mTower.set_rotation(angle); + mTurret.set_rotation(angle); mBody.set_rotation(angle); } @@ -108,7 +79,7 @@ sf::FloatRect Tank::get_body_rect() const { return mBody.get_sprite().getGlobalB void Tank::update() { mBody.update(); - mTower.update(); + mTurret.update(); mEngine->update(); update_position(); mTracesHandler->update(); @@ -125,21 +96,19 @@ void Tank::update_position() { mPos.y = new_pos.y; } mBody.get_sprite().setPosition(mPos); - mTower.set_position(mPos); + mTurret.set_position(mPos); mHealthBar.set_position(mPos); } -std::optional Tank::shoot() { return mTower.shoot(); } +std::optional Tank::shoot() { return mTurret.shoot(); } float Tank::get_current_speed() const { return mEngine->get_current_speed(); } bool Tank::is_alive() const { return mHealth > 0; } -float Tank::get_tower_rotation() const { return mTower.get_rotation(); } - void Tank::draw(sf::RenderWindow &window) { mBody.draw(window); - mTower.draw(window); + mTurret.draw(window); draw_tracks(window); mHealthBar.draw(window); } diff --git a/src/Tank/Tank.hpp b/src/Tank/Tank.hpp index 4873faf..5af8977 100644 --- a/src/Tank/Tank.hpp +++ b/src/Tank/Tank.hpp @@ -12,14 +12,14 @@ #include "Sound.hpp" #include "Tank/TankHealthBar.hpp" #include "Tank/TankPart.hpp" -#include "Tank/TankTower.hpp" +#include "Tank/TankTurret.hpp" #include "TracesHandler.hpp" constexpr int TANK_HEALTH = 100; struct TankTextures { std::reference_wrapper mBody; - std::reference_wrapper mTower; + std::reference_wrapper mTurret; std::reference_wrapper mShot; std::reference_wrapper mTracks; std::reference_wrapper mMissile; @@ -28,16 +28,16 @@ struct TankTextures { class Tank { public: Tank(float x, float y, const TankTextures& textures, std::unique_ptr&& engine, - const Sound& shot_sound, const TracesHandlerConfig& traces_handler_config = {}, + Sound&& shot_sound, const TracesHandlerConfig& traces_handler_config = {}, const std::chrono::milliseconds& shot_cooldown = std::chrono::milliseconds{500}); - Tank(const Tank& rhs); + Tank(const Tank& rhs) = delete; Tank(Tank&& rhs) noexcept; - Tank& operator=(const Tank& rhs); + Tank& operator=(const Tank& rhs) = delete; Tank& operator=(Tank&& rhs) noexcept; ~Tank() = default; void rotate_body(Rotation r); - void rotate_tower(Rotation r); + void rotate_turret(Rotation r); void set_rotation(float angle); void take_damage(unsigned int damage); @@ -46,7 +46,6 @@ class Tank { void update(); [[nodiscard]] std::optional shoot(); - [[nodiscard]] float get_tower_rotation() const; [[nodiscard]] sf::Vector2f get_position() const; [[nodiscard]] sf::FloatRect get_body_rect() const; [[nodiscard]] float get_current_speed() const; @@ -60,7 +59,7 @@ class Tank { int mHealth{TANK_HEALTH}; TankPart mBody; - TankTower mTower; + TankTurret mTurret; TankHealthBar mHealthBar{TANK_HEALTH}; std::unique_ptr mEngine; std::unique_ptr mTracesHandler; diff --git a/src/Tank/TankFactory.cpp b/src/Tank/TankFactory.cpp index 91086d4..1c844ef 100644 --- a/src/Tank/TankFactory.cpp +++ b/src/Tank/TankFactory.cpp @@ -7,16 +7,16 @@ #include "TextureStore.hpp" std::unique_ptr TankFactory::Random(TextureStore& store, const float x, const float y, - const Sound& shot_sound) { + Sound&& shot_sound) { using namespace std::string_literals; const sf::IntRect TRACKS_TEXTURE_RECT = {0, 0, 37, 48}; auto& body_texture = store.get_texture(one_of("tankBody_red.png"s, "tankBody_dark.png"s, "tankBody_blue.png"s, "tankBody_green.png"s)); body_texture.setSmooth(true); - auto& tower_texture = + auto& turret_texture = store.get_texture(one_of("tankDark_barrel2_outline.png", "tankRed_barrel2_outline.png", "tankGreen_barrel2_outline.png", "tankBlue_barrel2_outline.png")); - tower_texture.setSmooth(true); + turret_texture.setSmooth(true); auto& shot_texture = store.get_texture("shotOrange.png"); shot_texture.setSmooth(true); auto& tracks_texture = store.get_texture("tracksSmall.png", TRACKS_TEXTURE_RECT); @@ -27,12 +27,12 @@ std::unique_ptr TankFactory::Random(TextureStore& store, const float x, co return std::make_unique(x, y, TankTextures{.mBody = body_texture, - .mTower = tower_texture, + .mTurret = turret_texture, .mShot = shot_texture, .mTracks = tracks_texture, .mMissile = missile_texture}, std::make_unique( SquareRootEngineConfig{.mStepCount = 70, .mMaxSpeed = 5.f}), - shot_sound, + std::move(shot_sound), TracesHandlerConfig{.mMaxTraceAge = 50, .mDecayRate = 0.1f}); } diff --git a/src/Tank/TankFactory.hpp b/src/Tank/TankFactory.hpp index e3b7635..91c2f58 100644 --- a/src/Tank/TankFactory.hpp +++ b/src/Tank/TankFactory.hpp @@ -8,5 +8,5 @@ class Sound; class TankFactory { public: [[nodiscard]] static std::unique_ptr Random(TextureStore& store, float x, float y, - const Sound& shot_sound); + Sound&& shot_sound); }; diff --git a/src/Tank/TankTower.cpp b/src/Tank/TankTurret.cpp similarity index 59% rename from src/Tank/TankTower.cpp rename to src/Tank/TankTurret.cpp index cc27301..70e0161 100644 --- a/src/Tank/TankTower.cpp +++ b/src/Tank/TankTurret.cpp @@ -1,4 +1,4 @@ -#include "TankTower.hpp" +#include "TankTurret.hpp" #include @@ -10,53 +10,53 @@ constexpr std::chrono::milliseconds SHOT_ANIMATION_DURATION = std::chrono::milli constexpr auto SHOT_COOLDOWN = std::chrono::milliseconds{500}; constexpr unsigned int MISSLE_DAMAGE = 20; -TankTower::TankTower(const TankTowerTextures& textures, - const std::chrono::milliseconds& shot_cooldown, const Sound& shot_sound) - : mTower(textures.mTower), +TankTurret::TankTurret(const TankTurretTextures& textures, + const std::chrono::milliseconds& shot_cooldown, Sound&& shot_sound) + : mTurret(textures.mTurret), mShotAnimation(textures.mShotAnimation), mMissileTexture(textures.mMissile), - mShotSound(shot_sound), + mShotSound(std::move(shot_sound)), mShotCooldown(shot_cooldown) {} -void TankTower::set_position(const sf::Vector2f& pos) { - mTower.get_sprite().setPosition(pos); +void TankTurret::set_position(const sf::Vector2f& pos) { + mTurret.get_sprite().setPosition(pos); mShotAnimation.get_sprite().setPosition(calculate_shot_position()); } -sf::Vector2f TankTower::calculate_shot_position() const { - const auto tower_rotation = mTower.get_rotation(); - const auto& [x, y] = mTower.get_sprite().getPosition(); +sf::Vector2f TankTurret::calculate_shot_position() const { + const auto turret_rotation = mTurret.get_rotation(); + const auto& [x, y] = mTurret.get_sprite().getPosition(); return sf::Vector2f{ x + SHOT_ANIMATION_DISTANCE * - gsl::narrow_cast(cos(to_radians(tower_rotation - ROTATION_OFFSET))), + gsl::narrow_cast(cos(to_radians(turret_rotation - ROTATION_OFFSET))), y + SHOT_ANIMATION_DISTANCE * - gsl::narrow_cast(sin(to_radians(tower_rotation - ROTATION_OFFSET)))}; + gsl::narrow_cast(sin(to_radians(turret_rotation - ROTATION_OFFSET)))}; } -void TankTower::set_rotation(float angle) { - mTower.set_rotation(angle); +void TankTurret::set_rotation(float angle) { + mTurret.set_rotation(angle); mShotAnimation.set_rotation(angle); } -void TankTower::rotate(Rotation r) { - mTower.rotate(r); +void TankTurret::rotate(Rotation r) { + mTurret.rotate(r); mShotAnimation.rotate(r); } -void TankTower::draw(sf::RenderWindow& window) { - mTower.draw(window); +void TankTurret::draw(sf::RenderWindow& window) { + mTurret.draw(window); if (mDrawShot) { mShotAnimation.draw(window); } } -void TankTower::update() { - mTower.update(); +void TankTurret::update() { + mTurret.update(); mShotAnimation.update(); update_shot_time(); } -void TankTower::update_shot_time() { +void TankTurret::update_shot_time() { auto now = std::chrono::system_clock::now(); auto elapsed = std::chrono::duration_cast(now - mLastShot); if (elapsed > SHOT_ANIMATION_DURATION) { @@ -64,7 +64,7 @@ void TankTower::update_shot_time() { } } -std::optional TankTower::shoot() { +std::optional TankTurret::shoot() { auto now = std::chrono::system_clock::now(); auto elapsed = std::chrono::duration_cast(now - mLastShot); if (elapsed >= mShotCooldown) { @@ -73,10 +73,10 @@ std::optional TankTower::shoot() { mShotSound.play(); const auto& [x, y] = calculate_shot_position(); return std::make_optional( - mMissileTexture, MovementState{.mX = x, .mY = y, .mAngle = mTower.get_rotation()}, + mMissileTexture, MovementState{.mX = x, .mY = y, .mAngle = mTurret.get_rotation()}, MISSLE_DAMAGE); } return std::nullopt; } -float TankTower::get_rotation() const { return mTower.get_rotation(); } +float TankTurret::get_rotation() const { return mTurret.get_rotation(); } diff --git a/src/Tank/TankTower.hpp b/src/Tank/TankTurret.hpp similarity index 78% rename from src/Tank/TankTower.hpp rename to src/Tank/TankTurret.hpp index a10ff09..ba36023 100644 --- a/src/Tank/TankTower.hpp +++ b/src/Tank/TankTurret.hpp @@ -8,16 +8,16 @@ #include "Sound.hpp" #include "Tank/TankPart.hpp" -struct TankTowerTextures { - std::reference_wrapper mTower; +struct TankTurretTextures { + std::reference_wrapper mTurret; std::reference_wrapper mShotAnimation; std::reference_wrapper mMissile; }; -class TankTower { +class TankTurret { public: - TankTower(const TankTowerTextures& textures, const std::chrono::milliseconds& shot_cooldown, - const Sound& shot_sound); + TankTurret(const TankTurretTextures& textures, const std::chrono::milliseconds& shot_cooldown, + Sound&& shot_sound); void set_position(const sf::Vector2f& pos); void set_rotation(float angle); @@ -33,7 +33,7 @@ class TankTower { sf::Vector2f calculate_shot_position() const; void update_shot_time(); - TankPart mTower; + TankPart mTurret; TankPart mShotAnimation; Sound mShotSound; std::reference_wrapper mMissileTexture; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 217b12e..6ab0ce7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,7 +25,7 @@ add_executable(run_tests UtilityTests.cpp TracesHandlerTests.cpp TankPartTests.cpp - TankTowerTests.cpp + TankTurretTests.cpp ) target_include_directories(run_tests PRIVATE diff --git a/test/TankTests.cpp b/test/TankTests.cpp index 3fc604c..1bb9a76 100644 --- a/test/TankTests.cpp +++ b/test/TankTests.cpp @@ -21,13 +21,12 @@ struct EngineMock : Engine { struct TankTestData { std::unique_ptr mBody{create_dummy_texture()}; - std::unique_ptr mTower{create_dummy_texture()}; + std::unique_ptr mTurret{create_dummy_texture()}; std::unique_ptr mShot{create_dummy_texture()}; std::unique_ptr mTracks{create_dummy_texture()}; std::unique_ptr mMissile{create_dummy_texture()}; - Sound emptySound; TankTextures mTextures{.mBody = *mBody, - .mTower = *mTower, + .mTurret = *mTurret, .mShot = *mShot, .mTracks = *mTracks, .mMissile = *mMissile}; @@ -38,12 +37,12 @@ struct TankTestData { std::chrono::milliseconds shot_cooldown = std::chrono::milliseconds{500}; unsigned int health = 100; - Tank create_tank(std::unique_ptr>&& engine) { + Tank create_tank(std::unique_ptr>&& engine) const { return {0, 0, mTextures, std::move(engine), - emptySound, + Sound("tank_shot.flac"), TracesHandlerConfig{.mMaxTraceAge = 10, .mDecayRate = 0.1f}, shot_cooldown}; } @@ -52,8 +51,7 @@ struct TankTestData { struct TankTest : TankTestData, ::testing::Test { Tank mTankSUT{create_tank(std::move(mEngine))}; - float mSpeed{1.f}; - int mAngle{90}; + float mAngle{90}; }; TEST_F(TankTest, GivenAngleRotationWhenUpdateThenShouldCallGetPositionDeltaWithAngleRotation) { @@ -83,12 +81,12 @@ TEST_F(TankTest, GivenMultipleUpdatesWhenGetPositionThenReturnsPositionDeltaSum) expect_vec2f_eq(expected_position, mTankSUT.get_position()); } -TEST_F(TankTest, RotateTower_ShouldntAffectMoving) { +TEST_F(TankTest, RotateTurret_ShouldntAffectMoving) { EXPECT_CALL(*mEngineNiceMock, get_position_delta(to_radians(mAngle))); mTankSUT.set_rotation(mAngle); - mTankSUT.rotate_tower(Rotation::Clockwise); - mTankSUT.rotate_tower(Rotation::Clockwise); + mTankSUT.rotate_turret(Rotation::Clockwise); + mTankSUT.rotate_turret(Rotation::Clockwise); mTankSUT.update(); } diff --git a/test/TankTowerTests.cpp b/test/TankTowerTests.cpp deleted file mode 100644 index 79da99d..0000000 --- a/test/TankTowerTests.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include - -#include - -#include "Sound.hpp" -#include "Tank/TankTower.hpp" -#include "TestUtility.hpp" - -class TankTowerTest : public ::testing::Test { - public: - std::unique_ptr mTowerTex{create_dummy_texture()}; - std::unique_ptr mShotTex{create_dummy_texture()}; - std::unique_ptr mMissileTex{create_dummy_texture()}; - TankTowerTextures mTextures{ - .mTower = *mTowerTex, .mShotAnimation = *mShotTex, .mMissile = *mMissileTex}; - Sound mShotSound{"tank_shot.flac"}; -}; - -TEST_F(TankTowerTest, Given0MsCooldown_WhenShotCalled_ThenMissileIsAlwaysReturned) { - TankTower mTower{mTextures, std::chrono::milliseconds{0}, mShotSound}; - EXPECT_TRUE(mTower.shoot()); - EXPECT_TRUE(mTower.shoot()); - EXPECT_TRUE(mTower.shoot()); -} - -TEST_F(TankTowerTest, GivenNonZeroCooldown_WhenShotCalled_ThenMissileIsNotAlwaysReturned) { - TankTower mTower{mTextures, std::chrono::milliseconds{500}, mShotSound}; - EXPECT_TRUE(mTower.shoot()); - EXPECT_FALSE(mTower.shoot()); - EXPECT_FALSE(mTower.shoot()); -} diff --git a/test/TankTurretTests.cpp b/test/TankTurretTests.cpp new file mode 100644 index 0000000..d746577 --- /dev/null +++ b/test/TankTurretTests.cpp @@ -0,0 +1,30 @@ +#include + +#include + +#include "Sound.hpp" +#include "Tank/TankTurret.hpp" +#include "TestUtility.hpp" + +class TankTurretTest : public ::testing::Test { + public: + std::unique_ptr mTurretTex{create_dummy_texture()}; + std::unique_ptr mShotTex{create_dummy_texture()}; + std::unique_ptr mMissileTex{create_dummy_texture()}; + TankTurretTextures mTextures{ + .mTurret = *mTurretTex, .mShotAnimation = *mShotTex, .mMissile = *mMissileTex}; +}; + +TEST_F(TankTurretTest, Given0MsCooldown_WhenShotCalled_ThenMissileIsAlwaysReturned) { + TankTurret mTurret{mTextures, std::chrono::milliseconds{0}, Sound{"tank_shot.flac"}}; + EXPECT_TRUE(mTurret.shoot()); + EXPECT_TRUE(mTurret.shoot()); + EXPECT_TRUE(mTurret.shoot()); +} + +TEST_F(TankTurretTest, GivenNonZeroCooldown_WhenShotCalled_ThenMissileIsNotAlwaysReturned) { + TankTurret mTurret{mTextures, std::chrono::milliseconds{500}, Sound{"tank_shot.flac"}}; + EXPECT_TRUE(mTurret.shoot()); + EXPECT_FALSE(mTurret.shoot()); + EXPECT_FALSE(mTurret.shoot()); +}