#include "Context.h"
#include "constants.h"
- Game_state::Game_state() : game_map{}, player{}, helper{}, pause_game{false}
-Game_state::Game_state() : game_map{}, player{}, pause_game{false}, bottles{}, time_since_last_bottle{0.0f}
++Game_state::Game_state() : game_map{}, player{}, helper{}, pause_game{false}, bottles{}, time_since_last_bottle{0.0f}
{
-
- bottles.push_back(std::make_unique<Bottle> ());
++
++ bottles.push_back(std::make_unique<Bottle>());
}
--void Game_state::update(Context& context)
++void Game_state::update(Context &context)
{
if (pause_game)
-- {
-- context.saved_game.reset(static_cast<Game_state*>(context.current_state.release()));
--
++ {
++ context.saved_game.reset(static_cast<Game_state *>(context.current_state.release()));
++
context.next_state = std::make_unique<Pause_menu>();
pause_game = false;
return;
}
- //std::vector<Bottle> :: iterator it = bottles.begin();
++ // std::vector<Bottle> :: iterator it = bottles.begin();
+
+ /*for(std::unique_ptr<Bottle> &b: bottles)
+ {
+ if(player.collides(*b))
+ {
+ std::cout <<"crash" << std::endl;
-
++
+ 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])))
++ if (player.collides(*(bottles[i])))
+ {
- //std::cout <<"crash" << std::endl;
++ // std::cout <<"crash" << std::endl;
+ bottles.erase(bottles.begin() + i);
+ player.add_collected();
+ }
+ }
+ 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)
+ {
+ if (bottles.size() > 10)
+ {
+ bottles.erase(bottles.begin());
+ }
-
++
+ bottles.push_back(std::make_unique<Bottle>());
- //std::cout << "placed bottle"<< std::endl;
++ // std::cout << "placed bottle"<< std::endl;
+ time_since_last_bottle = 0;
-
+ }
game_map.update(context);
player.update(context);
+ helper.update(context);
}
--void Game_state::render(sf::RenderWindow& window) const
++void Game_state::render(sf::RenderWindow &window) const
{
game_map.render(window);
player.render(window);
- for (int i {}; i < int(bottles.size()); ++i)
+ helper.render(window);
++ for (int i{}; i < int(bottles.size()); ++i)
+ {
+ bottles[i]->render(window);
- }
-
++ }
}
--void Game_state::handle_input(sf::Event& event)
++void Game_state::handle_input(sf::Event &event)
{
switch (event.type)
{
case sf::Event::KeyPressed:
-- switch(event.key.code)
++ switch (event.key.code)
{
case sf::Keyboard::Escape:
pause_game = true;
texture.loadFromFile("assets/sack.png");
sprite.setTexture(texture);
-- sf::FloatRect gb {sprite.getGlobalBounds()};
++ sf::FloatRect gb{sprite.getGlobalBounds()};
sprite.setOrigin(gb.width / 2, gb.height / 2);
sprite.setPosition(S_WIDTH / 2, S_HEIGHT / 2);
}
--void Start_menu::update(Context& context)
++void Start_menu::update(Context &context)
{
if (exit_menu)
{
}
}
--void Start_menu::render(sf::RenderWindow& window) const
++void Start_menu::render(sf::RenderWindow &window) const
{
window.draw(sprite);
}
--void Start_menu::handle_input(sf::Event& event)
++void Start_menu::handle_input(sf::Event &event)
{
switch (event.type)
{
case sf::Event::KeyPressed:
-- switch(event.key.code)
++ switch (event.key.code)
{
case sf::Keyboard::Enter:
exit_menu = true;
texture.loadFromFile("assets/kir.png");
sprite.setTexture(texture);
-- sf::FloatRect gb {sprite.getGlobalBounds()};
++ sf::FloatRect gb{sprite.getGlobalBounds()};
sprite.setOrigin(gb.width / 2, gb.height / 2);
sprite.setPosition(S_WIDTH / 2, S_HEIGHT / 2);
}
--void Pause_menu::update(Context& context)
++void Pause_menu::update(Context &context)
{
if (resume_game)
{
}
}
--void Pause_menu::render(sf::RenderWindow& window) const
++void Pause_menu::render(sf::RenderWindow &window) const
{
window.draw(sprite);
}
--void Pause_menu::handle_input(sf::Event& event)
++void Pause_menu::handle_input(sf::Event &event)
{
switch (event.type)
{
case sf::Event::KeyPressed:
-- switch(event.key.code)
++ switch (event.key.code)
{
case sf::Keyboard::Enter:
resume_game = true;