From: Nils Forssén Date: Wed, 29 Nov 2023 12:23:21 +0000 (+0100) Subject: made some animation X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=f4874ebf11602bfa4727fc7c8b3dc78a5b820737;p=TDDC76_proj.git made some animation --- diff --git a/src/Bike_enemy.cc b/src/Bike_enemy.cc index c258638..aadcab7 100644 --- a/src/Bike_enemy.cc +++ b/src/Bike_enemy.cc @@ -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 } diff --git a/src/Player.cc b/src/Player.cc index bd24f88..6d56c3b 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -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(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; - } + } + } + //} } diff --git a/src/Player.h b/src/Player.h index 6676e33..c5614ac 100644 --- a/src/Player.h +++ b/src/Player.h @@ -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};