Skip to content

Commit

Permalink
Rename Tower to Turret, fix Sound class (wrazik#59)
Browse files Browse the repository at this point in the history
* Rename Tower to Turret, fix Sound class

* Install openal on MacOS

* Add nproc, add another mac dependency

* Add OpenAL
  • Loading branch information
wrazik authored Oct 16, 2023
1 parent fdcd598 commit 5ee9bd8
Show file tree
Hide file tree
Showing 19 changed files with 130 additions and 169 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/macos_clang.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu_gcc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
working-directory: ${{runner.workspace}}/build
shell: bash
# Execute the build. You can specify a specific target with "--target <NAME>"
run: cmake --build . --config $BUILD_TYPE
run: cmake --build . --config $BUILD_TYPE -- -j $(nproc)

- name: Test
working-directory: ${{runner.workspace}}/build
Expand Down
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
2 changes: 0 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
5 changes: 2 additions & 3 deletions src/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyboardPlayer>(
*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<DummyPlayer>(
*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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/DummyController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/Controllers/KeyboardController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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:
Expand Down
22 changes: 7 additions & 15 deletions src/Sound.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
#include "Sound.hpp"

Sound::Sound(const Sound& sound) {
mBuffer = sound.mBuffer;
mSound = sound.mSound;
mSound.setBuffer(mBuffer);
}
#include <iostream>

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();
}
14 changes: 6 additions & 8 deletions src/Sound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
55 changes: 12 additions & 43 deletions src/Tank/Tank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,42 @@
constexpr int TANK_INITIAL_ROTATION = 180;

Tank::Tank(float x, float y, const TankTextures &textures, std::unique_ptr<Engine> &&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<TracesHandler>(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<TracesHandler>(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<TracesHandler>(rhs.mTracesHandler->get_trace_texture(),
mBody.get_sprite(), mPos,
rhs.mTracesHandler->get_config())),
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<TracesHandler>(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;
}
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<TracesHandler>(rhs.mTracesHandler->get_trace_texture(), mBody.get_sprite(),
Expand All @@ -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);
}

Expand All @@ -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();
Expand All @@ -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<Missle> Tank::shoot() { return mTower.shoot(); }
std::optional<Missle> 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);
}
Expand Down
15 changes: 7 additions & 8 deletions src/Tank/Tank.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<sf::Texture> mBody;
std::reference_wrapper<sf::Texture> mTower;
std::reference_wrapper<sf::Texture> mTurret;
std::reference_wrapper<sf::Texture> mShot;
std::reference_wrapper<sf::Texture> mTracks;
std::reference_wrapper<sf::Texture> mMissile;
Expand All @@ -28,16 +28,16 @@ struct TankTextures {
class Tank {
public:
Tank(float x, float y, const TankTextures& textures, std::unique_ptr<Engine>&& 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);

Expand All @@ -46,7 +46,6 @@ class Tank {
void update();

[[nodiscard]] std::optional<Missle> 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;
Expand All @@ -60,7 +59,7 @@ class Tank {
int mHealth{TANK_HEALTH};

TankPart mBody;
TankTower mTower;
TankTurret mTurret;
TankHealthBar mHealthBar{TANK_HEALTH};
std::unique_ptr<Engine> mEngine;
std::unique_ptr<TracesHandler> mTracesHandler;
Expand Down
Loading

0 comments on commit 5ee9bd8

Please sign in to comment.