From e8800392ec7f451bb68846e2be34d0c8bfbac383 Mon Sep 17 00:00:00 2001 From: Joseph Aquino Date: Mon, 29 Sep 2025 20:52:42 -0400 Subject: [PATCH] each ball is now its own source of sound --- include/Game.h | 10 +++++--- src/Game.cpp | 66 ++++++++++++++++++++++++++------------------------ src/main.cpp | 2 +- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/include/Game.h b/include/Game.h index 1ae163c..d16daa8 100644 --- a/include/Game.h +++ b/include/Game.h @@ -11,13 +11,17 @@ struct Ball { - Ball(sf::Vector2f position_in, sf::Vector2f vel_in); + Ball(sf::Vector2f position_in, sf::Vector2f vel_in, sf::SoundBuffer& collideSound_in, sf::SoundBuffer& brickBreakSound_in, float volume = 10.f); + + sf::Sound collideSound; + sf::Sound brickBreakSound; sf::Vector2f pos; sf::Vector2f velocity; sf::Vector2f previousPos{}; bool alive{true}; bool collided{false}; + bool brokeBrick{false}; }; struct Brick @@ -89,11 +93,9 @@ private: sf::SoundBuffer collideSoundBuffer; sf::SoundBuffer brickBreakSoundBuffer; sf::SoundBuffer failSoundBuffer; - - sf::Sound collideSound; - sf::Sound brickBreakSound; sf::Sound failSound; + Player player; std::vector balls; std::vector bricks; diff --git a/src/Game.cpp b/src/Game.cpp index e6ade1f..78c818c 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -11,10 +11,15 @@ #include #include -Ball::Ball(sf::Vector2f position_in, sf::Vector2f vel_in) - : pos(position_in) - , velocity(vel_in) -{ } +Ball::Ball(sf::Vector2f position_in, sf::Vector2f vel_in, sf::SoundBuffer& collideSound_in, sf::SoundBuffer& brickBreakSound_in, float volume) + : collideSound(collideSound_in) + , brickBreakSound(brickBreakSound_in) + , pos(position_in) + , velocity(vel_in) +{ + collideSound.setVolume(volume); + brickBreakSound.setVolume(volume); +} Brick::Brick(sf::Vector2f position_in) : pos(position_in) @@ -34,8 +39,6 @@ Game::Game(bool useImgui_in) , collideSoundBuffer("assets/sounds/hit2.wav") , brickBreakSoundBuffer("assets/sounds/hit1.wav") , failSoundBuffer("assets/sounds/fail.wav") - , collideSound(collideSoundBuffer) - , brickBreakSound(brickBreakSoundBuffer) , failSound(failSoundBuffer) , volume(10) , useImgui(useImgui_in) @@ -51,8 +54,6 @@ Game::Game(bool useImgui_in) window.setFramerateLimit(framerate); - collideSound.setVolume(volume); - brickBreakSound.setVolume(volume); failSound.setVolume(volume); player.pos = playerStartPos; @@ -263,6 +264,7 @@ void Game::checkBallCollision() for (auto& ball : balls) { ball.collided = false; + ball.brokeBrick = false; const sf::FloatRect ballBounds = {ball.pos, {ballRadius, ballRadius}}; const sf::FloatRect previousBallBounds = {ball.previousPos, {ballRadius, ballRadius}}; @@ -276,8 +278,10 @@ void Game::checkBallCollision() if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0) { ball.collided = true; + ball.brokeBrick = true; brick.alive = false; ball.velocity.x *= -1; + if (ball.pos.x < brick.pos.x) {// from the left ball.pos.x -= intersect.x; @@ -292,8 +296,10 @@ void Game::checkBallCollision() if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0) { ball.collided = true; + ball.brokeBrick = true; brick.alive = false; ball.velocity.y *= -1; + if (ball.pos.y < brick.pos.y) {// from the top ball.pos.y -= intersect.y; @@ -316,9 +322,11 @@ void Game::checkBallCollision() if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0) { ball.collided = true; + ball.brokeBrick = true; brick.alive = false; spawnExtraBall = true; ball.velocity.x *= -1; + if (ball.pos.x < brick.pos.x) {// from the left ball.pos.x -= intersect.x; @@ -333,9 +341,11 @@ void Game::checkBallCollision() if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0) { ball.collided = true; + ball.brokeBrick = true; brick.alive = false; spawnExtraBall = true; ball.velocity.y *= -1; + if (ball.pos.y < brick.pos.y) {// from the top ball.pos.y -= intersect.y; @@ -429,7 +439,7 @@ void Game::updateEntities() for (size_t i = 0; i < limit; i++) { - balls.emplace_back(ballsToAdd[i], velocityInRandomDir(ballMaxSpeed)); + balls.emplace_back(ballsToAdd[i], velocityInRandomDir(ballMaxSpeed), collideSoundBuffer, brickBreakSoundBuffer, volume); ballsToAdd[i] = {0,0}; } @@ -483,9 +493,12 @@ void Game::imgui() if (ImGui::SliderFloat("Game Volume", &volume, 0, 100, "%.1f")) { - collideSound.setVolume(volume); - brickBreakSound.setVolume(volume); - failSound.setVolume(volume); + for (auto& ball : balls) + { + ball.collideSound.setVolume(volume); + ball.brickBreakSound.setVolume(volume); + failSound.setVolume(volume); + } } ImGui::Text("Level config:"); @@ -581,30 +594,19 @@ void Game::soundSystem() { if (ball.collided) { - collideSound.play(); + ball.collideSound.play(); } + + if (ball.brokeBrick) + { + ball.brickBreakSound.play(); + } + if (!ball.alive) { failSound.play(); } } - - for (auto& brick : bricks) - { - if (!brick.alive) - { - brickBreakSound.play(); - } - } - - for (auto& brick : specialBricks) - { - if (!brick.alive) - { - brickBreakSound.play(); - } - } - } void Game::scoreSystem() @@ -664,10 +666,10 @@ void Game::run() } checkEndGame(); - + if (useImgui) { - imgui(); + imgui(); } render(); diff --git a/src/main.cpp b/src/main.cpp index 421025f..1e71d56 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,6 @@ int main(int argc, char* argv[]) Game game(useImgui); - game.run(); + game.run(); } \ No newline at end of file