From: NilsForssen Date: Mon, 11 Dec 2023 08:11:31 +0000 (+0100) Subject: fixed function size X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=bc8ef788f9609b55f00dadd8e0685c40950dfe7d;p=TDDC76_proj.git fixed function size --- diff --git a/src/States.cc b/src/States.cc index 6063c65..ab7332e 100644 --- a/src/States.cc +++ b/src/States.cc @@ -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(context.current_state.release())); - context.next_state = std::make_unique(data); pause_game = false; return; } - - game_time += context.time.asSeconds(); - int remaining_time {static_cast(data["game_constants"]["game_time"]) - static_cast(game_time)}; + int remaining_time {static_cast(data["game_constants"]["game_time"]) - + static_cast(game_time)}; if (remaining_time < 0) { context.next_state = std::make_unique(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(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_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(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); } diff --git a/src/States.h b/src/States.h index 8e97c34..92c6fac 100644 --- a/src/States.h +++ b/src/States.h @@ -39,6 +39,10 @@ public: std::vector> bottles; protected: private: + void handle_collisions(); + void update_entities(Context& context); + + float time_since_last_bottle; float game_time; float time_since_last_yf;