From d4e9d6bbc0abe66acb149a358a25c9a0f385ce74 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 21 Apr 2019 18:06:55 +0500 Subject: Renamed GlobalState to Game --- src/Game.cpp | 238 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Game.hpp | 25 ++++++ src/GlobalState.cpp | 238 ------------------------------------------------- src/GlobalState.hpp | 25 ------ src/Render.cpp | 2 +- src/RendererEntity.cpp | 2 +- src/main.cpp | 2 +- 7 files changed, 266 insertions(+), 266 deletions(-) create mode 100644 src/Game.cpp create mode 100644 src/Game.hpp delete mode 100644 src/GlobalState.cpp delete mode 100644 src/GlobalState.hpp diff --git a/src/Game.cpp b/src/Game.cpp new file mode 100644 index 0000000..46785b1 --- /dev/null +++ b/src/Game.cpp @@ -0,0 +1,238 @@ +#include "Game.hpp" + +#include "NetworkClient.hpp" +#include "GameState.hpp" +#include "Render.hpp" +#include "DebugInfo.hpp" +#include "Event.hpp" + +//Global game variables +std::unique_ptr nc; +std::unique_ptr gs; +std::shared_ptr gsReadOnly; +std::unique_ptr render; +bool isRunning; +bool isPhysRunning; +EventListener listener; +bool isMoving[5] = { 0,0,0,0,0 }; +std::thread threadPhys; +State state; +std::mutex gsCopyMutex; + +void PhysExec(); + +void InitEvents() { + /* + * Network Events + */ + + listener.RegisterHandler("Exit", [](const Event&) { + isRunning = false; + }); + + listener.RegisterHandler("ConnectToServer", [](const Event& eventData) { + auto data = eventData.get >(); //address,port,username + if (std::get<0>(data) == "" || std::get<1>(data) == 0) + LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT"; + if (nc != nullptr) { + LOG(ERROR) << "Already connected"; + return; + } + LOG(INFO) << "Connecting to server at address " + std::get<0>(data) + ":" + std::to_string(std::get<1>(data)) + " as " + std::get<2>(data); + PUSH_EVENT("Connecting",0); + gs = std::make_unique(); + isPhysRunning = true; + threadPhys = std::thread(&PhysExec); + try { + nc = std::make_unique(std::get<0>(data), + std::get<1>(data), + std::get<2>(data)); + } catch (std::exception &e) { + LOG(WARNING) << "Connection failed"; + PUSH_EVENT("ConnectionFailed", std::string(e.what())); + isPhysRunning = false; + threadPhys.join(); + gs.reset(); + return; + } + LOG(INFO) << "Connected to server"; + PUSH_EVENT("ConnectionSuccessfull", 0); + }); + + listener.RegisterHandler("Disconnect", [](const Event& eventData) { + auto data = eventData.get(); + PUSH_EVENT("Disconnected", data); + LOG(INFO) << "Disconnected: " << data; + nc.reset(); + }); + + listener.RegisterHandler("NetworkClientException", [](const Event& eventData) { + auto data = eventData.get < std::string>(); + PUSH_EVENT("Disconnect", data); + }); + + /* + * GameState Events + */ + + listener.RegisterHandler("Exit", [](const Event&) { + isRunning = false; + }); + + listener.RegisterHandler("Disconnected", [](const Event&) { + if (!gs) + return; + isPhysRunning = false; + threadPhys.join(); + gs.reset(); + }); + + listener.RegisterHandler("SendChatMessage", [](const Event& eventData) { + auto message = eventData.get(); + auto packet = std::static_pointer_cast(std::make_shared(message)); + PUSH_EVENT("SendPacket",packet); + }); +} + +void PhysExec() { + EventListener listener; + + listener.RegisterHandler("KeyPressed", [](const Event& eventData) { + if (!gs) + return; + switch (eventData.get()) { + case SDL_SCANCODE_W: + isMoving[GameState::FORWARD] = true; + break; + case SDL_SCANCODE_A: + isMoving[GameState::LEFT] = true; + break; + case SDL_SCANCODE_S: + isMoving[GameState::BACKWARD] = true; + break; + case SDL_SCANCODE_D: + isMoving[GameState::RIGHT] = true; + break; + case SDL_SCANCODE_SPACE: + isMoving[GameState::JUMP] = true; + break; + default: + break; + } + }); + + listener.RegisterHandler("KeyReleased", [](const Event& eventData) { + if (!gs) + return; + switch (eventData.get()) { + case SDL_SCANCODE_W: + isMoving[GameState::FORWARD] = false; + break; + case SDL_SCANCODE_A: + isMoving[GameState::LEFT] = false; + break; + case SDL_SCANCODE_S: + isMoving[GameState::BACKWARD] = false; + break; + case SDL_SCANCODE_D: + isMoving[GameState::RIGHT] = false; + break; + case SDL_SCANCODE_SPACE: + isMoving[GameState::JUMP] = false; + break; + default: + break; + } + }); + + listener.RegisterHandler("MouseMove", [](const Event& eventData) { + if (!gs) + return; + auto data = eventData.get>(); + gs->HandleRotation(std::get<0>(data),std::get<1>(data)); + }); + + listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) { + std::shared_ptr packet = eventData.get>(); + gs->UpdatePacket(packet); + }); + + listener.RegisterHandler("LmbPressed",[](const Event& eventData) { + gs->StartDigging(); + }); + + listener.RegisterHandler("LmbReleased",[](const Event& eventData) { + gs->CancelDigging(); + }); + + listener.RegisterHandler("RmbPressed", [](const Event& eventData) { + gs->PlaceBlock(); + }); + + listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) { + //TODO: + //gs->CancelDigging(); + }); + + LoopExecutionTimeController timer(std::chrono::milliseconds(8)); + + while (isPhysRunning) { + DebugInfo::gameThreadTime = timer.GetRealDeltaS() * 1000'00.0f; + + if (state == 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()); + + listener.HandleAllEvents(); + + gsCopyMutex.lock(); + gsReadOnly = std::make_shared(*gs.get()); + gsCopyMutex.unlock(); + + timer.Update(); + } +} + +void GlobalState::Exec() { + render = std::make_unique(900, 480, "AltCraft"); + isRunning = true; + InitEvents(); + GlobalState::SetState(State::MainMenu); + while (isRunning) { + render->Update(); + listener.HandleAllEvents(); + } + PUSH_EVENT("Exit", 0); + isRunning = false; + render.reset(); +} + +std::shared_ptr GlobalState::GetGameState() { + std::lock_guard guard(gsCopyMutex); + return gsReadOnly; +} + +Render *GlobalState::GetRender() { + return render.get(); +} + +State GlobalState::GetState() { + return state; +} + +void GlobalState::SetState(const State &newState) { + if (newState != state) + PUSH_EVENT("StateUpdated", 0); + state = newState; +} diff --git a/src/Game.hpp b/src/Game.hpp new file mode 100644 index 0000000..bc7224f --- /dev/null +++ b/src/Game.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include + +class NetworkClient; +class GameState; +class Render; + +enum class State { + InitialLoading, + MainMenu, + Loading, + Playing, + Paused, + Inventory, + Chat, +}; + +struct GlobalState { + static std::shared_ptr GetGameState(); + static Render *GetRender(); + static void Exec(); + static State GetState(); + static void SetState(const State &newState); +}; \ No newline at end of file diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp deleted file mode 100644 index afa1a56..0000000 --- a/src/GlobalState.cpp +++ /dev/null @@ -1,238 +0,0 @@ -#include "GlobalState.hpp" - -#include "NetworkClient.hpp" -#include "GameState.hpp" -#include "Render.hpp" -#include "DebugInfo.hpp" -#include "Event.hpp" - -//Global game variables -std::unique_ptr nc; -std::unique_ptr gs; -std::shared_ptr gsReadOnly; -std::unique_ptr render; -bool isRunning; -bool isPhysRunning; -EventListener listener; -bool isMoving[5] = { 0,0,0,0,0 }; -std::thread threadPhys; -State state; -std::mutex gsCopyMutex; - -void PhysExec(); - -void InitEvents() { - /* - * Network Events - */ - - listener.RegisterHandler("Exit", [](const Event&) { - isRunning = false; - }); - - listener.RegisterHandler("ConnectToServer", [](const Event& eventData) { - auto data = eventData.get >(); //address,port,username - if (std::get<0>(data) == "" || std::get<1>(data) == 0) - LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT"; - if (nc != nullptr) { - LOG(ERROR) << "Already connected"; - return; - } - LOG(INFO) << "Connecting to server at address " + std::get<0>(data) + ":" + std::to_string(std::get<1>(data)) + " as " + std::get<2>(data); - PUSH_EVENT("Connecting",0); - gs = std::make_unique(); - isPhysRunning = true; - threadPhys = std::thread(&PhysExec); - try { - nc = std::make_unique(std::get<0>(data), - std::get<1>(data), - std::get<2>(data)); - } catch (std::exception &e) { - LOG(WARNING) << "Connection failed"; - PUSH_EVENT("ConnectionFailed", std::string(e.what())); - isPhysRunning = false; - threadPhys.join(); - gs.reset(); - return; - } - LOG(INFO) << "Connected to server"; - PUSH_EVENT("ConnectionSuccessfull", 0); - }); - - listener.RegisterHandler("Disconnect", [](const Event& eventData) { - auto data = eventData.get(); - PUSH_EVENT("Disconnected", data); - LOG(INFO) << "Disconnected: " << data; - nc.reset(); - }); - - listener.RegisterHandler("NetworkClientException", [](const Event& eventData) { - auto data = eventData.get < std::string>(); - PUSH_EVENT("Disconnect", data); - }); - - /* - * GameState Events - */ - - listener.RegisterHandler("Exit", [](const Event&) { - isRunning = false; - }); - - listener.RegisterHandler("Disconnected", [](const Event&) { - if (!gs) - return; - isPhysRunning = false; - threadPhys.join(); - gs.reset(); - }); - - listener.RegisterHandler("SendChatMessage", [](const Event& eventData) { - auto message = eventData.get(); - auto packet = std::static_pointer_cast(std::make_shared(message)); - PUSH_EVENT("SendPacket",packet); - }); -} - -void PhysExec() { - EventListener listener; - - listener.RegisterHandler("KeyPressed", [](const Event& eventData) { - if (!gs) - return; - switch (eventData.get()) { - case SDL_SCANCODE_W: - isMoving[GameState::FORWARD] = true; - break; - case SDL_SCANCODE_A: - isMoving[GameState::LEFT] = true; - break; - case SDL_SCANCODE_S: - isMoving[GameState::BACKWARD] = true; - break; - case SDL_SCANCODE_D: - isMoving[GameState::RIGHT] = true; - break; - case SDL_SCANCODE_SPACE: - isMoving[GameState::JUMP] = true; - break; - default: - break; - } - }); - - listener.RegisterHandler("KeyReleased", [](const Event& eventData) { - if (!gs) - return; - switch (eventData.get()) { - case SDL_SCANCODE_W: - isMoving[GameState::FORWARD] = false; - break; - case SDL_SCANCODE_A: - isMoving[GameState::LEFT] = false; - break; - case SDL_SCANCODE_S: - isMoving[GameState::BACKWARD] = false; - break; - case SDL_SCANCODE_D: - isMoving[GameState::RIGHT] = false; - break; - case SDL_SCANCODE_SPACE: - isMoving[GameState::JUMP] = false; - break; - default: - break; - } - }); - - listener.RegisterHandler("MouseMove", [](const Event& eventData) { - if (!gs) - return; - auto data = eventData.get>(); - gs->HandleRotation(std::get<0>(data),std::get<1>(data)); - }); - - listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) { - std::shared_ptr packet = eventData.get>(); - gs->UpdatePacket(packet); - }); - - listener.RegisterHandler("LmbPressed",[](const Event& eventData) { - gs->StartDigging(); - }); - - listener.RegisterHandler("LmbReleased",[](const Event& eventData) { - gs->CancelDigging(); - }); - - listener.RegisterHandler("RmbPressed", [](const Event& eventData) { - gs->PlaceBlock(); - }); - - listener.RegisterHandler("SelectedBlockChanged", [](const Event& eventData) { - //TODO: - //gs->CancelDigging(); - }); - - LoopExecutionTimeController timer(std::chrono::milliseconds(8)); - - while (isPhysRunning) { - DebugInfo::gameThreadTime = timer.GetRealDeltaS() * 1000'00.0f; - - if (state == 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()); - - listener.HandleAllEvents(); - - gsCopyMutex.lock(); - gsReadOnly = std::make_shared(*gs.get()); - gsCopyMutex.unlock(); - - timer.Update(); - } -} - -void GlobalState::Exec() { - render = std::make_unique(900, 480, "AltCraft"); - isRunning = true; - InitEvents(); - GlobalState::SetState(State::MainMenu); - while (isRunning) { - render->Update(); - listener.HandleAllEvents(); - } - PUSH_EVENT("Exit", 0); - isRunning = false; - render.reset(); -} - -std::shared_ptr GlobalState::GetGameState() { - std::lock_guard guard(gsCopyMutex); - return gsReadOnly; -} - -Render *GlobalState::GetRender() { - return render.get(); -} - -State GlobalState::GetState() { - return state; -} - -void GlobalState::SetState(const State &newState) { - if (newState != state) - PUSH_EVENT("StateUpdated", 0); - state = newState; -} diff --git a/src/GlobalState.hpp b/src/GlobalState.hpp deleted file mode 100644 index bc7224f..0000000 --- a/src/GlobalState.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include - -class NetworkClient; -class GameState; -class Render; - -enum class State { - InitialLoading, - MainMenu, - Loading, - Playing, - Paused, - Inventory, - Chat, -}; - -struct GlobalState { - static std::shared_ptr GetGameState(); - static Render *GetRender(); - static void Exec(); - static State GetState(); - static void SetState(const State &newState); -}; \ No newline at end of file diff --git a/src/Render.cpp b/src/Render.cpp index f86e5ec..a20ce21 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -8,7 +8,7 @@ #include "AssetManager.hpp" #include "Event.hpp" #include "DebugInfo.hpp" -#include "GlobalState.hpp" +#include "Game.hpp" #include "World.hpp" #include "GameState.hpp" #include "RendererWorld.hpp" diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index 25403be..ed1e854 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -7,7 +7,7 @@ #include "GameState.hpp" #include "Renderer.hpp" #include "AssetManager.hpp" -#include "GlobalState.hpp" +#include "Game.hpp" const GLfloat vertices[] = { -0.5f, 0.5f, 0.5f, diff --git a/src/main.cpp b/src/main.cpp index cb2daa8..100b7bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ #include "Event.hpp" #include "Utility.hpp" -#include "GlobalState.hpp" +#include "Game.hpp" #include -- cgit v1.2.3