joystick in game, Nils, Arvid, Stina
authorstian853 <stian853@su15-112.ad.liu.se>
Mon, 4 Dec 2023 08:26:43 +0000 (09:26 +0100)
committerstian853 <stian853@su15-112.ad.liu.se>
Mon, 4 Dec 2023 08:26:43 +0000 (09:26 +0100)
assets/highscore.csv
src/Player.cc

index 3cc2c0685d20d6ebc4ec66b83788424b4dbf1e0f..b5732abe77c568c85a7d654cae28a587dd47292b 100644 (file)
@@ -2,4 +2,4 @@ Gorilla,36
 Gorilla,27
 Gorilla,24
 Gorilla,18
-Gorilla,13
+Gorilla,18
index daa54193bec4b3c8ac528901c681163e11043f4b..78750b9c99dfa35e4c09cb9df54bbda2bc66874c 100644 (file)
@@ -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;
         }
     }  
 }