From: Nils Forssén Date: Tue, 28 Nov 2023 12:42:39 +0000 (+0100) Subject: fixed merge X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=c7f4b55b84cfe1b9dc9dcf2dfcb930d6741dec77;p=TDDC76_proj.git fixed merge --- c7f4b55b84cfe1b9dc9dcf2dfcb930d6741dec77 diff --cc src/States.cc index a522eeb,50a8ba3..537fc92 --- a/src/States.cc +++ b/src/States.cc @@@ -5,160 -5,40 +5,168 @@@ #include "Context.h" #include "constants.h" -Game_state::Game_state() : game_map{}, player{}, pause_game{false}, gameover{false} +#include "json.hpp" +#include +#include + +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(data["player"]); + helper = std::make_unique(data["helper"]); + game_map = std::make_unique(data["map"]); + enemy = std::make_unique(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(context.current_state.release())); - + { + context.saved_game.reset(static_cast(context.current_state.release())); + context.next_state = std::make_unique(); pause_game = false; return; } + + game_time += context.time.asSeconds(); + int remaining_time {static_cast(data["game_constants"]["game_time"]) - static_cast(game_time)}; + if (remaining_time < 0) + { + context.next_state = std::make_unique(); + 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_texture, data["bottle"])); + time_since_last_bottle = 0; + } + if (time_since_last_yf >= 1 / static_cast(data["game_constants"]["yf_per_second"])) + { + yf.push_back(std::make_unique(YF_texture)); + if(yf.size() > 1) + { + yf.erase(yf.begin()); + } + time_since_last_yf = 0; + } + 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 = false; + return; + } - - game_map.update(context); - player.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); + 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) { @@@ -334,167 -61,7 +189,7 @@@ 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(); - 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; - } - } diff --cc src/States.h index 033d30a,2b07839..920e493 --- a/src/States.h +++ b/src/States.h @@@ -38,27 -27,12 +38,28 @@@ public void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; - Map game_map; + std::vector> bottles; protected: private: - Player player; + float time_since_last_bottle; + float game_time; + float time_since_last_yf; + + std::unique_ptr game_map; + std::unique_ptr player; + std::unique_ptr helper; + std::unique_ptr enemy; + bool pause_game; + sf::Texture bottle_texture; + sf::Texture main_enemy_texture; + sf::Texture YF_texture; + std::vector> yf; + + json data; + int points; + + bool gameover; }; class Start_menu : public State