summaryrefslogtreecommitdiffstats
path: root/src/GameState.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-10-09 18:39:48 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-01-13 03:39:32 +0100
commita81bcb1942ddad5989115061338e72211443e633 (patch)
treecb1cc6feadb7dfb49f96cbc9b2a4f2bfb9ad8488 /src/GameState.cpp
parent2017-10-07 (diff)
downloadAltCraft-a81bcb1942ddad5989115061338e72211443e633.tar
AltCraft-a81bcb1942ddad5989115061338e72211443e633.tar.gz
AltCraft-a81bcb1942ddad5989115061338e72211443e633.tar.bz2
AltCraft-a81bcb1942ddad5989115061338e72211443e633.tar.lz
AltCraft-a81bcb1942ddad5989115061338e72211443e633.tar.xz
AltCraft-a81bcb1942ddad5989115061338e72211443e633.tar.zst
AltCraft-a81bcb1942ddad5989115061338e72211443e633.zip
Diffstat (limited to 'src/GameState.cpp')
-rw-r--r--src/GameState.cpp30
1 files changed, 18 insertions, 12 deletions
diff --git a/src/GameState.cpp b/src/GameState.cpp
index 08b29b4..3020bde 100644
--- a/src/GameState.cpp
+++ b/src/GameState.cpp
@@ -1,12 +1,7 @@
#include "GameState.hpp"
#include "Event.hpp"
#include <iomanip>
-
-GameState::GameState(std::shared_ptr<NetworkClient> networkClient) : nc(networkClient) {
- //Front = glm::vec3(0.0f, 0.0f, -1.0f);
- //this->SetPosition(glm::vec3(0.0f, 0.0f, 3.0f));
- //this->WorldUp = glm::vec3(0.0f, 1.0f, 0.0f);
-}
+#include "GlobalState.hpp"
void GameState::Update(float deltaTime) {
if (g_IsGameStarted) {
@@ -15,26 +10,38 @@ void GameState::Update(float deltaTime) {
auto delta = clock.now() - timeOfPreviousSendedPacket;
using namespace std::chrono_literals;
if (delta >= 50ms) {
+ packetsMutex.lock();
auto packetToSend = std::make_shared<PacketPlayerPositionAndLookSB>(player->pos.x, player->pos.y, player->pos.z, player->yaw, player->pitch, player->onGround);
- nc->SendPacket(packetToSend);
+ packets.push(packetToSend);
timeOfPreviousSendedPacket = clock.now();
+ packetsMutex.unlock();
}
bool prevOnGround = player->onGround;
world.UpdatePhysics(deltaTime);
if (player->onGround != prevOnGround) {
+ packetsMutex.lock();
auto updatePacket = std::make_shared<PacketPlayerPosition>(player->pos.x, player->pos.y, player->pos.z, player->onGround);
- nc->SendPacket(updatePacket);
+ packets.push(updatePacket);
+ packetsMutex.unlock();
}
}
}
-void GameState::UpdatePacket()
+void GameState::UpdatePacket(NetworkClient *nc)
{
+
+ packetsMutex.lock();
+ while (!packets.empty()) {
+ nc->SendPacket(packets.front());
+ packets.pop();
+ }
+ packetsMutex.unlock();
+
//Packet handling
auto ptr = nc->ReceivePacket();
- while (ptr) {
+ if (ptr) {
switch ((PacketNamePlayCB)ptr->GetPacketId()) {
case SpawnObject: {
auto packet = std::static_pointer_cast<PacketSpawnObject>(ptr);
@@ -202,7 +209,7 @@ void GameState::UpdatePacket()
g_ReducedDebugInfo = packet->ReducedDebugInfo;
LOG(INFO) << "Gamemode is " << g_Gamemode << ", Difficulty is " << (int)g_Difficulty
<< ", Level Type is " << g_LevelType;
- EventAgregator::PushEvent(EventType::PlayerConnected, PlayerConnectedData{ gs });
+ EventAgregator::PushEvent(EventType::PlayerConnected, PlayerConnectedData{});
break;
}
case Map:
@@ -393,7 +400,6 @@ void GameState::UpdatePacket()
case EntityEffect:
break;
}
- ptr = nc->ReceivePacket();
}
while (!playerInventory.pendingTransactions.empty()) {
nc->SendPacket(std::make_shared<PacketClickWindow>(playerInventory.pendingTransactions.front()));