From: stian853 Date: Thu, 30 Nov 2023 13:08:47 +0000 (+0100) Subject: allmänt fix, Stina, Arvid, Nils X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=ce6cfbd3e2ad7bbf7b7418dfebfc75179cfebae2;p=TDDC76_proj.git allmänt fix, Stina, Arvid, Nils --- diff --git a/assets/data.json b/assets/data.json index 4a28ed0..a1f1c49 100644 --- a/assets/data.json +++ b/assets/data.json @@ -37,15 +37,18 @@ }, "map": { - "points_pos": [5, 0], - "font_size": 40 + "points_pos": [5, 38], + "collected_pos": [5, 76], + "time_pos": [5, 0], + "font_size": 36 }, "main_enemy": { "scale" : [0.35, 0.35], "start_pos" : [50, 600], - "max_speed": 2.0 + "max_speed": 2.0, + "degrees_per_second": 360 }, "yf": { diff --git a/src/Main_enemy.cc b/src/Main_enemy.cc index e3d622c..2aaf506 100644 --- a/src/Main_enemy.cc +++ b/src/Main_enemy.cc @@ -4,15 +4,16 @@ #include "Main_enemy.h" #include "States.h" #include "Context.h" +#include "constants.h" -Main_enemy::Main_enemy(sf::Texture& txtr, json& params): max_speed{params["max_speed"]}, moving_to_bottle{false} +Main_enemy::Main_enemy(sf::Texture& txtr, json& params): max_speed{params["max_speed"]}, moving_to_bottle{false}, tumbling{false}, tumble_degrees{0}, rotation{params["degrees_per_second"]} { //texture.loadFromFile("assets/6Ling_figur2.png"); texture = txtr; sprite.setTexture(texture); position = {params["start_pos"][0], params["start_pos"][1]}; sprite.setScale(params["scale"][0], params["scale"][1]); - sf::FloatRect gb {sprite.getGlobalBounds()}; + sf::FloatRect gb {sprite.getLocalBounds()}; sprite.setOrigin(gb.width / 2, gb.height / 2); sprite.setPosition(position); @@ -24,25 +25,45 @@ void Main_enemy::collision(Object& other) } void Main_enemy::update(Context& context) { - - Game_state* game = static_cast(context.current_state.get()); - if ((game->bottles).size() != 0) + if(tumbling) { - moving_to_bottle = true; - direction = find_closest_bottle(game->bottles) - position; - float magnitude = sqrt(direction.x*direction.x + direction.y*direction.y); - direction /= magnitude; - move(); + sprite.rotate(rotation/FPS); + tumble_degrees += rotation/FPS; + if (tumble_degrees >= 360) + { + tumbling = false; + tumble_degrees = 0; + direction = {0,0}; + } } else { - moving_to_bottle = false; + 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; + move(); + } + else + { + moving_to_bottle = false; + } } } void Main_enemy::render(sf::RenderWindow& window) const { window.draw(sprite); } + +void Main_enemy::collision(Player& player) +{ + move(false); + tumbling = true; +} + void Main_enemy::move(bool forward) { if (moving_to_bottle) diff --git a/src/Main_enemy.h b/src/Main_enemy.h index 50a1d4a..8959e39 100644 --- a/src/Main_enemy.h +++ b/src/Main_enemy.h @@ -5,6 +5,7 @@ #include "Bottle.h" #include "Moving_object.h" +#include "Player.h" #include @@ -18,6 +19,7 @@ public: Main_enemy(sf::Texture& txtr, json& params); ~Main_enemy() = default; void collision(Object& other) override; + void collision(Player& player); void update(Context& context) override; void render(sf::RenderWindow& window) const override; @@ -25,6 +27,9 @@ protected: private: float max_speed; + float tumble_degrees; + int rotation; + bool tumbling; bool moving_to_bottle; void move(bool forward = true); float bottle_dist(std::unique_ptr const& bottle); diff --git a/src/Map.cc b/src/Map.cc index 5f0e13a..7e62975 100644 --- a/src/Map.cc +++ b/src/Map.cc @@ -2,34 +2,73 @@ #include "constants.h" #include -Map::Map(json& params) : texture{}, sprite{}, point_text{}, font{} +Map::Map(json& params) : texture{}, sprite{}, point_text{}, collected_text{}, time_text{}, bottle_texture{}, bottle_sprite{}, font{} { texture.loadFromFile("assets/bakgrund.png"); sprite.setTexture(texture); + bottle_texture.loadFromFile("assets/kir.png"); + bottle_sprite.setTexture(bottle_texture); sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); sprite.setPosition(0,0); + bottle_sprite.setScale(0.5, 0.5); + bottle_sprite.setPosition(params["collected_pos"][0], params["collected_pos"][1]); + if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") ) throw std::invalid_argument ("Unable to load font"); point_text = sf::Text{"Points: 0", font, params["font_size"]}; - sf::FloatRect gbts {point_text.getGlobalBounds()}; + collected_text = sf::Text{"0", font, params["font_size"]}; + time_text = sf::Text{"Time left: 0", font, params["font_size"]}; point_text.setPosition (params["points_pos"][0], params["points_pos"][1]); point_text.setFillColor(sf::Color::Black); + collected_text.setPosition (static_cast(params["collected_pos"][0]) + 40, params["collected_pos"][1]); + collected_text.setFillColor(sf::Color::Black); + time_text.setPosition (params["time_pos"][0], params["time_pos"][1]); + time_text.setFillColor(sf::Color::Black); } void Map::update(int const collected, int const points, int const time_left) { - point_text.setString("Collected: " + std::to_string(collected) + "\n" + "Points: " + std::to_string(points) + "\n" + "Time left: " + std::to_string(time_left) + " s"); + if (collected == 0) + { + collected_text.setFillColor(sf::Color::Red); + } + else + { + collected_text.setFillColor(sf::Color::Black); + } + if (points == 0) + { + point_text.setFillColor(sf::Color::Red); + } + else + { + point_text.setFillColor(sf::Color::Black); + } + if (time_left <= 5) + { + time_text.setFillColor(sf::Color::Red); + } + else + { + time_text.setFillColor(sf::Color::Black); + } + collected_text.setString("x" + std::to_string(collected)); + point_text.setString("Points: " + std::to_string(points)); + time_text.setString("Time left: " + std::to_string(time_left) + " s"); } void Map::render(sf::RenderWindow& window) const { window.draw(sprite); + window.draw(bottle_sprite); window.draw(point_text); + window.draw(collected_text); + window.draw(time_text); } bool Map::collides(Object& other) diff --git a/src/Map.h b/src/Map.h index 7578e47..41daf14 100644 --- a/src/Map.h +++ b/src/Map.h @@ -20,7 +20,11 @@ protected: private: sf::Texture texture; sf::Sprite sprite; + sf::Texture bottle_texture; + sf::Sprite bottle_sprite; sf::Text point_text; + sf::Text collected_text; + sf::Text time_text; sf::Font font; }; diff --git a/src/Player.cc b/src/Player.cc index 3920b94..daa5419 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -31,12 +31,20 @@ Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]}, tum void Player::collision(Object& other) { - move(false); + + } void Player::collision(Map& map) { - move(false); + if (tumbling) + { + move(); + } + else + { + move(false); + } } void Player::collision(YF& yf) diff --git a/src/States.cc b/src/States.cc index 2e8c1a9..3b84a1a 100644 --- a/src/States.cc +++ b/src/States.cc @@ -127,6 +127,10 @@ void Game_state::update(Context &context) player->collision(*helper); helper->collision(*player); } + if (player->collides(*enemy)) + { + enemy->collision(*player); + } if(yf != nullptr) { if (player->collides(*yf))