From f276d243ec445680a5624f9b04e60f4a388ae8d1 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Sun, 11 May 2025 01:28:07 +0200 Subject: [PATCH] feat: BallFactory for random ball generation --- src/Ball.cpp | 5 +++-- src/Ball.hpp | 3 ++- src/BallFactory.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/BallFactory.hpp | 10 ++++++++++ src/Constants.hpp | 3 ++- src/Game.cpp | 30 ++++++++++++++++++------------ 6 files changed, 73 insertions(+), 16 deletions(-) create mode 100644 src/BallFactory.cpp create mode 100644 src/BallFactory.hpp diff --git a/src/Ball.cpp b/src/Ball.cpp index eb9c311..764fcad 100644 --- a/src/Ball.cpp +++ b/src/Ball.cpp @@ -4,7 +4,8 @@ #include "Constants.hpp" -Ball::Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel) +Ball::Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel, + const sf::Color& color) : m_radius(radius), m_velocity(vel), m_lastPosition(pos), @@ -12,7 +13,7 @@ Ball::Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel) m_shape.setRadius(radius); m_shape.setOrigin(sf::Vector2f(radius, radius)); m_shape.setPosition(pos); - m_shape.setFillColor(sf::Color::Red); + m_shape.setFillColor(color); } void Ball::update(float dt) { diff --git a/src/Ball.hpp b/src/Ball.hpp index e835fc1..de11682 100644 --- a/src/Ball.hpp +++ b/src/Ball.hpp @@ -15,7 +15,8 @@ class Ball : public PhysicalObject { void handleWallCollision(); public: - Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel); + Ball(float radius, const sf::Vector2f& pos, const sf::Vector2f& vel, + const sf::Color& color); void update(float dt) override; void draw(sf::RenderWindow& window) override; diff --git a/src/BallFactory.cpp b/src/BallFactory.cpp new file mode 100644 index 0000000..0fcd093 --- /dev/null +++ b/src/BallFactory.cpp @@ -0,0 +1,38 @@ +#include "BallFactory.hpp" + +#include +#include + +#include "Constants.hpp" + +namespace BallFactory { +std::unique_ptr generateRandBall() { + float radius = Constants::BALL_RADIUS; + int diameter = static_cast(2 * radius); + int maxX = Constants::WIDTH - diameter; + int maxY = Constants::HEIGHT - diameter; + float x = static_cast((std::rand() % maxX) + radius); + float y = static_cast((std::rand() % maxY) + radius); + float vel = static_cast((std::rand() % 401) - 200); + + auto randColorComp = []() { + return static_cast(64 + (std::rand() % (256 - 64))); + }; + sf::Color color(randColorComp(), randColorComp(), randColorComp()); + + auto position = sf::Vector2f(x, y); + auto velocity = sf::Vector2f(vel, vel); + return std::make_unique(radius, position, velocity, color); +} + +std::vector> generateBalls() { + std::vector> balls; + std::srand(static_cast(std::time(nullptr))); + + for (int i = 0; i < Constants::BALL_QUANTITY; ++i) { + auto ball = generateRandBall(); + balls.push_back(std::move(ball)); + } + return balls; +} +} // namespace BallFactory \ No newline at end of file diff --git a/src/BallFactory.hpp b/src/BallFactory.hpp new file mode 100644 index 0000000..f547822 --- /dev/null +++ b/src/BallFactory.hpp @@ -0,0 +1,10 @@ +#pragma once +#include +#include + +#include "Ball.hpp" + +namespace BallFactory { +std::unique_ptr generateRandBall(); +std::vector> generateBalls(); +} // namespace BallFactory \ No newline at end of file diff --git a/src/Constants.hpp b/src/Constants.hpp index ae0124b..bf658fc 100644 --- a/src/Constants.hpp +++ b/src/Constants.hpp @@ -7,6 +7,7 @@ constexpr float GRAVITY = 8000.f; constexpr float RESTITUTION = 0.8f; constexpr float FRICTION = 0.9f; constexpr float BALL_RADIUS = 20.f; -constexpr float IMPULSE = 3000.f; +constexpr float IMPULSE = 2500.f; constexpr float REST_PIXEL_VELOCITY = 2.0f; +constexpr unsigned BALL_QUANTITY = 10; } // namespace Constants diff --git a/src/Game.cpp b/src/Game.cpp index a5f7720..2013520 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -3,6 +3,7 @@ #include #include +#include "BallFactory.hpp" #include "Constants.hpp" #include "DebugDraw.hpp" #include "VectorMath.hpp" @@ -16,10 +17,11 @@ Game::Game() m_window.setVerticalSyncEnabled(true); m_timeScale = 1.0f; m_objects.clear(); - m_objects.push_back(std::make_unique( - Constants::BALL_RADIUS, - sf::Vector2f(Constants::WIDTH / 2.f, Constants::HEIGHT / 4.f), - sf::Vector2f(400.f, 0.f))); + + auto balls = BallFactory::generateBalls(); + for (auto &ball : balls) { + m_objects.push_back(std::move(ball)); + } } void Game::processKeyPressed(const sf::Event::KeyPressed &kP) { @@ -37,14 +39,15 @@ void Game::processMousePressed(const sf::Event::MouseButtonPressed &mP) { } void Game::handleMouseClick(const sf::Vector2i &mousePos) { - auto *ball = dynamic_cast(m_objects[0].get()); - if (!ball) return; - sf::Vector2f mouseWorld(static_cast(mousePos.x), static_cast(mousePos.y)); - sf::Vector2f dir = mouseWorld - ball->getPosition(); + for (auto &object : m_objects) { + auto *ball = dynamic_cast(object.get()); + if (!ball) continue; - ball->applyImpulse(Constants::IMPULSE * VectorMath::normalize(dir)); + sf::Vector2f dir = mouseWorld - ball->getPosition(); + ball->applyImpulse(Constants::IMPULSE * VectorMath::normalize(dir)); + } } void Game::update() { @@ -54,10 +57,13 @@ void Game::update() { void Game::render() { m_window.clear(sf::Color::Black); - for (auto &object : m_objects) object->draw(m_window); - auto *ball = dynamic_cast(m_objects[0].get()); - if (ball) { + for (const auto &object : m_objects) { + object->draw(m_window); + + auto *ball = dynamic_cast(object.get()); + if (!ball) continue; + DebugDraw::drawDirectionLine(m_window, ball); DebugDraw::drawVelocityLine(m_window, ball); }