added bike enemy
authorstian853 <stian853@su15-111.ad.liu.se>
Mon, 27 Nov 2023 10:17:21 +0000 (11:17 +0100)
committerstian853 <stian853@su15-111.ad.liu.se>
Mon, 27 Nov 2023 10:17:21 +0000 (11:17 +0100)
assets/data.json
src/Bike_enemy.cc
src/Bike_enemy.h
src/States.cc
src/States.h
src/YF.cc
src/YF.h

index c3d1e55c4abbbcf4155f362d5464ce5b6817bc74..8a341b3b11131764c91739257e2435887abea3ff 100644 (file)
@@ -30,7 +30,8 @@
     {
         "bottles_per_second" : 2,
         "game_time" : 20,
-        "yf_per_second": 0.125
+        "yf_per_second": 0.125,
+        "bikes_per_second": 0.17
         
     },
     "map":
     {
         "scale" : [0.35, 0.35],
         "start_pos" : [50, 600]
+    },
+    "yf":
+    {
+        "scale" : [1, 1],
+        "max_speed" : 5.0,
+        "direction": [0, -1]
+        
+    },
+
+    "bike_enemy":
+    {
+        "scale" : [0.3, 0.3],
+        "max_speed" : 3.0,
+        "direction": [0, 1]
+
     }
+
 }
\ No newline at end of file
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..24c6cd1c12aa352cb1f051d7a37b45016f3ac859 100644 (file)
@@ -0,0 +1,38 @@
+
+#include "Bike_enemy.h"
+#include "constants.h"
+#include "Context.h"
+#include "States.h"
+#include <random>
+
+
+Bike_enemy::Bike_enemy(sf::Texture& txtr, json& params) : max_speed{params["max_speed"]}
+{
+    direction = {params["direction"][0], params["direction"][1]};
+    texture = txtr;
+    sprite.setTexture(texture);
+    //position = {500, 500};
+    position ={rand()%(S_WIDTH*5/7 - S_WIDTH/5 +1) + S_WIDTH/5, 0}; //x-pixel WIDTH/5 - WIDTH*5/7
+    //sprite.setScale(params["scale"][0], params["scale"][1]);
+    sprite.setScale(params["scale"][0],params["scale"][1]);
+    sf::FloatRect gb {sprite.getGlobalBounds()};
+    sprite.setOrigin(gb.width / 2, gb.height / 2);
+    sprite.setPosition(position);
+
+}
+
+
+void Bike_enemy::collision(Object &other) 
+{
+
+}
+void Bike_enemy::update(Context& context)
+{
+    Game_state* game = static_cast<Game_state*>(context.current_state.get());
+    position += direction*max_speed;
+    sprite.setPosition(position);
+}
+void Bike_enemy::render(sf::RenderWindow &window) const
+{
+    window.draw(sprite);
+}
index b00fbc959e8f36b5a23f55db5a5e691ad55f4e97..838258dc9b9959c56fa3a17197329997403ca107 100644 (file)
@@ -1,20 +1,27 @@
-#ifndef  BIKE_ENEMY_H
+#ifndef BIKE_ENEMY_H
 #define BIKE_ENEMY_H
 
-class Bike_enemy
+#include "Moving_object.h"
+#include "Object.h"
+#include <SFML/Graphics.hpp>
+#include "json.hpp"
+
+using json = nlohmann::json;
+
+class Bike_enemy : public Moving_object
 {
 public:
-    bool collides(Object& other) override;
-    void collision(Object& other) override;
-    void update() override;
-    void render(sf::RenderWindow& window) override;
+    Bike_enemy(sf::Texture& txtr, json& params);
+    ~Bike_enemy() = default;
+    void collision(Object &other) override;
+    void update(Context& context) override;
+    void render(sf::RenderWindow &window) const override;
+    
 private:
-    void move(Time) override;
+    //void move(Time) override;
+    float max_speed;
 
 protected:
-    double x_pos;
-    double y_pos;
-    sf::Sprite sprite;
 
 };
 
index a522eeb33359c5fda9787aadeb582727ccdd08b5..f1457fe9f04230670a2403a86e00c3232df616d2 100644 (file)
@@ -17,12 +17,15 @@ Game_state::Game_state() :
     player{},
     bottles{}, 
     helper{},
+    bike{},
     main_enemy_texture{},
     time_since_last_bottle{0.0f},
     game_time{0},
     time_since_last_yf{0.0f},
+    time_since_last_bike{0.0f},
     bottle_texture {},
     YF_texture {},
+    bike_texture{},
     yf{},
     data{},
     enemy{},
@@ -40,6 +43,7 @@ Game_state::Game_state() :
 
     bottle_texture.loadFromFile("assets/kir.png");
     YF_texture.loadFromFile("assets/YF.png");
+    bike_texture.loadFromFile("assets/cyklist.png");
 }
 
 void Game_state::update(Context &context)
@@ -91,12 +95,12 @@ void Game_state::update(Context &context)
         player->collision(*helper);
         helper->collision(*player);
     }
-    if(yf.size()>0)
+    if(yf != nullptr)
     {    
-        if (player->collides(*yf[0]))
+        if (player->collides(*yf))
         {
             player->zero_collected();
-            player->collision(*yf[0]);
+            player->collision(*yf);
 
             //player->tumble();
             
@@ -112,6 +116,7 @@ void Game_state::update(Context &context)
 
     time_since_last_bottle += context.time.asSeconds();
     time_since_last_yf += context.time.asSeconds();
+    time_since_last_bike += 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"]));
@@ -119,22 +124,24 @@ void Game_state::update(Context &context)
     }
     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());
-        }
+        yf = std::make_unique<YF>(YF_texture, data["yf"]);
         time_since_last_yf = 0;
 
     }
+    if(time_since_last_bike >= 1 / static_cast<float>(data["game_constants"]["bikes_per_second"]))
+    {
+        bike = std::make_unique<Bike_enemy>(bike_texture, data["bike_enemy"]);
+        time_since_last_bike = 0;
+    }
     enemy->update(context);
     game_map->update(points, remaining_time);
-    if (yf.size()>0)
+    if (yf != nullptr)
     {
-        for(unsigned int i {0}; i < yf.size(); ++i)
-        {
-            yf[i]->update(context);
-        }
+        yf->update(context);
+    }
+    if(bike != nullptr)
+    {
+        bike->update(context);
     }
     
 }
@@ -145,11 +152,15 @@ void Game_state::render(sf::RenderWindow &window) const
     player->render(window);
     enemy->render(window);
     helper->render(window);
+    if(bike != nullptr)
+    {
+        bike->render(window);
+    }
+   
     
-    
-    for(unsigned int i {0}; i < yf.size(); ++i)
+    if(yf != nullptr)
     {
-        yf[i]->render(window);
+        yf->render(window);
     }
     
     for (int i{}; i < int(bottles.size()); ++i)
index 033d30a13aafa42453c01adc9a9bc798ef0e5857..55c5a5e56995504205695d6d9712b9db71c5a3b8 100644 (file)
@@ -12,6 +12,7 @@
 #include "Helper.h"
 #include "Bottle.h"
 #include "YF.h"
+#include "Bike_enemy.h"
 #include "json.hpp"
 
 using json = nlohmann::json;
@@ -44,17 +45,21 @@ private:
     float time_since_last_bottle;
     float game_time;
     float time_since_last_yf;
+    float time_since_last_bike;
     
     std::unique_ptr<Map> game_map;
     std::unique_ptr<Player> player;
     std::unique_ptr<Helper> helper;
     std::unique_ptr<Main_enemy> enemy;
+    std::unique_ptr<YF> yf;
+    std::unique_ptr<Bike_enemy> bike;
 
     bool pause_game;
     sf::Texture bottle_texture;
     sf::Texture main_enemy_texture;
     sf::Texture YF_texture;
-    std::vector<std::unique_ptr<YF>> yf;
+    sf::Texture bike_texture;
+    //std::vector<std::unique_ptr<YF>> yf;
     
     json data;
     int points;
index 77593034ec357ae02078c7d6bc3e9798df225f42..fcf3ed65460bc853e5af88bacddbb9f76597dc1f 100644 (file)
--- a/src/YF.cc
+++ b/src/YF.cc
@@ -5,17 +5,18 @@
 #include <random>
 
 
-YF::YF(sf::Texture& txtr)//, json& params)
+YF::YF(sf::Texture& txtr, json& params) : max_speed{params["max_speed"]}
 {
     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);
+    sprite.setScale(params["scale"][0],params["scale"][1]);
     sf::FloatRect gb {sprite.getGlobalBounds()};
     sprite.setOrigin(gb.width / 2, gb.height / 2);
     sprite.setPosition(position);
+    direction = {params["direction"][0], params["direction"][1]};
 
 }
 
@@ -27,8 +28,7 @@ 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;
+    position += direction*max_speed;
     sprite.setPosition(position);
 }
 void YF::render(sf::RenderWindow &window) const
index a8a98ba333bbcb12146beb72e1b569d15fc81747..6054da1fd0a24109020f373602394e22f0bb37b8 100644 (file)
--- a/src/YF.h
+++ b/src/YF.h
@@ -11,7 +11,7 @@ using json = nlohmann::json;
 class YF : public Moving_object
 {
 public:
-    YF(sf::Texture& txtr);//, json& params);
+    YF(sf::Texture& txtr, json& params);
     ~YF() = default;
     void collision(Object &other) override;
     void update(Context& context) override;
@@ -19,6 +19,7 @@ public:
 
 private:
     //void move(Time) override;
+    float max_speed;
 
 protected: