From 058181259463ec0cbcde86392fb818783dabe9cb Mon Sep 17 00:00:00 2001 From: lukel495 Date: Mon, 20 Nov 2023 10:06:38 +0100 Subject: [PATCH] collision between helper and player + added points to map Lukas and Nils --- assets/data.json | 7 ++++++- src/Helper.cc | 25 ++++++++++++++----------- src/Helper.h | 1 + src/Map.cc | 20 +++++++++++++++++--- src/Map.h | 9 ++++++--- src/Player.cc | 24 ++++++++++++++++++------ src/Player.h | 5 ++++- src/States.cc | 22 ++++++++++++---------- src/States.h | 3 ++- 9 files changed, 80 insertions(+), 36 deletions(-) diff --git a/assets/data.json b/assets/data.json index f43f99e..f123922 100644 --- a/assets/data.json +++ b/assets/data.json @@ -8,7 +8,7 @@ "helper": { "start_pos": [1000, 50], "scale": [0.3, 0.3], - "max_speed": 5, + "max_speed": 2, "direction": [0.35, 1], "stop_top": 50, @@ -21,5 +21,10 @@ "game_constants": { "bottles_per_second" : 2 + }, + "map": + { + "points_pos": [100, 100], + "font_size": 24 } } \ No newline at end of file diff --git a/src/Helper.cc b/src/Helper.cc index 56671c6..0ec4073 100644 --- a/src/Helper.cc +++ b/src/Helper.cc @@ -2,6 +2,7 @@ #include "constants.h" #include "States.h" #include "Context.h" +#include #include @@ -25,23 +26,25 @@ Helper::Helper(json& params) : stop_bot{params["stop_bot"]}, stop_top{params["st void Helper::collision(Object& other) { - - + move(false); } void Helper::update(Context& context) -{ - position += direction * max_speed; - - if (position.y >= stop_bot || position.y <= stop_top ) - { - direction *= -1.0f; - } - - +{ + move(); //Game_state* game = static_cast(context.current_state.get()); + +} + +void Helper::move(bool forward) +{ + position += static_cast(pow(-1, forward - 1)) * direction * max_speed; + if (position.y >= stop_bot || position.y <= stop_top ) + { + direction *= -1.0f; + } sprite.setPosition(position); } diff --git a/src/Helper.h b/src/Helper.h index 84beb13..0920156 100644 --- a/src/Helper.h +++ b/src/Helper.h @@ -21,6 +21,7 @@ public: protected: //void move(Time); private: + void move(bool forward=true); float stop_bot; float stop_top; float max_speed; diff --git a/src/Map.cc b/src/Map.cc index 10e3a84..36728ab 100644 --- a/src/Map.cc +++ b/src/Map.cc @@ -1,19 +1,33 @@ #include "Map.h" #include "constants.h" +#include -Map::Map() : texture{}, sprite{} +Map::Map(json& params) : texture{}, sprite{}, point_text{}, font{} { texture.loadFromFile("assets/bakgrund.png"); sprite.setTexture(texture); sprite.setScale(S_SCALE_KOEFF, S_SCALE_KOEFF); sprite.setPosition(0,0); + + if ( !font.loadFromFile ("assets/fonts/Philosopher-Regular.ttf") ) + throw std::invalid_argument ("Unable to load font"); + + point_text = sf::Text{"Points: 0", font, params["font_size"] }; + sf::FloatRect gbts {point_text.getGlobalBounds()}; + point_text.setOrigin(gbts.width / 2, gbts.height / 2); + + point_text.setPosition (params["points_pos"][0], params["points_pos"][1]); + point_text.setFillColor(sf::Color::Red); } -void Map::update(Context& context) -{} +void Map::update(int points) +{ + point_text.setString("Points: " + std::to_string(points)); +} void Map::render(sf::RenderWindow& window) const { window.draw(sprite); + window.draw(point_text); } \ No newline at end of file diff --git a/src/Map.h b/src/Map.h index afa8c69..4891901 100644 --- a/src/Map.h +++ b/src/Map.h @@ -2,21 +2,24 @@ #define MAP_H #include +#include "json.hpp" -struct Context; +using json = nlohmann::json; class Map { public: - Map(); + Map(json& params); ~Map() = default; - void update(Context& context); + void update(int points); void render(sf::RenderWindow& window) const; protected: private: sf::Texture texture; sf::Sprite sprite; + sf::Text point_text; + sf::Font font; }; #endif \ No newline at end of file diff --git a/src/Player.cc b/src/Player.cc index 13b6fdb..5748cd7 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -32,20 +32,24 @@ Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]} void Player::collision(Object& other) { - return; + move(false); } 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()); + //Game_state* game = static_cast(context.current_state.get()); + move(); - position += max_speed * direction; - + return; +} + +void Player::move(bool forward) +{ + position += static_cast(pow(-1, forward - 1)) * max_speed * direction; sprite.setPosition(position); sack.setPosition(position + sack_offset); - return; } void Player::render(sf::RenderWindow& window) const @@ -83,8 +87,16 @@ void Player::handle_input(sf::Event& event) } } -int Player::add_collected() +void Player::add_collected() { collected += 1; +} +int Player::get_collected() +{ return collected; +} + +void Player::zero_collected() +{ + collected = 0; } \ No newline at end of file diff --git a/src/Player.h b/src/Player.h index 0c3f0ee..ad8f93a 100644 --- a/src/Player.h +++ b/src/Player.h @@ -20,9 +20,12 @@ public: void update(Context& context) override; void render(sf::RenderWindow& window) const override; void handle_input(sf::Event& event); - int add_collected(); + void add_collected(); + int get_collected(); + void zero_collected(); protected: private: + void move(bool forward=true); int collected; float max_speed; sf::Sprite sack; diff --git a/src/States.cc b/src/States.cc index 0845900..6c20417 100644 --- a/src/States.cc +++ b/src/States.cc @@ -19,7 +19,8 @@ Game_state::Game_state() : time_since_last_bottle{0.0f}, bottle_texture {}, data{}, - enemy{} + enemy{}, + points{0} { std::ifstream f("assets/data.json"); data = json::parse(f); @@ -27,6 +28,7 @@ Game_state::Game_state() : player = std::make_unique(data["player"]); helper = std::make_unique(data["helper"]); + game_map = std::make_unique(data["map"]); bottle_texture.loadFromFile("assets/kir.png"); } @@ -70,12 +72,17 @@ void Game_state::update(Context &context) } } + helper->update(context); + player->update(context); if (player->collides(*helper)) { - //reset add collected - // reset hitbox of sack + points += player->get_collected(); + player->zero_collected(); + player->collision(*helper); + helper->collision(*player); } + time_since_last_bottle += context.time.asSeconds(); //std::cout << std::fixed << std::setprecision(3) << time_since_last_bottle << std::endl; @@ -85,18 +92,13 @@ void Game_state::update(Context &context) std::cout << "placed bottle"<< std::endl; time_since_last_bottle = 0; } - game_map.update(context); - if (!(player->collides(*helper))) - { - helper->update(context); - } - player->update(context); + game_map->update(points); enemy.update(context); } void Game_state::render(sf::RenderWindow &window) const { - game_map.render(window); + game_map->render(window); player->render(window); enemy.render(window); helper->render(window); diff --git a/src/States.h b/src/States.h index 52d0090..379c648 100644 --- a/src/States.h +++ b/src/States.h @@ -42,13 +42,14 @@ protected: private: float time_since_last_bottle; - Map game_map; + std::unique_ptr game_map; std::unique_ptr player; std::unique_ptr helper; Main_enemy enemy; bool pause_game; sf::Texture bottle_texture; json data; + int points; }; -- 2.30.2