collision between enemy andyf and bike
authorstian853 <stian853@su10-106.ad.liu.se>
Wed, 29 Nov 2023 13:25:21 +0000 (14:25 +0100)
committerstian853 <stian853@su10-106.ad.liu.se>
Wed, 29 Nov 2023 13:25:21 +0000 (14:25 +0100)
assets/data.json
assets/highscore.csv
src/Main_enemy.cc
src/Main_enemy.h
src/Player.cc
src/Player.h
src/States.cc

index 05c496547c14ca17ae1971221966c052870124e2..4a28ed058db23d9d46d78b0efe212501cdad4a55 100644 (file)
@@ -7,7 +7,7 @@
         "degrees_per_second": 360,
         "sack" : 
         {
-            "offset" : [30, 18],
+            "offset" : [25, -5],
             "scale" : [0.3, 0.3],
             "angle" : -25
         }
     },
     "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
         
     },
     "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]
 
     }
index 5d2ef842adfd5db7f2ca3c84df9b02a1852584ea..fb3da424f08d1c362a90fcbcfb153e441b06dfd2 100644 (file)
@@ -1,5 +1,5 @@
-Gorilla,4
-Gorilla,3
-Stina,2
-Gorilla,2
-Gorilla,2
+Gorilla,14
+Gorilla,10
+Gorilla,9
+Gorilla,7
+Gorilla,6
index 68002b9cf9a11ab0f5acff90f1c8e25d033820f7..e3d622c06fbad0c5d72f6c3ee38be39a8b3dbfb1 100644 (file)
@@ -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<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;
-        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<float>(pow(-1, forward - 1)) * max_speed * direction;
+        sprite.setPosition(position);
+    }
+}
 
 sf::Vector2f Main_enemy::find_closest_bottle(std::vector<std::unique_ptr<Bottle>> & bottles)
 {
index fa1ecf49fc0c420dc9770a628cbe50569e3b836f..50a1d4a94db668db3b3ef7e214d03ae038f36ef6 100644 (file)
@@ -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<Bottle> const& bottle);
     sf::Vector2f find_closest_bottle(std::vector<std::unique_ptr<Bottle>> & bottles);
 };
index 6d56c3b3576039854f39bd59ed9a4a203d6f38e3..1bf0ce550173f83cd926687914ea0d13e7770e4f 100644 (file)
@@ -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
index c5614ac4177d9b4424058487243bc4a8e1fb4a63..459b3ef6acaa446aac5d42365dbeaff1ecffd239 100644 (file)
@@ -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
index 874c966528d3c39da841941362d4236dcdd04dfe..212937187934b227d5cec599ff385d8cf5b97bd9 100644 (file)
@@ -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);
     }