From a33df10e04b26bcc81e1910d2b3bacb0b0e0dcb4 Mon Sep 17 00:00:00 2001 From: Elisey Puzko Date: Sun, 18 Feb 2018 14:18:48 +0300 Subject: Another attempt at increasing readability --- src/World.cpp | 62 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 26 deletions(-) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index 4678964..f593bce 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -21,13 +21,13 @@ void World::ParseChunkData(std::shared_ptr packet) { if (!sections.insert(std::make_pair(chunkPosition, std::make_unique
(section))).second) { LOG(ERROR) << "New chunk not created " << chunkPosition << " potential memory leak"; } + UpdateSectionsList(); + } else { + std::swap(*sections.at(chunkPosition).get(), section); } - else { - using std::swap; - swap(*sections.at(chunkPosition).get(), section); - } - PUSH_EVENT("ChunkChanged", chunkPosition); + + PUSH_EVENT("ChunkChanged", chunkPosition); } } } @@ -53,7 +53,9 @@ Section World::ParseSection(StreamInput *data, Vector position) { std::vector blockArray(blockData, blockData + dataArray.size() / sizeof(long long)); - return Section(position, bitsPerBlock, std::move(palette), std::move(blockArray), std::move(blockLight), std::move(skyLight)); + return Section( + position, bitsPerBlock, std::move(palette),std::move(blockArray), + std::move(blockLight), std::move(skyLight)); } World::~World() { @@ -64,7 +66,8 @@ World::World() { bool World::isPlayerCollides(double X, double Y, double Z) { Vector PlayerChunk(floor(X / 16.0), floor(Y / 16.0), floor(Z / 16.0)); - if (sections.find(PlayerChunk) == sections.end() || sections.find(PlayerChunk - Vector(0, 1, 0)) == sections.end()) + if (sections.find(PlayerChunk) == sections.end() || + sections.find(PlayerChunk - Vector(0, 1, 0)) == sections.end()) return false; std::vector closestSectionsCoordinates = { @@ -154,8 +157,7 @@ RaycastResult World::Raycast(glm::vec3 position, glm::vec3 direction) { return result; } -void World::UpdatePhysics(float delta) -{ +void World::UpdatePhysics(float delta) { struct CollisionResult { bool isCollide; //Vector block; @@ -164,7 +166,6 @@ void World::UpdatePhysics(float delta) }; auto testCollision = [this](double width, double height, VectorF pos)->CollisionResult { - int blockXBegin = pos.x - width - 1.0; int blockXEnd = pos.x + width + 0.5; int blockYBegin = pos.y - 0.5; @@ -211,6 +212,7 @@ void World::UpdatePhysics(float delta) it.pos = newPos; } } + { //Horizontal velocity VectorF newPos = it.pos + VectorF(it.vel.x, 0, it.vel.z) * delta; auto coll = testCollision(it.width, it.height, newPos); @@ -231,8 +233,7 @@ void World::UpdatePhysics(float delta) DebugInfo::totalSections = sections.size(); } -Entity & World::GetEntity(unsigned int EntityId) -{ +Entity& World::GetEntity(unsigned int EntityId){ entitiesMutex.lock(); for (auto& it : entities) { if (it.entityId == EntityId) { @@ -241,12 +242,12 @@ Entity & World::GetEntity(unsigned int EntityId) } } entitiesMutex.unlock(); + static Entity fallback; return fallback; } -std::vector World::GetEntitiesList() -{ +std::vector World::GetEntitiesList() { entitiesMutex.lock(); std::vector ret; for (auto& it : entities) { @@ -256,8 +257,7 @@ std::vector World::GetEntitiesList() return ret; } -void World::AddEntity(Entity entity) -{ +void World::AddEntity(Entity entity) { entitiesMutex.lock(); for (auto& it : entities) { if (it.entityId == entity.entityId) { @@ -270,8 +270,7 @@ void World::AddEntity(Entity entity) entitiesMutex.unlock(); } -void World::DeleteEntity(unsigned int EntityId) -{ +void World::DeleteEntity(unsigned int EntityId) { entitiesMutex.lock(); auto it = entities.begin(); for (; it != entities.end(); ++it) { @@ -285,10 +284,16 @@ void World::DeleteEntity(unsigned int EntityId) } void World::ParseChunkData(std::shared_ptr packet) { - SetBlockId(packet->Position, BlockId{(unsigned short) (packet->BlockId >> 4),(unsigned char) (packet->BlockId & 0xF) }); - - Vector sectionPos(std::floor(packet->Position.x / 16.0), std::floor(packet->Position.y / 16.0), std::floor(packet->Position.z / 16.0)); - PUSH_EVENT("ChunkChanged", sectionPos); + SetBlockId(packet->Position, + BlockId { + (unsigned short) (packet->BlockId >> 4), + (unsigned char) (packet->BlockId & 0xF) + }); + + Vector sectionPos(std::floor(packet->Position.x / 16.0), + std::floor(packet->Position.y / 16.0), + std::floor(packet->Position.z / 16.0)); + PUSH_EVENT("ChunkChanged", sectionPos); } void World::ParseChunkData(std::shared_ptr packet) { @@ -306,7 +311,7 @@ void World::ParseChunkData(std::shared_ptr packet) { } for (auto& sectionPos : changedSections) - PUSH_EVENT("ChunkChanged", sectionPos); + PUSH_EVENT("ChunkChanged", sectionPos); } void World::ParseChunkData(std::shared_ptr packet) { @@ -316,7 +321,7 @@ void World::ParseChunkData(std::shared_ptr packet) { toRemove.push_back(it); } for (auto& it : toRemove) { - PUSH_EVENT("ChunkDeleted", it->first); + PUSH_EVENT("ChunkDeleted", it->first); sections.erase(it); } UpdateSectionsList(); @@ -332,14 +337,19 @@ void World::UpdateSectionsList() { } BlockId World::GetBlockId(Vector pos) { - Vector sectionPos(std::floor(pos.x / 16.0), std::floor(pos.y / 16.0), std::floor(pos.z / 16.0)); + Vector sectionPos(std::floor(pos.x / 16.0), + std::floor(pos.y / 16.0), + std::floor(pos.z / 16.0)); Section* section = GetSectionPtr(sectionPos); return !section ? BlockId{0, 0} : section->GetBlockId(pos - (sectionPos * 16)); } void World::SetBlockId(Vector pos, BlockId block) { - Vector sectionPos(std::floor(pos.x / 16.0), std::floor(pos.y / 16.0), std::floor(pos.z / 16.0)); + Vector sectionPos(std::floor(pos.x / 16.0), + std::floor(pos.y / 16.0), + std::floor(pos.z / 16.0)); + Section* section = GetSectionPtr(sectionPos); section->SetBlockId(pos - (sectionPos * 16), block); PUSH_EVENT("ChunkChanged",sectionPos); -- cgit v1.2.3 From ab8278e3b61e009253e34d13d6706da7702dbb6c Mon Sep 17 00:00:00 2001 From: Elisey Puzko Date: Fri, 23 Feb 2018 11:51:09 +0300 Subject: Bare-bones face detection for a block placement --- src/World.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index f593bce..110b16b 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -142,12 +142,12 @@ RaycastResult World::Raycast(glm::vec3 position, glm::vec3 direction) { const float maxLen = 5.0; const float step = 0.01; glm::vec3 pos; - float len=0; + float len = 0; Vector blockPos = Vector(position.x,position.y,position.z); while (GetBlockId(blockPos) == BlockId{0, 0} && len <= maxLen) { pos = position + direction * len; len += step; - blockPos = Vector(floor(pos.x),floor(pos.y),floor(pos.z)); + blockPos = Vector(floor(pos.x), floor(pos.y), floor(pos.z)); } RaycastResult result; -- cgit v1.2.3 From 05126872d522a3a031086dcce866de9643f38c3f Mon Sep 17 00:00:00 2001 From: Elisey Puzko Date: Fri, 23 Feb 2018 12:47:46 +0300 Subject: Mouse capture fix --- src/World.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index 110b16b..59399f5 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -138,6 +138,7 @@ const Section &World::GetSection(Vector sectionPos) { } } +// TODO: skip liquid blocks RaycastResult World::Raycast(glm::vec3 position, glm::vec3 direction) { const float maxLen = 5.0; const float step = 0.01; -- cgit v1.2.3