From: lukel495 Date: Tue, 14 Nov 2023 12:46:56 +0000 (+0100) Subject: merged helper and bottle X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=668d79d0f6d64a02c634f49c641e5fbd4de67067;p=TDDC76_proj.git merged helper and bottle --- 668d79d0f6d64a02c634f49c641e5fbd4de67067 diff --cc src/Helper.cc index 89fe161,e69de29..df7a5cf --- a/src/Helper.cc +++ b/src/Helper.cc @@@ -1,59 -1,0 +1,53 @@@ +#include "Helper.h" +#include "constants.h" +#include "States.h" +#include "Context.h" + + +#include + +Helper::Helper() : stop_pos{500} +{ + texture.loadFromFile("assets/ysex.png"); + sprite.setTexture(texture); + + sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); + sf::FloatRect gb {sprite.getGlobalBounds()}; + sprite.setOrigin(gb.width / 2, gb.height / 2); + position = {1000, 50}; + sprite.setPosition(position); +} + + - bool Helper::collides(Object& other) - { - - } - +void Helper::collision(Object& other) +{ + + +} + +void Helper::update(Context& context) +{ + direction = {0.35, 1}; + if (position.y == stop_pos && stop_pos > 50) + { + position -= direction; + stop_pos -= 1; + } + else + { + position += direction; + if (stop_pos == 50) + { + stop_pos = 500; - std::cout << stop_pos << std::endl; + } + } + //Game_state* game = static_cast(context.current_state.get()); + sprite.setPosition(position); + + return; +} + +void Helper::render(sf::RenderWindow& window) const +{ + window.draw(sprite); +} diff --cc src/Helper.h index c848c09,0aa1c54..08d430e --- a/src/Helper.h +++ b/src/Helper.h @@@ -2,23 -2,23 +2,22 @@@ #define HELPER_H #include +#include "Autonomous_object.h" -#include "Autonomus_object.h - -#include - -class Helper : public Autonomus_object +class Helper : public Autonomous_object { - public: - bool collides(Object& other) override; - void collision(Object& other) override; - void update() override; - void render(sf::RenderWindow& window) override; +public: + Helper(); + ~Helper() = default; - bool collides(Object& other) override; - protected: - void move(Time) override; + void collision(Object& other) override; + void update(Context& context) override; + void render(sf::RenderWindow& window) const override; - private: +protected: + //void move(Time); +private: + int stop_pos; }; #endif diff --cc src/Object.h index 56e11e3,1262cff..a7b7170 --- a/src/Object.h +++ b/src/Object.h @@@ -8,10 -8,13 +8,13 @@@ struct Context class Object { public: - Object() : position{}, sprite{}, texture{} {}; + Object() : position{}, sprite{}, texture{}, direction{} {}; virtual ~Object() = default; - virtual bool collides(Object& other) = 0; + bool collides(Object& other) + { + return get_hitbox().intersects(other.get_hitbox()); + } virtual void collision(Object& other) = 0; virtual void update(Context& context) = 0; virtual void render(sf::RenderWindow& window) const = 0; diff --cc src/States.cc index 4b66310,0d1422f..5a35d06 --- a/src/States.cc +++ b/src/States.cc @@@ -4,38 -5,77 +5,77 @@@ #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 ()); ++ ++ bottles.push_back(std::make_unique()); } --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; } - //std::vector :: iterator it = bottles.begin(); ++ // std::vector :: iterator it = bottles.begin(); + + /*for(std::unique_ptr &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()); - //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; @@@ -57,12 -97,12 +97,12 @@@ Start_menu::Start_menu() : texture{}, s 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) { @@@ -70,17 -110,17 +110,17 @@@ } } --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; @@@ -99,12 -139,12 +139,12 @@@ Pause_menu::Pause_menu() : texture{}, s 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) { @@@ -117,17 -157,17 +157,17 @@@ } } --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; diff --cc src/States.h index a2340e3,f311aba..eba4f92 --- a/src/States.h +++ b/src/States.h @@@ -5,7 -7,7 +7,8 @@@ #include "Map.h" #include "Player.h" +#include "Helper.h" + #include "Bottle.h" struct Context; //finns en strukt som säger att Context finns innan den är deklarerad @@@ -29,9 -31,10 +32,11 @@@ public void handle_input(sf::Event& event) override; protected: private: + float time_since_last_bottle; + std::vector> bottles; Map game_map; Player player; + Helper helper; bool pause_game; };