diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-07 17:08:15 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-08-07 17:08:15 +0200 |
commit | 8b275c0b0a1064a813ef14a109b64e1fce461893 (patch) | |
tree | 7bb1cc13d2eb9be2a344b4cd35f5e581df970f0d /src/ThreadGame.cpp | |
parent | 2017-08-05 (diff) | |
download | AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.gz AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.bz2 AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.lz AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.xz AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.tar.zst AltCraft-8b275c0b0a1064a813ef14a109b64e1fce461893.zip |
Diffstat (limited to 'src/ThreadGame.cpp')
-rw-r--r-- | src/ThreadGame.cpp | 75 |
1 files changed, 68 insertions, 7 deletions
diff --git a/src/ThreadGame.cpp b/src/ThreadGame.cpp index 03c2c14..7bb3678 100644 --- a/src/ThreadGame.cpp +++ b/src/ThreadGame.cpp @@ -13,22 +13,83 @@ void ThreadGame::Execute() { EventListener listener; - listener.RegisterHandler(EventType::GlobalAppState, [this](EventData eventData) { - auto data = std::get<GlobalAppStateData>(eventData); - state = data.state; - }); + listener.RegisterHandler(EventType::Exit, [this] (EventData eventData) { + isRunning = false; + }); listener.RegisterHandler(EventType::ConnectionSuccessfull, [this](EventData eventData) { auto data = std::get<ConnectionSuccessfullData>(eventData); gs = new GameState(data.ptr); }); + listener.RegisterHandler(EventType::Disconnected, [this](EventData eventData) { + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + delete gs; + gs = nullptr; + }); + + listener.RegisterHandler(EventType::KeyPressed, [this](EventData eventData) { + if (!gs) + return; + switch (std::get<KeyPressedData>(eventData).key) { + case sf::Keyboard::W: + isMoving[GameState::FORWARD] = true; + break; + case sf::Keyboard::A: + isMoving[GameState::LEFT] = true; + break; + case sf::Keyboard::S: + isMoving[GameState::BACKWARD] = true; + break; + case sf::Keyboard::D: + isMoving[GameState::RIGHT] = true; + break; + } + }); + + listener.RegisterHandler(EventType::KeyReleased, [this](EventData eventData) { + if (!gs) + return; + switch (std::get<KeyReleasedData>(eventData).key) { + case sf::Keyboard::W: + isMoving[GameState::FORWARD] = false; + break; + case sf::Keyboard::A: + isMoving[GameState::LEFT] = false; + break; + case sf::Keyboard::S: + isMoving[GameState::BACKWARD] = false; + break; + case sf::Keyboard::D: + isMoving[GameState::RIGHT] = false; + break; + } + }); + + listener.RegisterHandler(EventType::MouseMoved, [this](EventData eventData) { + if (!gs) + return; + auto data = std::get<MouseMovedData>(eventData); + gs->HandleRotation(data.x, data.y); + }); + LoopExecutionTimeController timer(std::chrono::milliseconds(int(1.0f / 60.0f * 1000.0f))); - while (state != GlobalState::Exiting) { + while (isRunning) { + if (gs != nullptr) + gs->Update(timer.GetDeltaS()); listener.HandleEvent(); - if (gs != nullptr) - gs->Update(timer.GetDeltaMs()); + if (gs != nullptr) { + gs->UpdatePacket(); + if (isMoving[GameState::FORWARD]) + gs->HandleMovement(GameState::FORWARD, timer.GetDeltaS()); + if (isMoving[GameState::BACKWARD]) + gs->HandleMovement(GameState::BACKWARD, timer.GetDeltaS()); + if (isMoving[GameState::LEFT]) + gs->HandleMovement(GameState::LEFT, timer.GetDeltaS()); + if (isMoving[GameState::RIGHT]) + gs->HandleMovement(GameState::RIGHT, timer.GetDeltaS()); + } timer.Update(); } delete gs; |