From abe317e31bb9b9a88b0c88b1477a952240d1847f Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 13 Jan 2018 14:58:40 +0500 Subject: Replaced all usages of NC to new implementation --- src/GameState.cpp | 96 ++++++++++++++++++++++------------------------------- src/GameState.hpp | 4 +-- src/GlobalState.cpp | 11 +++--- 3 files changed, 47 insertions(+), 64 deletions(-) diff --git a/src/GameState.cpp b/src/GameState.cpp index f5eb8d4..a2f7d87 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -5,48 +5,34 @@ #include "Event.hpp" #include "Packet.hpp" -#include "NetworkClient.hpp" void GameState::Update(float deltaTime) { if (g_IsGameStarted) { std::chrono::steady_clock clock; static auto timeOfPreviousSendedPacket(clock.now()); - auto delta = clock.now() - timeOfPreviousSendedPacket; + auto delta = clock.now() - timeOfPreviousSendedPacket; using namespace std::chrono_literals; if (delta >= 50ms) { - packetsMutex.lock(); auto packetToSend = std::make_shared(player->pos.x, player->pos.y, player->pos.z, player->yaw, player->pitch, player->onGround); - packets.push(packetToSend); + auto packet = std::static_pointer_cast(packetToSend); + PUSH_EVENT("SendPacket",packet); timeOfPreviousSendedPacket = clock.now(); - packetsMutex.unlock(); } bool prevOnGround = player->onGround; world.UpdatePhysics(deltaTime); if (player->onGround != prevOnGround) { - packetsMutex.lock(); auto updatePacket = std::make_shared(player->pos.x, player->pos.y, player->pos.z, player->onGround); - packets.push(updatePacket); - packetsMutex.unlock(); + auto packet = std::static_pointer_cast(updatePacket); + PUSH_EVENT("SendPacket",packet); } } } -void GameState::UpdatePacket(NetworkClient *nc) +void GameState::UpdatePacket(std::shared_ptr ptr) { - - packetsMutex.lock(); - while (!packets.empty()) { - nc->SendPacket(packets.front()); - packets.pop(); - } - packetsMutex.unlock(); - - //Packet handling - auto ptr = nc->ReceivePacket(); - if (ptr) { - switch ((PacketNamePlayCB)ptr->GetPacketId()) { + switch ((PacketNamePlayCB)ptr->GetPacketId()) { case SpawnObject: { auto packet = std::static_pointer_cast(ptr); Entity entity = CreateObject(static_cast(packet->Type)); @@ -56,9 +42,9 @@ void GameState::UpdatePacket(NetworkClient *nc) entity.vel = Entity::DecodeVelocity(packet->VelocityX, packet->VelocityY, packet->VelocityZ); entity.yaw = packet->Yaw / 256.0; entity.pitch = packet->Pitch / 256.0; - entity.renderColor = glm::vec3(0,1,0); + entity.renderColor = glm::vec3(0, 1, 0); world.AddEntity(entity); - PUSH_EVENT("EntityChanged", entity.entityId); + PUSH_EVENT("EntityChanged", entity.entityId); break; } case SpawnExperienceOrb: @@ -74,9 +60,9 @@ void GameState::UpdatePacket(NetworkClient *nc) entity.vel = Entity::DecodeVelocity(packet->VelocityX, packet->VelocityY, packet->VelocityZ); entity.yaw = packet->Yaw / 256.0; entity.pitch = packet->Pitch / 256.0; - entity.renderColor = glm::vec3(0,0,1); + entity.renderColor = glm::vec3(0, 0, 1); world.AddEntity(entity); - PUSH_EVENT("EntityChanged", entity.entityId); + PUSH_EVENT("EntityChanged", entity.entityId); break; } case SpawnPainting: @@ -93,7 +79,7 @@ void GameState::UpdatePacket(NetworkClient *nc) entity.height = 1.8; entity.width = 0.6; world.AddEntity(entity); - PUSH_EVENT("EntityChanged", entity.entityId); + PUSH_EVENT("EntityChanged", entity.entityId); break; } case AnimationCB: @@ -120,7 +106,7 @@ void GameState::UpdatePacket(NetworkClient *nc) case ChatMessageCB: { auto packet = std::static_pointer_cast(ptr); LOG(INFO) << "Message (" << int(packet->Position) << "): " << packet->JsonData.text; - PUSH_EVENT("ChatMessageReceived", std::make_tuple(packet->JsonData, packet->Position)); + PUSH_EVENT("ChatMessageReceived", std::make_tuple(packet->JsonData, packet->Position)); break; } case MultiBlockChange: { @@ -134,7 +120,7 @@ void GameState::UpdatePacket(NetworkClient *nc) try { playerInventory.ConfirmTransaction(*packet); } catch (std::exception &e) { - PUSH_EVENT("Disconnected", std::string("Transaction failed")); + PUSH_EVENT("Disconnected", std::string("Transaction failed")); } } break; @@ -151,7 +137,7 @@ void GameState::UpdatePacket(NetworkClient *nc) auto packet = std::static_pointer_cast(ptr); if (packet->WindowId == 0) { playerInventory.WindowId = 0; - playerInventory.slots = packet->SlotData; + playerInventory.slots = packet->SlotData; } break; } @@ -173,7 +159,7 @@ void GameState::UpdatePacket(NetworkClient *nc) case DisconnectPlay: { auto packet = std::static_pointer_cast(ptr); LOG(INFO) << "Disconnect reason: " << packet->Reason; - PUSH_EVENT("Disconnected", packet->Reason); + PUSH_EVENT("Disconnected", packet->Reason); break; } case EntityStatus: @@ -215,16 +201,16 @@ void GameState::UpdatePacket(NetworkClient *nc) g_MaxPlayers = packet->MaxPlayers; g_LevelType = packet->LevelType; g_ReducedDebugInfo = packet->ReducedDebugInfo; - LOG(INFO) << "Gamemode is " << g_Gamemode << ", Difficulty is " << (int)g_Difficulty - << ", Level Type is " << g_LevelType; - PUSH_EVENT("PlayerConnected", 0); + LOG(INFO) << "Gamemode is " << g_Gamemode << ", Difficulty is " << (int) g_Difficulty + << ", Level Type is " << g_LevelType; + PUSH_EVENT("PlayerConnected", 0); break; } case Map: break; case EntityRelativeMove: { auto packet = std::static_pointer_cast(ptr); - Entity& entity = world.GetEntity(packet->EntityId); + Entity &entity = world.GetEntity(packet->EntityId); entity.pos = entity.pos + Entity::DecodeDeltaPos(packet->DeltaX, packet->DeltaY, packet->DeltaZ); if (entity.entityId != 0) LOG(INFO) << "M: " << packet->EntityId; @@ -232,15 +218,15 @@ void GameState::UpdatePacket(NetworkClient *nc) } case EntityLookAndRelativeMove: { auto packet = std::static_pointer_cast(ptr); - Entity& entity = world.GetEntity(packet->EntityId); + Entity &entity = world.GetEntity(packet->EntityId); entity.pos = entity.pos + Entity::DecodeDeltaPos(packet->DeltaX, packet->DeltaY, packet->DeltaZ); entity.pitch = packet->Pitch / 256.0; - entity.yaw = packet->Yaw / 256.0; + entity.yaw = packet->Yaw / 256.0; break; } case EntityLook: { auto packet = std::static_pointer_cast(ptr); - Entity& entity = world.GetEntity(packet->EntityId); + Entity &entity = world.GetEntity(packet->EntityId); entity.pitch = packet->Pitch / 256.0; entity.yaw = packet->Yaw / 256.0; //LOG(INFO) << "L: " << packet->EntityId; @@ -262,45 +248,40 @@ void GameState::UpdatePacket(NetworkClient *nc) auto packet = std::static_pointer_cast(ptr); if ((packet->Flags & 0x10) != 0) { player->pitch += packet->Pitch; - } - else { + } else { player->pitch = packet->Pitch; }; if ((packet->Flags & 0x08) != 0) { player->yaw += packet->Yaw; - } - else { + } else { player->yaw = packet->Yaw; } if ((packet->Flags & 0x01) != 0) { player->pos.x += packet->X; - } - else { + } else { player->pos.x = packet->X; } if ((packet->Flags & 0x02) != 0) { player->pos.y += packet->Y; - } - else { + } else { player->pos.y = packet->Y; } if ((packet->Flags & 0x04) != 0) { player->pos.z += packet->Z; - } - else { + } else { player->pos.z = packet->Z; } - PUSH_EVENT("PlayerPosChanged", player->pos); + PUSH_EVENT("PlayerPosChanged", player->pos); LOG(INFO) << "PlayerPos is " << player->pos << "\t\tAngle: " << player->yaw << "," << player->pitch;; if (!g_IsGameStarted) { LOG(INFO) << "Game is started"; - PUSH_EVENT("RemoveLoadingScreen",0); + PUSH_EVENT("RemoveLoadingScreen", 0); } g_IsGameStarted = true; @@ -308,8 +289,8 @@ void GameState::UpdatePacket(NetworkClient *nc) auto packetResponse = std::make_shared(packet->TeleportId); auto packetPerformRespawn = std::make_shared(0); - nc->SendPacket(packetResponse); - nc->SendPacket(packetPerformRespawn); + PUSH_EVENT("SendPacket",std::static_pointer_cast(packetResponse)); + PUSH_EVENT("SendPacket",std::static_pointer_cast(packetPerformRespawn)); break; } case UseBed: @@ -347,7 +328,7 @@ void GameState::UpdatePacket(NetworkClient *nc) break; case EntityVelocity: { auto packet = std::static_pointer_cast(ptr); - Entity& entity = world.GetEntity(packet->EntityId); + Entity &entity = world.GetEntity(packet->EntityId); entity.vel = Entity::DecodeVelocity(packet->VelocityX, packet->VelocityY, packet->VelocityZ); break; } @@ -361,7 +342,7 @@ void GameState::UpdatePacket(NetworkClient *nc) if (g_PlayerHealth <= 0) { LOG(INFO) << "Player is dead. Respawning..."; auto packetPerformRespawn = std::make_shared(0); - nc->SendPacket(packetPerformRespawn); + PUSH_EVENT("SendPacket",std::static_pointer_cast(packetPerformRespawn)); } break; } @@ -376,7 +357,8 @@ void GameState::UpdatePacket(NetworkClient *nc) case SpawnPosition: { auto packet = std::static_pointer_cast(ptr); g_SpawnPosition = packet->Location; - LOG(INFO) << "Spawn position is " << g_SpawnPosition.x << " " << g_SpawnPosition.y << " " << g_SpawnPosition.z; + LOG(INFO) << "Spawn position is " << g_SpawnPosition.x << " " << g_SpawnPosition.y << " " + << g_SpawnPosition.z; break; } case TimeUpdate: { @@ -395,7 +377,7 @@ void GameState::UpdatePacket(NetworkClient *nc) break; case EntityTeleport: { auto packet = std::static_pointer_cast(ptr); - Entity& entity = world.GetEntity(packet->EntityId); + Entity &entity = world.GetEntity(packet->EntityId); entity.pos = VectorF(packet->X, packet->Y, packet->Z); entity.pitch = packet->Pitch / 256.0; entity.yaw = packet->Yaw / 256.0; @@ -407,11 +389,11 @@ void GameState::UpdatePacket(NetworkClient *nc) break; case EntityEffect: break; - } } while (!playerInventory.pendingTransactions.empty()) { - nc->SendPacket(std::make_shared(playerInventory.pendingTransactions.front())); + auto packet = std::make_shared(playerInventory.pendingTransactions.front()); playerInventory.pendingTransactions.pop(); + PUSH_EVENT("SendPacket",std::static_pointer_cast(packet)); } } diff --git a/src/GameState.hpp b/src/GameState.hpp index 4894628..7145df5 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -14,8 +14,6 @@ class NetworkClient; class Entity; class GameState { - std::mutex packetsMutex; - std::queue> packets; public: GameState() = default; @@ -24,7 +22,7 @@ public: void Update(float deltaTime); - void UpdatePacket(NetworkClient *nc); + void UpdatePacket(std::shared_ptr ptr); enum Direction { FORWARD, BACKWARD, LEFT, RIGHT, JUMP diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp index f04bb35..57b42ea 100644 --- a/src/GlobalState.cpp +++ b/src/GlobalState.cpp @@ -64,6 +64,11 @@ void InitEvents() { PUSH_EVENT("Disconnect", data); }); + listener.RegisterHandler("ReceivedPacket", [](const Event& eventData) { + std::shared_ptr packet = eventData.get>(); + gs->UpdatePacket(packet); + }); + /* * GameState Events */ @@ -88,7 +93,8 @@ void InitEvents() { listener.RegisterHandler("SendChatMessage", [](const Event& eventData) { auto message = eventData.get(); - nc->SendPacket(std::make_shared(message)); + auto packet = std::static_pointer_cast(std::make_shared(message)); + PUSH_EVENT("SendPacket",packet); }); } @@ -183,9 +189,6 @@ void GsExec() { while (isRunning) { try { while (nc && gs) { - nc->UpdatePacket(); - - gs->UpdatePacket(nc.get()); listener.HandleAllEvents(); } } catch (std::exception &e) { -- cgit v1.2.3