From c2ebd7df454d573975571b1a1e8725fbc66d9fe3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Arvid=20Sj=C3=B6blom?= Date: Mon, 20 Nov 2023 09:54:54 +0100 Subject: [PATCH] =?utf8?q?lagt=20till=20ett=20yf-t=C3=A5g,=20arvid,=20stin?= =?utf8?q?a?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/States.cc | 29 ++++++++++++++++++++++++++++- src/States.h | 5 +++++ src/Voi_enemy.cc | 0 src/Voi_enemy.h | 23 ----------------------- src/YF.cc | 37 +++++++++++++++++++++++++++++++++++++ src/YF.h | 27 +++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 24 deletions(-) delete mode 100644 src/Voi_enemy.cc delete mode 100644 src/Voi_enemy.h create mode 100644 src/YF.cc create mode 100644 src/YF.h diff --git a/src/States.cc b/src/States.cc index ee4a815..95cba14 100644 --- a/src/States.cc +++ b/src/States.cc @@ -16,9 +16,13 @@ Game_state::Game_state() : player{}, bottles{}, time_since_last_bottle{0.0f}, + time_since_last_yf{0.0f}, bottle_texture {}, + YF_texture {}, + yf{}, data{}, enemy{} + { std::ifstream f("assets/data.json"); data = json::parse(f); @@ -28,6 +32,7 @@ Game_state::Game_state() : helper = std::make_unique(data["helper"]); bottle_texture.loadFromFile("assets/kir.png"); + YF_texture.loadFromFile("assets/YF.png"); } void Game_state::update(Context &context) @@ -70,17 +75,32 @@ void Game_state::update(Context &context) } } time_since_last_bottle += context.time.asSeconds(); + time_since_last_yf += context.time.asSeconds(); //std::cout << std::fixed << std::setprecision(3) << time_since_last_bottle << std::endl; if (time_since_last_bottle >= data["game_constants"]["bottles_per_second"]) { bottles.push_back(std::make_unique(bottle_texture, data["bottle"])); - std::cout << "placed bottle"<< std::endl; + //std::cout << "placed bottle"<< std::endl; time_since_last_bottle = 0; } + if (time_since_last_yf >= 5) + { + yf.push_back(std::make_unique(YF_texture)); + std::cout << "yf spawned"<< std::endl; + time_since_last_yf = 0; + } game_map.update(context); player->update(context); helper->update(context); enemy.update(context); + if (yf.size()>0) + { + for(unsigned int i {0}; i < yf.size(); ++i) + { + yf[i]->update(context); + } + } + } void Game_state::render(sf::RenderWindow &window) const @@ -89,6 +109,13 @@ void Game_state::render(sf::RenderWindow &window) const 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); diff --git a/src/States.h b/src/States.h index 52d0090..45fff0c 100644 --- a/src/States.h +++ b/src/States.h @@ -11,6 +11,7 @@ #include "Main_enemy.h" #include "Helper.h" #include "Bottle.h" +#include "YF.h" #include "json.hpp" using json = nlohmann::json; @@ -41,6 +42,7 @@ public: protected: private: float time_since_last_bottle; + float time_since_last_yf; Map game_map; std::unique_ptr player; @@ -48,6 +50,9 @@ private: Main_enemy enemy; bool pause_game; sf::Texture bottle_texture; + sf::Texture YF_texture; + std::vector> yf; + json data; }; diff --git a/src/Voi_enemy.cc b/src/Voi_enemy.cc deleted file mode 100644 index e69de29..0000000 diff --git a/src/Voi_enemy.h b/src/Voi_enemy.h deleted file mode 100644 index 4401545..0000000 --- a/src/Voi_enemy.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef VOI_ENEMY_H -#define VOI_ENEMY_H - -#include - -class Voi_enemy -{ -public: - bool collides(Object &other) override; - void collision(Object &other) override; - void update() override; - void render(sf::RenderWindow &window) override; - -private: - void move(Time) override; - -protected: - double x_pos; - double y_pos; - sf::Sprite sprite; -}; - -#endif \ No newline at end of file diff --git a/src/YF.cc b/src/YF.cc new file mode 100644 index 0000000..7759303 --- /dev/null +++ b/src/YF.cc @@ -0,0 +1,37 @@ +#include "YF.h" +#include "constants.h" +#include "Context.h" +#include "States.h" +#include + + +YF::YF(sf::Texture& txtr)//, json& params) +{ + texture = txtr; + sprite.setTexture(texture); + //position = {500, 500}; + position ={rand()%(S_WIDTH*5/7 - S_WIDTH/5 +1) + S_WIDTH/5, S_HEIGHT}; //x-pixel WIDTH/5 - WIDTH*5/7 + //sprite.setScale(params["scale"][0], params["scale"][1]); + sprite.setScale(1, 1); + sf::FloatRect gb {sprite.getGlobalBounds()}; + sprite.setOrigin(gb.width / 2, gb.height / 2); + sprite.setPosition(position); + +} + + +void YF::collision(Object &other) +{ + +} +void YF::update(Context& context) +{ + Game_state* game = static_cast(context.current_state.get()); + direction = {0, -1}; + position += direction*2.0f; + sprite.setPosition(position); +} +void YF::render(sf::RenderWindow &window) const +{ + window.draw(sprite); +} diff --git a/src/YF.h b/src/YF.h new file mode 100644 index 0000000..a8a98ba --- /dev/null +++ b/src/YF.h @@ -0,0 +1,27 @@ +#ifndef YF_H +#define YF_H + +#include "Moving_object.h" +#include "Object.h" +#include +#include "json.hpp" + +using json = nlohmann::json; + +class YF : public Moving_object +{ +public: + YF(sf::Texture& txtr);//, json& params); + ~YF() = default; + void collision(Object &other) override; + void update(Context& context) override; + void render(sf::RenderWindow &window) const override; + +private: + //void move(Time) override; + +protected: + +}; + +#endif \ No newline at end of file -- 2.30.2