From 2ee4080dc476dced0d84eba6dfa00bfc4e81317e Mon Sep 17 00:00:00 2001 From: lukel495 Date: Thu, 23 Nov 2023 12:02:32 +0100 Subject: [PATCH] Added json, fixed time, fixed scales and positions, Nils Lukas --- Makefile | 2 +- assets/data.json | 21 +++++++++++++++------ src/Main_enemy.cc | 2 +- src/Map.cc | 10 +++++----- src/Map.h | 2 +- src/Player.cc | 7 ++++--- src/States.cc | 34 ++++++++++++++++++++++------------ src/States.h | 1 + 8 files changed, 50 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index bd5e408..8446431 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ test-target: $(OBJDIR)/_test.o $(OBJECTS) $(CC) $(LDFLAGS) $^ -o $(TEST) $(LDFLAGS) $(OBJDIR)/%.o: %.cc $(SRC_H) - $(CC) $(CCFLAGS) -c -o $@ $< + $(CC) $(CCFLAGS) $(LDFLAGS) -c -o $@ $< run: ./$(EXE) diff --git a/assets/data.json b/assets/data.json index 5ed7ec8..7e8b395 100644 --- a/assets/data.json +++ b/assets/data.json @@ -3,7 +3,14 @@ { "start_pos": [200, 200], "scale": [0.5, 0.5], - "max_speed": 5 + "max_speed": 5, + "sack" : + { + "offset" : [30, 18], + "scale" : [0.3, 0.3], + "angle" : -25 + } + }, "helper": { "start_pos": [1000, 50], @@ -20,17 +27,19 @@ }, "game_constants": { - "bottles_per_second" : 2 + "bottles_per_second" : 2, + "game_time" : 20 + }, "map": { - "points_pos": [100, 100], - "font_size": 24 + "points_pos": [5, 0], + "font_size": 40 }, "main_enemy": { - "scale" : [0.35, 0.35] + "scale" : [0.35, 0.35], + "start_pos" : [50, 600] } - } \ No newline at end of file diff --git a/src/Main_enemy.cc b/src/Main_enemy.cc index 3b3ae83..68002b9 100644 --- a/src/Main_enemy.cc +++ b/src/Main_enemy.cc @@ -10,7 +10,7 @@ Main_enemy::Main_enemy(sf::Texture& txtr, json& params) //texture.loadFromFile("assets/6Ling_figur2.png"); texture = txtr; sprite.setTexture(texture); - + position = {params["start_pos"][0], params["start_pos"][1]}; sprite.setScale(params["scale"][0], params["scale"][1]); sf::FloatRect gb {sprite.getGlobalBounds()}; sprite.setOrigin(gb.width / 2, gb.height / 2); diff --git a/src/Map.cc b/src/Map.cc index 36728ab..0251aca 100644 --- a/src/Map.cc +++ b/src/Map.cc @@ -13,17 +13,17 @@ Map::Map(json& params) : texture{}, sprite{}, point_text{}, font{} 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"] }; + 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); + point_text.setFillColor(sf::Color::Black); + } -void Map::update(int points) +void Map::update(int const points, int const game_time) { - point_text.setString("Points: " + std::to_string(points)); + point_text.setString("Points: " + std::to_string(points) + "\n" + "Time left: " + std::to_string(game_time) + " s"); } void Map::render(sf::RenderWindow& window) const diff --git a/src/Map.h b/src/Map.h index 4891901..5d2c9c1 100644 --- a/src/Map.h +++ b/src/Map.h @@ -12,7 +12,7 @@ public: Map(json& params); ~Map() = default; - void update(int points); + void update(int const points, int const game_state); void render(sf::RenderWindow& window) const; protected: private: diff --git a/src/Player.cc b/src/Player.cc index 71eddcb..7f52064 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -20,11 +20,12 @@ Player::Player(json& params) : collected{0}, max_speed{params["max_speed"]} sack_texture.loadFromFile("assets/sack.png"); sack.setTexture(sack_texture); - sack.setScale(0.5, 0.5); + sack.setScale(params["sack"]["scale"][0], params["sack"]["scale"][1]); sf::FloatRect gb_s {sack.getGlobalBounds()}; - sack.setOrigin(gb_s.width / 2, gb_s.height / 2); - sack_offset = {gb.width/4, -gb.height/15}; + 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); } diff --git a/src/States.cc b/src/States.cc index 7c57cac..eff307c 100644 --- a/src/States.cc +++ b/src/States.cc @@ -7,6 +7,7 @@ #include "json.hpp" #include +#include using json = nlohmann::json; @@ -18,6 +19,7 @@ Game_state::Game_state() : helper{}, main_enemy_texture{}, time_since_last_bottle{0.0f}, + game_time{0}, bottle_texture {}, data{}, enemy{}, @@ -47,17 +49,16 @@ void Game_state::update(Context &context) pause_game = false; return; } - // std::vector :: iterator it = bottles.begin(); - - /*for(std::unique_ptr &b: bottles) + + game_time += context.time.asSeconds(); + int remaining_time {static_cast(data["game_constants"]["game_time"]) - static_cast(game_time)}; + if (remaining_time < 0) { - if(player.collides(*b)) - { - std::cout <<"crash" << std::endl; + context.next_state = std::make_unique(); + return; + } + - player.add_collected(); - } - }*/ for (unsigned int i {0}; i < bottles.size(); ++i) { if (player->collides(*(bottles[i]))) @@ -76,8 +77,9 @@ void Game_state::update(Context &context) } } - helper->update(context); player->update(context); + helper->update(context); + if (player->collides(*helper)) { points += player->get_collected(); @@ -86,9 +88,15 @@ void Game_state::update(Context &context) player->collision(*helper); helper->collision(*player); } + /* + if (player->get_x() < 0) player->set_x(0); + if (player->get_x() > S_WIDTH) player->set_x(S_WIDTH); + if (player->get_y() < 0) player->set_y(0); + if (player->get_y() > S_HEIGHT) player->set_y(S_HEIGHT); + */ - time_since_last_bottle += context.time.asSeconds(); + //std::cout << std::fixed << std::setprecision(3) << time_since_last_bottle << std::endl; if (time_since_last_bottle >= data["game_constants"]["bottles_per_second"]) { @@ -96,8 +104,10 @@ void Game_state::update(Context &context) std::cout << "placed bottle"<< std::endl; time_since_last_bottle = 0; } - game_map->update(points); + //game time enemy->update(context); + game_map->update(points, remaining_time); + } void Game_state::render(sf::RenderWindow &window) const diff --git a/src/States.h b/src/States.h index 1c8c148..9eedbf5 100644 --- a/src/States.h +++ b/src/States.h @@ -41,6 +41,7 @@ public: protected: private: float time_since_last_bottle; + float game_time; std::unique_ptr game_map; std::unique_ptr player; -- 2.30.2