added sound effects
This commit is contained in:
parent
5f181f45e0
commit
73b4fa5920
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -5,6 +5,7 @@
|
|||
#include <Util.h>
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Audio.hpp>
|
||||
#include <imgui-SFML.h>
|
||||
#include <imgui.h>
|
||||
|
||||
|
|
@ -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<Ball> balls;
|
||||
|
|
@ -102,6 +113,5 @@ private:
|
|||
Color brickColor{};
|
||||
Color playerColor{};
|
||||
Color specialBrickColor{};
|
||||
bool windowCollasped{false};
|
||||
static constexpr unsigned int numPhysicsUpdates{4};
|
||||
};
|
||||
58
src/Game.cpp
58
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<float>(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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue