diff options
-rw-r--r-- | src/Entity.cpp | 8 | ||||
-rw-r--r-- | src/Entity.hpp | 14 | ||||
-rw-r--r-- | src/GameState.cpp | 23 | ||||
-rw-r--r-- | src/Network.cpp | 3 | ||||
-rw-r--r-- | src/Packet.hpp | 37 | ||||
-rw-r--r-- | src/Render.cpp | 14 | ||||
-rw-r--r-- | src/RendererSection.cpp | 6 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 11 | ||||
-rw-r--r-- | src/Section.cpp | 2 | ||||
-rw-r--r-- | src/Stream.cpp | 8 | ||||
-rw-r--r-- | src/Stream.hpp | 4 | ||||
-rw-r--r-- | src/ThreadGame.cpp | 26 | ||||
-rw-r--r-- | src/ThreadGame.hpp | 2 | ||||
-rw-r--r-- | src/Utility.cpp | 26 | ||||
-rw-r--r-- | src/Utility.hpp | 8 | ||||
-rw-r--r-- | src/Vector.hpp | 32 | ||||
-rw-r--r-- | src/World.cpp | 75 | ||||
-rw-r--r-- | src/World.hpp | 15 | ||||
-rw-r--r-- | src/main.cpp | 6 |
19 files changed, 232 insertions, 88 deletions
diff --git a/src/Entity.cpp b/src/Entity.cpp new file mode 100644 index 0000000..8f76818 --- /dev/null +++ b/src/Entity.cpp @@ -0,0 +1,8 @@ +#include "Entity.hpp" + +VectorF Entity::DecodeVelocity(short x, short y, short z) +{ + const float ticksPerSecond = 20; + const double velMod = 1 / 8000.0; + return VectorF(x * velMod * ticksPerSecond, y*velMod*ticksPerSecond, z*velMod*ticksPerSecond); +} diff --git a/src/Entity.hpp b/src/Entity.hpp new file mode 100644 index 0000000..6df7983 --- /dev/null +++ b/src/Entity.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include "Utility.hpp" +#include "Vector.hpp" + +struct Entity { + Uuid uuid; + VectorF pos; + VectorF vel; + unsigned int entityId; + double yaw; + double pitch; + static VectorF DecodeVelocity(short x, short y, short z); +};
\ No newline at end of file diff --git a/src/GameState.cpp b/src/GameState.cpp index 52b5f0f..18ea722 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -1,5 +1,6 @@ #include "GameState.hpp" #include "Event.hpp" +#include <iomanip> GameState::GameState(NetworkClient *networkClient) : nc(networkClient) { Front = glm::vec3(0.0f, 0.0f, -1.0f); @@ -51,6 +52,8 @@ void GameState::Update(float deltaTime) { vel += resistForce; g_PlayerVelocityX = vel.x; g_PlayerVelocityZ = vel.z; + + world.UpdatePhysics(deltaTime); } } @@ -60,8 +63,19 @@ void GameState::UpdatePacket() auto ptr = nc->ReceivePacket(); if (ptr) { switch ((PacketNamePlayCB)ptr->GetPacketId()) { - case SpawnObject: - break; + case SpawnObject: { + auto packet = std::static_pointer_cast<PacketSpawnObject>(ptr); + Entity entity; + entity.entityId = packet->EntityId; + entity.pitch = packet->Pitch; + entity.pos = VectorF(packet->X, packet->Y, packet->Z); + entity.uuid = packet->ObjectUuid; + entity.vel = Entity::DecodeVelocity(packet->VelocityX, packet->VelocityY, packet->VelocityZ); + entity.yaw = packet->Yaw; + if (entity.vel != VectorF()) + world.entities.push_back(entity); + break; + } case SpawnExperienceOrb: break; case SpawnGlobalEntity: @@ -160,7 +174,7 @@ void GameState::UpdatePacket() break; case EntityLook: break; - case Entity: + case EntityCB: break; case VehicleMove: break; @@ -282,8 +296,7 @@ void GameState::UpdatePacket() case SpawnPosition: { auto packet = std::static_pointer_cast<PacketSpawnPosition>(ptr); g_SpawnPosition = packet->Location; - LOG(INFO) << "Spawn position is " << g_SpawnPosition.GetX() << "," << g_SpawnPosition.GetY() << "," - << g_SpawnPosition.GetZ(); + LOG(INFO) << "Spawn position is " << g_SpawnPosition.x << " " << g_SpawnPosition.y << " " << g_SpawnPosition.z; break; } case TimeUpdate: diff --git a/src/Network.cpp b/src/Network.cpp index 62ad1f9..e70cce9 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -68,6 +68,7 @@ std::shared_ptr<Packet> Network::ReceivePacketByPacketId(int packetId, Connectio std::shared_ptr<Packet> Network::ParsePacketPlay(PacketNamePlayCB id) { switch (id) { case SpawnObject: + return std::make_shared<PacketSpawnObject>(); break; case SpawnExperienceOrb: break; @@ -147,7 +148,7 @@ std::shared_ptr<Packet> Network::ParsePacketPlay(PacketNamePlayCB id) { break; case EntityLook: break; - case Entity: + case EntityCB: break; case VehicleMove: break; diff --git a/src/Packet.hpp b/src/Packet.hpp index d615332..f48088d 100644 --- a/src/Packet.hpp +++ b/src/Packet.hpp @@ -94,7 +94,7 @@ enum PacketNamePlayCB { EntityRelativeMove, EntityLookAndRelativeMove, EntityLook, - Entity, + EntityCB, VehicleMove, OpenSignEditor, PlayerAbilitiesCB, @@ -518,4 +518,39 @@ struct PacketUpdateHealth : Packet { float Health; int Food; float FoodSaturation; +}; + +struct PacketSpawnObject : Packet { + void ToStream(StreamOutput *stream) override { + + } + + void FromStream(StreamInput *stream) override { + EntityId = stream->ReadVarInt(); + ObjectUuid = stream->ReadUuid(); + Type = stream->ReadByte(); + X = stream->ReadDouble(); + Y = stream->ReadDouble(); + Z = stream->ReadDouble(); + Pitch = stream->ReadAngle(); + Yaw = stream->ReadAngle(); + Data = stream->ReadInt(); + VelocityX = stream->ReadShort(); + VelocityY = stream->ReadShort(); + VelocityZ = stream->ReadShort(); + } + + int GetPacketId() override { + return PacketNamePlayCB::SpawnObject; + } + + int EntityId; + Uuid ObjectUuid; + unsigned char Type; + double X, Y, Z; + unsigned char Pitch, Yaw; + int Data; + short VelocityX; + short VelocityY; + short VelocityZ; };
\ No newline at end of file diff --git a/src/Render.cpp b/src/Render.cpp index 38a6ab0..aec60aa 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -125,6 +125,9 @@ void Render::HandleEvents() { case sf::Keyboard::D: EventAgregator::PushEvent(EventType::KeyPressed, KeyPressedData{ sf::Keyboard::D }); break; + case sf::Keyboard::Space: + EventAgregator::PushEvent(EventType::KeyPressed, KeyPressedData{ sf::Keyboard::Space }); + break; default: break; } @@ -143,6 +146,9 @@ void Render::HandleEvents() { case sf::Keyboard::D: EventAgregator::PushEvent(EventType::KeyReleased, KeyReleasedData{ sf::Keyboard::D }); break; + case sf::Keyboard::Space: + EventAgregator::PushEvent(EventType::KeyReleased, KeyReleasedData{ sf::Keyboard::Space }); + break; default: break; } @@ -158,7 +164,7 @@ void Render::HandleMouseCapture() { sf::Mouse::setPosition(center, *window); mouseXDelta = (mousePos - center).x, mouseYDelta = (center - mousePos).y; const float Sensetivity = 0.7f; - EventAgregator::DirectEventCall(EventType::MouseMoved, MouseMovedData{ mouseXDelta * Sensetivity, mouseYDelta }); + EventAgregator::DirectEventCall(EventType::MouseMoved, MouseMovedData{ mouseXDelta * Sensetivity, mouseYDelta * Sensetivity}); } void Render::SetMouseCapture(bool IsCaptured) { @@ -203,7 +209,7 @@ void Render::ExecuteRenderLoop() { window->setTitle("Connecting"); }); - LoopExecutionTimeController timer(std::chrono::milliseconds(16)); + LoopExecutionTimeController timer(std::chrono::milliseconds(32)); while (isRunning) { HandleEvents(); if (isMouseCaptured) HandleMouseCapture(); @@ -212,8 +218,8 @@ void Render::ExecuteRenderLoop() { RenderFrame(); while (listener.IsEventsQueueIsNotEmpty()) listener.HandleEvent(); - if (renderWorld) - window->setTitle("FPS: "+std::to_string(1.0/timer.GetDeltaMs()*1000.0)); + if (renderWorld) + window->setTitle("FPS: " + std::to_string(1.0 / timer.GetRealDeltaS())); timer.Update(); } EventAgregator::PushEvent(EventType::Exit, ExitData{}); diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index fbb9c19..439fab1 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -187,7 +187,7 @@ void RendererSection::PrepareResources() { for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { for (int x = 0; x < 16; x++) { - Vector blockPos = Vector(x, y, z) + (sectionPosition * 16); + Vector blockPos = Vector(x, y, z) + (sectionPosition * 16u); Block block = world->GetBlock(blockPos); if (block.id == 0) continue; @@ -214,9 +214,7 @@ void RendererSection::PrepareResources() { continue; glm::mat4 transform; - transform = glm::translate(transform, glm::vec3(sectionPosition.GetX() * 16, - sectionPosition.GetY() * 16, - sectionPosition.GetZ() * 16)); + transform = glm::translate(transform, glm::vec3 (sectionPosition * 16u)); transform = glm::translate(transform, glm::vec3(x, y, z)); glm::vec3 biomeColor(0.275, 0.63, 0.1); glm::vec3 color(0.0f, 0.0f, 0.0f); diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 2ec621a..7d965f6 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -7,7 +7,7 @@ void RendererWorld::LoadedSectionController() { std::vector<Vector> suitableChunks; for (auto& it : gs->world.GetSectionsList()) { - double distance = (Vector(it.GetX(),0,it.GetZ()) - playerChunk).GetMagnitude(); + double distance = (Vector(it.x, 0, it.z) - playerChunk).GetLength(); if (distance > MaxRenderingDistance) continue; suitableChunks.push_back(it); @@ -37,7 +37,7 @@ void RendererWorld::LoadedSectionController() { auto vec = std::get<ChunkChangedData>(eventData).chunkPosition; Vector playerChunk(std::floor(gs->g_PlayerX / 16), 0, std::floor(gs->g_PlayerZ / 16)); - if ((playerChunk - Vector(vec.GetX(), 0, vec.GetZ())).GetMagnitude() > MaxRenderingDistance) + if ((Vector(vec.x,0,vec.z) - playerChunk).GetLength() > MaxRenderingDistance) return; sectionsMutex.lock(); auto& result = sections.find(vec); @@ -130,14 +130,17 @@ void RendererWorld::Render(RenderState & renderState) { glm::mat4 projection = glm::perspective(45.0f, (float)renderState.WindowWidth / (float)renderState.WindowHeight, 0.1f, 10000000.0f); glm::mat4 view = gs->GetViewMatrix(); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); + glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); glUniform2f(windowSizeLoc, renderState.WindowWidth, renderState.WindowHeight); glCheckError(); sectionsMutex.lock(); - for (auto& it : sections) + for (auto& it : sections) { + it.second.Render(renderState); + } + sectionsMutex.unlock(); listener.HandleEvent(); diff --git a/src/Section.cpp b/src/Section.cpp index 8b86afd..b2d9ac9 100644 --- a/src/Section.cpp +++ b/src/Section.cpp @@ -33,7 +33,7 @@ Section::~Section() { } Block &Section::GetBlock(Vector pos) { - return m_blocks[pos.GetY() * 256 + pos.GetZ() * 16 + pos.GetX()]; + return m_blocks[pos.y * 256 + pos.z * 16 + pos.x]; } double totalParsingTime = 0; diff --git a/src/Stream.cpp b/src/Stream.cpp index 3931a40..d7b1072 100644 --- a/src/Stream.cpp +++ b/src/Stream.cpp @@ -150,11 +150,11 @@ unsigned char StreamInput::ReadAngle() { return ReadUByte(); } -std::vector<unsigned char> StreamInput::ReadUuid() { +Uuid StreamInput::ReadUuid() { unsigned char buff[16]; ReadData(buff, 16); endswap(buff, 16); - return std::vector<unsigned char>(buff, buff + 16); + return Uuid(buff,buff+16); } std::vector<unsigned char> StreamInput::ReadByteArray(size_t arrLength) { @@ -264,14 +264,14 @@ void StreamOutput::WriteNbtTag(std::vector<unsigned char> value) { } void StreamOutput::WritePosition(Vector value) { - LOG(FATAL) << "Used unimplemented Position: " << value.GetX() << ", " << value.GetY() << " " << value.GetZ(); + LOG(FATAL) << "Used unimplemented Position: " << value.x << ", " << value.y << " " << value.z; } void StreamOutput::WriteAngle(unsigned char value) { WriteUByte(value); } -void StreamOutput::WriteUuid(std::vector<unsigned char> value) { +void StreamOutput::WriteUuid(Uuid value) { WriteByteArray(value); } diff --git a/src/Stream.hpp b/src/Stream.hpp index b856d5f..012addf 100644 --- a/src/Stream.hpp +++ b/src/Stream.hpp @@ -40,7 +40,7 @@ public: std::vector<unsigned char> ReadNbtTag(); Vector ReadPosition(); unsigned char ReadAngle(); - std::vector<unsigned char> ReadUuid(); + Uuid ReadUuid(); std::vector<unsigned char> ReadByteArray(size_t arrLength); }; @@ -66,7 +66,7 @@ public: void WriteNbtTag(std::vector<unsigned char> value); void WritePosition(Vector value); void WriteAngle(unsigned char value); - void WriteUuid(std::vector<unsigned char> value); + void WriteUuid(Uuid value); void WriteByteArray(std::vector<unsigned char> value); }; diff --git a/src/ThreadGame.cpp b/src/ThreadGame.cpp index 7bb3678..74802bb 100644 --- a/src/ThreadGame.cpp +++ b/src/ThreadGame.cpp @@ -39,11 +39,14 @@ void ThreadGame::Execute() { isMoving[GameState::LEFT] = true; break; case sf::Keyboard::S: - isMoving[GameState::BACKWARD] = true; - break; + isMoving[GameState::BACKWARD] = true; + break; case sf::Keyboard::D: - isMoving[GameState::RIGHT] = true; - break; + isMoving[GameState::RIGHT] = true; + break; + case sf::Keyboard::Space: + isMoving[GameState::JUMP] = true; + break; } }); @@ -63,6 +66,9 @@ void ThreadGame::Execute() { case sf::Keyboard::D: isMoving[GameState::RIGHT] = false; break; + case sf::Keyboard::Space: + isMoving[GameState::JUMP] = false; + break; } }); @@ -77,18 +83,20 @@ void ThreadGame::Execute() { while (isRunning) { if (gs != nullptr) - gs->Update(timer.GetDeltaS()); + gs->Update(timer.GetRealDeltaS()); listener.HandleEvent(); if (gs != nullptr) { gs->UpdatePacket(); if (isMoving[GameState::FORWARD]) - gs->HandleMovement(GameState::FORWARD, timer.GetDeltaS()); + gs->HandleMovement(GameState::FORWARD, timer.GetRealDeltaS()); if (isMoving[GameState::BACKWARD]) - gs->HandleMovement(GameState::BACKWARD, timer.GetDeltaS()); + gs->HandleMovement(GameState::BACKWARD, timer.GetRealDeltaS()); if (isMoving[GameState::LEFT]) - gs->HandleMovement(GameState::LEFT, timer.GetDeltaS()); + gs->HandleMovement(GameState::LEFT, timer.GetRealDeltaS()); if (isMoving[GameState::RIGHT]) - gs->HandleMovement(GameState::RIGHT, timer.GetDeltaS()); + gs->HandleMovement(GameState::RIGHT, timer.GetRealDeltaS()); + if (isMoving[GameState::JUMP]) + gs->HandleMovement(GameState::JUMP, timer.GetRealDeltaS()); } timer.Update(); } diff --git a/src/ThreadGame.hpp b/src/ThreadGame.hpp index 1254183..9894235 100644 --- a/src/ThreadGame.hpp +++ b/src/ThreadGame.hpp @@ -7,7 +7,7 @@ class ThreadGame: Thread { GameState *gs = nullptr; bool isRunning = true; - bool isMoving[4] = { 0,0,0,0 }; + bool isMoving[5] = { 0,0,0,0,0 }; public: ThreadGame(); ~ThreadGame(); diff --git a/src/Utility.cpp b/src/Utility.cpp index b4e1d99..ebb6981 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -55,27 +55,29 @@ unsigned long long LoopExecutionTimeController::GetIterations() { void LoopExecutionTimeController::Update() { iterations++; - auto timeToSleep = delayLength - GetDelta(); - if (timeToSleep.count()>0) - std::this_thread::sleep_for(timeToSleep); - previousUpdate = clock::now(); + auto timeToSleep = delayLength - GetDelta(); + if (timeToSleep.count() > 0) + std::this_thread::sleep_for(timeToSleep); + previousPreviousUpdate = previousUpdate; + previousUpdate = clock::now(); } double LoopExecutionTimeController::GetDeltaMs() { - auto now = clock::now(); - return duration(now-previousUpdate).count(); + std::chrono::duration<double, std::milli> delta = GetDelta(); + return delta.count(); } LoopExecutionTimeController::duration LoopExecutionTimeController::GetDelta() { auto now = clock::now(); - //std::cerr<<duration(now-previousUpdate).count()<<std::endl; return duration(now-previousUpdate); } -double LoopExecutionTimeController::GetDeltaS() -{ - auto now = clock::now(); - return std::chrono::duration<double, std::ratio<1, 1>>(now - previousUpdate).count(); +double LoopExecutionTimeController::GetDeltaS() { + std::chrono::duration<double, std::ratio<1,1>> delta = GetDelta(); + return delta.count(); } - +double LoopExecutionTimeController::GetRealDeltaS() +{ + return std::chrono::duration<double,std::ratio<1,1>>(previousUpdate - previousPreviousUpdate).count(); +} diff --git a/src/Utility.hpp b/src/Utility.hpp index 0ae3540..c7ec358 100644 --- a/src/Utility.hpp +++ b/src/Utility.hpp @@ -7,6 +7,10 @@ #include <easylogging++.h> #include <GL/glew.h> +//using Uuid = std::array<unsigned char, 16>; +//using Uuid = unsigned char[16]; +using Uuid = std::vector<uint8_t>; + template<class T> void endswap(T *objp) { unsigned char *memp = reinterpret_cast<unsigned char *>(objp); @@ -27,12 +31,12 @@ GLenum glCheckError_(const char *file, int line); #define glCheckError() glCheckError_(__FILE__, __LINE__) - class LoopExecutionTimeController { using clock = std::chrono::steady_clock ; using timePoint = std::chrono::time_point<clock>; using duration = std::chrono::duration<double,std::milli>; timePoint previousUpdate; + timePoint previousPreviousUpdate; duration delayLength; unsigned long long iterations=0; public: @@ -51,4 +55,6 @@ public: duration GetDelta(); double GetDeltaS(); + + double GetRealDeltaS(); }; diff --git a/src/Vector.hpp b/src/Vector.hpp index a2d5c6a..c89154f 100644 --- a/src/Vector.hpp +++ b/src/Vector.hpp @@ -7,40 +7,32 @@ #include <glm/vec3.hpp> template<class T> -class Vector3 { +struct Vector3 { T x, y, z; -public: + Vector3(T X = 0, T Y = 0, T Z = 0) : x(X), y(Y), z(Z) {} Vector3(const Vector3 &rhs) : x(rhs.x), y(rhs.y), z(rhs.z) {} ~Vector3() = default; - void SetX(T X) { x = X; } - - void SetY(T Y) { y = Y; } - - void SetZ(T Z) { z = Z; } - - T GetX() const { return x; } - - T GetY() const { return y; } - - T GetZ() const { return z; } - - double GetMagnitude() const { return std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2)); } + double GetLength() const { return std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2)); } operator glm::vec3() const { return glm::vec3(x, y, z); } - void swap(Vector3 &rhs) { + glm::vec3 glm() const { + return (glm::vec3)(*this); + } + + void swap(Vector3 &rhs) noexcept { std::swap(x, rhs.x); std::swap(y, rhs.y); std::swap(z, rhs.z); } - Vector3 &operator=(Vector3 rhs) { + Vector3 &operator=(Vector3 rhs) noexcept { rhs.swap(*this); return *this; } @@ -104,7 +96,7 @@ public: bool operator<(const Vector3 &rhs) const { return std::tie(x, y, z) < std::tie(rhs.x, rhs.y, rhs.z); } - + friend std::ostream &operator<<(std::ostream &os, const Vector3 &vector3) { os << vector3.x << ", " << vector3.y << ", " << vector3.z; @@ -112,5 +104,5 @@ public: } }; -typedef Vector3<double> VectorF; -typedef Vector3<signed long long> Vector;
\ No newline at end of file +using VectorF = Vector3<double>; +using Vector = Vector3<signed long long>;
\ No newline at end of file diff --git a/src/World.cpp b/src/World.cpp index 723b006..17f9cd2 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -2,13 +2,16 @@ #include "Event.hpp" void World::ParseChunkData(std::shared_ptr<PacketChunkData> packet) { - StreamBuffer chunkData(packet->Data.data(), packet->Data.size()); + StreamBuffer chunkData(packet->Data.data(), packet->Data.size()); std::bitset<16> bitmask(packet->PrimaryBitMask); for (int i = 0; i < 16; i++) { if (bitmask[i]) { Vector chunkPosition = Vector(packet->ChunkX, i, packet->ChunkZ); Section section = ParseSection(&chunkData, chunkPosition); - section.Parse(); + parseMutex.lock(); + toParse.push(section); + parseMutex.unlock(); + /*section.Parse(); sectionMutexes[chunkPosition].lock(); auto it = sections.find(chunkPosition); if (it == sections.end()) { @@ -19,7 +22,7 @@ void World::ParseChunkData(std::shared_ptr<PacketChunkData> packet) { } sectionMutexes[chunkPosition].unlock(); - EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ chunkPosition }); + EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ chunkPosition });*/ } } } @@ -41,23 +44,56 @@ Section World::ParseSection(StreamInput *data, Vector position) { (skyLight.size() > 0 ? skyLight.data() : nullptr), bitsPerBlock, palette); } +void World::ParserFunc() +{ + LoopExecutionTimeController timer(std::chrono::milliseconds(32)); + while (isRunning) { + parseMutex.lock(); + while (toParse.size() > 0) { + Section section = toParse.front(); + toParse.pop(); + parseMutex.unlock(); + + section.Parse(); + sectionMutexes[section.GetPosition()].lock(); + auto it = sections.find(section.GetPosition()); + if (it == sections.end()) { + sections.insert(std::make_pair(section.GetPosition(), section)); + } + else { + using std::swap; + swap(it->second, section); + } + sectionMutexes[section.GetPosition()].unlock(); + + EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ section.GetPosition() }); + + parseMutex.lock(); + } + parseMutex.unlock(); + timer.Update(); + } +} + World::~World() { + isRunning = false; + parser.join(); } World::World() { - + parser = std::thread(&World::ParserFunc, this); } bool World::isPlayerCollides(double X, double Y, double Z) { Vector PlayerChunk(floor(X / 16.0), floor(Y / 16.0), floor(Z / 16.0)); std::vector<Vector> closestSectionsCoordinates = { - Vector(PlayerChunk.GetX(), PlayerChunk.GetY(), PlayerChunk.GetZ()), - Vector(PlayerChunk.GetX() + 1, PlayerChunk.GetY(), PlayerChunk.GetZ()), - Vector(PlayerChunk.GetX() - 1, PlayerChunk.GetY(), PlayerChunk.GetZ()), - Vector(PlayerChunk.GetX(), PlayerChunk.GetY() + 1, PlayerChunk.GetZ()), - Vector(PlayerChunk.GetX(), PlayerChunk.GetY() - 1, PlayerChunk.GetZ()), - Vector(PlayerChunk.GetX(), PlayerChunk.GetY(), PlayerChunk.GetZ() + 1), - Vector(PlayerChunk.GetX(), PlayerChunk.GetY(), PlayerChunk.GetZ() - 1), + Vector(PlayerChunk.x, PlayerChunk.y, PlayerChunk.z), + Vector(PlayerChunk.x + 1, PlayerChunk.y, PlayerChunk.z), + Vector(PlayerChunk.x - 1, PlayerChunk.y, PlayerChunk.z), + Vector(PlayerChunk.x, PlayerChunk.y + 1, PlayerChunk.z), + Vector(PlayerChunk.x, PlayerChunk.y - 1, PlayerChunk.z), + Vector(PlayerChunk.x, PlayerChunk.y, PlayerChunk.z + 1), + Vector(PlayerChunk.x, PlayerChunk.y, PlayerChunk.z - 1), }; std::vector<std::map<Vector, Section>::iterator> closestSections; for (auto &coord:closestSectionsCoordinates) { @@ -88,9 +124,9 @@ bool World::isPlayerCollides(double X, double Y, double Z) { Block block = it->second.GetBlock(Vector(x, y, z)); if (block.id == 0 || block.id == 31) continue; - AABB blockColl{(x + it->first.GetX() * 16.0), - (y + it->first.GetY() * 16.0), - (z + it->first.GetZ() * 16.0), 1, 1, 1}; + AABB blockColl{(x + it->first.x * 16.0), + (y + it->first.y * 16.0), + (z + it->first.z * 16.0), 1, 1, 1}; if (TestCollision(playerColl, blockColl)) return true; } @@ -101,8 +137,8 @@ bool World::isPlayerCollides(double X, double Y, double Z) { } Block &World::GetBlock(Vector pos) { - Vector sectionPos (floor(pos.GetX() / 16.0f),floor(pos.GetY() / 16.0f),floor(pos.GetZ()/16.0f)); - Vector inSectionPos = pos - (sectionPos * 16); + Vector sectionPos (floor(pos.x / 16.0f),floor(pos.y / 16.0f),floor(pos.z/16.0f)); + Vector inSectionPos = pos - (sectionPos * 16u); if (sections.find(sectionPos)==sections.end()){ static Block block(0,0); return block; @@ -130,3 +166,10 @@ Section &World::GetSection(Vector sectionPos) { glm::vec3 World::Raycast(glm::vec3 position, glm::vec3 direction, float maxLength, float minPrecision) { return glm::vec3(position * direction / maxLength * minPrecision); } + +void World::UpdatePhysics(float delta) +{ + for (auto& it : entities) { + it.pos = it.pos + it.vel * delta; + } +} diff --git a/src/World.hpp b/src/World.hpp index 805084c..2415610 100644 --- a/src/World.hpp +++ b/src/World.hpp @@ -2,9 +2,12 @@ #include <map> #include <bitset> +#include <queue> #include <easylogging++.h> + +#include "Entity.hpp" #include "Block.hpp" #include "Section.hpp" #include "Packet.hpp" @@ -17,6 +20,14 @@ class World { int dimension = 0; Section ParseSection(StreamInput *data, Vector position); + + void ParserFunc(); + + std::queue<Section> toParse; + std::mutex parseMutex; + + bool isRunning = true; + std::thread parser; public: World(); @@ -34,4 +45,8 @@ public: Section &GetSection(Vector sectionPos); glm::vec3 Raycast(glm::vec3 position, glm::vec3 direction, float maxLength = 1000.0f, float minPrecision = 0.01f); + + std::vector<Entity> entities; + + void UpdatePhysics(float delta); };
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7ff88e0..2500fb2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,12 +32,12 @@ int main() { LOG(WARNING) << "Sizeof EventData is " << sizeof(EventData); ThreadGame game; - ThreadNetwork network; - ThreadRender render; - std::thread threadGame(&ThreadGame::Execute, game); + + ThreadNetwork network; std::thread threadNetwork(&ThreadNetwork::Execute, network); + ThreadRender render; render.Execute(); threadGame.join(); |