mirror of
https://github.com/Floriansylvain/SFMLplayground.git
synced 2026-08-19 11:43:24 +02:00
refactor: input handling & impulse constant
This commit is contained in:
@@ -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
|
||||
|
||||
+11
-22
@@ -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<Ball*>(m_objects[0].get());
|
||||
if (!ball) return;
|
||||
sf::Vector2f ballPos = ball->getPosition();
|
||||
|
||||
sf::Vector2f mouseWorld(static_cast<float>(mousePos.x),
|
||||
static_cast<float>(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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user