diff --git a/src/Constants.hpp b/src/Constants.hpp index ead0e47..21a3c4e 100644 --- a/src/Constants.hpp +++ b/src/Constants.hpp @@ -7,4 +7,5 @@ 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; } // namespace Constants diff --git a/src/Game.cpp b/src/Game.cpp index 4c3f39e..3efe85f 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -18,23 +18,18 @@ Game::Game() { sf::Vector2f(400.f, 0.f))); } -void Game::processKeyPressed(const sf::Event::KeyPressed& keyPressed) { - if (keyPressed.code == sf::Keyboard::Key::Add || - keyPressed.code == sf::Keyboard::Key::Equal) { +void Game::processKeyPressed(const sf::Event::KeyPressed& kP) { + if (kP.code == sf::Keyboard::Key::Equal) { m_timeScale *= 1.1f; - } else if (keyPressed.code == sf::Keyboard::Key::Subtract || - keyPressed.code == sf::Keyboard::Key::Hyphen) { + } else if (kP.code == sf::Keyboard::Key::Hyphen) { m_timeScale /= 1.1f; if (m_timeScale < 0.1f) m_timeScale = 0.1f; } } -void Game::processMousePressed( - const sf::Event::MouseButtonPressed& mousePressed) { - if (mousePressed.button == sf::Mouse::Button::Left) { - handleMouseClick( - sf::Vector2i(mousePressed.position.x, mousePressed.position.y)); - } +void Game::processMousePressed(const sf::Event::MouseButtonPressed& mP) { + if (mP.button != sf::Mouse::Button::Left) return; + handleMouseClick(sf::Vector2i(mP.position.x, mP.position.y)); } void Game::processEvents() { @@ -53,28 +48,22 @@ void Game::processEvents() { void Game::handleMouseClick(const sf::Vector2i& mousePos) { auto* ball = dynamic_cast(m_objects[0].get()); if (!ball) return; - sf::Vector2f ballPos = ball->getPosition(); + sf::Vector2f mouseWorld(static_cast(mousePos.x), static_cast(mousePos.y)); - sf::Vector2f dir = mouseWorld - ballPos; + sf::Vector2f dir = mouseWorld - ball->getPosition(); - dir = VectorMath::normalize(dir); - - ball->applyImpulse(2000.f * dir); + ball->applyImpulse(Constants::IMPULSE * VectorMath::normalize(dir)); } void Game::update() { float dt = m_clock.restart().asSeconds() * m_timeScale; - for (auto& object : m_objects) { - object->update(dt); - } + for (auto& object : m_objects) object->update(dt); } void Game::render() { m_window.clear(sf::Color::Black); - for (auto& object : m_objects) { - object->draw(m_window); - } + for (auto& object : m_objects) object->draw(m_window); m_window.display(); }