From: stian853 Date: Mon, 27 Nov 2023 09:21:37 +0000 (+0100) Subject: kollision yf och player, stina arvid X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=9751912c46a6d0bf927c0f5131947277cb73222e;p=TDDC76_proj.git kollision yf och player, stina arvid --- diff --git a/assets/data.json b/assets/data.json index ecbd6de..c3d1e55 100644 --- a/assets/data.json +++ b/assets/data.json @@ -4,6 +4,7 @@ "start_pos": [200, 200], "scale": [0.5, 0.5], "max_speed": 5, + "degrees_per_second": 360, "sack" : { "offset" : [30, 18], diff --git a/src/Player.cc b/src/Player.cc index 7f52064..bd24f88 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -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(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 diff --git a/src/Player.h b/src/Player.h index ad8f93a..6676e33 100644 --- a/src/Player.h +++ b/src/Player.h @@ -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}; diff --git a/src/States.cc b/src/States.cc index 48ac63f..a522eeb 100644 --- a/src/States.cc +++ b/src/States.cc @@ -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);