diff --git a/assets/sounds/fail.wav b/assets/sounds/fail.wav new file mode 100644 index 0000000..6130163 Binary files /dev/null and b/assets/sounds/fail.wav differ diff --git a/assets/sounds/hit1.wav b/assets/sounds/hit1.wav new file mode 100644 index 0000000..c76550d Binary files /dev/null and b/assets/sounds/hit1.wav differ diff --git a/assets/sounds/hit2.wav b/assets/sounds/hit2.wav new file mode 100644 index 0000000..36822ab Binary files /dev/null and b/assets/sounds/hit2.wav differ diff --git a/include/Game.h b/include/Game.h index 474a94b..cb5f34f 100644 --- a/include/Game.h +++ b/include/Game.h @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -17,6 +18,7 @@ struct Ball sf::Vector2f velocity; sf::Vector2f previousPos{}; bool alive{true}; + bool collided{false}; }; struct Brick @@ -71,12 +73,21 @@ private: void setupLevel(); + void soundSystem(); + private: sf::Clock clock; sf::RenderWindow window; sf::Font font; sf::Text lives; + sf::SoundBuffer collideSoundBuffer; + sf::SoundBuffer brickBreakSoundBuffer; + sf::SoundBuffer failSoundBuffer; + + sf::Sound collideSound; + sf::Sound brickBreakSound; + sf::Sound failSound; Player player; std::vector balls; @@ -102,6 +113,5 @@ private: Color brickColor{}; Color playerColor{}; Color specialBrickColor{}; - bool windowCollasped{false}; static constexpr unsigned int numPhysicsUpdates{4}; }; \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 118b20f..8c3c180 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -29,6 +29,12 @@ Game::Game() : window({sf::VideoMode({ 1920u, 1080u }), "project-breakout"}) , font("assets/fonts/ChakraPetch-Regular.ttf") , lives(font) + , collideSoundBuffer("assets/sounds/hit2.wav") + , brickBreakSoundBuffer("assets/sounds/hit1.wav") + , failSoundBuffer("assets/sounds/fail.wav") + , collideSound(collideSoundBuffer) + , brickBreakSound(brickBreakSoundBuffer) + , failSound(failSoundBuffer) { if (!ImGui::SFML::Init(window)) return; @@ -38,6 +44,10 @@ Game::Game() lives.setPosition({10, static_cast(window.getSize().y - 40)}); window.setFramerateLimit(framerate); + collideSound.setVolume(50); + brickBreakSound.setVolume(50); + failSound.setVolume(50); + player.pos = playerStartPos; playerHalfSize = playerSize / 2.f; brickHalfSize = brickSize / 2.f; @@ -179,11 +189,6 @@ void Game::input() player.right = true; break; - case sf::Keyboard::Scan::F1: - windowCollasped = !windowCollasped; - ImGui::SetNextWindowCollapsed(windowCollasped); - break; - case sf::Keyboard::Scan::R: resetGame(); break; @@ -250,6 +255,7 @@ void Game::checkBallCollision() { for (auto& ball : balls) { + ball.collided = false; const sf::FloatRect ballBounds = {ball.pos, {ballRadius, ballRadius}}; const sf::FloatRect previousBallBounds = {ball.previousPos, {ballRadius, ballRadius}}; @@ -262,6 +268,7 @@ void Game::checkBallCollision() // coming from x directon if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0) { + ball.collided = true; brick.alive = false; ball.velocity.x *= -1; if (ball.pos.x < brick.pos.x) @@ -277,6 +284,7 @@ void Game::checkBallCollision() // coming from y direction if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0) { + ball.collided = true; brick.alive = false; ball.velocity.y *= -1; if (ball.pos.y < brick.pos.y) @@ -300,6 +308,7 @@ void Game::checkBallCollision() // coming from x direction if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0) { + ball.collided = true; brick.alive = false; spawnExtraBall = true; ball.velocity.x *= -1; @@ -316,6 +325,7 @@ void Game::checkBallCollision() // coming from y direction if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0) { + ball.collided = true; brick.alive = false; spawnExtraBall = true; ball.velocity.y *= -1; @@ -343,18 +353,21 @@ void Game::checkBallCollision() if (ball.pos.x - ballRadius <= 0) { + ball.collided = true; ball.pos.x = ballRadius; ball.velocity.x *= -1; } if (ball.pos.x + ballRadius >= window.getSize().x) { + ball.collided = true; ball.pos.x = window.getSize().x - ballRadius; ball.velocity.x *= -1; } if (ball.pos.y - ballRadius < 0) { + ball.collided = true; ball.pos.y = ballRadius; ball.velocity.y *= -1; } @@ -369,6 +382,7 @@ void Game::checkBallCollision() // coming from x direction if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0) { + ball.collided = true; ball.velocity.x *= -1; if (ball.pos.x < player.pos.x) {// from the left @@ -383,6 +397,7 @@ void Game::checkBallCollision() // coming from y direction if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0) { + ball.collided = true; ball.velocity.y *= -1; if (ball.pos.y < player.pos.y) {// from the top @@ -447,7 +462,7 @@ void Game::movement() void Game::imgui() { - ImGui::SetNextWindowCollapsed(windowCollasped, ImGuiCond_Once); + ImGui::SetNextWindowCollapsed(false, ImGuiCond_Once); ImGui::SetNextWindowSize({490,375}, ImGuiCond_Once); ImGui::SetNextWindowPos({26,29}, ImGuiCond_Once); if(!ImGui::Begin("menu")) @@ -459,7 +474,6 @@ void Game::imgui() ImGui::Indent(); ImGui::Text("A/D or arrow keys: move left and right"); ImGui::Text("R: reset game"); - ImGui::Text("F1: expand/collaspe this window"); ImGui::Unindent(); ImGui::Text("Level config:"); @@ -543,6 +557,30 @@ void Game::render() window.display(); } +void Game::soundSystem() +{ + for (auto& ball : balls) + { + if (ball.collided) + { + collideSound.play(); + } + if (!ball.alive) + { + failSound.play(); + } + } + + for (auto& brick : bricks) + { + if (!brick.alive) + { + brickBreakSound.play(); + } + } + +} + void Game::resetGame() { bricks.clear(); @@ -568,8 +606,10 @@ void Game::run() movement(); collision(); + + soundSystem(); } - + checkEndGame(); imgui(); @@ -596,6 +636,8 @@ void Game::runNoImgui() movement(); collision(); + + soundSystem(); } checkEndGame();