each ball is now its own source of sound
This commit is contained in:
parent
cb31a38427
commit
e8800392ec
|
|
@ -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<Ball> balls;
|
||||
std::vector<Brick> bricks;
|
||||
|
|
|
|||
58
src/Game.cpp
58
src/Game.cpp
|
|
@ -11,10 +11,15 @@
|
|||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
Ball::Ball(sf::Vector2f position_in, sf::Vector2f vel_in)
|
||||
: pos(position_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,10 +493,13 @@ void Game::imgui()
|
|||
|
||||
if (ImGui::SliderFloat("Game Volume", &volume, 0, 100, "%.1f"))
|
||||
{
|
||||
collideSound.setVolume(volume);
|
||||
brickBreakSound.setVolume(volume);
|
||||
for (auto& ball : balls)
|
||||
{
|
||||
ball.collideSound.setVolume(volume);
|
||||
ball.brickBreakSound.setVolume(volume);
|
||||
failSound.setVolume(volume);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::Text("Level config:");
|
||||
if(ImGui::InputInt("Total bricks", &totalBricks))
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in New Issue