made some animation
authorNils Forssén <nilfo359@student.liu.se>
Wed, 29 Nov 2023 12:23:21 +0000 (13:23 +0100)
committerNils Forssén <nilfo359@student.liu.se>
Wed, 29 Nov 2023 12:23:21 +0000 (13:23 +0100)
src/Bike_enemy.cc
src/Player.cc
src/Player.h

index c258638f0a0f957566c1de193767cc2e04ae911d..aadcab7daa8ea11e0ac3115b1f68c84bee08f4b7 100644 (file)
@@ -18,7 +18,6 @@ Bike_enemy::Bike_enemy(sf::Texture& txtr, json& params) : max_speed{params["max_
     sf::FloatRect gb {sprite.getGlobalBounds()};
     sprite.setOrigin(gb.width / 2, gb.height / 2);
     sprite.setPosition(position);
-    // komm
 }
 
 
index bd24f880cf712ecf3cad217526006ffdaf164a43..6d56c3b3576039854f39bd59ed9a4a203d6f38e3 100644 (file)
@@ -6,9 +6,7 @@
 #include "Context.h"
 #include "constants.h"
 
-
-Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]}/*, falling {false},
-                               rotation{params["degrees_per_second"]}, degrees{0}*/
+Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]}, tumbling{false}, tumble_degrees{0}, rotation{params["degrees_per_second"]}
 {
     position = {params["start_pos"][0], params["start_pos"][1]};
 
@@ -28,7 +26,6 @@ Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]}/*, f
     sack_offset = {params["sack"]["offset"][0], params["sack"]["offset"][1]};
     sack.rotate(params["sack"]["angle"]);
     sack.setPosition(position + sack_offset);
-
 }
 
 
@@ -37,20 +34,34 @@ void Player::collision(Object& other)
     move(false);
 }
 
+void Player::collision(YF& yf)
+{
+    move(false);
+    tumbling = true;
+}
+
 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)
+    
+    if(tumbling)
     {
-        
-        degrees += rotation/FPS;
-        sprite.rotate(degrees);
-        falling = false;
-    }*/
+        sprite.rotate(rotation/FPS);
+        tumble_degrees += rotation/FPS;
+        if (tumble_degrees >= 360)
+        {
+            tumbling = false;
+            tumble_degrees = 0;
+            direction = {0,0};
+        }
+    }
+    else
+    {
+        move();
+    }
 }
 
 void Player::move(bool forward) 
@@ -69,7 +80,9 @@ void Player::render(sf::RenderWindow& window) const
 void Player::handle_input(sf::Event& event)
 {
     //while (falling == false)
-    //{    
+    //{  
+    if (!tumbling)
+    {
         direction = {0,0};
 
         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
@@ -94,7 +107,9 @@ void Player::handle_input(sf::Event& event)
         {
             direction.x *= M_SQRT1_2;
             direction.y *= M_SQRT1_2;
-        }    
+        }
+    }
+        
         
     //}
 }
index 6676e33b1c23ddde56b5abf6074195dd610cf651..c5614ac4177d9b4424058487243bc4a8e1fb4a63 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "Moving_object.h"
 #include "json.hpp"
+#include "YF.h"
 
 using json = nlohmann::json;
 
@@ -17,6 +18,7 @@ public:
     ~Player() = default;
     
     void collision(Object& other) override;
+    void collision(YF& yf);
     void update(Context& context) override;
     void render(sf::RenderWindow& window) const override;
     void handle_input(sf::Event& event);
@@ -28,10 +30,10 @@ protected:
 private:
     void move(bool forward=true);
     int collected;
-    //float degrees;
-    //float rotation; 
+    float tumble_degrees;
+    int rotation; 
     float max_speed;
-    //bool falling;
+    bool tumbling;
     sf::Sprite sack;
     sf::Texture sack_texture;
     sf::Vector2f sack_offset; //{20, -30};