summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-01-13 10:58:40 +0100
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-01-13 10:58:40 +0100
commitabe317e31bb9b9a88b0c88b1477a952240d1847f (patch)
treead9e706d4ecb3237b62a05bd3f408fdc87a7dcff
parentNew implementation of NC is using event-system (diff)
downloadAltCraft-abe317e31bb9b9a88b0c88b1477a952240d1847f.tar
AltCraft-abe317e31bb9b9a88b0c88b1477a952240d1847f.tar.gz
AltCraft-abe317e31bb9b9a88b0c88b1477a952240d1847f.tar.bz2
AltCraft-abe317e31bb9b9a88b0c88b1477a952240d1847f.tar.lz
AltCraft-abe317e31bb9b9a88b0c88b1477a952240d1847f.tar.xz
AltCraft-abe317e31bb9b9a88b0c88b1477a952240d1847f.tar.zst
AltCraft-abe317e31bb9b9a88b0c88b1477a952240d1847f.zip
Diffstat (limited to '')
-rw-r--r--src/GameState.cpp96
-rw-r--r--src/GameState.hpp4
-rw-r--r--src/GlobalState.cpp11
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<PacketPlayerPositionAndLookSB>(player->pos.x, player->pos.y, player->pos.z, player->yaw, player->pitch, player->onGround);
- packets.push(packetToSend);
+ auto packet = std::static_pointer_cast<Packet>(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<PacketPlayerPosition>(player->pos.x, player->pos.y, player->pos.z, player->onGround);
- packets.push(updatePacket);
- packetsMutex.unlock();
+ auto packet = std::static_pointer_cast<Packet>(updatePacket);
+ PUSH_EVENT("SendPacket",packet);
}
}
}
-void GameState::UpdatePacket(NetworkClient *nc)
+void GameState::UpdatePacket(std::shared_ptr<Packet> 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<PacketSpawnObject>(ptr);
Entity entity = CreateObject(static_cast<ObjectType>(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<PacketChatMessageCB>(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<PacketWindowItems>(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<PacketDisconnectPlay>(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<PacketEntityRelativeMove>(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<PacketEntityLookAndRelativeMove>(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<PacketEntityLook>(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<PacketPlayerPositionAndLookCB>(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<PacketTeleportConfirm>(packet->TeleportId);
auto packetPerformRespawn = std::make_shared<PacketClientStatus>(0);
- nc->SendPacket(packetResponse);
- nc->SendPacket(packetPerformRespawn);
+ PUSH_EVENT("SendPacket",std::static_pointer_cast<Packet>(packetResponse));
+ PUSH_EVENT("SendPacket",std::static_pointer_cast<Packet>(packetPerformRespawn));
break;
}
case UseBed:
@@ -347,7 +328,7 @@ void GameState::UpdatePacket(NetworkClient *nc)
break;
case EntityVelocity: {
auto packet = std::static_pointer_cast<PacketEntityVelocity>(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<PacketClientStatus>(0);
- nc->SendPacket(packetPerformRespawn);
+ PUSH_EVENT("SendPacket",std::static_pointer_cast<Packet>(packetPerformRespawn));
}
break;
}
@@ -376,7 +357,8 @@ void GameState::UpdatePacket(NetworkClient *nc)
case SpawnPosition: {
auto packet = std::static_pointer_cast<PacketSpawnPosition>(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<PacketEntityTeleport>(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<PacketClickWindow>(playerInventory.pendingTransactions.front()));
+ auto packet = std::make_shared<PacketClickWindow>(playerInventory.pendingTransactions.front());
playerInventory.pendingTransactions.pop();
+ PUSH_EVENT("SendPacket",std::static_pointer_cast<Packet>(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<std::shared_ptr<Packet>> packets;
public:
GameState() = default;
@@ -24,7 +22,7 @@ public:
void Update(float deltaTime);
- void UpdatePacket(NetworkClient *nc);
+ void UpdatePacket(std::shared_ptr<Packet> 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> packet = eventData.get<std::shared_ptr<Packet>>();
+ gs->UpdatePacket(packet);
+ });
+
/*
* GameState Events
*/
@@ -88,7 +93,8 @@ void InitEvents() {
listener.RegisterHandler("SendChatMessage", [](const Event& eventData) {
auto message = eventData.get<std::string>();
- nc->SendPacket(std::make_shared<PacketChatMessageSB>(message));
+ auto packet = std::static_pointer_cast<Packet>(std::make_shared<PacketChatMessageSB>(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) {