From d9f02bfbc0da4c9b245d8dcb446a98539835d694 Mon Sep 17 00:00:00 2001 From: NilsForssen Date: Fri, 8 Dec 2023 12:04:27 +0100 Subject: [PATCH] Removed warnings --- assets/data.json | 158 ++++++++++++++++++++++++----------------- assets/highscore.csv | 1 + src/Bike_enemy.cc | 7 +- src/Bike_enemy.h | 3 +- src/Bottle.cc | 10 ++- src/Bottle.h | 1 - src/GameOver_menu.cc | 40 ++++++----- src/Helper.cc | 14 ++-- src/Helper.h | 4 +- src/Main_enemy.cc | 28 +++++--- src/Main_enemy.h | 15 ++-- src/Map.cc | 16 ++++- src/Map.h | 7 +- src/Object.h | 1 - src/Pause_menu.cc | 25 ++++++- src/Player.cc | 43 +++++++---- src/Player.h | 14 ++-- src/Scoreboard_menu.cc | 26 ++++++- src/Start_menu.cc | 25 ++++++- src/States.cc | 93 ++++++++++++++---------- src/States.h | 66 +++++++++++------ src/YF.cc | 13 ++-- src/YF.h | 3 +- 23 files changed, 406 insertions(+), 207 deletions(-) diff --git a/assets/data.json b/assets/data.json index df0fe24..a235262 100644 --- a/assets/data.json +++ b/assets/data.json @@ -1,75 +1,111 @@ { - "player" : - { - "start_pos": [200, 200], - "scale": [0.5, 0.5], + "player": { + "start_pos": [ + 200, + 200 + ], + "scale": [ + 0.5, + 0.5 + ], "max_speed": 5, "degrees_per_second": 360, - "sack" : - { - "offset" : [25, -5], - "scale" : [0.3, 0.3], - "angle" : -25 + "sack": { + "offset": [ + 25, + -5 + ], + "scale": [ + 0.3, + 0.3 + ], + "angle": -25 } - }, "helper": { - "start_pos": [1000, 50], - "scale": [0.3, 0.3], + "start_pos": [ + 1000, + 50 + ], + "scale": [ + 0.3, + 0.3 + ], "max_speed": 2, - - "direction": [0.35, 1], + "direction": [ + 0.35, + 1 + ], "stop_top": 50, "stop_bot": 500 }, - "bottle": - { - "scale": [0.5, 0.5] + "bottle": { + "scale": [ + 0.5, + 0.5 + ] }, - "game_constants": - { - "scoreboard" : 5, - "bottles_per_second" : 1.5, - "game_time" : 10, + "game_constants": { + "scoreboard": 5, + "bottles_per_second": 1.5, + "game_time": 30, "yf_per_second": 0.125, "bikes_per_second": 0.17, "point_scale_div": 3.0, "highscore_file": "assets/highscore.csv" }, - "map": - { - "points_pos": [5, 38], - "collected_pos": [5, 76], - "time_pos": [5, 0], + "map": { + "points_pos": [ + 5, + 38 + ], + "collected_pos": [ + 5, + 76 + ], + "time_pos": [ + 5, + 0 + ], "font_size": 36 }, - - "main_enemy": - { - "scale" : [0.35, 0.35], - "start_pos" : [50, 600], + "main_enemy": { + "scale": [ + 0.35, + 0.35 + ], + "start_pos": [ + 50, + 600 + ], "max_speed": 2.0, "degrees_per_second": 360 }, - "yf": - { - "scale" : [1, 1], - "max_speed" : 2.0, - "direction": [0, -1] - + "yf": { + "scale": [ + 1, + 1 + ], + "max_speed": 2.0, + "direction": [ + 0, + -1 + ] }, - - "bike_enemy": - { - "scale" : [0.25, 0.25], - "max_speed" : 5.0, - "direction": [0, 1], + "bike_enemy": { + "scale": [ + 0.25, + 0.25 + ], + "max_speed": 5.0, + "direction": [ + 0, + 1 + ], "sin_amplitude": 50, "sin_omega": 5 }, - - "game_state_assets": - { + "game_state_assets": { "kir_file": "assets/kir.png", "YF_file": "assets/YF.png", "cyklist_file": "assets/cyklist.png", @@ -79,30 +115,24 @@ "helper_file": "assets/ysex.png", "map_file": "assets/bakgrund.png" }, - "gameover_menu": - { + "gameover_menu": { "texture": "assets/game_over.png", - "texture_mouse" : "assets/muspekareRed.png", - "font" : "assets/fonts/Philosopher-Regular.ttf" + "texture_mouse": "assets/muspekareRed.png", + "font": "assets/fonts/Philosopher-Regular.ttf" }, - - "pause_menu": - { + "pause_menu": { "texture": "assets/Pause_bild.png", - "texture_mouse" : "assets/muspekareGul.png", + "texture_mouse": "assets/muspekareGul.png", "font": "assets/fonts/Philosopher-Regular.ttf" }, - "scoreboard_menu": - { + "scoreboard_menu": { "texture": "assets/scoreboard.png", - "texture_mouse" : "assets/muspekare2.png", + "texture_mouse": "assets/muspekare2.png", "font": "assets/fonts/Philosopher-Regular.ttf" }, - "start_menu": - { - "texture": "assets/meny_bild.png", - "texture_mouse" : "assets/muspekareGul.png", - "font": "assets/fonts/Philosopher-Regular.ttf" + "start_menu": { + "texture": "assets/meny_bild.png", + "texture_mouse": "assets/muspekareGul.png", + "font": "assets/fonts/Philosopher-Regular.ttf" } - } \ No newline at end of file diff --git a/assets/highscore.csv b/assets/highscore.csv index 4f8ab68..b569f55 100644 --- a/assets/highscore.csv +++ b/assets/highscore.csv @@ -1,3 +1,4 @@ Nisseboi, 5 Lukas is KUNG, 5 +NissePisse, 4 Nils, 1 diff --git a/src/Bike_enemy.cc b/src/Bike_enemy.cc index fcaed47..360866b 100644 --- a/src/Bike_enemy.cc +++ b/src/Bike_enemy.cc @@ -20,11 +20,12 @@ Bike_enemy::Bike_enemy(sf::Texture& txtr, json& params) : max_speed{params["max_ sprite.setPosition(position); } +void Bike_enemy::collision_player() +{} -void Bike_enemy::collision(Object &other) -{ +void Bike_enemy::collision_enemy() +{} -} void Bike_enemy::update(Context& context) { //Game_state* game = static_cast(context.current_state.get()); diff --git a/src/Bike_enemy.h b/src/Bike_enemy.h index 03b88d5..1254157 100644 --- a/src/Bike_enemy.h +++ b/src/Bike_enemy.h @@ -13,7 +13,8 @@ class Bike_enemy : public Moving_object public: Bike_enemy(sf::Texture& txtr, json& params); ~Bike_enemy() = default; - void collision(Object &other) override; + void collision_player(); + void collision_enemy(); void update(Context& context) override; void render(sf::RenderWindow &window) const override; diff --git a/src/Bottle.cc b/src/Bottle.cc index 2ead946..64b2d3c 100644 --- a/src/Bottle.cc +++ b/src/Bottle.cc @@ -3,26 +3,24 @@ #include "Bottle.h" #include "constants.h" #include "Context.h" +#include "States.h" Bottle::Bottle(sf::Texture& txtr, json& params) { texture = txtr; sprite.setTexture(texture); - position ={rand()%(S_WIDTH*5/7 - S_WIDTH/5 +1) + S_WIDTH/5, rand()%S_HEIGHT+1}; //x-pixel WIDTH/5 - WIDTH*5/7 + position ={rand()%static_cast(S_WIDTH*5/7.0f - S_WIDTH/5.0f +1) + S_WIDTH/5.0f, + static_cast(rand()%static_cast(S_HEIGHT+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 Bottle::collision(Object& other) -{ - -} void Bottle::update(Context& context) { - + static_cast(context.current_state.get()); } void Bottle::render(sf::RenderWindow& window) const diff --git a/src/Bottle.h b/src/Bottle.h index 15d6056..645dc7c 100644 --- a/src/Bottle.h +++ b/src/Bottle.h @@ -14,7 +14,6 @@ class Bottle : public Static_object public: Bottle(sf::Texture& txtr, json& params); - void collision(Object& other) override; void update(Context& context) override; void render(sf::RenderWindow& window) const override; void set_texture(sf::Texture& txtr); diff --git a/src/GameOver_menu.cc b/src/GameOver_menu.cc index b54de21..f298b6b 100644 --- a/src/GameOver_menu.cc +++ b/src/GameOver_menu.cc @@ -12,27 +12,35 @@ using json = nlohmann::json; -GameOver_menu::GameOver_menu(int const points, json& params) : +GameOver_menu::GameOver_menu(int const points, json& params) : + menu_index{1}, + points{points}, + + exit_game{false}, + menu{false}, + texture{}, texture2{}, + sprite{}, - rectangle{}, mouse_l{}, mouse_r{}, + + rectangle{}, + + playerInput{}, + + playerText{}, linetext{}, funfacttext{}, entertext{}, pointstext{}, - playerInput{}, - playerText{}, - data{}, savetext{}, quittext{}, + font{}, - exit_game{false}, - menu{false}, - menu_index{1}, - points{points} + + data{} { data = std::move(params); texture.loadFromFile(data["gameover_menu"]["texture"]); @@ -178,7 +186,7 @@ void GameOver_menu::handle_input(sf::Event& event) case 0: // A if(menu_index == 1) { - load_to_csv(playerText); + load_to_csv(); menu = true; menu_index = 1; } @@ -227,6 +235,9 @@ void GameOver_menu::handle_input(sf::Event& event) break; } break; + + default: + break; } break; case sf::Event::KeyPressed: @@ -236,7 +247,7 @@ void GameOver_menu::handle_input(sf::Event& event) if(menu_index == 1) { - load_to_csv(playerText); + load_to_csv(); menu = true; menu_index = 1; } @@ -299,22 +310,17 @@ void GameOver_menu::handle_input(sf::Event& event) } } - // if not backspace, print letters else if(playerInput.getSize() < 20) { playerInput += event.text.unicode; } - playerText.setString(playerInput); - } - - } //spara namn till fil -void GameOver_menu::load_to_csv(sf::Text playertext) +void GameOver_menu::load_to_csv() { std::string current_line{playerText.getString() + ", " + std::to_string(points)}; diff --git a/src/Helper.cc b/src/Helper.cc index 7bcd2a7..6516956 100644 --- a/src/Helper.cc +++ b/src/Helper.cc @@ -5,7 +5,11 @@ #include "constants.h" #include "Context.h" -Helper::Helper(sf::Texture& txtr, json& params) : stop_bot{params["stop_bot"]}, stop_top{params["stop_top"]}, max_speed{params["max_speed"]}, movement{} +Helper::Helper(sf::Texture& txtr, json& params) : + stop_bot{params["stop_bot"]}, + stop_top{params["stop_top"]}, + max_speed{params["max_speed"]}, + movement{} { texture = txtr; position = {params["start_pos"][0], params["start_pos"][1]}; @@ -23,18 +27,16 @@ Helper::Helper(sf::Texture& txtr, json& params) : stop_bot{params["stop_bot"]}, } -void Helper::collision(Object& other) +void Helper::collision_player() { move(false); } void Helper::update(Context& context) -{ +{ + static_cast(context.current_state.get()); move(); - - //Game_state* game = static_cast(context.current_state.get()); - } void Helper::move(bool forward) diff --git a/src/Helper.h b/src/Helper.h index 5db3cea..7b92ff7 100644 --- a/src/Helper.h +++ b/src/Helper.h @@ -15,7 +15,7 @@ public: Helper(sf::Texture& txtr, json& params); ~Helper() = default; - void collision(Object& other) override; + void collision_player(); void update(Context& context) override; void render(sf::RenderWindow& window) const override; @@ -23,9 +23,11 @@ protected: //void move(Time); private: void move(bool forward=true); + float stop_bot; float stop_top; float max_speed; + sf::Vector2f movement; }; diff --git a/src/Main_enemy.cc b/src/Main_enemy.cc index f9f9d9e..52dcc25 100644 --- a/src/Main_enemy.cc +++ b/src/Main_enemy.cc @@ -5,7 +5,13 @@ #include "Context.h" #include "constants.h" -Main_enemy::Main_enemy(sf::Texture& txtr, json& params): max_speed{params["max_speed"]}, moving_to_bottle{false}, tumbling{false}, tumble_degrees{0}, rotation{params["degrees_per_second"]} +Main_enemy::Main_enemy(sf::Texture& txtr, json& params): + max_speed{params["max_speed"]}, + tumble_degrees{0}, + rotation{params["degrees_per_second"]}, + + tumbling{false}, + moving_to_bottle{false} { //texture.loadFromFile("assets/6Ling_figur2.png"); texture = txtr; @@ -18,10 +24,22 @@ Main_enemy::Main_enemy(sf::Texture& txtr, json& params): max_speed{params["max_s } -void Main_enemy::collision(Object& other) +void Main_enemy::collision_yf() +{ + move(false); +} + +void Main_enemy::collision_bike() +{ + move(false); +} + +void Main_enemy::collision_player() { move(false); + tumbling = true; } + void Main_enemy::update(Context& context) { if(tumbling) @@ -57,12 +75,6 @@ void Main_enemy::render(sf::RenderWindow& window) const window.draw(sprite); } -void Main_enemy::collision(Player& player) -{ - move(false); - tumbling = true; -} - void Main_enemy::move(bool forward) { if (moving_to_bottle) diff --git a/src/Main_enemy.h b/src/Main_enemy.h index dad5e8a..087fa2f 100644 --- a/src/Main_enemy.h +++ b/src/Main_enemy.h @@ -18,22 +18,25 @@ class Main_enemy : public Moving_object public: Main_enemy(sf::Texture& txtr, json& params); ~Main_enemy() = default; - void collision(Object& other) override; - void collision(Player& player); + void collision_yf(); + void collision_bike(); + void collision_player(); void update(Context& context) override; void render(sf::RenderWindow& window) const override; protected: private: + float bottle_dist(std::unique_ptr const& bottle); + sf::Vector2f find_closest_bottle(std::vector> & bottles); + void move(bool forward = true); + float max_speed; float tumble_degrees; - int rotation; + int rotation; + bool tumbling; bool moving_to_bottle; - void move(bool forward = true); - float bottle_dist(std::unique_ptr const& bottle); - sf::Vector2f find_closest_bottle(std::vector> & bottles); }; #endif \ No newline at end of file diff --git a/src/Map.cc b/src/Map.cc index 4be2c42..9b6b80d 100644 --- a/src/Map.cc +++ b/src/Map.cc @@ -3,7 +3,18 @@ #include -Map::Map(sf::Texture& txtr, sf::Texture& bottle_txtr, json& params) : texture{txtr}, sprite{}, point_text{}, collected_text{}, time_text{}, bottle_texture{bottle_txtr}, bottle_sprite{}, font{} +Map::Map(sf::Texture& txtr, sf::Texture& bottle_txtr, json& params) : + texture{txtr}, + bottle_texture{bottle_txtr}, + + sprite{}, + bottle_sprite{}, + + point_text{}, + collected_text{}, + time_text{}, + + font{} { sprite.setTexture(texture); @@ -61,6 +72,9 @@ void Map::update(int const collected, int const points, int const time_left) time_text.setString("Time left: " + std::to_string(time_left) + " s"); } +void Map::collision_player() +{} + void Map::render(sf::RenderWindow& window) const { window.draw(sprite); diff --git a/src/Map.h b/src/Map.h index a5e2643..58c7569 100644 --- a/src/Map.h +++ b/src/Map.h @@ -13,19 +13,22 @@ class Map public: Map(sf::Texture& txtr, sf::Texture& bottle_txtr, json& params); ~Map() = default; - + void collision_player(); void update(int const collected, int const points, int const time_left); void render(sf::RenderWindow& window) const; bool collides(Object& other); protected: private: sf::Texture texture; - sf::Sprite sprite; sf::Texture bottle_texture; + + sf::Sprite sprite; sf::Sprite bottle_sprite; + sf::Text point_text; sf::Text collected_text; sf::Text time_text; + sf::Font font; }; diff --git a/src/Object.h b/src/Object.h index 2b7e660..20c564a 100644 --- a/src/Object.h +++ b/src/Object.h @@ -15,7 +15,6 @@ public: { return get_hitbox().intersects(other.get_hitbox()); } - virtual void collision(Object& other) = 0; virtual void update(Context& context) = 0; virtual void render(sf::RenderWindow& window) const = 0; diff --git a/src/Pause_menu.cc b/src/Pause_menu.cc index cacd995..8f51a29 100644 --- a/src/Pause_menu.cc +++ b/src/Pause_menu.cc @@ -8,8 +8,27 @@ using json = nlohmann::json; -Pause_menu::Pause_menu(json& params) : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, resumetext{}, startmenutext{}, - quittext{}, font{}, resume_game{false}, exit_game{false}, menu{false}, menu_index{1}, data{} +Pause_menu::Pause_menu(json& params) : + menu_index{1}, + + resume_game{false}, + exit_game{false}, + menu{false}, + + texture{}, + texture2{}, + + sprite{}, + mouse_l{}, + mouse_r{}, + + resumetext{}, + startmenutext{}, + quittext{}, + + font{}, + + data{} { data = params; texture.loadFromFile(data["pause_menu"]["texture"]); @@ -173,6 +192,8 @@ void Pause_menu::handle_input(sf::Event& event) break; } break; + default: + break; } break; case sf::Event::KeyPressed: diff --git a/src/Player.cc b/src/Player.cc index a4fdef0..cec7c49 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -4,8 +4,19 @@ #include "Context.h" #include "constants.h" -Player::Player(sf::Texture& player_txtr, sf::Texture& sack_txtr, json& params) : sack_texture{sack_txtr}, collected{0}, max_speed{params["max_speed"]}, tumbling{false}, tumble_degrees{0}, rotation{params["degrees_per_second"]} +Player::Player(sf::Texture& player_txtr, sf::Texture& sack_txtr, json& params) : + collected{0}, + rotation{params["degrees_per_second"]}, + tumble_degrees{0}, + max_speed{params["max_speed"]}, + + tumbling{false}, + + sack{}, + sack_texture{sack_txtr}, + sack_offset{params["sack"]["offset"][0], params["sack"]["offset"][1]} { + texture = player_txtr; position = {params["start_pos"][0], params["start_pos"][1]}; @@ -22,19 +33,17 @@ Player::Player(sf::Texture& player_txtr, sf::Texture& sack_txtr, json& params) : sf::FloatRect gb_s {sack.getLocalBounds()}; sack.setOrigin(gb_s.width / 3, gb_s.height / 5); - sack_offset = {params["sack"]["offset"][0], params["sack"]["offset"][1]}; sack.rotate(params["sack"]["angle"]); sack.setPosition(position + sack_offset); } -void Player::collision(Object& other) +void Player::collision_helper() { move(false); - } -void Player::collision(Map& map) +void Player::collision_map() { if (tumbling) { @@ -46,24 +55,25 @@ void Player::collision(Map& map) } } -void Player::collision(YF& yf) +void Player::collision_yf() { move(false); tumbling = true; } -void Player::collision(Bike_enemy& bike) + +void Player::collision_bike() { move(false); tumbling = true; } +void Player::collision_enemy() +{} + void Player::update(Context& context) { - - // Get game_state from context, static_cast since we know that game is currently running - // game_state is still managed by context - //Game_state* game = static_cast(context.current_state.get()); - + static_cast(context.current_state.get()); + if(tumbling) { sprite.rotate(-1*std::copysign(1, direction.x)*rotation/FPS); @@ -95,7 +105,12 @@ void Player::render(sf::RenderWindow& window) const } void Player::handle_input(sf::Event& event) -{ +{ + switch(event.type) + { + default: + break; + } if (!tumbling) { if (!sf::Joystick::isConnected(0)) @@ -133,7 +148,7 @@ void Player::handle_input(sf::Event& event) sf::Joystick::getAxisPosition(0, sf::Joystick::Y) / 100 }; - float len{pow(direction.x, 2) + pow(direction.y, 2)}; + double len{pow(direction.x, 2) + pow(direction.y, 2)}; if (len > 1) { direction.x /= sqrt(len); diff --git a/src/Player.h b/src/Player.h index 6bfef29..76b362d 100644 --- a/src/Player.h +++ b/src/Player.h @@ -20,10 +20,11 @@ public: Player(sf::Texture& player_txtr, sf::Texture& sack_txtr, json& params); ~Player() = default; - void collision(Object& other) override; - void collision(Map& map); - void collision(YF& yf); - void collision(Bike_enemy& bike); + void collision_yf(); + void collision_helper(); + void collision_enemy(); + void collision_bike(); + void collision_map(); void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event); @@ -34,11 +35,14 @@ public: protected: private: void move(bool forward=true); + int collected; - float tumble_degrees; int rotation; + float tumble_degrees; float max_speed; + bool tumbling; + sf::Sprite sack; sf::Texture sack_texture; sf::Vector2f sack_offset; diff --git a/src/Scoreboard_menu.cc b/src/Scoreboard_menu.cc index daa70dd..33fc74b 100644 --- a/src/Scoreboard_menu.cc +++ b/src/Scoreboard_menu.cc @@ -9,9 +9,27 @@ using json = nlohmann::json; -Scoreboard_menu::Scoreboard_menu(json& params) : texture{}, texture2{}, sprite{}, menu{false}, exit_game{false}, - startmenutext{}, quittext{}, mouse_r{}, mouse_l{}, font{}, menu_index{1}, data{} - +Scoreboard_menu::Scoreboard_menu(json& params) : + menu_index{1}, + + exit_game{false}, + menu{false}, + + texture{}, + texture2{}, + + sprite{}, + mouse_l{}, + mouse_r{}, + + scoreboardtext{}, + firstplacetext{}, + startmenutext{}, + quittext{}, + + font{}, + + data{} { data = std::move(params); texture.loadFromFile(data["scoreboard_menu"]["texture"]); @@ -180,6 +198,8 @@ void Scoreboard_menu::handle_input(sf::Event& event) break; } break; + default: + break; } break; case sf::Event::KeyPressed: diff --git a/src/Start_menu.cc b/src/Start_menu.cc index e5c4b85..1531c51 100644 --- a/src/Start_menu.cc +++ b/src/Start_menu.cc @@ -8,8 +8,27 @@ using json = nlohmann::json; -Start_menu::Start_menu(json& params) : texture{}, texture2{}, sprite{}, mouse_l{}, mouse_r{}, starttext{}, scoreboardtext{}, - quittext{}, font{}, start_game{false}, exit_game{false}, scoreboard{false}, menu_index{1}, data{} +Start_menu::Start_menu(json& params) : + start_game{false}, + exit_game{false}, + scoreboard{false}, + + menu_index{1}, + + texture{}, + texture2{}, + + sprite{}, + mouse_l{}, + mouse_r{}, + + starttext{}, + scoreboardtext{}, + quittext{}, + + font{}, + + data{} { data = std::move(params); @@ -176,6 +195,8 @@ void Start_menu::handle_input(sf::Event& event) break; } break; + default: + break; } break; case sf::Event::KeyPressed: diff --git a/src/States.cc b/src/States.cc index 6063c65..65b46eb 100644 --- a/src/States.cc +++ b/src/States.cc @@ -9,24 +9,29 @@ using json = nlohmann::json; -Game_state::Game_state(json& params) : - game_map{}, - pause_game{false}, - player{}, +Game_state::Game_state(json& params) : bottles{}, - helper{}, - bike{}, + time_since_last_bottle{0.0f}, game_time{0}, time_since_last_yf{0.0f}, time_since_last_bike{0.0f}, + points{0}, + + pause_game{false}, + + game_map{}, + player{}, + helper{}, + enemy{}, + yf{}, + bike{}, + bottle_texture {}, YF_texture {}, bike_texture{}, - yf{}, - data{}, - enemy{}, - points{0} + + data{} { data = std::move(params); sf::Texture main_enemy_texture; @@ -73,59 +78,79 @@ void Game_state::update(Context &context) return; } + // Update player and helper - for (unsigned int i {0}; i < bottles.size(); ++i) - { - if (enemy->collides(*(bottles[i]))) - { - - bottles.erase(bottles.begin() + i); - - } - else if (player->collides(*(bottles[i]))) - { - bottles.erase(bottles.begin() + i); - player->add_collected(); - } - } - player->update(context); helper->update(context); + // Handle collision + if (player->collides(*helper)) { points += player->get_collected() + floor(player->get_collected() / static_cast(data["game_constants"]["point_scale_div"])); player->zero_collected(); - player->collision(*helper); - helper->collision(*player); + player->collision_helper(); + helper->collision_player(); } if (player->collides(*enemy)) { - enemy->collision(*player); + enemy->collision_player(); + player->collision_enemy(); } + if(yf != nullptr) { if (player->collides(*yf)) { player->zero_collected(); - player->collision(*yf); + + player->collision_yf(); + yf->collision_player(); } if(enemy->collides(*yf)) { - enemy->collision(*yf); + enemy->collision_yf(); + yf->collision_enemy(); } } + if(bike != nullptr) { if (player->collides(*bike)) { player->zero_collected(); - player->collision(*bike); + + player->collision_bike(); + bike->collision_player(); } if(enemy->collides(*bike)) { - enemy->collision(*bike); + enemy->collision_bike(); + bike->collision_enemy(); + } + } + + if(game_map->collides(*player)) + { + player->collision_map(); + game_map->collision_player(); + } + + // Collect bottles + + for (unsigned int i {0}; i < bottles.size(); ++i) + { + if (enemy->collides(*(bottles[i]))) + { + + bottles.erase(bottles.begin() + i); + + } + else if (player->collides(*(bottles[i]))) + { + bottles.erase(bottles.begin() + i); + player->add_collected(); } } @@ -159,10 +184,6 @@ void Game_state::update(Context &context) { bike->update(context); } - if(game_map->collides(*player)) - { - player->collision(*game_map); - } } void Game_state::render(sf::RenderWindow &window) const diff --git a/src/States.h b/src/States.h index 8e97c34..77f8bd3 100644 --- a/src/States.h +++ b/src/States.h @@ -43,6 +43,9 @@ private: float game_time; float time_since_last_yf; float time_since_last_bike; + int points; + + bool pause_game; std::unique_ptr game_map; std::unique_ptr player; @@ -51,12 +54,11 @@ private: std::unique_ptr yf; std::unique_ptr bike; - bool pause_game; sf::Texture bottle_texture; sf::Texture YF_texture; sf::Texture bike_texture; + json data; - int points; }; class Start_menu : public State @@ -69,22 +71,26 @@ public: void handle_input(sf::Event& event) override; private: + bool start_game; + bool exit_game; + bool scoreboard; + + int menu_index; + sf::Texture texture; sf::Texture texture2; + sf::Sprite sprite; sf::Sprite mouse_l; sf::Sprite mouse_r; + sf::Text starttext; sf::Text scoreboardtext; sf::Text quittext; + sf::Font font; - bool start_game; - bool exit_game; - bool scoreboard; json data; - - int menu_index; }; class Pause_menu : public State @@ -96,22 +102,26 @@ public: void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; private: + int menu_index; + + bool resume_game; + bool exit_game; + bool menu; + sf::Texture texture; sf::Texture texture2; + sf::Sprite sprite; sf::Sprite mouse_l; sf::Sprite mouse_r; + sf::Text resumetext; sf::Text startmenutext; sf::Text quittext; + sf::Font font; - bool resume_game; - bool exit_game; - bool menu; json data; - - int menu_index; }; @@ -123,22 +133,27 @@ public: void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; -private: +private: + int menu_index; + + bool exit_game; + bool menu; + sf::Texture texture; sf::Texture texture2; + sf::Sprite sprite; sf::Sprite mouse_l; sf::Sprite mouse_r; + sf::Text scoreboardtext; sf::Text firstplacetext; sf::Text startmenutext; sf::Text quittext; + sf::Font font; - bool exit_game; - bool menu; json data; - int menu_index; }; @@ -150,16 +165,26 @@ public: void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; - void load_to_csv(sf::Text playerText); + void load_to_csv(); private: + int menu_index; + int points; + + bool exit_game; + bool menu; + sf::Texture texture; sf::Texture texture2; + sf::Sprite sprite; - sf::CircleShape rectangle; sf::Sprite mouse_l; sf::Sprite mouse_r; + + sf::CircleShape rectangle; + sf::String playerInput; + sf::Text playerText; sf::Text linetext; sf::Text funfacttext; @@ -167,13 +192,10 @@ private: sf::Text pointstext; sf::Text savetext; sf::Text quittext; + sf::Font font; - bool exit_game; - bool menu; json data; - int menu_index; - int points; }; diff --git a/src/YF.cc b/src/YF.cc index 803ed25..831812b 100644 --- a/src/YF.cc +++ b/src/YF.cc @@ -3,13 +3,14 @@ #include "YF.h" #include "constants.h" #include "Context.h" +#include "States.h" 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 + position ={rand()%static_cast(S_WIDTH*5/7.0f - S_WIDTH/5.0f +1) + S_WIDTH/5.0f, S_HEIGHT}; //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()}; @@ -19,14 +20,16 @@ YF::YF(sf::Texture& txtr, json& params) : max_speed{params["max_speed"]} } +void YF::collision_player() +{} -void YF::collision(Object &other) -{ +void YF::collision_enemy() +{} -} void YF::update(Context& context) { - //Game_state* game = static_cast(context.current_state.get()); + static_cast(context.current_state.get()); + position += direction*max_speed; sprite.setPosition(position); } diff --git a/src/YF.h b/src/YF.h index 97f4e71..340e435 100644 --- a/src/YF.h +++ b/src/YF.h @@ -14,7 +14,8 @@ class YF : public Moving_object public: YF(sf::Texture& txtr, json& params); ~YF() = default; - void collision(Object &other) override; + void collision_enemy(); + void collision_player(); void update(Context& context) override; void render(sf::RenderWindow &window) const override; -- 2.30.2