{
"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
+
+#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);
+}
-#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;
};
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{},
bottle_texture.loadFromFile("assets/kir.png");
YF_texture.loadFromFile("assets/YF.png");
+ bike_texture.loadFromFile("assets/cyklist.png");
}
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();
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"]));
}
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);
}
}
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)
#include "Helper.h"
#include "Bottle.h"
#include "YF.h"
+#include "Bike_enemy.h"
#include "json.hpp"
using json = nlohmann::json;
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;
#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]};
}
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
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;
private:
//void move(Time) override;
+ float max_speed;
protected: