kollision yf och player, stina arvid
authorstian853 <stian853@su15-111.ad.liu.se>
Mon, 27 Nov 2023 09:21:37 +0000 (10:21 +0100)
committerstian853 <stian853@su15-111.ad.liu.se>
Mon, 27 Nov 2023 09:21:37 +0000 (10:21 +0100)
assets/data.json
src/Player.cc
src/Player.h
src/States.cc

index ecbd6def66d8727e10e97ece2682617d6ff419c5..c3d1e55c4abbbcf4155f362d5464ce5b6817bc74 100644 (file)
@@ -4,6 +4,7 @@
         "start_pos": [200, 200],
         "scale": [0.5, 0.5],
         "max_speed": 5,
+        "degrees_per_second": 360,
         "sack" : 
         {
             "offset" : [30, 18],
index 7f52064984422e139b4b795aefba91cfc54f883e..bd24f880cf712ecf3cad217526006ffdaf164a43 100644 (file)
@@ -7,7 +7,8 @@
 #include "constants.h"
 
 
-Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]}
+Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]}/*, falling {false},
+                               rotation{params["degrees_per_second"]}, degrees{0}*/
 {
     position = {params["start_pos"][0], params["start_pos"][1]};
 
@@ -38,10 +39,18 @@ void Player::collision(Object& other)
 
 void Player::update(Context& context)
 {
+    
     // Get game_state from context, static_cast since we know that game is currently running
     // game_state is still managed by context
     //Game_state* game = static_cast<Game_state*>(context.current_state.get());
     move();
+    /*if(falling == true)
+    {
+        
+        degrees += rotation/FPS;
+        sprite.rotate(degrees);
+        falling = false;
+    }*/
 }
 
 void Player::move(bool forward) 
@@ -59,31 +68,35 @@ void Player::render(sf::RenderWindow& window) const
 
 void Player::handle_input(sf::Event& event)
 {
-    direction = {0,0};
+    //while (falling == false)
+    //{    
+        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))
-    {
-        direction.x += 1;
-    }
-    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
-    {
-        direction.x -= 1;
-    }
+        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;
-    }    
+        // 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;
+        }    
+        
+    //}
 }
 
 void Player::add_collected()
@@ -98,4 +111,8 @@ int Player::get_collected()
 void Player::zero_collected()
 {
     collected = 0;
-}
\ No newline at end of file
+}
+/*void Player::tumble()
+{
+    falling = true;
+}*/
\ No newline at end of file
index ad8f93ae72e9c8ce3a82a2a95dd95065f1a6271e..6676e33b1c23ddde56b5abf6074195dd610cf651 100644 (file)
@@ -23,11 +23,15 @@ public:
     void add_collected();
     int get_collected();
     void zero_collected();
+    //void tumble();
 protected:
 private:
     void move(bool forward=true);
     int collected;
+    //float degrees;
+    //float rotation; 
     float max_speed;
+    //bool falling;
     sf::Sprite sack;
     sf::Texture sack_texture;
     sf::Vector2f sack_offset; //{20, -30};
index 48ac63fe4a8598cfaeb94c9f5e735d5b946c5074..a522eeb33359c5fda9787aadeb582727ccdd08b5 100644 (file)
@@ -91,6 +91,18 @@ void Game_state::update(Context &context)
         player->collision(*helper);
         helper->collision(*player);
     }
+    if(yf.size()>0)
+    {    
+        if (player->collides(*yf[0]))
+        {
+            player->zero_collected();
+            player->collision(*yf[0]);
+
+            //player->tumble();
+            
+        }
+    }
+
     /*
     if (player->get_x() < 0) player->set_x(0);
     if (player->get_x() > S_WIDTH) player->set_x(S_WIDTH);