From f52ccbd19ff99d4305a64f89241830b80d21d266 Mon Sep 17 00:00:00 2001 From: stian853 Date: Mon, 4 Dec 2023 09:26:43 +0100 Subject: [PATCH] joystick in game, Nils, Arvid, Stina --- assets/highscore.csv | 2 +- src/Player.cc | 60 ++++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/assets/highscore.csv b/assets/highscore.csv index 3cc2c06..b5732ab 100644 --- a/assets/highscore.csv +++ b/assets/highscore.csv @@ -2,4 +2,4 @@ Gorilla,36 Gorilla,27 Gorilla,24 Gorilla,18 -Gorilla,13 +Gorilla,18 diff --git a/src/Player.cc b/src/Player.cc index daa5419..78750b9 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -99,30 +99,48 @@ void Player::handle_input(sf::Event& event) { if (!tumbling) { - direction = {0,0}; - - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) - { - direction.y -= 1; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) - { - direction.y += 1; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) + if (!sf::Joystick::isConnected(0)) { - direction.x += 1; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) - { - direction.x -= 1; - } + direction = {0,0}; - // If the vector is diagonal, normalize its length - if (abs(direction.x) + abs(direction.y) == 2) + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) + { + direction.y -= 1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) + { + direction.y += 1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) + { + direction.x += 1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) + { + direction.x -= 1; + } + + // If the vector is diagonal, normalize its length + if (abs(direction.x) + abs(direction.y) == 2) + { + direction.x *= M_SQRT1_2; + direction.y *= M_SQRT1_2; + } + } + else { - direction.x *= M_SQRT1_2; - direction.y *= M_SQRT1_2; + direction = { + sf::Joystick::getAxisPosition(0, sf::Joystick::X) / 100, + sf::Joystick::getAxisPosition(0, sf::Joystick::Y) / 100 + }; + + float len{pow(direction.x, 2) + pow(direction.y, 2)}; + if (len > 1) + { + direction.x /= sqrt(len); + direction.y /= sqrt(len); + } + std::cout << "x: " << direction.x << "Y: " << direction.y << std::endl; } } } -- 2.30.2