small update

This commit is contained in:
Joseph Aquino 2025-09-25 11:27:57 -04:00
parent bd090c1b52
commit 4d7579a45e
4 changed files with 46 additions and 23 deletions

View File

@ -17,8 +17,8 @@ struct Ball
sf::Vector2f pos;
sf::Vector2f velocity;
sf::Vector2f previousPos{};
bool alive{true};
bool collided{false};
bool alive{};
bool collided{};
};
struct Brick
@ -26,7 +26,7 @@ struct Brick
Brick(sf::Vector2f position_in);
sf::Vector2f pos;
bool alive{true};
bool alive{};
};
struct Player
@ -37,10 +37,10 @@ struct Player
sf::Vector2f pos{};
sf::Vector2f previousPos{};
int score{};
int lives{3};
int mult{1};
bool left{false};
bool right{false};
int lives{};
int mult{};
bool left{};
bool right{};
};
class Game
@ -111,7 +111,7 @@ private:
int totalSpecialBricks{};
int totalBricks{};
int framerate{};
float volume{10};
float volume{};
float playerSpeed{};
float ballRadius{};
float ballMaxSpeed{};

View File

@ -14,14 +14,21 @@
Ball::Ball(sf::Vector2f position_in, sf::Vector2f vel_in)
: pos(position_in)
, velocity(vel_in)
, alive(true)
, collided(false)
{ }
Brick::Brick(sf::Vector2f position_in)
: pos(position_in)
, alive(true)
{ }
Player::Player(sf::Vector2f position_in)
: pos(position_in)
, lives(3)
, mult(1)
, left(false)
, right{false}
{ }
@ -37,6 +44,7 @@ Game::Game()
, collideSound(collideSoundBuffer)
, brickBreakSound(brickBreakSoundBuffer)
, failSound(failSoundBuffer)
, volume(10)
{
if (!ImGui::SFML::Init(window)) return;

View File

@ -4,31 +4,45 @@
#include <Random.h>
sf::Vector2f getOverlap(sf::FloatRect first, sf::FloatRect second)
sf::Vector2f getOverlap(const sf::FloatRect first, const sf::FloatRect second)
{
sf::Vector2f result;
sf::Vector2f delta;
delta.x = std::abs(second.position.x - first.position.x);
delta.y = std::abs(second.position.y - first.position.y);
const float deltaX = std::abs(second.position.x - first.position.x);
const float deltaY = std::abs(second.position.y - first.position.y);
result.x = (first.size.x + second.size.x) - delta.x;
result.y = (first.size.y + second.size.y) - delta.y;
return result;
const float resultX = (first.size.x + second.size.x) - deltaX;
const float resultY = (first.size.y + second.size.y) - deltaY;
return {resultX, resultY};
}
sf::Vector2f velocityInRandomDir(const float speed_in)
{
const bool upOrDown{(bool)Random::get(0,1)};
const bool leftOrRight{(bool)Random::get(0,1)};
// get angle within certain ranges so that the ball does not
// start moving straight side to side or up and down
sf::Angle angle = sf::degrees(upOrDown ? (float)Random::get(225, 315) : (float)Random::get(45, 135));
if (angle.asDegrees() == 90.f || angle.asDegrees() == 270)
{
angle += sf::degrees(1);
if (upOrDown && leftOrRight)
{// up left
const sf::Angle angle = sf::degrees((float)Random::get(195, 255));
return sf::Vector2f{speed_in * std::cos(angle.asRadians()), speed_in * std::sin(angle.asRadians())};
}
else if (upOrDown && !leftOrRight)
{// up right
const sf::Angle angle = sf::degrees((float)Random::get(285, 355));
return sf::Vector2f{speed_in * std::cos(angle.asRadians()), speed_in * std::sin(angle.asRadians())};
}
else if (!upOrDown && leftOrRight)
{// down left
const sf::Angle angle = sf::degrees((float)Random::get(105, 165));
return sf::Vector2f{speed_in * std::cos(angle.asRadians()), speed_in * std::sin(angle.asRadians())};
}
else if (!upOrDown && !leftOrRight)
{// down right
const sf::Angle angle = sf::degrees((float)Random::get(15, 75));
return sf::Vector2f{speed_in * std::cos(angle.asRadians()), speed_in * std::sin(angle.asRadians())};
}
return sf::Vector2f{speed_in * std::cos(angle.asRadians()), speed_in * std::sin(angle.asRadians())};
return {0,0};// means something went wrong
}
// used for debugging

View File

@ -2,9 +2,10 @@
int main(int argc, char* argv[])
{
bool useImgui{true};
Game game;
// parse command-line arguments
bool useImgui{true};
for (int i = 0; i < argc; i++)
{
if (!strcmp(argv[i], "--noImgui"))