From: stian853 Date: Wed, 29 Nov 2023 13:25:21 +0000 (+0100) Subject: collision between enemy andyf and bike X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=d4f9ef83eea24c660085ae8f0678042ca0c15496;p=TDDC76_proj.git collision between enemy andyf and bike --- diff --git a/assets/data.json b/assets/data.json index 05c4965..4a28ed0 100644 --- a/assets/data.json +++ b/assets/data.json @@ -7,7 +7,7 @@ "degrees_per_second": 360, "sack" : { - "offset" : [30, 18], + "offset" : [25, -5], "scale" : [0.3, 0.3], "angle" : -25 } @@ -28,10 +28,10 @@ }, "game_constants": { - "bottles_per_second" : 2, + "bottles_per_second" : 1, "game_time" : 20, "yf_per_second": 0.125, - "bikes_per_second": 0.17, + "bikes_per_second": 0.25, "scoreboard" : 5 }, @@ -44,20 +44,21 @@ "main_enemy": { "scale" : [0.35, 0.35], - "start_pos" : [50, 600] + "start_pos" : [50, 600], + "max_speed": 2.0 }, "yf": { "scale" : [1, 1], - "max_speed" : 5.0, + "max_speed" : 2.0, "direction": [0, -1] }, "bike_enemy": { - "scale" : [0.3, 0.3], - "max_speed" : 3.0, + "scale" : [0.25, 0.25], + "max_speed" : 5.0, "direction": [0, 1] } diff --git a/assets/highscore.csv b/assets/highscore.csv index 5d2ef84..fb3da42 100644 --- a/assets/highscore.csv +++ b/assets/highscore.csv @@ -1,5 +1,5 @@ -Gorilla,4 -Gorilla,3 -Stina,2 -Gorilla,2 -Gorilla,2 +Gorilla,14 +Gorilla,10 +Gorilla,9 +Gorilla,7 +Gorilla,6 diff --git a/src/Main_enemy.cc b/src/Main_enemy.cc index 68002b9..e3d622c 100644 --- a/src/Main_enemy.cc +++ b/src/Main_enemy.cc @@ -5,7 +5,7 @@ #include "States.h" #include "Context.h" -Main_enemy::Main_enemy(sf::Texture& txtr, json& params) +Main_enemy::Main_enemy(sf::Texture& txtr, json& params): max_speed{params["max_speed"]}, moving_to_bottle{false} { //texture.loadFromFile("assets/6Ling_figur2.png"); texture = txtr; @@ -20,26 +20,37 @@ Main_enemy::Main_enemy(sf::Texture& txtr, json& params) void Main_enemy::collision(Object& other) { - return; + move(false); } void Main_enemy::update(Context& context) { + Game_state* game = static_cast(context.current_state.get()); if ((game->bottles).size() != 0) { + moving_to_bottle = true; direction = find_closest_bottle(game->bottles) - position; float magnitude = sqrt(direction.x*direction.x + direction.y*direction.y); direction /= magnitude; - position += direction*2.0f ; - sprite.setPosition(position); - //std::cout << find_closest_bottle(game->bottles).x << std::endl; - } - + move(); + } + else + { + moving_to_bottle = false; + } } void Main_enemy::render(sf::RenderWindow& window) const { window.draw(sprite); } +void Main_enemy::move(bool forward) +{ + if (moving_to_bottle) + { + position += static_cast(pow(-1, forward - 1)) * max_speed * direction; + sprite.setPosition(position); + } +} sf::Vector2f Main_enemy::find_closest_bottle(std::vector> & bottles) { diff --git a/src/Main_enemy.h b/src/Main_enemy.h index fa1ecf4..50a1d4a 100644 --- a/src/Main_enemy.h +++ b/src/Main_enemy.h @@ -22,9 +22,11 @@ public: void render(sf::RenderWindow& window) const override; protected: - //void move(Time) override; private: + float max_speed; + bool moving_to_bottle; + void move(bool forward = true); float bottle_dist(std::unique_ptr const& bottle); sf::Vector2f find_closest_bottle(std::vector> & bottles); }; diff --git a/src/Player.cc b/src/Player.cc index 6d56c3b..1bf0ce5 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -13,15 +13,15 @@ Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]}, tum texture.loadFromFile("assets/4V_figur.png"); sprite.setTexture(texture); sprite.setScale(params["scale"][0], params["scale"][1]); - sf::FloatRect gb {sprite.getGlobalBounds()}; - sprite.setOrigin(gb.width / 2, gb.height / 2); + sf::FloatRect glb {sprite.getLocalBounds()}; + sprite.setOrigin(glb.width / 2, glb.height / 2); sprite.setPosition(position); sack_texture.loadFromFile("assets/sack.png"); sack.setTexture(sack_texture); sack.setScale(params["sack"]["scale"][0], params["sack"]["scale"][1]); - sf::FloatRect gb_s {sack.getGlobalBounds()}; + sf::FloatRect gb_s {sack.getLocalBounds()}; sack.setOrigin(gb_s.width / 3, gb_s.height / 5); sack_offset = {params["sack"]["offset"][0], params["sack"]["offset"][1]}; sack.rotate(params["sack"]["angle"]); @@ -39,6 +39,11 @@ void Player::collision(YF& yf) move(false); tumbling = true; } +void Player::collision(Bike_enemy& bike) +{ + move(false); + tumbling = true; +} void Player::update(Context& context) { @@ -78,9 +83,7 @@ void Player::render(sf::RenderWindow& window) const } void Player::handle_input(sf::Event& event) -{ - //while (falling == false) - //{ +{ if (!tumbling) { direction = {0,0}; @@ -108,10 +111,7 @@ void Player::handle_input(sf::Event& event) direction.x *= M_SQRT1_2; direction.y *= M_SQRT1_2; } - } - - - //} + } } void Player::add_collected() @@ -127,7 +127,3 @@ void Player::zero_collected() { collected = 0; } -/*void Player::tumble() -{ - falling = true; -}*/ \ No newline at end of file diff --git a/src/Player.h b/src/Player.h index c5614ac..459b3ef 100644 --- a/src/Player.h +++ b/src/Player.h @@ -8,6 +8,7 @@ #include "Moving_object.h" #include "json.hpp" #include "YF.h" +#include "Bike_enemy.h" using json = nlohmann::json; @@ -19,13 +20,14 @@ public: void collision(Object& other) override; void collision(YF& yf); + void collision(Bike_enemy& bike); void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event); void add_collected(); int get_collected(); void zero_collected(); - //void tumble(); + protected: private: void move(bool forward=true); @@ -36,7 +38,7 @@ private: bool tumbling; sf::Sprite sack; sf::Texture sack_texture; - sf::Vector2f sack_offset; //{20, -30}; + sf::Vector2f sack_offset; }; #endif \ No newline at end of file diff --git a/src/States.cc b/src/States.cc index 874c966..2129371 100644 --- a/src/States.cc +++ b/src/States.cc @@ -103,22 +103,19 @@ void Game_state::update(Context &context) for (unsigned int i {0}; i < bottles.size(); ++i) { - if (player->collides(*(bottles[i]))) + if (enemy->collides(*(bottles[i]))) { - // std::cout <<"crash" << std::endl; + bottles.erase(bottles.begin() + i); - player->add_collected(); + } - } - for (unsigned int i {0}; i < bottles.size(); ++i) - { - if (enemy->collides(*(bottles[i]))) + else if (player->collides(*(bottles[i]))) { - // std::cout <<"crash" << std::endl; bottles.erase(bottles.begin() + i); - + player->add_collected(); } } + player->update(context); helper->update(context); @@ -136,18 +133,25 @@ void Game_state::update(Context &context) { player->zero_collected(); player->collision(*yf); - - //player->tumble(); - + } + if(enemy->collides(*yf)) + { + enemy->collision(*yf); + } + } + if(bike != nullptr) + { + if (player->collides(*bike)) + { + player->zero_collected(); + player->collision(*bike); + } + if(enemy->collides(*bike)) + { + enemy->collision(*bike); } } - /* - if (player->get_x() < 0) player->set_x(0); - if (player->get_x() > S_WIDTH) player->set_x(S_WIDTH); - if (player->get_y() < 0) player->set_y(0); - if (player->get_y() > S_HEIGHT) player->set_y(S_HEIGHT); - */ time_since_last_bottle += context.time.asSeconds(); time_since_last_yf += context.time.asSeconds(); @@ -198,7 +202,7 @@ void Game_state::render(sf::RenderWindow &window) const yf->render(window); } - for (int i{}; i < int(bottles.size()); ++i) + for (int i{0}; i < int(bottles.size()); ++i) { bottles[i]->render(window); }