From c3472b8abd8185f392e6c2afb68a7411232396d6 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 27 Jan 2019 08:10:36 +0500 Subject: Added slow GameState sync --- src/GameState.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/GameState.cpp') diff --git a/src/GameState.cpp b/src/GameState.cpp index ace2488..df227b3 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -10,6 +10,8 @@ void GameState::Update(float deltaTime) { if (!gameStatus.isGameStarted) return; + std::lock_guard guard(accessMutex); + std::chrono::steady_clock clock; static auto timeOfPreviousSendedPacket(clock.now()); auto delta = clock.now() - timeOfPreviousSendedPacket; @@ -67,6 +69,7 @@ void GameState::Update(float deltaTime) { } void GameState::UpdatePacket(std::shared_ptr ptr) { + std::lock_guard guard(accessMutex); switch ((PacketNamePlayCB)ptr->GetPacketId()) { case SpawnObject: { auto packet = std::static_pointer_cast(ptr); @@ -487,6 +490,8 @@ void GameState::HandleMovement(GameState::MoveType direction, float deltaTime) { if (!gameStatus.isGameStarted) return; + std::lock_guard guard(accessMutex); + const double playerSpeed = 43; float velocity = playerSpeed * deltaTime; @@ -543,6 +548,8 @@ void GameState::HandleRotation(double yaw, double pitch) { if (!gameStatus.isGameStarted) return; + std::lock_guard guard(accessMutex); + double playerYaw = Entity::DecodeYaw(player->yaw); double playerPitch = Entity::DecodePitch(player->pitch); playerYaw += yaw; @@ -556,6 +563,8 @@ void GameState::HandleRotation(double yaw, double pitch) { } glm::mat4 GameState::GetViewMatrix() { + std::lock_guard guard(accessMutex); + double playerYaw = Entity::DecodeYaw(player->yaw); double playerPitch = Entity::DecodePitch(player->pitch); glm::vec3 front, right, worldUp, up; @@ -580,6 +589,8 @@ void GameState::StartDigging() { if (!selectionStatus.isBlockSelected) return; + std::lock_guard guard(accessMutex); + auto packetStart = std::make_shared(0, selectionStatus.selectedBlock, 1); auto packet = std::static_pointer_cast(packetStart); PUSH_EVENT("SendPacket", packet); @@ -588,6 +599,8 @@ void GameState::StartDigging() { } void GameState::FinishDigging() { + std::lock_guard guard(accessMutex); + auto packetFinish = std::make_shared(2, selectionStatus.selectedBlock, 1); auto packet = std::static_pointer_cast(packetFinish); PUSH_EVENT("SendPacket", packet); @@ -599,6 +612,8 @@ void GameState::FinishDigging() { // send_packet(packet_type=start_digging_packet) // remove_delayed_action(finish_digging) void GameState::CancelDigging() { + std::lock_guard guard(accessMutex); + auto packetCancel = std::make_shared(1, selectionStatus.selectedBlock, 1); auto packet = std::static_pointer_cast(packetCancel); PUSH_EVENT("SendPacket", packet); @@ -637,6 +652,8 @@ void GameState::PlaceBlock() { if (!selectionStatus.isBlockSelected) return; + std::lock_guard guard(accessMutex); + BlockFacing face = detectHitFace(selectionStatus.raycastHit, selectionStatus.selectedBlock); auto packetPlace = std::make_shared( selectionStatus.selectedBlock, (unsigned char)face, 0, 0, 0, 0); -- cgit v1.2.3