From 5f010b71415a7dc0337c67cac4666eacdf472751 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sun, 27 Jan 2019 11:25:45 +0500 Subject: Added const-keyword to World --- src/GameState.hpp | 12 ++++----- src/Render.cpp | 28 ++++++++++----------- src/RendererEntity.cpp | 4 +-- src/RendererEntity.hpp | 2 +- src/World.cpp | 67 ++++++++++++++++++++++++++++---------------------- src/World.hpp | 24 +++++++++--------- 6 files changed, 73 insertions(+), 64 deletions(-) diff --git a/src/GameState.hpp b/src/GameState.hpp index 41e7080..8318c8a 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -96,27 +96,27 @@ public: return player; } - inline World &GetWorld() { + inline const World &GetWorld() const { return world; } - inline TimeStatus &GetTimeStatus() { + inline const TimeStatus &GetTimeStatus() const { return timeStatus; } - inline GameStatus &GetGameStatus() { + inline const GameStatus &GetGameStatus() const { return gameStatus; } - inline PlayerStatus &GetPlayerStatus() { + inline const PlayerStatus &GetPlayerStatus() const { return playerStatus; } - inline SelectionStatus &GetSelectionStatus() { + inline const SelectionStatus &GetSelectionStatus() const { return selectionStatus; } - inline Window &GetInventory() { + inline const Window &GetInventory() const { return playerInventory; } }; diff --git a/src/Render.cpp b/src/Render.cpp index 0268bee..a081dbb 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -387,7 +387,7 @@ void Render::RenderGui() { if (world) { Entity *playerPtr = world->GameStatePtr()->GetPlayer(); SelectionStatus selectionStatus = world->GameStatePtr()->GetSelectionStatus(); - World *worldPtr = &world->GameStatePtr()->GetWorld(); + const World *worldPtr = &world->GameStatePtr()->GetWorld(); ImGui::Text("TPS: %.1f (%.2fms)", 1000.0f / gameTime, gameTime); ImGui::Text("Sections loaded: %d", (int) DebugInfo::totalSections); @@ -506,58 +506,58 @@ void Render::RenderGui() { }; ImGui::SetNextWindowPosCenter(); ImGui::Begin("Inventory", 0, windowFlags); - Window& inventory = world->GameStatePtr()->GetInventory(); + const Window& inventory = world->GameStatePtr()->GetInventory(); //Hand and drop slots if (renderSlot(inventory.handSlot, -1)) { } ImGui::SameLine(); if (ImGui::Button("Drop")) { - inventory.MakeClick(-1, true, true); + //inventory.MakeClick(-1, true, true); } ImGui::SameLine(); ImGui::Text("Hand slot and drop mode"); ImGui::Separator(); //Crafting if (renderSlot(inventory.slots[1], 1)) { - inventory.MakeClick(1, true); + //inventory.MakeClick(1, true); } ImGui::SameLine(); if (renderSlot(inventory.slots[2], 2)) { - inventory.MakeClick(2, true); + //inventory.MakeClick(2, true); } //Crafting result ImGui::SameLine(); ImGui::Text("Result"); ImGui::SameLine(); if (renderSlot(inventory.slots[0], 0)) { - inventory.MakeClick(0, true); + //inventory.MakeClick(0, true); } //Crafting second line if (renderSlot(inventory.slots[3], 3)) { - inventory.MakeClick(3, true); + //inventory.MakeClick(3, true); } ImGui::SameLine(); if (renderSlot(inventory.slots[4], 4)) { - inventory.MakeClick(4, true); + //inventory.MakeClick(4, true); } ImGui::Separator(); //Armor and offhand for (int i = 5; i < 8 + 1; i++) { if (renderSlot(inventory.slots[i], i)) { - inventory.MakeClick(i, true); + //inventory.MakeClick(i, true); } ImGui::SameLine(); } if (renderSlot(inventory.slots[45], 45)) { - inventory.MakeClick(45, true); + //inventory.MakeClick(45, true); } ImGui::SameLine(); ImGui::Text("Armor and offhand"); ImGui::Separator(); for (int i = 36; i < 44 + 1; i++) { if (renderSlot(inventory.slots[i], i)) { - inventory.MakeClick(i, true); + //inventory.MakeClick(i, true); } ImGui::SameLine(); } @@ -566,21 +566,21 @@ void Render::RenderGui() { ImGui::Text("Main inventory"); for (int i = 9; i < 17 + 1; i++) { if (renderSlot(inventory.slots[i], i)) { - inventory.MakeClick(i, true); + //inventory.MakeClick(i, true); } ImGui::SameLine(); } ImGui::Text(""); for (int i = 18; i < 26 + 1; i++) { if (renderSlot(inventory.slots[i], i)) { - inventory.MakeClick(i, true); + //inventory.MakeClick(i, true); } ImGui::SameLine(); } ImGui::Text(""); for (int i = 27; i < 35 + 1; i++) { if (renderSlot(inventory.slots[i], i)) { - inventory.MakeClick(i, true); + //inventory.MakeClick(i, true); } ImGui::SameLine(); } diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index 698c934..25403be 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -123,9 +123,9 @@ RendererEntity::RendererEntity(unsigned int id) RendererEntity::~RendererEntity() { } -void RendererEntity::Render(RenderState& renderState, World *world) { +void RendererEntity::Render(RenderState& renderState, const World *world) { glm::mat4 model = glm::mat4(1.0); - Entity &entity = world->GetEntity(entityId); + const Entity &entity = world->GetEntity(entityId); model = glm::translate(model, entity.pos.glm()); model = glm::translate(model, glm::vec3(0, entity.height / 2.0, 0)); model = glm::scale(model, glm::vec3(entity.width, entity.height, entity.width)); diff --git a/src/RendererEntity.hpp b/src/RendererEntity.hpp index 7f42f7d..346f9fb 100644 --- a/src/RendererEntity.hpp +++ b/src/RendererEntity.hpp @@ -11,7 +11,7 @@ public: RendererEntity(unsigned int id); ~RendererEntity(); - void Render(RenderState& renderState, World *world); + void Render(RenderState& renderState, const World *world); static GLuint GetVao(); }; diff --git a/src/World.cpp b/src/World.cpp index 928c56a..da0a33b 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -57,7 +57,7 @@ Section World::ParseSection(StreamInput *data, Vector position) { std::move(blockLight), std::move(skyLight)); } -bool World::isPlayerCollides(double X, double Y, double Z) { +bool World::isPlayerCollides(double X, double Y, double Z) const { 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()) @@ -111,14 +111,14 @@ bool World::isPlayerCollides(double X, double Y, double Z) { return false; } -std::vector World::GetSectionsList() { +std::vector World::GetSectionsList() const { auto vec = sectionsList; return vec; } static Section fallbackSection; -const Section &World::GetSection(Vector sectionPos) { +const Section &World::GetSection(Vector sectionPos) const { auto result = sections.find(sectionPos); if (result == sections.end()) { //LOG(ERROR) << "Accessed not loaded section " << sectionPos; @@ -130,7 +130,7 @@ const Section &World::GetSection(Vector sectionPos) { } // TODO: skip liquid blocks -RaycastResult World::Raycast(glm::vec3 position, glm::vec3 direction) { +RaycastResult World::Raycast(glm::vec3 position, glm::vec3 direction) const { const float maxLen = 5.0; const float step = 0.01; glm::vec3 pos = glm::vec3(0.0); @@ -251,7 +251,18 @@ Entity& World::GetEntity(unsigned int EntityId){ return fallback; } -std::vector World::GetEntitiesList() { +const Entity &World::GetEntity(unsigned int EntityId) const { + for (auto& it : entities) { + if (it.entityId == EntityId) { + return it; + } + } + + static Entity fallback; + return fallback; +} + +std::vector World::GetEntitiesList() const { std::vector ret; for (auto& it : entities) { ret.push_back(it.entityId); @@ -331,12 +342,12 @@ void World::UpdateSectionsList() { } } -BlockId World::GetBlockId(Vector pos) { +BlockId World::GetBlockId(Vector pos) const { Vector sectionPos(std::floor(pos.x / 16.0), std::floor(pos.y / 16.0), std::floor(pos.z / 16.0)); - Section* section = GetSectionPtr(sectionPos); + const Section* section = GetSectionPtr(sectionPos); return !section ? BlockId{0, 0} : section->GetBlockId(pos - (sectionPos * 16)); } @@ -371,7 +382,7 @@ void World::SetBlockSkyLight(Vector pos, unsigned char light) { } -Section *World::GetSectionPtr(Vector position) { +const Section *World::GetSectionPtr(Vector position) const { auto it = sections.find(position); if (it == sections.end()) @@ -389,21 +400,20 @@ Entity* World::GetEntityPtr(unsigned int EntityId) { return nullptr; } -unsigned char World::GetBlockLight(Vector pos) -{ +unsigned char World::GetBlockLight(Vector pos) const { Vector sectionPos(std::floor(pos.x / 16.0), std::floor(pos.y / 16.0), std::floor(pos.z / 16.0)); Vector blockPos = pos - (sectionPos * 16); - Section* section = GetSectionPtr(sectionPos); - Section* yp = GetSectionPtr(sectionPos + Vector(0, 1, 0)); - Section* yn = GetSectionPtr(sectionPos + Vector(0, -1, 0)); - Section* xp = GetSectionPtr(sectionPos + Vector(1, 0, 0)); - Section* xn = GetSectionPtr(sectionPos + Vector(-1, 0, 0)); - Section* zp = GetSectionPtr(sectionPos + Vector(0, 0, 1)); - Section* zn = GetSectionPtr(sectionPos + Vector(0, 0, -1)); + const Section* section = GetSectionPtr(sectionPos); + const Section* yp = GetSectionPtr(sectionPos + Vector(0, 1, 0)); + const Section* yn = GetSectionPtr(sectionPos + Vector(0, -1, 0)); + const Section* xp = GetSectionPtr(sectionPos + Vector(1, 0, 0)); + const Section* xn = GetSectionPtr(sectionPos + Vector(-1, 0, 0)); + const Section* zp = GetSectionPtr(sectionPos + Vector(0, 0, 1)); + const Section* zn = GetSectionPtr(sectionPos + Vector(0, 0, -1)); if (!section) return 0; @@ -411,8 +421,7 @@ unsigned char World::GetBlockLight(Vector pos) return GetBlockLight(blockPos, section, xp, xn, yp, yn, zp, zn); } -unsigned char World::GetBlockLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) -{ +unsigned char World::GetBlockLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) const { static const Vector directions[] = { Vector(0,0,0), Vector(1,0,0), @@ -450,21 +459,20 @@ unsigned char World::GetBlockLight(const Vector &blockPos, const Section *sectio return value; } -unsigned char World::GetBlockSkyLight(Vector pos) -{ +unsigned char World::GetBlockSkyLight(Vector pos) const { Vector sectionPos( std::floor(pos.x / 16.0), std::floor(pos.y / 16.0), std::floor(pos.z / 16.0)); Vector blockPos = pos - (sectionPos * 16); - Section* section = GetSectionPtr(sectionPos); - Section* yp = GetSectionPtr(sectionPos + Vector(0, 1, 0)); - Section* yn = GetSectionPtr(sectionPos + Vector(0, -1, 0)); - Section* xp = GetSectionPtr(sectionPos + Vector(1, 0, 0)); - Section* xn = GetSectionPtr(sectionPos + Vector(-1, 0, 0)); - Section* zp = GetSectionPtr(sectionPos + Vector(0, 0, 1)); - Section* zn = GetSectionPtr(sectionPos + Vector(0, 0, -1)); + const Section* section = GetSectionPtr(sectionPos); + const Section* yp = GetSectionPtr(sectionPos + Vector(0, 1, 0)); + const Section* yn = GetSectionPtr(sectionPos + Vector(0, -1, 0)); + const Section* xp = GetSectionPtr(sectionPos + Vector(1, 0, 0)); + const Section* xn = GetSectionPtr(sectionPos + Vector(-1, 0, 0)); + const Section* zp = GetSectionPtr(sectionPos + Vector(0, 0, 1)); + const Section* zn = GetSectionPtr(sectionPos + Vector(0, 0, -1)); if (!section) return 0; @@ -472,8 +480,7 @@ unsigned char World::GetBlockSkyLight(Vector pos) return GetBlockSkyLight(blockPos, section, xp, xn, yp, yn, zp, zn); } -unsigned char World::GetBlockSkyLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) -{ +unsigned char World::GetBlockSkyLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) const { static const Vector directions[] = { Vector(0,0,0), Vector(1,0,0), diff --git a/src/World.hpp b/src/World.hpp index 591e7a7..6c9a615 100644 --- a/src/World.hpp +++ b/src/World.hpp @@ -48,13 +48,13 @@ public: void ParseChunkData(std::shared_ptr packet); - bool isPlayerCollides(double X, double Y, double Z); + bool isPlayerCollides(double X, double Y, double Z) const; - std::vector GetSectionsList(); + std::vector GetSectionsList() const; - const Section &GetSection(Vector sectionPos); + const Section &GetSection(Vector sectionPos) const; - RaycastResult Raycast(glm::vec3 position, glm::vec3 direction); + RaycastResult Raycast(glm::vec3 position, glm::vec3 direction) const; void UpdatePhysics(float delta); @@ -62,13 +62,15 @@ public: Entity* GetEntityPtr(unsigned int EntityId); - std::vector GetEntitiesList(); + const Entity& GetEntity(unsigned int EntityId) const; + + std::vector GetEntitiesList() const; void AddEntity(Entity entity); void DeleteEntity(unsigned int EntityId); - BlockId GetBlockId(Vector pos); + BlockId GetBlockId(Vector pos) const; void SetBlockId(Vector pos, BlockId block); @@ -76,13 +78,13 @@ public: void SetBlockSkyLight(Vector pos, unsigned char light); - Section *GetSectionPtr(Vector position); + const Section *GetSectionPtr(Vector position) const; - unsigned char GetBlockLight(Vector pos); + unsigned char GetBlockLight(Vector pos) const; - unsigned char GetBlockLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn); + unsigned char GetBlockLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) const; - unsigned char GetBlockSkyLight(Vector pos); + unsigned char GetBlockSkyLight(Vector pos) const; - unsigned char GetBlockSkyLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn); + unsigned char GetBlockSkyLight(const Vector &blockPos, const Section *section, const Section *xp, const Section *xn, const Section *yp, const Section *yn, const Section *zp, const Section *zn) const; }; \ No newline at end of file -- cgit v1.2.3