From 4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Thu, 18 May 2017 19:03:05 +0500 Subject: 2017-05-18 --- src/gamestate/Game.cpp | 173 ++++++++++++++++++++++++++++++++++++++++++++ src/gamestate/Game.hpp | 58 +++++++++++++++ src/gamestate/GameState.cpp | 1 + src/gamestate/GameState.hpp | 8 ++ 4 files changed, 240 insertions(+) create mode 100644 src/gamestate/Game.cpp create mode 100644 src/gamestate/Game.hpp create mode 100644 src/gamestate/GameState.cpp create mode 100644 src/gamestate/GameState.hpp (limited to 'src/gamestate') diff --git a/src/gamestate/Game.cpp b/src/gamestate/Game.cpp new file mode 100644 index 0000000..15f83e6 --- /dev/null +++ b/src/gamestate/Game.cpp @@ -0,0 +1,173 @@ +#include "Game.hpp" +#include "../packet/PacketParser.hpp" +#include "../packet/PacketBuilder.hpp" +#include + +Game::Game() { + m_display = new Display(1280, 720, "AltCraft", &m_world); + m_nc = new NetworkClient("127.0.0.1", 25565, "HelloOne"); + Packet &response = *m_nc->GetPacket(); + if (response.GetId() != 0x02) { + std::cout << response.GetId() << std::endl; + throw 127; + } + PacketParser::Parse(response, Login); + g_PlayerUuid = response.GetField(0).GetString(); + g_PlayerName = response.GetField(1).GetString(); + delete &response; + m_networkState = ConnectionState::Play; + std::cout << g_PlayerName << "'s UUID is " << g_PlayerUuid << std::endl; +} + +Game::~Game() { + std::cout << "Stopping game thread..." << std::endl; + m_exit = true; + m_gameThread.join(); + std::cout << "Stopping graphics..." << std::endl; + delete m_display; + std::cout << "Stopping network..." << std::endl; + delete m_nc; +} + +void Game::MainLoop() { + while (!m_exit) { + + ParsePackets(); + if (m_display->IsClosed()) + m_exit = true; + } +} + +void Game::ParsePackets() { + Packet *packetPtr = m_nc->GetPacket(); + if (!packetPtr) { + using namespace std::chrono_literals; + std::this_thread::sleep_for(16ms); + return; + } + Packet packet = *packetPtr; + delete packetPtr; + PacketParser::Parse(packet); + nlohmann::json json; + //std::cout<<"Parsing packet: "<AddPacketToQueue(PacketBuilder::CPlay0x03(0)); + m_nc->AddPacketToQueue(PacketBuilder::CPlay0x00(packet.GetField(6).GetVarInt())); + std::cout << "Game is started! " << std::endl; + std::cout << "PlayerPos is " << g_PlayerX << ", " << g_PlayerY << ", " << g_PlayerZ << "\tAngle: " + << g_PlayerYaw + << "," << g_PlayerPitch << std::endl; + m_display->SetPlayerPos(g_PlayerX, g_PlayerY, g_PlayerZ); + gameStartWaiter.notify_all(); + break; + case 0x1A: + json = nlohmann::json::parse(packet.GetField(0).GetString()); + std::cout << "Disconnect reason: " << json["text"].get() << std::endl; + throw 119; + break; + case 0x20: + m_world.ParseChunkData(packet); + { + std::vectorvec; + for (auto &it:m_world.m_sections) { + for (auto& it2:vec){ + if (it2==it.first) + std::cout< delta = clock.now() - timeOfPreviousSendedPpalPacket; + if (delta.count() >= 50) { + m_nc->AddPacketToQueue( + PacketBuilder::CPlay0x0D(g_PlayerX, g_PlayerY, g_PlayerZ, g_PlayerYaw, g_PlayerPitch, true)); + timeOfPreviousSendedPpalPacket = clock.now(); + /*std::cout << "PlayerPos is " << g_PlayerX << "," << g_PlayerY << "," << g_PlayerZ << " " << g_PlayerYaw + << "," << g_PlayerPitch << std::endl;*/ + } + } +} + +void Game::Exec() { + m_gameThread = std::thread(&Game::MainLoop, this); + m_display->MainLoop(); +} diff --git a/src/gamestate/Game.hpp b/src/gamestate/Game.hpp new file mode 100644 index 0000000..adf9059 --- /dev/null +++ b/src/gamestate/Game.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include "../utility/Vector.hpp" +#include "../network/NetworkClient.hpp" +#include "../world/World.hpp" +#include "../graphics/Display.hpp" + +class Game { +public: + Game(); + + ~Game(); + + void Exec(); + +private: + //utility variables + NetworkClient *m_nc; + std::thread m_ncThread; + bool m_exit = false; + ConnectionState m_networkState = ConnectionState::Handshaking; + Display *m_display; + std::thread m_gameThread; + + //utility methods + void ParsePackets(); + void MainLoop(); + + //GameState update - condVars + std::condition_variable gameStartWaiter; + + //game state variables + World m_world; + + std::string g_PlayerUuid; + std::string g_PlayerName; + int g_PlayerEid; + byte g_Gamemode; + byte g_Difficulty; + int g_Dimension; + byte g_MaxPlayers; + std::string g_LevelType; + bool g_ReducedDebugInfo; + Vector g_SpawnPosition; + bool g_PlayerInvulnerable; + bool g_PlayerFlying; + bool g_PlayerAllowFlying; + bool g_PlayerCreativeMode; + int g_PlayerFlyingSpeed; + int g_PlayerFovModifier; + bool g_IsGameStarted = false; + float g_PlayerPitch; + float g_PlayerYaw; + double g_PlayerX; + double g_PlayerY; + double g_PlayerZ; +}; + diff --git a/src/gamestate/GameState.cpp b/src/gamestate/GameState.cpp new file mode 100644 index 0000000..7bcc2d5 --- /dev/null +++ b/src/gamestate/GameState.cpp @@ -0,0 +1 @@ +#include "GameState.hpp" diff --git a/src/gamestate/GameState.hpp b/src/gamestate/GameState.hpp new file mode 100644 index 0000000..e9c433d --- /dev/null +++ b/src/gamestate/GameState.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include "../world/World.hpp" + +class GameState { +public: + World world; +}; -- cgit v1.2.3