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 <Util.h>
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <SFML/Audio.hpp>
|
||||||
#include <imgui-SFML.h>
|
#include <imgui-SFML.h>
|
||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
|
|
||||||
|
|
@ -17,6 +18,7 @@ struct Ball
|
||||||
sf::Vector2f velocity;
|
sf::Vector2f velocity;
|
||||||
sf::Vector2f previousPos{};
|
sf::Vector2f previousPos{};
|
||||||
bool alive{true};
|
bool alive{true};
|
||||||
|
bool collided{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Brick
|
struct Brick
|
||||||
|
|
@ -71,12 +73,21 @@ private:
|
||||||
|
|
||||||
void setupLevel();
|
void setupLevel();
|
||||||
|
|
||||||
|
void soundSystem();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::Clock clock;
|
sf::Clock clock;
|
||||||
sf::RenderWindow window;
|
sf::RenderWindow window;
|
||||||
sf::Font font;
|
sf::Font font;
|
||||||
sf::Text lives;
|
sf::Text lives;
|
||||||
|
sf::SoundBuffer collideSoundBuffer;
|
||||||
|
sf::SoundBuffer brickBreakSoundBuffer;
|
||||||
|
sf::SoundBuffer failSoundBuffer;
|
||||||
|
|
||||||
|
sf::Sound collideSound;
|
||||||
|
sf::Sound brickBreakSound;
|
||||||
|
sf::Sound failSound;
|
||||||
|
|
||||||
Player player;
|
Player player;
|
||||||
std::vector<Ball> balls;
|
std::vector<Ball> balls;
|
||||||
|
|
@ -102,6 +113,5 @@ private:
|
||||||
Color brickColor{};
|
Color brickColor{};
|
||||||
Color playerColor{};
|
Color playerColor{};
|
||||||
Color specialBrickColor{};
|
Color specialBrickColor{};
|
||||||
bool windowCollasped{false};
|
|
||||||
static constexpr unsigned int numPhysicsUpdates{4};
|
static constexpr unsigned int numPhysicsUpdates{4};
|
||||||
};
|
};
|
||||||
56
src/Game.cpp
56
src/Game.cpp
|
|
@ -29,6 +29,12 @@ Game::Game()
|
||||||
: window({sf::VideoMode({ 1920u, 1080u }), "project-breakout"})
|
: window({sf::VideoMode({ 1920u, 1080u }), "project-breakout"})
|
||||||
, font("assets/fonts/ChakraPetch-Regular.ttf")
|
, font("assets/fonts/ChakraPetch-Regular.ttf")
|
||||||
, lives(font)
|
, 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;
|
if (!ImGui::SFML::Init(window)) return;
|
||||||
|
|
@ -38,6 +44,10 @@ Game::Game()
|
||||||
lives.setPosition({10, static_cast<float>(window.getSize().y - 40)});
|
lives.setPosition({10, static_cast<float>(window.getSize().y - 40)});
|
||||||
window.setFramerateLimit(framerate);
|
window.setFramerateLimit(framerate);
|
||||||
|
|
||||||
|
collideSound.setVolume(50);
|
||||||
|
brickBreakSound.setVolume(50);
|
||||||
|
failSound.setVolume(50);
|
||||||
|
|
||||||
player.pos = playerStartPos;
|
player.pos = playerStartPos;
|
||||||
playerHalfSize = playerSize / 2.f;
|
playerHalfSize = playerSize / 2.f;
|
||||||
brickHalfSize = brickSize / 2.f;
|
brickHalfSize = brickSize / 2.f;
|
||||||
|
|
@ -179,11 +189,6 @@ void Game::input()
|
||||||
player.right = true;
|
player.right = true;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case sf::Keyboard::Scan::F1:
|
|
||||||
windowCollasped = !windowCollasped;
|
|
||||||
ImGui::SetNextWindowCollapsed(windowCollasped);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case sf::Keyboard::Scan::R:
|
case sf::Keyboard::Scan::R:
|
||||||
resetGame();
|
resetGame();
|
||||||
break;
|
break;
|
||||||
|
|
@ -250,6 +255,7 @@ void Game::checkBallCollision()
|
||||||
{
|
{
|
||||||
for (auto& ball : balls)
|
for (auto& ball : balls)
|
||||||
{
|
{
|
||||||
|
ball.collided = 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}};
|
||||||
|
|
||||||
|
|
@ -262,6 +268,7 @@ void Game::checkBallCollision()
|
||||||
// coming from x directon
|
// coming from x directon
|
||||||
if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
|
if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
|
||||||
{
|
{
|
||||||
|
ball.collided = 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)
|
||||||
|
|
@ -277,6 +284,7 @@ void Game::checkBallCollision()
|
||||||
// coming from y direction
|
// coming from y direction
|
||||||
if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
|
if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
|
||||||
{
|
{
|
||||||
|
ball.collided = 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)
|
||||||
|
|
@ -300,6 +308,7 @@ void Game::checkBallCollision()
|
||||||
// coming from x direction
|
// coming from x direction
|
||||||
if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
|
if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
|
||||||
{
|
{
|
||||||
|
ball.collided = true;
|
||||||
brick.alive = false;
|
brick.alive = false;
|
||||||
spawnExtraBall = true;
|
spawnExtraBall = true;
|
||||||
ball.velocity.x *= -1;
|
ball.velocity.x *= -1;
|
||||||
|
|
@ -316,6 +325,7 @@ void Game::checkBallCollision()
|
||||||
// coming from y direction
|
// coming from y direction
|
||||||
if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
|
if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
|
||||||
{
|
{
|
||||||
|
ball.collided = true;
|
||||||
brick.alive = false;
|
brick.alive = false;
|
||||||
spawnExtraBall = true;
|
spawnExtraBall = true;
|
||||||
ball.velocity.y *= -1;
|
ball.velocity.y *= -1;
|
||||||
|
|
@ -343,18 +353,21 @@ void Game::checkBallCollision()
|
||||||
|
|
||||||
if (ball.pos.x - ballRadius <= 0)
|
if (ball.pos.x - ballRadius <= 0)
|
||||||
{
|
{
|
||||||
|
ball.collided = true;
|
||||||
ball.pos.x = ballRadius;
|
ball.pos.x = ballRadius;
|
||||||
ball.velocity.x *= -1;
|
ball.velocity.x *= -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ball.pos.x + ballRadius >= window.getSize().x)
|
if (ball.pos.x + ballRadius >= window.getSize().x)
|
||||||
{
|
{
|
||||||
|
ball.collided = true;
|
||||||
ball.pos.x = window.getSize().x - ballRadius;
|
ball.pos.x = window.getSize().x - ballRadius;
|
||||||
ball.velocity.x *= -1;
|
ball.velocity.x *= -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ball.pos.y - ballRadius < 0)
|
if (ball.pos.y - ballRadius < 0)
|
||||||
{
|
{
|
||||||
|
ball.collided = true;
|
||||||
ball.pos.y = ballRadius;
|
ball.pos.y = ballRadius;
|
||||||
ball.velocity.y *= -1;
|
ball.velocity.y *= -1;
|
||||||
}
|
}
|
||||||
|
|
@ -369,6 +382,7 @@ void Game::checkBallCollision()
|
||||||
// coming from x direction
|
// coming from x direction
|
||||||
if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
|
if (intersect.x > 0 && previousIntersect.x <= 0 && intersect.y > 0)
|
||||||
{
|
{
|
||||||
|
ball.collided = true;
|
||||||
ball.velocity.x *= -1;
|
ball.velocity.x *= -1;
|
||||||
if (ball.pos.x < player.pos.x)
|
if (ball.pos.x < player.pos.x)
|
||||||
{// from the left
|
{// from the left
|
||||||
|
|
@ -383,6 +397,7 @@ void Game::checkBallCollision()
|
||||||
// coming from y direction
|
// coming from y direction
|
||||||
if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
|
if (intersect.y > 0 && previousIntersect.y <= 0 && intersect.x > 0)
|
||||||
{
|
{
|
||||||
|
ball.collided = true;
|
||||||
ball.velocity.y *= -1;
|
ball.velocity.y *= -1;
|
||||||
if (ball.pos.y < player.pos.y)
|
if (ball.pos.y < player.pos.y)
|
||||||
{// from the top
|
{// from the top
|
||||||
|
|
@ -447,7 +462,7 @@ void Game::movement()
|
||||||
|
|
||||||
void Game::imgui()
|
void Game::imgui()
|
||||||
{
|
{
|
||||||
ImGui::SetNextWindowCollapsed(windowCollasped, ImGuiCond_Once);
|
ImGui::SetNextWindowCollapsed(false, ImGuiCond_Once);
|
||||||
ImGui::SetNextWindowSize({490,375}, ImGuiCond_Once);
|
ImGui::SetNextWindowSize({490,375}, ImGuiCond_Once);
|
||||||
ImGui::SetNextWindowPos({26,29}, ImGuiCond_Once);
|
ImGui::SetNextWindowPos({26,29}, ImGuiCond_Once);
|
||||||
if(!ImGui::Begin("menu"))
|
if(!ImGui::Begin("menu"))
|
||||||
|
|
@ -459,7 +474,6 @@ void Game::imgui()
|
||||||
ImGui::Indent();
|
ImGui::Indent();
|
||||||
ImGui::Text("A/D or arrow keys: move left and right");
|
ImGui::Text("A/D or arrow keys: move left and right");
|
||||||
ImGui::Text("R: reset game");
|
ImGui::Text("R: reset game");
|
||||||
ImGui::Text("F1: expand/collaspe this window");
|
|
||||||
ImGui::Unindent();
|
ImGui::Unindent();
|
||||||
|
|
||||||
ImGui::Text("Level config:");
|
ImGui::Text("Level config:");
|
||||||
|
|
@ -543,6 +557,30 @@ void Game::render()
|
||||||
window.display();
|
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()
|
void Game::resetGame()
|
||||||
{
|
{
|
||||||
bricks.clear();
|
bricks.clear();
|
||||||
|
|
@ -568,6 +606,8 @@ void Game::run()
|
||||||
movement();
|
movement();
|
||||||
|
|
||||||
collision();
|
collision();
|
||||||
|
|
||||||
|
soundSystem();
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEndGame();
|
checkEndGame();
|
||||||
|
|
@ -596,6 +636,8 @@ void Game::runNoImgui()
|
||||||
movement();
|
movement();
|
||||||
|
|
||||||
collision();
|
collision();
|
||||||
|
|
||||||
|
soundSystem();
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEndGame();
|
checkEndGame();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue