lagt till ett yf-tåg, arvid, stina
authorArvid Sjöblom <arvsj277@student.liu.se>
Mon, 20 Nov 2023 08:54:54 +0000 (09:54 +0100)
committerArvid Sjöblom <arvsj277@student.liu.se>
Mon, 20 Nov 2023 08:54:54 +0000 (09:54 +0100)
src/States.cc
src/States.h
src/Voi_enemy.cc [deleted file]
src/Voi_enemy.h [deleted file]
src/YF.cc [new file with mode: 0644]
src/YF.h [new file with mode: 0644]

index ee4a815fdf116a133eb4adb73c62dc22b0813bca..95cba1422cc889e1a5448cb84290716dbc36e054 100644 (file)
@@ -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<Helper>(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>(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>(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);
index 52d0090128c58aa4f1814a1570a1e1a8b67b3376..45fff0c2657cab92e45b89da90d720fca57a1e57 100644 (file)
@@ -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> player;
@@ -48,6 +50,9 @@ private:
     Main_enemy enemy;
     bool pause_game;
     sf::Texture bottle_texture;
+    sf::Texture YF_texture;
+    std::vector<std::unique_ptr<YF>> yf;
+    
     json data;
 
 };
diff --git a/src/Voi_enemy.cc b/src/Voi_enemy.cc
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/src/Voi_enemy.h b/src/Voi_enemy.h
deleted file mode 100644 (file)
index 4401545..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifndef VOI_ENEMY_H
-#define VOI_ENEMY_H
-
-#include <SFML/Graphics.hpp>
-
-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 (file)
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 <random>
+
+
+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<Game_state*>(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 (file)
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 <SFML/Graphics.hpp>
+#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