From b74d34c0b0fd21fc9496758452eccde15d07c6db Mon Sep 17 00:00:00 2001 From: stian853 Date: Mon, 27 Nov 2023 11:17:21 +0100 Subject: [PATCH] added bike enemy --- assets/data.json | 19 ++++++++++++++++++- src/Bike_enemy.cc | 38 ++++++++++++++++++++++++++++++++++++++ src/Bike_enemy.h | 27 +++++++++++++++++---------- src/States.cc | 43 +++++++++++++++++++++++++++---------------- src/States.h | 7 ++++++- src/YF.cc | 8 ++++---- src/YF.h | 3 ++- 7 files changed, 112 insertions(+), 33 deletions(-) diff --git a/assets/data.json b/assets/data.json index c3d1e55..8a341b3 100644 --- a/assets/data.json +++ b/assets/data.json @@ -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": @@ -43,5 +44,21 @@ { "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 diff --git a/src/Bike_enemy.cc b/src/Bike_enemy.cc index e69de29..24c6cd1 100644 --- a/src/Bike_enemy.cc +++ b/src/Bike_enemy.cc @@ -0,0 +1,38 @@ + +#include "Bike_enemy.h" +#include "constants.h" +#include "Context.h" +#include "States.h" +#include + + +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(context.current_state.get()); + position += direction*max_speed; + sprite.setPosition(position); +} +void Bike_enemy::render(sf::RenderWindow &window) const +{ + window.draw(sprite); +} diff --git a/src/Bike_enemy.h b/src/Bike_enemy.h index b00fbc9..838258d 100644 --- a/src/Bike_enemy.h +++ b/src/Bike_enemy.h @@ -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 +#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; }; diff --git a/src/States.cc b/src/States.cc index a522eeb..f1457fe 100644 --- a/src/States.cc +++ b/src/States.cc @@ -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_texture, data["bottle"])); @@ -119,22 +124,24 @@ void Game_state::update(Context &context) } if (time_since_last_yf >= 1 / static_cast(data["game_constants"]["yf_per_second"])) { - yf.push_back(std::make_unique(YF_texture)); - if(yf.size() > 1) - { - yf.erase(yf.begin()); - } + yf = std::make_unique(YF_texture, data["yf"]); time_since_last_yf = 0; } + if(time_since_last_bike >= 1 / static_cast(data["game_constants"]["bikes_per_second"])) + { + bike = std::make_unique(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) diff --git a/src/States.h b/src/States.h index 033d30a..55c5a5e 100644 --- a/src/States.h +++ b/src/States.h @@ -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 game_map; std::unique_ptr player; std::unique_ptr helper; std::unique_ptr enemy; + std::unique_ptr yf; + std::unique_ptr bike; bool pause_game; sf::Texture bottle_texture; sf::Texture main_enemy_texture; sf::Texture YF_texture; - std::vector> yf; + sf::Texture bike_texture; + //std::vector> yf; json data; int points; diff --git a/src/YF.cc b/src/YF.cc index 7759303..fcf3ed6 100644 --- a/src/YF.cc +++ b/src/YF.cc @@ -5,17 +5,18 @@ #include -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(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 diff --git a/src/YF.h b/src/YF.h index a8a98ba..6054da1 100644 --- 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: -- 2.30.2