Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

synchronization #5

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
387 changes: 3 additions & 384 deletions .gitattributes

Large diffs are not rendered by default.

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
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ add_library(
src/background/Ground.cpp
src/background/RoadGenerator.cpp
src/background/SurfaceGenerator.cpp
src/Animation.cpp
src/Board.cpp
src/Controllers/KeyboardController.cpp
src/Controllers/DummyController.cpp
src/Missle.cpp
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 @@ -72,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
40 changes: 20 additions & 20 deletions res/License.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
Top-down Tanks Redux
by Kenney Vleugels (Kenney.nl)
------------------------------
License (Creative Commons Zero, CC0)
http://creativecommons.org/publicdomain/zero/1.0/
You may use these assets in personal and commercial projects.
Credit (Kenney or www.kenney.nl) would be nice but is not mandatory.
------------------------------
Donate: http://support.kenney.nl
Request: http://request.kenney.nl
Follow on Twitter for updates:


Top-down Tanks Redux

by Kenney Vleugels (Kenney.nl)

------------------------------

License (Creative Commons Zero, CC0)
http://creativecommons.org/publicdomain/zero/1.0/

You may use these assets in personal and commercial projects.
Credit (Kenney or www.kenney.nl) would be nice but is not mandatory.

------------------------------

Donate: http://support.kenney.nl
Request: http://request.kenney.nl

Follow on Twitter for updates:
@KenneyNL
3 changes: 3 additions & 0 deletions res/Sounds/explosion.flac
Git LFS file not shown
29 changes: 29 additions & 0 deletions src/Animation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "Animation.hpp"

#include <SFML/Graphics/Sprite.hpp>

Animation::Animation(const std::string& file_name, int frame_count, double animation_time,
sf::Vector2f position, TextureStore& store)
: mPosition(position), mStore(store) {
for (int i = 1; i <= frame_count; ++i) {
mAnimationFiles.push_back(file_name + std::to_string(i) + ".png");
}
mDeltaTime = animation_time / frame_count;
}
void Animation::draw(sf::RenderWindow& render_window) {
auto elapsed_time = mClock.getElapsedTime();

if (elapsed_time.asMilliseconds() > mDeltaTime) {
mClock.restart();
mCurrentFrame++;
if (mCurrentFrame >= mAnimationFiles.size()) {
mIsDrawingFinished = true;
return;
}
}
sf::Sprite sprite;
sprite.setTexture(mStore.get().get_texture(mAnimationFiles[mCurrentFrame]));
sprite.setPosition(mPosition - sf::Vector2f{sprite.getGlobalBounds().width / 2,
sprite.getGlobalBounds().height / 2});
render_window.draw(sprite);
}
28 changes: 28 additions & 0 deletions src/Animation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once
#include <SFML/Graphics/RenderWindow.hpp>
#include <string>
#include <vector>

#include "TextureStore.hpp"

class Animation {
public:
Animation(const std::string& file_name, int frame_count, double animation_time,
sf::Vector2f position, TextureStore& store);
void draw(sf::RenderWindow& render_window);
bool is_finished() const { return mIsDrawingFinished; }

Animation(const Animation&) = delete;
Animation& operator=(const Animation&) = delete;
Animation(Animation&&) = default;
Animation& operator=(Animation&&) = default;

private:
std::vector<std::string> mAnimationFiles;
int mCurrentFrame = 0;
bool mIsDrawingFinished = false;
double mDeltaTime = 0;
sf::Clock mClock;
sf::Vector2f mPosition;
std::reference_wrapper<TextureStore> mStore;
};
18 changes: 14 additions & 4 deletions src/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
#include "Tank/TankFactory.hpp"
#include "TracesHandler.hpp"

Board::Board() : mWindow(sf::VideoMode(WIDTH, HEIGHT), "TankBotFight"), mBackground(mStore) {
Board::Board()
: mWindow(sf::VideoMode(WIDTH, HEIGHT), "TankBotFight"),
mBackground(mStore),
mTankExplodeSound("explosion.flac") {
constexpr float TANK_X = WIDTH / 2.0f;
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 All @@ -40,6 +42,9 @@ void Board::draw() {
for (auto& missle : mMissles) {
missle.draw(mWindow);
}
for (auto& animation : mAnimations) {
animation.draw(mWindow);
};
display_speed();
mWindow.display();
}
Expand Down Expand Up @@ -100,11 +105,16 @@ void Board::update_players() {
}
player->get_tank().take_damage((*it).get_damage());
if (!player->get_tank().is_alive()) {
mTankExplodeSound.play();
mAnimations.push_back(
Animation("explosion", 5, 500, player->get_tank().get_position(), mStore));
player.reset();
}
missiles_collided.push_back(*it);
};

std::erase_if(mAnimations, [](const auto& animation) { return animation.is_finished(); });

update_player_if_hit(mKeyboardPlayer);
update_player_if_hit(mDummyPlayer);

Expand Down
5 changes: 4 additions & 1 deletion src/Board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
#include <SFML/Graphics.hpp>
#include <memory>

#include "Animation.hpp"
#include "Missle.hpp"
#include "Players/DummyPlayer.hpp"
#include "Players/KeyboardPlayer.hpp"
#include "background/Background.hpp"

class Board {
public:
Board();

void register_missile(const Missle& missile);
void register_animation(const Animation& animation);
void run();

private:
Expand All @@ -29,4 +30,6 @@ class Board {
std::vector<Missle> mMissles;
sf::Font mFont;
sf::Text mText;
Sound mTankExplodeSound;
std::vector<Animation> mAnimations;
};
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
1 change: 1 addition & 0 deletions src/Missle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <SFML/Graphics.hpp>

#include "MovementState.hpp"

class Missle {
public:
Missle(sf::Texture& texture, const MovementState& state, unsigned int damage);
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);
};
Loading
Loading