#include "Context.h"
#include "constants.h"
-Game_state::Game_state() : game_map{}, player{}, pause_game{false}, gameover{false}
+#include "json.hpp"
+#include <fstream>
+#include <cmath>
+
+using json = nlohmann::json;
+
+Game_state::Game_state() :
+ game_map{},
+ pause_game{false},
++ gameover{false},
+ player{},
+ bottles{},
+ helper{},
+ main_enemy_texture{},
+ time_since_last_bottle{0.0f},
+ game_time{0},
+ time_since_last_yf{0.0f},
+ bottle_texture {},
+ YF_texture {},
+ yf{},
+ data{},
+ enemy{},
+ points{0}
{
+ std::ifstream f("assets/data.json");
+ data = json::parse(f);
+
+ main_enemy_texture.loadFromFile("assets/6Ling_figur2.png");
+
+ player = std::make_unique<Player>(data["player"]);
+ helper = std::make_unique<Helper>(data["helper"]);
+ game_map = std::make_unique<Map>(data["map"]);
+ enemy = std::make_unique<Main_enemy>(main_enemy_texture, data["main_enemy"]);
+
+ bottle_texture.loadFromFile("assets/kir.png");
+ YF_texture.loadFromFile("assets/YF.png");
}
-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;
}
+
+ game_time += context.time.asSeconds();
+ 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<Start_menu>();
+ return;
+ }
+
+
+ for (unsigned int i {0}; i < bottles.size(); ++i)
+ {
+ if (player->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])))
+ {
+ // std::cout <<"crash" << std::endl;
+ bottles.erase(bottles.begin() + i);
+
+ }
+ }
+ player->update(context);
+ helper->update(context);
+
+ if (player->collides(*helper))
+ {
+ points += player->get_collected();
+ player->zero_collected();
+
+ player->collision(*helper);
+ helper->collision(*player);
+ }
+ if(yf.size()>0)
+ {
+ if (player->collides(*yf[0]))
+ {
+ player->zero_collected();
+ player->collision(*yf[0]);
+
+ //player->tumble();
+
+ }
+ }
+
+ /*
+ 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();
+ if (time_since_last_bottle >= data["game_constants"]["bottles_per_second"])
+ {
+ bottles.push_back(std::make_unique<Bottle>(bottle_texture, data["bottle"]));
+ time_since_last_bottle = 0;
+ }
+ if (time_since_last_yf >= 1 / static_cast<float>(data["game_constants"]["yf_per_second"]))
+ {
+ yf.push_back(std::make_unique<YF>(YF_texture));
+ if(yf.size() > 1)
+ {
+ yf.erase(yf.begin());
+ }
+ time_since_last_yf = 0;
-
- game_map.update(context);
- player.update(context);
-
+ }
+ enemy->update(context);
+ game_map->update(points, remaining_time);
+ if (yf.size()>0)
+ {
+ for(unsigned int i {0}; i < yf.size(); ++i)
+ {
+ yf[i]->update(context);
+ }
+ }
+
+ if (gameover)
+ {
+ context.next_state = std::make_unique<GameOver_menu>();
+ gameover = false;
+ return;
+ }
++
}
-void Game_state::render(sf::RenderWindow& window) const
+void Game_state::render(sf::RenderWindow &window) const
{
- game_map.render(window);
- player.render(window);
+ game_map->render(window);
+ player->render(window);
+ enemy->render(window);
+ helper->render(window);
+
+
+ for(unsigned int i {0}; i < yf.size(); ++i)
+ {
+ yf[i]->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)
{
default:
break;
}
- }
-
- // Pause menu --------------------------------------------
- Pause_menu::Pause_menu() : texture{}, texture2{}, sprite{}, resume_game{false}, menu{false}, exit_game{false},
- resumetext{}, startmenutext{}, quittext{}, mouse_r{}, mouse_l{}, font{}, menu_index{1}
- {
- texture.loadFromFile("assets/Pause_bild.png");
- sprite.setTexture(texture);;
-
- sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF);
- sf::FloatRect gb {sprite.getGlobalBounds()};
- sprite.setPosition(0, 0);
-
- // mouse
- texture2.loadFromFile("assets/muspekareGul.png");
- mouse_l.setTexture(texture2);
-
- mouse_l.setScale(0.1, 0.1);
- mouse_l.setRotation(180);
- sf::FloatRect gbm {mouse_l.getGlobalBounds()};
-
- mouse_r.setTexture(texture2);
- mouse_r.setScale(0.1, 0.1);
-
-
- //load textfont
- if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") )
- throw std::invalid_argument ("Unable to load font");
-
-
- // Resume text
- resumetext = sf::Text{ "Resume", font, 24 };
- sf::FloatRect gbr {resumetext.getGlobalBounds()};
- resumetext.setOrigin(gbr.width / 2, gbr.height / 2);
- resumetext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) - 30));
-
- // Start menu text
- startmenutext = sf::Text{ "Start menu", font, 24 };
- sf::FloatRect gbs {startmenutext.getGlobalBounds()};
- startmenutext.setOrigin(gbs.width / 2, gbs.height / 2);
- startmenutext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) + 20));
-
- //quit text
- quittext = sf::Text{ "Quit", font, 24 };
- sf::FloatRect gbq {quittext.getGlobalBounds()};
- quittext.setOrigin(gbq.width / 2, gbq.height / 2 );
- quittext.setPosition (S_WIDTH / 2, ((S_HEIGHT / 2) + 70));
- }
-
- void Pause_menu::update(Context &context)
- {
- if (resume_game)
- {
- context.next_state = std::move(context.saved_game);
- }
- else if (menu)
- {
- context.next_state = std::make_unique<Start_menu>();
- context.saved_game.release();
- }
-
- // changes color on text depending on selection
- if( menu_index == 1)
- {
- resumetext.setFillColor(sf::Color::Yellow);
- startmenutext.setFillColor(sf::Color::Black);
- quittext.setFillColor(sf::Color::Black);
- }
- else if( menu_index == 2)
- {
- resumetext.setFillColor(sf::Color::Black);
- startmenutext.setFillColor(sf::Color::Yellow);
- quittext.setFillColor(sf::Color::Black);
- }
- else if( menu_index == 3)
- {
- resumetext.setFillColor(sf::Color::Black);
- startmenutext.setFillColor(sf::Color::Black);
- quittext.setFillColor(sf::Color::Yellow);
- }
-
- // mouse placement
- mouse_r.setPosition(((S_WIDTH / 2) + 60), ((S_HEIGHT / 2) - 45 + 50*(menu_index - 1)));
- mouse_l.setPosition(((S_WIDTH / 2) - 58), ((S_HEIGHT / 2) + 2 + 50*(menu_index - 1)));
- }
-
- void Pause_menu::render(sf::RenderWindow &window) const
- {
- window.draw(sprite);
- window.draw(mouse_l);
- window.draw(mouse_r);
-
- window.draw(resumetext);
- window.draw(startmenutext);
- window.draw(quittext);
-
- if (exit_game)
- {
- window.close();
- }
- player.handle_input(event);
++ player->handle_input(event);
}
- void Pause_menu::handle_input(sf::Event &event)
- {
- switch (event.type)
- {
- case sf::Event::KeyPressed:
- switch (event.key.code)
- {
- case sf::Keyboard::Enter:
- if(menu_index == 1)
- {
- resume_game = true;
- menu_index = 1;
- }
-
- else if(menu_index == 2)
- {
- menu = true;
- menu_index = 1;
- }
-
- else if(menu_index == 3)
- {
- exit_game = true;
- menu_index = 1;
- }
- break;
-
-
- case sf::Keyboard::Up:
- if(menu_index == 1)
- {
- break;
- }
-
- else;
- {
- menu_index -= 1;
- }
- break;
-
- case sf::Keyboard::Down:
- if(menu_index == 3)
- {
- break;
- }
-
- else;
- {
- menu_index += 1;
- }
- break;
-
- default:
- break;
- }
-
- break;
- default:
- break;
- }
- }