summaryrefslogtreecommitdiffstats
path: root/src/Game.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2020-07-31 07:05:46 +0200
committerGitHub <noreply@github.com>2020-07-31 07:05:46 +0200
commit28caa598e29b7eaa36d0877dd57947d29fe92e06 (patch)
tree14ce54f1349966ce3482d270c1b104bdac6cc86d /src/Game.cpp
parentMerge pull request #42 from LaG1924/ftr/chat_component_parse (diff)
parentCorrected Game Start conditions (diff)
downloadAltCraft-28caa598e29b7eaa36d0877dd57947d29fe92e06.tar
AltCraft-28caa598e29b7eaa36d0877dd57947d29fe92e06.tar.gz
AltCraft-28caa598e29b7eaa36d0877dd57947d29fe92e06.tar.bz2
AltCraft-28caa598e29b7eaa36d0877dd57947d29fe92e06.tar.lz
AltCraft-28caa598e29b7eaa36d0877dd57947d29fe92e06.tar.xz
AltCraft-28caa598e29b7eaa36d0877dd57947d29fe92e06.tar.zst
AltCraft-28caa598e29b7eaa36d0877dd57947d29fe92e06.zip
Diffstat (limited to 'src/Game.cpp')
-rw-r--r--src/Game.cpp54
1 files changed, 42 insertions, 12 deletions
diff --git a/src/Game.cpp b/src/Game.cpp
index c69ff5d..f0266d4 100644
--- a/src/Game.cpp
+++ b/src/Game.cpp
@@ -18,35 +18,61 @@ std::unique_ptr<Render> render;
std::unique_ptr<LoopExecutionTimeController> timer;
EventListener listener;
-void InitEvents() {
- /*
- * Network Events
- */
+std::thread connThread;
+std::unique_ptr<NetworkClient> connNc;
+std::unique_ptr<GameState> connGs;
+
+void ConnectionThreadExec() {
+ EventListener connListener;
- listener.RegisterHandler("ConnectToServer", [](const Event & eventData) {
+ bool connRun = true;
+
+ connListener.RegisterHandler("ConnectToServer", [](const Event& eventData) {
auto data = eventData.get <std::tuple<std::string, unsigned short, std::string>>(); //address,port,username
if (std::get<0>(data) == "" || std::get<1>(data) == 0)
LOG(FATAL) << "NOT VALID CONNECT-TO-SERVER EVENT";
- if (nc != nullptr) {
+ if (connNc != 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<GameState>();
+ PUSH_EVENT("Connecting", 0);
+ connGs = std::make_unique<GameState>();
try {
- nc = std::make_unique<NetworkClient>(std::get<0>(data),
+ connNc = std::make_unique<NetworkClient>(std::get<0>(data),
std::get<1>(data),
std::get<2>(data));
- } catch (std::exception &e) {
+ }
+ catch (std::exception& e) {
LOG(WARNING) << "Connection failed";
PUSH_EVENT("ConnectionFailed", std::string(e.what()));
- gs.reset();
+ connGs.reset();
return;
}
LOG(INFO) << "Connected to server";
PUSH_EVENT("ConnectionSuccessfull", 0);
- });
+ });
+
+ connListener.RegisterHandler("Exit", [&](const Event&) {
+ connRun = false;
+ });
+
+ LoopExecutionTimeController timer(std::chrono::milliseconds(50));
+ while (connRun) {
+ connListener.HandleAllEvents();
+ timer.Update();
+ }
+}
+
+void InitEvents() {
+ /*
+ * Network Events
+ */
+
+ listener.RegisterHandler("ConnectionSuccessfull", [](const Event&) {
+ nc = std::move(connNc);
+ gs = std::move(connGs);
+ });
listener.RegisterHandler("Disconnect", [](const Event& eventData) {
auto data = eventData.get<std::string>();
@@ -186,6 +212,8 @@ void RunGame() {
render = std::make_unique<Render>(900, 480, "AltCraft");
+ connThread = std::thread(ConnectionThreadExec);
+
SetState(State::MainMenu);
while (isRunning) {
@@ -212,6 +240,8 @@ void RunGame() {
}
render.reset();
+
+ connThread.join();
}
State GetState() {