From 9b0be70fecaba5e18dcc05becbda5466377290f7 Mon Sep 17 00:00:00 2001 From: NilsForssen Date: Fri, 8 Dec 2023 12:04:56 +0100 Subject: [PATCH] Revert "Removed warnings" This reverts commit 48b24a03cee821f0dce08d3181cc529d9863aeb0. --- 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, 207 insertions(+), 406 deletions(-) diff --git a/assets/data.json b/assets/data.json index a235262..df0fe24 100644 --- a/assets/data.json +++ b/assets/data.json @@ -1,111 +1,75 @@ { - "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": 30, + "game_constants": + { + "scoreboard" : 5, + "bottles_per_second" : 1.5, + "game_time" : 10, "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", @@ -115,24 +79,30 @@ "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 b569f55..4f8ab68 100644 --- a/assets/highscore.csv +++ b/assets/highscore.csv @@ -1,4 +1,3 @@ Nisseboi, 5 Lukas is KUNG, 5 -NissePisse, 4 Nils, 1 diff --git a/src/Bike_enemy.cc b/src/Bike_enemy.cc index 360866b..fcaed47 100644 --- a/src/Bike_enemy.cc +++ b/src/Bike_enemy.cc @@ -20,12 +20,11 @@ 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_enemy() -{} +void Bike_enemy::collision(Object &other) +{ +} 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 1254157..03b88d5 100644 --- a/src/Bike_enemy.h +++ b/src/Bike_enemy.h @@ -13,8 +13,7 @@ class Bike_enemy : public Moving_object public: Bike_enemy(sf::Texture& txtr, json& params); ~Bike_enemy() = default; - void collision_player(); - void collision_enemy(); + void collision(Object &other) override; void update(Context& context) override; void render(sf::RenderWindow &window) const override; diff --git a/src/Bottle.cc b/src/Bottle.cc index 64b2d3c..2ead946 100644 --- a/src/Bottle.cc +++ b/src/Bottle.cc @@ -3,24 +3,26 @@ #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()%static_cast(S_WIDTH*5/7.0f - S_WIDTH/5.0f +1) + S_WIDTH/5.0f, - static_cast(rand()%static_cast(S_HEIGHT+1))}; + position ={rand()%(S_WIDTH*5/7 - S_WIDTH/5 +1) + S_WIDTH/5, rand()%S_HEIGHT+1}; //x-pixel WIDTH/5 - WIDTH*5/7 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 645dc7c..15d6056 100644 --- a/src/Bottle.h +++ b/src/Bottle.h @@ -14,6 +14,7 @@ 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 f298b6b..b54de21 100644 --- a/src/GameOver_menu.cc +++ b/src/GameOver_menu.cc @@ -12,35 +12,27 @@ using json = nlohmann::json; -GameOver_menu::GameOver_menu(int const points, json& params) : - menu_index{1}, - points{points}, - - exit_game{false}, - menu{false}, - +GameOver_menu::GameOver_menu(int const points, json& params) : texture{}, texture2{}, - sprite{}, + rectangle{}, mouse_l{}, mouse_r{}, - - rectangle{}, - - playerInput{}, - - playerText{}, linetext{}, funfacttext{}, entertext{}, pointstext{}, + playerInput{}, + playerText{}, + data{}, savetext{}, quittext{}, - font{}, - - data{} + exit_game{false}, + menu{false}, + menu_index{1}, + points{points} { data = std::move(params); texture.loadFromFile(data["gameover_menu"]["texture"]); @@ -186,7 +178,7 @@ void GameOver_menu::handle_input(sf::Event& event) case 0: // A if(menu_index == 1) { - load_to_csv(); + load_to_csv(playerText); menu = true; menu_index = 1; } @@ -235,9 +227,6 @@ void GameOver_menu::handle_input(sf::Event& event) break; } break; - - default: - break; } break; case sf::Event::KeyPressed: @@ -247,7 +236,7 @@ void GameOver_menu::handle_input(sf::Event& event) if(menu_index == 1) { - load_to_csv(); + load_to_csv(playerText); menu = true; menu_index = 1; } @@ -310,17 +299,22 @@ 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() +void GameOver_menu::load_to_csv(sf::Text playertext) { std::string current_line{playerText.getString() + ", " + std::to_string(points)}; diff --git a/src/Helper.cc b/src/Helper.cc index 6516956..7bcd2a7 100644 --- a/src/Helper.cc +++ b/src/Helper.cc @@ -5,11 +5,7 @@ #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]}; @@ -27,16 +23,18 @@ Helper::Helper(sf::Texture& txtr, json& params) : } -void Helper::collision_player() +void Helper::collision(Object& other) { 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 7b92ff7..5db3cea 100644 --- a/src/Helper.h +++ b/src/Helper.h @@ -15,7 +15,7 @@ public: Helper(sf::Texture& txtr, json& params); ~Helper() = default; - void collision_player(); + void collision(Object& other) override; void update(Context& context) override; void render(sf::RenderWindow& window) const override; @@ -23,11 +23,9 @@ 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 52dcc25..f9f9d9e 100644 --- a/src/Main_enemy.cc +++ b/src/Main_enemy.cc @@ -5,13 +5,7 @@ #include "Context.h" #include "constants.h" -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} +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"]} { //texture.loadFromFile("assets/6Ling_figur2.png"); texture = txtr; @@ -24,22 +18,10 @@ Main_enemy::Main_enemy(sf::Texture& txtr, json& params): } -void Main_enemy::collision_yf() -{ - move(false); -} - -void Main_enemy::collision_bike() -{ - move(false); -} - -void Main_enemy::collision_player() +void Main_enemy::collision(Object& other) { move(false); - tumbling = true; } - void Main_enemy::update(Context& context) { if(tumbling) @@ -75,6 +57,12 @@ 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 087fa2f..dad5e8a 100644 --- a/src/Main_enemy.h +++ b/src/Main_enemy.h @@ -18,25 +18,22 @@ class Main_enemy : public Moving_object public: Main_enemy(sf::Texture& txtr, json& params); ~Main_enemy() = default; - void collision_yf(); - void collision_bike(); - void collision_player(); + void collision(Object& other) override; + void collision(Player& 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 9b6b80d..4be2c42 100644 --- a/src/Map.cc +++ b/src/Map.cc @@ -3,18 +3,7 @@ #include -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{} +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{} { sprite.setTexture(texture); @@ -72,9 +61,6 @@ 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 58c7569..a5e2643 100644 --- a/src/Map.h +++ b/src/Map.h @@ -13,22 +13,19 @@ 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::Texture bottle_texture; - sf::Sprite sprite; + sf::Texture bottle_texture; 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 20c564a..2b7e660 100644 --- a/src/Object.h +++ b/src/Object.h @@ -15,6 +15,7 @@ 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 8f51a29..cacd995 100644 --- a/src/Pause_menu.cc +++ b/src/Pause_menu.cc @@ -8,27 +8,8 @@ using json = nlohmann::json; -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{} +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{} { data = params; texture.loadFromFile(data["pause_menu"]["texture"]); @@ -192,8 +173,6 @@ 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 cec7c49..a4fdef0 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -4,19 +4,8 @@ #include "Context.h" #include "constants.h" -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]} +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"]} { - texture = player_txtr; position = {params["start_pos"][0], params["start_pos"][1]}; @@ -33,17 +22,19 @@ 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_helper() +void Player::collision(Object& other) { move(false); + } -void Player::collision_map() +void Player::collision(Map& map) { if (tumbling) { @@ -55,25 +46,24 @@ void Player::collision_map() } } -void Player::collision_yf() +void Player::collision(YF& yf) { move(false); tumbling = true; } - -void Player::collision_bike() +void Player::collision(Bike_enemy& bike) { move(false); tumbling = true; } -void Player::collision_enemy() -{} - void Player::update(Context& context) { - static_cast(context.current_state.get()); - + + // 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()); + if(tumbling) { sprite.rotate(-1*std::copysign(1, direction.x)*rotation/FPS); @@ -105,12 +95,7 @@ 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)) @@ -148,7 +133,7 @@ void Player::handle_input(sf::Event& event) sf::Joystick::getAxisPosition(0, sf::Joystick::Y) / 100 }; - double len{pow(direction.x, 2) + pow(direction.y, 2)}; + float 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 76b362d..6bfef29 100644 --- a/src/Player.h +++ b/src/Player.h @@ -20,11 +20,10 @@ public: Player(sf::Texture& player_txtr, sf::Texture& sack_txtr, json& params); ~Player() = default; - void collision_yf(); - void collision_helper(); - void collision_enemy(); - void collision_bike(); - void collision_map(); + void collision(Object& other) override; + void collision(Map& map); + void collision(YF& yf); + void collision(Bike_enemy& bike); void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event); @@ -35,14 +34,11 @@ public: protected: private: void move(bool forward=true); - int collected; - int rotation; float tumble_degrees; + int rotation; 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 33fc74b..daa70dd 100644 --- a/src/Scoreboard_menu.cc +++ b/src/Scoreboard_menu.cc @@ -9,27 +9,9 @@ using json = nlohmann::json; -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{} +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{} + { data = std::move(params); texture.loadFromFile(data["scoreboard_menu"]["texture"]); @@ -198,8 +180,6 @@ 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 1531c51..e5c4b85 100644 --- a/src/Start_menu.cc +++ b/src/Start_menu.cc @@ -8,27 +8,8 @@ using json = nlohmann::json; -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{} +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{} { data = std::move(params); @@ -195,8 +176,6 @@ 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 65b46eb..6063c65 100644 --- a/src/States.cc +++ b/src/States.cc @@ -9,29 +9,24 @@ using json = nlohmann::json; -Game_state::Game_state(json& params) : +Game_state::Game_state(json& params) : + game_map{}, + pause_game{false}, + player{}, 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{}, - - data{} + yf{}, + data{}, + enemy{}, + points{0} { data = std::move(params); sf::Texture main_enemy_texture; @@ -78,79 +73,59 @@ 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(); - player->collision_enemy(); + enemy->collision(*player); } - if(yf != nullptr) { if (player->collides(*yf)) { player->zero_collected(); - - player->collision_yf(); - yf->collision_player(); + player->collision(*yf); } if(enemy->collides(*yf)) { - enemy->collision_yf(); - yf->collision_enemy(); + enemy->collision(*yf); } } - if(bike != nullptr) { if (player->collides(*bike)) { player->zero_collected(); - - player->collision_bike(); - bike->collision_player(); + player->collision(*bike); } if(enemy->collides(*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(); + enemy->collision(*bike); } } @@ -184,6 +159,10 @@ 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 77f8bd3..8e97c34 100644 --- a/src/States.h +++ b/src/States.h @@ -43,9 +43,6 @@ 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; @@ -54,11 +51,12 @@ 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 @@ -71,26 +69,22 @@ 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 @@ -102,26 +96,22 @@ 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; }; @@ -133,27 +123,22 @@ public: void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; -private: - int menu_index; - - bool exit_game; - bool menu; - +private: 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; }; @@ -165,26 +150,16 @@ public: void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event) override; - void load_to_csv(); + void load_to_csv(sf::Text playerText); 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; @@ -192,10 +167,13 @@ 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 831812b..803ed25 100644 --- a/src/YF.cc +++ b/src/YF.cc @@ -3,14 +3,13 @@ #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()%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 + 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(params["scale"][0],params["scale"][1]); sf::FloatRect gb {sprite.getGlobalBounds()}; @@ -20,16 +19,14 @@ YF::YF(sf::Texture& txtr, json& params) : max_speed{params["max_speed"]} } -void YF::collision_player() -{} -void YF::collision_enemy() -{} +void YF::collision(Object &other) +{ +} void YF::update(Context& context) { - static_cast(context.current_state.get()); - + //Game_state* game = static_cast(context.current_state.get()); position += direction*max_speed; sprite.setPosition(position); } diff --git a/src/YF.h b/src/YF.h index 340e435..97f4e71 100644 --- a/src/YF.h +++ b/src/YF.h @@ -14,8 +14,7 @@ class YF : public Moving_object public: YF(sf::Texture& txtr, json& params); ~YF() = default; - void collision_enemy(); - void collision_player(); + void collision(Object &other) override; void update(Context& context) override; void render(sf::RenderWindow &window) const override; -- 2.30.2