collision between helper and player + added points to map Lukas and Nils
authorlukel495 <lukel495@su15-109.ad.liu.se>
Mon, 20 Nov 2023 09:06:38 +0000 (10:06 +0100)
committerlukel495 <lukel495@su15-109.ad.liu.se>
Mon, 20 Nov 2023 09:06:38 +0000 (10:06 +0100)
assets/data.json
src/Helper.cc
src/Helper.h
src/Map.cc
src/Map.h
src/Player.cc
src/Player.h
src/States.cc
src/States.h

index f43f99e97a4d5912c8bde96ccf569422856e4108..f123922bba96e3d0352cf5391c2bb0c95b0c6d58 100644 (file)
@@ -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,
     "game_constants":
     {
         "bottles_per_second" : 2
+    },
+    "map":
+    {
+        "points_pos": [100, 100],
+        "font_size": 24
     }
 }
\ No newline at end of file
index 56671c6037c2e76bd3def6a20b17f3a87b8c190f..0ec40739ba75c9d2a2d0fd49f58f6a62c9bbe81e 100644 (file)
@@ -2,6 +2,7 @@
 #include "constants.h"
 #include "States.h"
 #include "Context.h"
+#include <cmath>
 
 
 #include <iostream>
@@ -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<Game_state*>(context.current_state.get());
+    
+}
+
+void Helper::move(bool forward)
+{
+    position += static_cast<float>(pow(-1, forward - 1)) *  direction * max_speed;
+    if (position.y >= stop_bot || position.y <= stop_top )
+    {
+        direction *= -1.0f;
+    }
     sprite.setPosition(position);
 }
 
index 84beb13f568c1901c50bca836da88393ec116691..092015668be88b3f04f6f3d22d1fe30250240799 100644 (file)
@@ -21,6 +21,7 @@ public:
 protected:
     //void move(Time);
 private:
+    void move(bool forward=true);
     float stop_bot;
     float stop_top;
     float max_speed;
index 10e3a848d9c8c04456dd8706ce329fc7d5d001a3..36728abb35b2cf624c242aa82add5afddad4f092 100644 (file)
@@ -1,19 +1,33 @@
 #include "Map.h"
 #include "constants.h"
+#include <string>
 
-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
index afa8c697c640567ebc33d80c9f5c00695b00d3e3..48919014162cf8476cf357e604353e80c45f9ae4 100644 (file)
--- a/src/Map.h
+++ b/src/Map.h
@@ -2,21 +2,24 @@
 #define MAP_H
 
 #include <SFML/Graphics.hpp>
+#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
index 13b6fdb4170b843fcab97fa096c1a93af6fd4907..5748cd7f92f833d922a265b42ba087c64fa9ea26 100644 (file)
@@ -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<Game_state*>(context.current_state.get());
+    //Game_state* game = static_cast<Game_state*>(context.current_state.get());
+    move();
 
-    position += max_speed * direction;
-    
+    return;
+}
+
+void Player::move(bool forward) 
+{
+    position += static_cast<float>(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
index 0c3f0ee5151f428068d73e8f4cc2920c05b6109a..ad8f93ae72e9c8ce3a82a2a95dd95065f1a6271e 100644 (file)
@@ -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;
index 08459002c0b3008649f7ea5b07498ac17affe577..6c20417b9cf93fcf651462744fda6dc47ffdc750 100644 (file)
@@ -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<Player>(data["player"]);
     helper = std::make_unique<Helper>(data["helper"]);
+    game_map = std::make_unique<Map>(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);
index 52d0090128c58aa4f1814a1570a1e1a8b67b3376..379c6482a08a3015d66ab9e2a45f57e77cde1d95 100644 (file)
@@ -42,13 +42,14 @@ protected:
 private:
     float time_since_last_bottle;
     
-    Map game_map;
+    std::unique_ptr<Map> game_map;
     std::unique_ptr<Player> player;
     std::unique_ptr<Helper> helper;
     Main_enemy enemy;
     bool pause_game;
     sf::Texture bottle_texture;
     json data;
+    int points;
 
 };