each ball is now its own source of sound
This commit is contained in:
parent
cb31a38427
commit
e8800392ec
|
|
@ -11,13 +11,17 @@
|
||||||
|
|
||||||
struct Ball
|
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 pos;
|
||||||
sf::Vector2f velocity;
|
sf::Vector2f velocity;
|
||||||
sf::Vector2f previousPos{};
|
sf::Vector2f previousPos{};
|
||||||
bool alive{true};
|
bool alive{true};
|
||||||
bool collided{false};
|
bool collided{false};
|
||||||
|
bool brokeBrick{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Brick
|
struct Brick
|
||||||
|
|
@ -89,11 +93,9 @@ private:
|
||||||
sf::SoundBuffer collideSoundBuffer;
|
sf::SoundBuffer collideSoundBuffer;
|
||||||
sf::SoundBuffer brickBreakSoundBuffer;
|
sf::SoundBuffer brickBreakSoundBuffer;
|
||||||
sf::SoundBuffer failSoundBuffer;
|
sf::SoundBuffer failSoundBuffer;
|
||||||
|
|
||||||
sf::Sound collideSound;
|
|
||||||
sf::Sound brickBreakSound;
|
|
||||||
sf::Sound failSound;
|
sf::Sound failSound;
|
||||||
|
|
||||||
|
|
||||||
Player player;
|
Player player;
|
||||||
std::vector<Ball> balls;
|
std::vector<Ball> balls;
|
||||||
std::vector<Brick> bricks;
|
std::vector<Brick> bricks;
|
||||||
|
|
|
||||||
66
src/Game.cpp
66
src/Game.cpp
|
|
@ -11,10 +11,15 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
Ball::Ball(sf::Vector2f position_in, sf::Vector2f vel_in)
|
Ball::Ball(sf::Vector2f position_in, sf::Vector2f vel_in, sf::SoundBuffer& collideSound_in, sf::SoundBuffer& brickBreakSound_in, float volume)
|
||||||
: pos(position_in)
|
: collideSound(collideSound_in)
|
||||||
, velocity(vel_in)
|
, brickBreakSound(brickBreakSound_in)
|
||||||
{ }
|
, pos(position_in)
|
||||||
|
, velocity(vel_in)
|
||||||
|
{
|
||||||
|
collideSound.setVolume(volume);
|
||||||
|
brickBreakSound.setVolume(volume);
|
||||||
|
}
|
||||||
|
|
||||||
Brick::Brick(sf::Vector2f position_in)
|
Brick::Brick(sf::Vector2f position_in)
|
||||||
: pos(position_in)
|
: pos(position_in)
|
||||||
|
|
@ -34,8 +39,6 @@ Game::Game(bool useImgui_in)
|
||||||
, collideSoundBuffer("assets/sounds/hit2.wav")
|
, collideSoundBuffer("assets/sounds/hit2.wav")
|
||||||
, brickBreakSoundBuffer("assets/sounds/hit1.wav")
|
, brickBreakSoundBuffer("assets/sounds/hit1.wav")
|
||||||
, failSoundBuffer("assets/sounds/fail.wav")
|
, failSoundBuffer("assets/sounds/fail.wav")
|
||||||
, collideSound(collideSoundBuffer)
|
|
||||||
, brickBreakSound(brickBreakSoundBuffer)
|
|
||||||
, failSound(failSoundBuffer)
|
, failSound(failSoundBuffer)
|
||||||
, volume(10)
|
, volume(10)
|
||||||
, useImgui(useImgui_in)
|
, useImgui(useImgui_in)
|
||||||
|
|
@ -51,8 +54,6 @@ Game::Game(bool useImgui_in)
|
||||||
|
|
||||||
window.setFramerateLimit(framerate);
|
window.setFramerateLimit(framerate);
|
||||||
|
|
||||||
collideSound.setVolume(volume);
|
|
||||||
brickBreakSound.setVolume(volume);
|
|
||||||
failSound.setVolume(volume);
|
failSound.setVolume(volume);
|
||||||
|
|
||||||
player.pos = playerStartPos;
|
player.pos = playerStartPos;
|
||||||
|
|
@ -263,6 +264,7 @@ void Game::checkBallCollision()
|
||||||
for (auto& ball : balls)
|
for (auto& ball : balls)
|
||||||
{
|
{
|
||||||
ball.collided = false;
|
ball.collided = false;
|
||||||
|
ball.brokeBrick = false;
|
||||||
const sf::FloatRect ballBounds = {ball.pos, {ballRadius, ballRadius}};
|
const sf::FloatRect ballBounds = {ball.pos, {ballRadius, ballRadius}};
|
||||||
const sf::FloatRect previousBallBounds = {ball.previousPos, {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)
|
if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
|
||||||
{
|
{
|
||||||
ball.collided = true;
|
ball.collided = true;
|
||||||
|
ball.brokeBrick = true;
|
||||||
brick.alive = false;
|
brick.alive = false;
|
||||||
ball.velocity.x *= -1;
|
ball.velocity.x *= -1;
|
||||||
|
|
||||||
if (ball.pos.x < brick.pos.x)
|
if (ball.pos.x < brick.pos.x)
|
||||||
{// from the left
|
{// from the left
|
||||||
ball.pos.x -= intersect.x;
|
ball.pos.x -= intersect.x;
|
||||||
|
|
@ -292,8 +296,10 @@ void Game::checkBallCollision()
|
||||||
if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
|
if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
|
||||||
{
|
{
|
||||||
ball.collided = true;
|
ball.collided = true;
|
||||||
|
ball.brokeBrick = true;
|
||||||
brick.alive = false;
|
brick.alive = false;
|
||||||
ball.velocity.y *= -1;
|
ball.velocity.y *= -1;
|
||||||
|
|
||||||
if (ball.pos.y < brick.pos.y)
|
if (ball.pos.y < brick.pos.y)
|
||||||
{// from the top
|
{// from the top
|
||||||
ball.pos.y -= intersect.y;
|
ball.pos.y -= intersect.y;
|
||||||
|
|
@ -316,9 +322,11 @@ void Game::checkBallCollision()
|
||||||
if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
|
if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
|
||||||
{
|
{
|
||||||
ball.collided = true;
|
ball.collided = true;
|
||||||
|
ball.brokeBrick = true;
|
||||||
brick.alive = false;
|
brick.alive = false;
|
||||||
spawnExtraBall = true;
|
spawnExtraBall = true;
|
||||||
ball.velocity.x *= -1;
|
ball.velocity.x *= -1;
|
||||||
|
|
||||||
if (ball.pos.x < brick.pos.x)
|
if (ball.pos.x < brick.pos.x)
|
||||||
{// from the left
|
{// from the left
|
||||||
ball.pos.x -= intersect.x;
|
ball.pos.x -= intersect.x;
|
||||||
|
|
@ -333,9 +341,11 @@ void Game::checkBallCollision()
|
||||||
if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
|
if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
|
||||||
{
|
{
|
||||||
ball.collided = true;
|
ball.collided = true;
|
||||||
|
ball.brokeBrick = true;
|
||||||
brick.alive = false;
|
brick.alive = false;
|
||||||
spawnExtraBall = true;
|
spawnExtraBall = true;
|
||||||
ball.velocity.y *= -1;
|
ball.velocity.y *= -1;
|
||||||
|
|
||||||
if (ball.pos.y < brick.pos.y)
|
if (ball.pos.y < brick.pos.y)
|
||||||
{// from the top
|
{// from the top
|
||||||
ball.pos.y -= intersect.y;
|
ball.pos.y -= intersect.y;
|
||||||
|
|
@ -429,7 +439,7 @@ void Game::updateEntities()
|
||||||
|
|
||||||
for (size_t i = 0; i < limit; i++)
|
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};
|
ballsToAdd[i] = {0,0};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -483,9 +493,12 @@ void Game::imgui()
|
||||||
|
|
||||||
if (ImGui::SliderFloat("Game Volume", &volume, 0, 100, "%.1f"))
|
if (ImGui::SliderFloat("Game Volume", &volume, 0, 100, "%.1f"))
|
||||||
{
|
{
|
||||||
collideSound.setVolume(volume);
|
for (auto& ball : balls)
|
||||||
brickBreakSound.setVolume(volume);
|
{
|
||||||
failSound.setVolume(volume);
|
ball.collideSound.setVolume(volume);
|
||||||
|
ball.brickBreakSound.setVolume(volume);
|
||||||
|
failSound.setVolume(volume);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui::Text("Level config:");
|
ImGui::Text("Level config:");
|
||||||
|
|
@ -581,30 +594,19 @@ void Game::soundSystem()
|
||||||
{
|
{
|
||||||
if (ball.collided)
|
if (ball.collided)
|
||||||
{
|
{
|
||||||
collideSound.play();
|
ball.collideSound.play();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ball.brokeBrick)
|
||||||
|
{
|
||||||
|
ball.brickBreakSound.play();
|
||||||
|
}
|
||||||
|
|
||||||
if (!ball.alive)
|
if (!ball.alive)
|
||||||
{
|
{
|
||||||
failSound.play();
|
failSound.play();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& brick : bricks)
|
|
||||||
{
|
|
||||||
if (!brick.alive)
|
|
||||||
{
|
|
||||||
brickBreakSound.play();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& brick : specialBricks)
|
|
||||||
{
|
|
||||||
if (!brick.alive)
|
|
||||||
{
|
|
||||||
brickBreakSound.play();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::scoreSystem()
|
void Game::scoreSystem()
|
||||||
|
|
@ -664,10 +666,10 @@ void Game::run()
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEndGame();
|
checkEndGame();
|
||||||
|
|
||||||
if (useImgui)
|
if (useImgui)
|
||||||
{
|
{
|
||||||
imgui();
|
imgui();
|
||||||
}
|
}
|
||||||
|
|
||||||
render();
|
render();
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,6 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
Game game(useImgui);
|
Game game(useImgui);
|
||||||
|
|
||||||
game.run();
|
game.run();
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue