added enemy, arvid, stina
authorArvid Sjöblom <arvsj277@student.liu.se>
Wed, 15 Nov 2023 13:29:32 +0000 (14:29 +0100)
committerArvid Sjöblom <arvsj277@student.liu.se>
Wed, 15 Nov 2023 13:29:32 +0000 (14:29 +0100)
src/Bottle.cc
src/Bottle.h
src/Main_enemy.cc
src/Main_enemy.h
src/States.cc
src/States.h

index 7bc1e4d72bd4c8f7cf741f7f94677c8deaa0abc9..16e63284c162e842c6f3ef3aeb910b2b442fecd2 100644 (file)
@@ -27,3 +27,7 @@ void Bottle::render(sf::RenderWindow& window) const
 {
     window.draw(sprite);
 }
+sf::Vector2f Bottle::get_pos()
+{
+    return position;
+}
\ No newline at end of file
index f7b2afa5b0aab63d4ff9184755f5ab037b6da75f..3afc401b7468e8b0827e1a292b355fa125a132e9 100644 (file)
@@ -12,6 +12,7 @@ public:
     void update(Context& context) override;
     void render(sf::RenderWindow& window) const override;
     void set_texture(sf::Texture& txtr);
+    sf::Vector2f get_pos();
 private:
         
 };
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8b5dafe165203e46b1008bd0c370e59f53b8d42a 100644 (file)
@@ -0,0 +1,65 @@
+#include <cmath>
+#include <memory>
+#include <iostream>
+#include "Main_enemy.h"
+#include "States.h"
+#include "Context.h"
+
+Main_enemy::Main_enemy()
+{
+    texture.loadFromFile("assets/6Ling_figur2.png");
+    sprite.setTexture(texture);
+
+    sprite.setScale(0.7, 0.7);
+    sf::FloatRect gb {sprite.getGlobalBounds()};
+    sprite.setOrigin(gb.width / 2, gb.height / 2);
+    sprite.setPosition(position);
+    
+} 
+
+void Main_enemy::collision(Object& other) 
+{
+    return;
+}
+void Main_enemy::update(Context& context) 
+{
+    Game_state* game = static_cast<Game_state*>(context.current_state.get());
+    if ((game->bottles).size() != 0)
+    {
+        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;
+    } 
+    
+}
+void Main_enemy::render(sf::RenderWindow& window) const
+{
+    window.draw(sprite);
+}
+
+sf::Vector2f Main_enemy::find_closest_bottle(std::vector<std::unique_ptr<Bottle>> & bottles)
+{
+    //return {0,0};
+    
+    unsigned int closest_bottle_index {0};
+    float min_dist {10000000.0f};
+
+    for(unsigned int i {0}; i < bottles.size(); ++i)
+    {
+        float distance {bottle_dist(bottles[i])};
+        if(distance < min_dist)
+        {
+            min_dist = distance;
+            closest_bottle_index = i;
+        }
+    } 
+    return bottles[closest_bottle_index]->get_pos();
+}
+float Main_enemy::bottle_dist(std::unique_ptr<Bottle> const& bottle)
+{
+    sf::Vector2f dist = position - bottle->get_pos();
+    return sqrt(dist.x*dist.x + dist.y*dist.y);
+}
index 4370d69b4b928af7b614947b8291273d0497d6e5..4027848e327d1d33e30172a4fa62891e353ea4d1 100644 (file)
@@ -4,22 +4,25 @@
 #include <SFML/Graphics.hpp> 
 
 #include "Bottle.h"
+#include "Moving_object.h"
 
 #include <vector>
 
-class Main_enemy
+class Main_enemy : public Moving_object
 {
 public:
-    bool collides(Object& other) override;
+    Main_enemy();
+    ~Main_enemy() = default;
     void collision(Object& other) override;
-    void update() override;
-    void render(sf::RenderWindow& window) override;
+    void update(Context& context) override;
+    void render(sf::RenderWindow& window) const override;
 
 protected:
-    void move(Time) override;
+    //void move(Time) override;
 
 private:
-    Bottle find_closest_bottle(std::vector<Bottle>);
+    float bottle_dist(std::unique_ptr<Bottle> const& bottle);
+    sf::Vector2f find_closest_bottle(std::vector<std::unique_ptr<Bottle>> & bottles);
 };
 
 #endif
\ No newline at end of file
index 64187f7d3a28c8ef85422ca6fb2dc9a3d613d444..40ff02505562b1be9541d6a02c26712d264ac194 100644 (file)
@@ -5,7 +5,7 @@
 #include "Context.h"
 #include "constants.h"
 
-Game_state::Game_state() : game_map{}, player{}, pause_game{false}, bottles{}, time_since_last_bottle{0.0f}, bottle_texture {}
+Game_state::Game_state() : game_map{}, player{}, enemy{}, pause_game{false}, bottles{}, time_since_last_bottle{0.0f}, bottle_texture {}
 {
     bottle_texture.loadFromFile("assets/kir.png");
 }
@@ -31,7 +31,7 @@ void Game_state::update(Context &context)
             player.add_collected();
         }
     }*/
-    for (unsigned int i = 0; i < bottles.size(); ++i)
+    for (unsigned int i {0}; i < bottles.size(); ++i)
     {
         if (player.collides(*(bottles[i])))
         {
@@ -40,16 +40,26 @@ void Game_state::update(Context &context)
             player.add_collected();
         }
     }
+    for (unsigned int i {0}; i < bottles.size(); ++i)
+    {
+        if (enemy.collides(*(bottles[i])))
+        {
+            // std::cout <<"crash" << std::endl;
+            bottles.erase(bottles.begin() + i);
+            
+        }
+    }
     time_since_last_bottle += context.time.asSeconds();
-    std::cout << std::fixed << std::setprecision(3) << time_since_last_bottle << std::endl;
+    //std::cout << std::fixed << std::setprecision(3) << time_since_last_bottle << std::endl;
     if (time_since_last_bottle >= 2)
     {
         bottles.push_back(std::make_unique<Bottle>(bottle_texture));
-        std::cout << "placed bottle"<< std::endl;
+        //std::cout << "placed bottle"<< std::endl;
         time_since_last_bottle = 0;
     }
     game_map.update(context);
     player.update(context);
+    enemy.update(context);
     helper.update(context);
 }
 
@@ -57,6 +67,7 @@ void Game_state::render(sf::RenderWindow &window) const
 {
     game_map.render(window);
     player.render(window);
+    enemy.render(window);
     helper.render(window);
     for (int i{}; i < int(bottles.size()); ++i)
     {
index 2c39109e5fbf5404996b36153863cae6d88ee5cf..5825ff615143ecc0c609a13902095b320a6de8fc 100644 (file)
@@ -7,9 +7,11 @@
 
 #include "Map.h"
 #include "Player.h"
+#include "Main_enemy.h"
 #include "Helper.h"
 #include "Bottle.h"
 
+
 struct Context; //finns en strukt som säger att Context finns innan den är deklarerad
 
 class State
@@ -30,13 +32,15 @@ public:
     void update(Context& context) override;
     void render(sf::RenderWindow& window) const override;
     void handle_input(sf::Event& event) override;
+    std::vector<std::unique_ptr<Bottle>> bottles;
 protected:
 private:
     float time_since_last_bottle;
-    std::vector<std::unique_ptr<Bottle>> bottles;
+    
     Map game_map;
     Player player;
     Helper helper;
+    Main_enemy enemy;
     bool pause_game;
     sf::Texture bottle_texture;