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 RESTITUTION = 0.8f;
|
||||||
constexpr float FRICTION = 0.9f;
|
constexpr float FRICTION = 0.9f;
|
||||||
constexpr float BALL_RADIUS = 20.f;
|
constexpr float BALL_RADIUS = 20.f;
|
||||||
|
constexpr float IMPULSE = 3000.f;
|
||||||
} // namespace Constants
|
} // namespace Constants
|
||||||
|
|||||||
+11
-22
@@ -18,23 +18,18 @@ Game::Game() {
|
|||||||
sf::Vector2f(400.f, 0.f)));
|
sf::Vector2f(400.f, 0.f)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::processKeyPressed(const sf::Event::KeyPressed& keyPressed) {
|
void Game::processKeyPressed(const sf::Event::KeyPressed& kP) {
|
||||||
if (keyPressed.code == sf::Keyboard::Key::Add ||
|
if (kP.code == sf::Keyboard::Key::Equal) {
|
||||||
keyPressed.code == sf::Keyboard::Key::Equal) {
|
|
||||||
m_timeScale *= 1.1f;
|
m_timeScale *= 1.1f;
|
||||||
} else if (keyPressed.code == sf::Keyboard::Key::Subtract ||
|
} else if (kP.code == sf::Keyboard::Key::Hyphen) {
|
||||||
keyPressed.code == sf::Keyboard::Key::Hyphen) {
|
|
||||||
m_timeScale /= 1.1f;
|
m_timeScale /= 1.1f;
|
||||||
if (m_timeScale < 0.1f) m_timeScale = 0.1f;
|
if (m_timeScale < 0.1f) m_timeScale = 0.1f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::processMousePressed(
|
void Game::processMousePressed(const sf::Event::MouseButtonPressed& mP) {
|
||||||
const sf::Event::MouseButtonPressed& mousePressed) {
|
if (mP.button != sf::Mouse::Button::Left) return;
|
||||||
if (mousePressed.button == sf::Mouse::Button::Left) {
|
handleMouseClick(sf::Vector2i(mP.position.x, mP.position.y));
|
||||||
handleMouseClick(
|
|
||||||
sf::Vector2i(mousePressed.position.x, mousePressed.position.y));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::processEvents() {
|
void Game::processEvents() {
|
||||||
@@ -53,28 +48,22 @@ void Game::processEvents() {
|
|||||||
void Game::handleMouseClick(const sf::Vector2i& mousePos) {
|
void Game::handleMouseClick(const sf::Vector2i& mousePos) {
|
||||||
auto* ball = dynamic_cast<Ball*>(m_objects[0].get());
|
auto* ball = dynamic_cast<Ball*>(m_objects[0].get());
|
||||||
if (!ball) return;
|
if (!ball) return;
|
||||||
sf::Vector2f ballPos = ball->getPosition();
|
|
||||||
sf::Vector2f mouseWorld(static_cast<float>(mousePos.x),
|
sf::Vector2f mouseWorld(static_cast<float>(mousePos.x),
|
||||||
static_cast<float>(mousePos.y));
|
static_cast<float>(mousePos.y));
|
||||||
sf::Vector2f dir = mouseWorld - ballPos;
|
sf::Vector2f dir = mouseWorld - ball->getPosition();
|
||||||
|
|
||||||
dir = VectorMath::normalize(dir);
|
ball->applyImpulse(Constants::IMPULSE * VectorMath::normalize(dir));
|
||||||
|
|
||||||
ball->applyImpulse(2000.f * dir);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::update() {
|
void Game::update() {
|
||||||
float dt = m_clock.restart().asSeconds() * m_timeScale;
|
float dt = m_clock.restart().asSeconds() * m_timeScale;
|
||||||
for (auto& object : m_objects) {
|
for (auto& object : m_objects) object->update(dt);
|
||||||
object->update(dt);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::render() {
|
void Game::render() {
|
||||||
m_window.clear(sf::Color::Black);
|
m_window.clear(sf::Color::Black);
|
||||||
for (auto& object : m_objects) {
|
for (auto& object : m_objects) object->draw(m_window);
|
||||||
object->draw(m_window);
|
|
||||||
}
|
|
||||||
m_window.display();
|
m_window.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user