diff options
-rw-r--r-- | src/Game.cpp | 41 | ||||
-rw-r--r-- | src/Game.hpp | 5 | ||||
-rw-r--r-- | src/GameState.cpp | 2 | ||||
-rw-r--r-- | src/GameState.hpp | 2 | ||||
-rw-r--r-- | src/Render.cpp | 21 | ||||
-rw-r--r-- | src/Render.hpp | 1 |
6 files changed, 54 insertions, 18 deletions
diff --git a/src/Game.cpp b/src/Game.cpp index 927cdc2..a3f2b50 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -12,6 +12,7 @@ State state; std::unique_ptr<NetworkClient> nc; std::unique_ptr<GameState> gs; std::unique_ptr<Render> render; +std::unique_ptr<LoopExecutionTimeController> timer; EventListener listener; void InitEvents() { @@ -126,6 +127,7 @@ void InitEvents() { default: break; } + LOG(INFO) << "Changed key"; }); listener.RegisterHandler("MouseMove", [](const Event& eventData) { @@ -136,23 +138,33 @@ void InitEvents() { }); listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) { + if (!gs) + return; std::shared_ptr<Packet> packet = eventData.get<std::shared_ptr<Packet>>(); gs->UpdatePacket(packet); }); listener.RegisterHandler("LmbPressed",[](const Event& eventData) { + if (!gs) + return; gs->StartDigging(); }); listener.RegisterHandler("LmbReleased",[](const Event& eventData) { + if (!gs) + return; gs->CancelDigging(); }); listener.RegisterHandler("RmbPressed", [](const Event& eventData) { + if (!gs) + return; gs->PlaceBlock(); }); listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) { + if (!gs) + return; //TODO: //gs->CancelDigging(); }); @@ -161,17 +173,32 @@ void InitEvents() { void RunGame() { InitEvents(); + timer = std::make_unique<LoopExecutionTimeController>(std::chrono::milliseconds(16)); + render = std::make_unique<Render>(900, 480, "AltCraft"); - SetState(State::MainMenu); - LoopExecutionTimeController time(std::chrono::milliseconds(16)); + SetState(State::MainMenu); while (isRunning) { listener.HandleAllEvents(); - if (gs) - gs->Update(time.GetDeltaS()); + if (gs) { + if (GetState() == State::Playing) { + if (isMoving[GameState::FORWARD]) + gs->HandleMovement(GameState::FORWARD, timer->GetRealDeltaS()); + if (isMoving[GameState::BACKWARD]) + gs->HandleMovement(GameState::BACKWARD, timer->GetRealDeltaS()); + if (isMoving[GameState::LEFT]) + gs->HandleMovement(GameState::LEFT, timer->GetRealDeltaS()); + if (isMoving[GameState::RIGHT]) + gs->HandleMovement(GameState::RIGHT, timer->GetRealDeltaS()); + if (isMoving[GameState::JUMP]) + gs->HandleMovement(GameState::JUMP, timer->GetRealDeltaS()); + } + + gs->Update(timer->GetRealDeltaS()); + } render->Update(); - time.Update(); + timer->Update(); } render.reset(); @@ -198,3 +225,7 @@ Render *GetRender() { NetworkClient *GetNetworkClient() { return nc.get(); } + +LoopExecutionTimeController* GetTime() { + return timer.get(); +} diff --git a/src/Game.hpp b/src/Game.hpp index 633585f..f7efd11 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -3,6 +3,7 @@ class GameState; class Render; class NetworkClient; +class LoopExecutionTimeController; enum class State { InitialLoading, @@ -24,4 +25,6 @@ GameState* GetGameState(); Render* GetRender(); -NetworkClient* GetNetworkClient();
\ No newline at end of file +NetworkClient* GetNetworkClient(); + +LoopExecutionTimeController *GetTime();
\ No newline at end of file diff --git a/src/GameState.cpp b/src/GameState.cpp index ace2488..9e92c1f 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -6,7 +6,7 @@ #include "Event.hpp" #include "Packet.hpp" -void GameState::Update(float deltaTime) { +void GameState::Update(double deltaTime) { if (!gameStatus.isGameStarted) return; diff --git a/src/GameState.hpp b/src/GameState.hpp index 8318c8a..5489ac6 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -70,7 +70,7 @@ class GameState { std::vector<Window> openedWindows; public: - void Update(float deltaTime); + void Update(double deltaTime); void UpdatePacket(std::shared_ptr<Packet> ptr); diff --git a/src/Render.cpp b/src/Render.cpp index 2e69369..00daf99 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -8,17 +8,16 @@ #include "AssetManager.hpp" #include "Event.hpp" #include "DebugInfo.hpp" +#include "Game.hpp" #include "World.hpp" #include "GameState.hpp" #include "RendererWorld.hpp" #include "Settings.hpp" #include "Framebuffer.hpp" #include "Plugin.hpp" -#include "Game.hpp" Render::Render(unsigned int windowWidth, unsigned int windowHeight, - std::string windowTitle) - : timer(std::chrono::milliseconds(16)) { + std::string windowTitle) { InitEvents(); Settings::Load(); @@ -48,9 +47,9 @@ Render::Render(unsigned int windowWidth, unsigned int windowHeight, if (fieldSensetivity != sensetivity) sensetivity = fieldSensetivity; isWireframe = fieldWireframe; - timer.SetDelayLength(std::chrono::duration<double, std::milli>(1.0 / fieldTargetFps * 1000.0)); + GetTime()->SetDelayLength(std::chrono::duration<double, std::milli>(1.0 / fieldTargetFps * 1000.0)); if (fieldVsync) { - timer.SetDelayLength(std::chrono::milliseconds(0)); + GetTime()->SetDelayLength(std::chrono::milliseconds(0)); SDL_GL_SetSwapInterval(1); } else @@ -191,7 +190,7 @@ void Render::RenderFrame() { RenderGui(); if (world) { - world->Update(timer.RemainTimeMs()); + world->Update(GetTime()->RemainTimeMs()); } SDL_GL_SwapWindow(window); @@ -358,7 +357,6 @@ void Render::Update() { RenderFrame(); listener.HandleAllEvents(); - timer.Update(); } void Render::RenderGui() { @@ -629,9 +627,9 @@ void Render::RenderGui() { GetGameState()->GetPlayer()->isFlying = fieldFlight; isWireframe = fieldWireframe; - timer.SetDelayLength(std::chrono::duration<double, std::milli>(1.0 / fieldTargetFps * 1000.0)); + GetTime()->SetDelayLength(std::chrono::duration<double, std::milli>(1.0 / fieldTargetFps * 1000.0)); if (fieldVsync) { - timer.SetDelayLength(std::chrono::milliseconds(0)); + GetTime()->SetDelayLength(std::chrono::milliseconds(0)); SDL_GL_SetSwapInterval(1); } else SDL_GL_SetSwapInterval(0); @@ -721,18 +719,23 @@ void Render::InitEvents() { break; case State::InitialLoading: PluginSystem::CallOnChangeState("InitialLoading"); + SetMouseCapture(false); break; case State::MainMenu: PluginSystem::CallOnChangeState("MainMenu"); + SetMouseCapture(false); break; case State::Loading: PluginSystem::CallOnChangeState("Loading"); + SetMouseCapture(false); break; case State::Paused: PluginSystem::CallOnChangeState("Paused"); + SetMouseCapture(false); break; case State::Inventory: PluginSystem::CallOnChangeState("Inventory"); + SetMouseCapture(false); break; case State::Chat: PluginSystem::CallOnChangeState("Chat"); diff --git a/src/Render.hpp b/src/Render.hpp index eea5450..8e2e233 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -25,7 +25,6 @@ class Render { std::unique_ptr<RendererWorld> world; bool renderWorld = false; RenderState renderState; - LoopExecutionTimeController timer; std::map<SDL_Scancode, bool> isKeyPressed; bool HasFocus=true; float sensetivity = 0.1f; |