fixed function size
authorNilsForssen <forssennils@gmail.com>
Mon, 11 Dec 2023 08:11:31 +0000 (09:11 +0100)
committerNilsForssen <forssennils@gmail.com>
Mon, 11 Dec 2023 08:11:31 +0000 (09:11 +0100)
src/States.cc
src/States.h

index 6063c652647858cf01fff133339a36c24402ec37..ab7332ea127487c25fe62efc4e16c38aec0cd3cf 100644 (file)
@@ -9,26 +9,31 @@
 
 using json = nlohmann::json;
 
-Game_state::Game_state(json& params) : 
-    game_map{}, 
-    pause_game{false},
-    player{},
+Game_state::Game_state(json& params) :
     bottles{}, 
-    helper{},
-    bike{},
+
     time_since_last_bottle{0.0f},
     game_time{0},
     time_since_last_yf{0.0f},
     time_since_last_bike{0.0f},
+    points{0},
+
+    pause_game{false},
+
+    game_map{}, 
+    player{},
+    helper{},
+    enemy{},
+    yf{},
+    bike{},
+
     bottle_texture {},
     YF_texture {},
     bike_texture{},
-    yf{},
-    data{},
-    enemy{},
-    points{0}
+
+    data{}
 {
-    data = std::move(params);
+    data = params;
     sf::Texture main_enemy_texture;
     sf::Texture sack_texture;
     sf::Texture player_texture;
@@ -58,77 +63,23 @@ void Game_state::update(Context &context)
     if (pause_game)
     {
         context.saved_game.reset(static_cast<Game_state *>(context.current_state.release()));
-
         context.next_state = std::make_unique<Pause_menu>(data);
         pause_game = false;
         return;
     }
-       
-        
     game_time += context.time.asSeconds();
-    int remaining_time {static_cast<int>(data["game_constants"]["game_time"]) - static_cast<int>(game_time)};
+    int remaining_time {static_cast<int>(data["game_constants"]["game_time"]) -
+        static_cast<int>(game_time)};
     if (remaining_time < 0)
     {
         context.next_state = std::make_unique<GameOver_menu>(points, data);
         return;
     }
-    
-
-    for (unsigned int i {0}; i < bottles.size(); ++i)
-    {
-        if (enemy->collides(*(bottles[i])))
-        {
-            
-            bottles.erase(bottles.begin() + i);
-            
-        }
-        else if (player->collides(*(bottles[i])))
-        {
-            bottles.erase(bottles.begin() + i);
-            player->add_collected();
-        }
-    }
-    
-    player->update(context);
-    helper->update(context);
-
-    if (player->collides(*helper))
-    {
-        points += player->get_collected() + floor(player->get_collected() / static_cast<float>(data["game_constants"]["point_scale_div"]));
-        player->zero_collected();
 
-        player->collision(*helper);
-        helper->collision(*player);
-    }
-    if (player->collides(*enemy))
-    {
-        enemy->collision(*player);
-    }
-    if(yf != nullptr)
-    {    
-        if (player->collides(*yf))
-        {
-            player->zero_collected();
-            player->collision(*yf);
-        }
-        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);
-        }
-    }
+    update_entities(context);
+    game_map->update(player->get_collected(), points, remaining_time);
     
+    handle_collisions();
 
     time_since_last_bottle += context.time.asSeconds();
     time_since_last_yf += context.time.asSeconds();
@@ -149,20 +100,6 @@ void Game_state::update(Context &context)
         bike = std::make_unique<Bike_enemy>(bike_texture, data["bike_enemy"]);
         time_since_last_bike = 0;
     }
-    enemy->update(context);
-    game_map->update(player->get_collected(), points, remaining_time);
-    if (yf != nullptr)
-    {
-        yf->update(context);
-    }
-    if(bike != nullptr)
-    {
-        bike->update(context);
-    }
-    if(game_map->collides(*player))
-    {
-        player->collision(*game_map);
-    }
 }
 
 void Game_state::render(sf::RenderWindow &window) const
@@ -175,8 +112,6 @@ void Game_state::render(sf::RenderWindow &window) const
     {
         bike->render(window);
     }
-   
-    
     if(yf != nullptr)
     {
         yf->render(window);
@@ -188,6 +123,87 @@ void Game_state::render(sf::RenderWindow &window) const
     }
 }
 
+void Game_state::update_entities(Context& context)
+{
+    player->update(context);
+    helper->update(context);
+    enemy->update(context);
+    if (yf != nullptr)
+    {
+        yf->update(context);
+    }
+    if(bike != nullptr)
+    {
+        bike->update(context);
+    }
+}
+
+void Game_state::handle_collisions()
+{
+    if (player->collides(*helper))
+    {
+        points += player->get_collected() +
+            floor(player->get_collected() /
+            static_cast<float>(data["game_constants"]["point_scale_div"]));
+        player->zero_collected();
+
+        player->collision_helper();
+        helper->collision_player();
+    }
+    if (player->collides(*enemy))
+    {
+        enemy->collision_player();
+        player->collision_enemy();
+    }
+    if(yf != nullptr)
+    {    
+        if (player->collides(*yf))
+        {
+            player->zero_collected();
+
+            player->collision_yf();
+            yf->collision_player();
+        }
+        if(enemy->collides(*yf))
+        {
+            enemy->collision_yf();
+            yf->collision_enemy();
+        }
+    }
+    if(bike != nullptr)
+    {    
+        if (player->collides(*bike))
+        {
+            player->zero_collected();
+
+            player->collision_bike();
+            bike->collision_player();
+        }
+        if(enemy->collides(*bike))
+        {
+            enemy->collision_bike();
+            bike->collision_enemy();
+        }
+    }
+    if(game_map->collides(*player))
+    {
+        player->collision_map();
+        game_map->collision_player();
+    }
+    for (unsigned int i {0}; i < bottles.size(); ++i)
+    {
+        if (enemy->collides(*(bottles[i])))
+        {
+            bottles.erase(bottles.begin() + i);
+        }
+        else if (player->collides(*(bottles[i])))
+        {
+            bottles.erase(bottles.begin() + i);
+            player->add_collected();
+        }
+    }
+}
+
 void Game_state::handle_input(sf::Event &event)
 {
     switch (event.type)
@@ -203,7 +219,6 @@ void Game_state::handle_input(sf::Event &event)
                 
                 break;
         }
-        
         break;
     case sf::Event::KeyPressed:
         switch (event.key.code)
@@ -219,7 +234,6 @@ void Game_state::handle_input(sf::Event &event)
     default:
         break;
     }
-
     player->handle_input(event);
 }
 
index 8e97c34315666263ba4e4f1f4720f09673ddc724..92c6facdab066670e3dbb79ef35b04decabf81bd 100644 (file)
@@ -39,6 +39,10 @@ public:
     std::vector<std::unique_ptr<Bottle>> bottles;
 protected:
 private:
+    void handle_collisions();
+    void update_entities(Context& context);
+
+
     float time_since_last_bottle;
     float game_time;
     float time_since_last_yf;