allmänt fix, Stina, Arvid, Nils
authorstian853 <stian853@tlvm-4-2-3.ad.liu.se>
Thu, 30 Nov 2023 13:08:47 +0000 (14:08 +0100)
committerstian853 <stian853@tlvm-4-2-3.ad.liu.se>
Thu, 30 Nov 2023 13:08:47 +0000 (14:08 +0100)
assets/data.json
src/Main_enemy.cc
src/Main_enemy.h
src/Map.cc
src/Map.h
src/Player.cc
src/States.cc

index 4a28ed058db23d9d46d78b0efe212501cdad4a55..a1f1c49df31887087d693d1eee3488b78d307657 100644 (file)
     },
     "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":
     {
index e3d622c06fbad0c5d72f6c3ee38be39a8b3dbfb1..2aaf50602fabc7fe5b73061d4a66c1849ab58321 100644 (file)
@@ -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<Game_state*>(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<Game_state*>(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)
index 50a1d4a94db668db3b3ef7e214d03ae038f36ef6..8959e392b31f01ea997b87ea57d928f48a1bda75 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "Bottle.h"
 #include "Moving_object.h"
+#include "Player.h"
 
 #include <vector>
 
@@ -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<Bottle> const& bottle);
index 5f0e13ace4b328590ea30d9a91f9c1548cf696a5..7e62975d0cb0d1448c793c1646368a5a2e497519 100644 (file)
@@ -2,34 +2,73 @@
 #include "constants.h"
 #include <string>
 
-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<float>(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)
index 7578e4705dd2b3dd49a512c3c93e70bce2e8f850..41daf1481b3a2a844a14c54f171707fd3c5e6cba 100644 (file)
--- 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;
 };
 
index 3920b945f6402a63f09a63b9237a159a0b3a708c..daa54193bec4b3c8ac528901c681163e11043f4b 100644 (file)
@@ -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)
index 2e8c1a90e672a4be6b8c448da050495611b65331..3b84a1adedf57bdec1df7a4b9642e86d240ad240 100644 (file)
@@ -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))