Fixed time_delta
authorNils Forssén <nilfo359@student.liu.se>
Mon, 13 Nov 2023 10:16:28 +0000 (11:16 +0100)
committerNils Forssén <nilfo359@student.liu.se>
Mon, 13 Nov 2023 10:16:28 +0000 (11:16 +0100)
src/Context.h
src/States.cc
src/States.h
src/_main.cc

index bdf0d1c649644491f25e6c534912fcc11d063bbb..327387dc14462a64a272d07cb6d0f58223ee8cce 100644 (file)
@@ -9,7 +9,7 @@ struct Context
     std::unique_ptr<State> current_state {nullptr};
     std::unique_ptr<State> next_state {nullptr};
     std::unique_ptr<Game_state> saved_game {nullptr};
-    sf::Int32 time;
+    sf::Time time;
 };
 
 #endif
\ No newline at end of file
index b7002510c8e5c622dfdbf2262033b5925de517a0..659d4b79412702ad2fa9aae4f57712069d64e4e5 100644 (file)
@@ -1,10 +1,11 @@
 #include <memory>
 #include <iostream>
+#include <iomanip>
 #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 {};
index a9a73580ec713f825008f05975190a3c856e7a34..595aa317f36b3da98ea9957390c7ba8cf43d82eb 100644 (file)
@@ -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<Bottle*> bottles;
     Map game_map;
     Player player;
index 7b58650ad29bca59a00918880c81c170813b506e..0808a0581707a9d760f92ce1e618a861e68a70ce 100644 (file)
@@ -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();
 
     }
 }