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;
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();
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
{
bike->render(window);
}
-
-
if(yf != nullptr)
{
yf->render(window);
}
}
+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)
break;
}
-
break;
case sf::Event::KeyPressed:
switch (event.key.code)
default:
break;
}
-
player->handle_input(event);
}