From c9c78e191dd71bacd769bbeccd58b80e4376184b Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 5 Aug 2017 20:07:10 +0500 Subject: 2017-08-05 --- src/Event.cpp | 4 ++++ src/Event.hpp | 13 +++++++++++-- src/FSM.hpp | 20 ++++++++++++-------- src/GameState.cpp | 2 ++ src/GameState.hpp | 1 + src/Network.cpp | 2 -- src/Network.hpp | 4 ++-- src/NetworkClient.cpp | 5 ++--- src/NetworkClient.hpp | 3 +-- src/Render.cpp | 12 ++++++++---- src/ThreadNetwork.cpp | 26 +++++++------------------- 11 files changed, 50 insertions(+), 42 deletions(-) diff --git a/src/Event.cpp b/src/Event.cpp index 19c93f2..e58a6f4 100644 --- a/src/Event.cpp +++ b/src/Event.cpp @@ -105,3 +105,7 @@ void EventAgregator::EventHandlingLoop() { queueMutex.unlock(); } } + +void SetGlobalState(GlobalState state) { + EventAgregator::PushEvent(EventType::GlobalAppState, GlobalAppStateData{state}); +} \ No newline at end of file diff --git a/src/Event.hpp b/src/Event.hpp index 229da19..c553058 100644 --- a/src/Event.hpp +++ b/src/Event.hpp @@ -11,6 +11,7 @@ #include "Vector.hpp" #include "Packet.hpp" +#include "FSM.hpp" enum class EventType { Echo, @@ -23,6 +24,7 @@ enum class EventType { RegisterNetworkClient, PlayerConnected, RemoveLoadingScreen, + ConnectionFailed, }; struct EchoData { @@ -47,12 +49,15 @@ struct ConnectionSuccessfullData { enum class GlobalState { InitialLoading, MainMenu, + Connecting, Loading, - InGame, + Playing, PauseMenu, Exiting, }; +void SetGlobalState(GlobalState state); + struct GlobalAppStateData { GlobalState state; }; @@ -87,9 +92,13 @@ struct RemoveLoadingScreenData { }; +struct ConnectionFailedData { + std::string reason; +}; + using EventData = std::variant; + RegisterNetworkClientData, PlayerConnectedData, RemoveLoadingScreenData, ConnectionFailedData>; struct Event { EventType type; diff --git a/src/FSM.hpp b/src/FSM.hpp index 9570e26..346d0f0 100644 --- a/src/FSM.hpp +++ b/src/FSM.hpp @@ -6,27 +6,31 @@ template class FSM { - T previousState; - T &state; - std::map> handlers; public: using Transaction = std::pair; + using Handler = std::function; - FSM(T &value) : state(value), previousState(value) {} + FSM(T initialState) : state(initialState), previousState(initialState) {} ~FSM() = default; void Update() { - auto handler = handlers[Transaction{previousState, state}]; + auto &handler = handlers[Transaction{previousState, state}]; if (handler) - handler(); + handler(state); + previousState = state; } - void RegisterHandler(T state, std::function handler) { + void RegisterHandler(T state, Handler handler) { handlers[Transaction{state, state}] = handler; } - void RegisterTransactionHandler(Transaction transaction, std::function handler) { + void RegisterTransactionHandler(Transaction transaction, Handler handler) { handlers[transaction] = handler; } + +private: + T previousState; + T state; + std::map handlers; }; \ No newline at end of file diff --git a/src/GameState.cpp b/src/GameState.cpp index e9c2cef..a2c5182 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -148,6 +148,7 @@ void GameState::Update(float deltaTime) { g_ReducedDebugInfo = packet->ReducedDebugInfo; LOG(INFO) << "Gamemode is " << g_Gamemode << ", Difficulty is " << (int) g_Difficulty << ", Level Type is " << g_LevelType; + SetGlobalState(GlobalState::Loading); EventAgregator::PushEvent(EventType::PlayerConnected, PlayerConnectedData{this}); break; } @@ -210,6 +211,7 @@ void GameState::Update(float deltaTime) { if (!g_IsGameStarted) { LOG(INFO) << "Game is started"; EventAgregator::PushEvent(EventType::RemoveLoadingScreen, RemoveLoadingScreenData{}); + SetGlobalState(GlobalState::Playing); } g_IsGameStarted = true; diff --git a/src/GameState.hpp b/src/GameState.hpp index 076c81b..dbe8f9d 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -7,6 +7,7 @@ #include "World.hpp" #include "NetworkClient.hpp" #include "Vector.hpp" +#include "Event.hpp" class GameState { NetworkClient *nc; diff --git a/src/Network.cpp b/src/Network.cpp index 6cd9baa..47ae751 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -5,7 +5,6 @@ Network::Network(std::string address, unsigned short port) { socket = new Socket(address, port); } catch (std::exception &e) { LOG(WARNING) << "CONNECT FAULT"; - delete socket; throw; } @@ -13,7 +12,6 @@ Network::Network(std::string address, unsigned short port) { stream = new StreamSocket(socket); } catch (std::exception &e) { LOG(WARNING) << "NOT STREAMED"; - LOG(FATAL) << e.what(); } } diff --git a/src/Network.hpp b/src/Network.hpp index 1281289..6263e1b 100644 --- a/src/Network.hpp +++ b/src/Network.hpp @@ -12,8 +12,8 @@ enum ConnectionState { }; class Network { - Socket *socket; - StreamSocket *stream; + Socket *socket = nullptr; + StreamSocket *stream = nullptr; std::shared_ptr ReceivePacketByPacketId(int packetId, ConnectionState state, StreamInput &stream); public: diff --git a/src/NetworkClient.cpp b/src/NetworkClient.cpp index 235251f..d944f54 100644 --- a/src/NetworkClient.cpp +++ b/src/NetworkClient.cpp @@ -1,7 +1,7 @@ #include "NetworkClient.hpp" -NetworkClient::NetworkClient(std::string address, unsigned short port, std::string username, bool &quit) - : network(address, port), isRunning(quit) { +NetworkClient::NetworkClient(std::string address, unsigned short port, std::string username) + : network(address, port) { state = Handshaking; PacketHandshake handshake; @@ -86,7 +86,6 @@ void NetworkClient::NetworkLoop() { } } catch (std::exception &e) { LOG(ERROR) << "Exception catched in NetworkLoop: " << e.what(); - isRunning = false; } LOG(INFO) << "Network thread is stopped"; } diff --git a/src/NetworkClient.hpp b/src/NetworkClient.hpp index cf41f91..83db517 100644 --- a/src/NetworkClient.hpp +++ b/src/NetworkClient.hpp @@ -14,11 +14,10 @@ class NetworkClient { std::queue > toSend; std::queue > toReceive; bool isActive=true; - bool &isRunning; ConnectionState state; void NetworkLoop(); public: - NetworkClient(std::string address, unsigned short port, std::string username, bool &quit); + NetworkClient(std::string address, unsigned short port, std::string username); ~NetworkClient(); std::shared_ptr ReceivePacket(); diff --git a/src/Render.cpp b/src/Render.cpp index d45d5b6..9121d1a 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -124,7 +124,12 @@ void Render::SetMouseCapture(bool IsCaptured) { void Render::ExecuteRenderLoop() { EventListener listener; - listener.RegisterHandler(EventType::ConnectionSuccessfull, [this](EventData eventData) { + + listener.RegisterHandler(EventType::GlobalAppState, [this](EventData eventData) { + + }); + + /*listener.RegisterHandler(EventType::ConnectionSuccessfull, [this](EventData eventData) { auto data = std::get(eventData); window->setTitle("Connected"); }); @@ -136,7 +141,7 @@ void Render::ExecuteRenderLoop() { listener.RegisterHandler(EventType::RemoveLoadingScreen, [this](EventData eventData) { window->setTitle("Loaded"); - }); + });*/ using namespace std::chrono_literals; LoopExecutionTimeController timer(16ms); @@ -150,6 +155,5 @@ void Render::ExecuteRenderLoop() { listener.HandleEvent(); timer.Update(); } - EventData data = GlobalAppStateData{GlobalState::Exiting}; - EventAgregator::PushEvent(EventType::GlobalAppState, data); + SetGlobalState(GlobalState::Exiting); } diff --git a/src/ThreadNetwork.cpp b/src/ThreadNetwork.cpp index b97a3f0..51c8989 100644 --- a/src/ThreadNetwork.cpp +++ b/src/ThreadNetwork.cpp @@ -9,7 +9,7 @@ ThreadNetwork::~ThreadNetwork() { } void ThreadNetwork::Execute() { - bool isRunning; + state = GlobalState::InitialLoading; EventListener listener; listener.RegisterHandler(EventType::GlobalAppState, [this](EventData eventData) { @@ -17,7 +17,7 @@ void ThreadNetwork::Execute() { state = data.state; }); - listener.RegisterHandler(EventType::ConnectToServer, [this, &isRunning](EventData eventData) { + listener.RegisterHandler(EventType::ConnectToServer, [this](EventData eventData) { auto data = std::get(eventData); if (data.address == "" || data.port == 0) LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT"; @@ -25,13 +25,14 @@ void ThreadNetwork::Execute() { LOG(ERROR) << "Already connected"; return; } - + SetGlobalState(GlobalState::Connecting); LOG(INFO) << "Connecting to server"; try { - nc = new NetworkClient(data.address, data.port, "HelloOne", isRunning); + nc = new NetworkClient(data.address, data.port, "HelloOne"); } catch (std::exception &e) { - LOG(WARNING) << "CONNECTION FAIL"; - LOG(FATAL) << "Can't connect to server: " << e.what(); + LOG(WARNING) << "Connection failed"; + EventAgregator::PushEvent(EventType::ConnectionFailed, ConnectionFailedData{e.what()}); + return; } LOG(INFO) << "Connected to server"; EventAgregator::PushEvent(EventType::ConnectionSuccessfull, ConnectionSuccessfullData{nc}); @@ -41,20 +42,7 @@ void ThreadNetwork::Execute() { EventAgregator::PushEvent(EventType::RegisterNetworkClient, RegisterNetworkClientData{nc}); }); - /*listener.RegisterHandler(EventType::SendPacket, [this](EventData eventData) { - auto data = std::get(eventData); - auto packet = data.packet; - if (nc) - nc->SendPacket(packet); - else - LOG(ERROR) << "Send packet, while not connected to server"; - });*/ - - while (state != GlobalState::Exiting) { - /*auto packet = nc ? nc->ReceivePacket() : nullptr; - if (packet != nullptr) - EventAgregator::PushEvent(EventType::ReceivePacket, ReceivePacketData{packet});*/ listener.HandleEvent(); } } \ No newline at end of file -- cgit v1.2.3