From 7b17a341ed507651a02723594a656a11500382ef Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Mon, 13 Nov 2023 11:16:28 +0100 Subject: [PATCH] Fixed time_delta --- src/Context.h | 2 +- src/States.cc | 7 ++++--- src/States.h | 2 +- src/_main.cc | 5 ++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Context.h b/src/Context.h index bdf0d1c..327387d 100644 --- a/src/Context.h +++ b/src/Context.h @@ -9,7 +9,7 @@ struct Context std::unique_ptr current_state {nullptr}; std::unique_ptr next_state {nullptr}; std::unique_ptr saved_game {nullptr}; - sf::Int32 time; + sf::Time time; }; #endif \ No newline at end of file diff --git a/src/States.cc b/src/States.cc index b700251..659d4b7 100644 --- a/src/States.cc +++ b/src/States.cc @@ -1,10 +1,11 @@ #include #include +#include #include "States.h" #include "Context.h" #include "constants.h" -Game_state::Game_state() : game_map{}, player{}, pause_game{false}, bottles{} +Game_state::Game_state() : game_map{}, player{}, pause_game{false}, bottles{}, time_since_last_bottle{0.0f} { Bottle* bottle = new Bottle {}; bottles.push_back(bottle); @@ -20,8 +21,8 @@ void Game_state::update(Context& context) pause_game = false; return; } - time_since_last_bottle += context.time; - //std::cout << time_since_last_bottle << std::endl; + 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 >= 2) { Bottle* bottle = new Bottle {}; diff --git a/src/States.h b/src/States.h index a9a7358..595aa31 100644 --- a/src/States.h +++ b/src/States.h @@ -30,7 +30,7 @@ public: void handle_input(sf::Event& event) override; protected: private: - float time_since_last_bottle {0}; + float time_since_last_bottle; std::vector bottles; Map game_map; Player player; diff --git a/src/_main.cc b/src/_main.cc index 7b58650..0808a05 100644 --- a/src/_main.cc +++ b/src/_main.cc @@ -40,8 +40,7 @@ int main () //innehåller all spelstatus game_context.current_state->handle_input(event); } - - //game_context.time = game_clock.getElapsedtime().asSeconds() + // Update startmeny game_context.current_state->update(game_context); @@ -61,7 +60,7 @@ int main () // Make game run at constant fps // std::cout << "frametime: " << game_clock.getElapsedTime().asSeconds() << std::endl; sf::sleep(sf::milliseconds(1000.0 / FPS) - game_clock.getElapsedTime()); - game_clock.restart(); + game_context.time = game_clock.restart(); } } -- 2.30.2