From 1e3be664959415933f9c57e4941aad1d699e89d3 Mon Sep 17 00:00:00 2001 From: maka70vv <25.makarovv@gmail.com> Date: Sun, 2 Mar 2025 03:10:01 +0600 Subject: [PATCH] removing object from map after adding to inventory --- GameObjectManager.cpp | 13 ++++++++----- Inventory.cpp | 9 --------- Inventory.h | 3 --- RenderSystem.cpp | 2 ++ 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp index 596155b..ada28c3 100644 --- a/GameObjectManager.cpp +++ b/GameObjectManager.cpp @@ -121,12 +121,15 @@ void GameObjectManager::handleEvent(const SDL_Event& event) { switch_room(1); } else if (event.type == SDL_MOUSEBUTTONDOWN) { - for (auto& ao : activeObjects) { - if (ao.highlighted){ - AddItemToInventory(ao.name, ao.activeObjectTexturePtr); - PrintInventory(); - } + for (size_t i = 0; i < activeObjects.size(); ++i) { + auto& ao = activeObjects[i]; + if (ao.highlighted) { + AddItemToInventory(ao.name, ao.activeObjectTexturePtr); + activeObjects.erase(activeObjects.begin() + i); + // Можно выйти из цикла, если объект удален, чтобы избежать ошибок индексации. + break; } + } // bx.Interpolate(animationCounter); // animationCounter += 2; } diff --git a/Inventory.cpp b/Inventory.cpp index 0d3df50..bf12ad3 100644 --- a/Inventory.cpp +++ b/Inventory.cpp @@ -32,15 +32,6 @@ namespace ZL } } - bool HasObject(const std::string& name){ - for (const auto& item : gInventory) { - if (item.name == name) { - return true; - } - } - return false; - } - const std::vector& ReturnInventory() { return gInventory; diff --git a/Inventory.h b/Inventory.h index 7b06fcc..8f771fd 100644 --- a/Inventory.h +++ b/Inventory.h @@ -24,9 +24,6 @@ namespace ZL // Удалить предмет из инвентаря void RemoveItemFromInventory(const std::string& name); -// todo check if object exists - bool HasObject(const std::string name); - // Вывести все предметы в инвентаре void PrintInventory(); diff --git a/RenderSystem.cpp b/RenderSystem.cpp index 4ce5996..3df87f7 100644 --- a/RenderSystem.cpp +++ b/RenderSystem.cpp @@ -94,6 +94,7 @@ void RenderSystem::drawUI(const GameObjectManager& gameObjects) { // Draw highlighted objects UI for (const auto& ao : gameObjects.activeObjects) { if (ao.highlighted) { + if (ao.activeObjectScreenTexturePtr){ int screenX, screenY; worldToScreenCoordinates(ao.objectPos, currentProjectionModelView, Environment::width, Environment::height, screenX, screenY); @@ -102,6 +103,7 @@ void RenderSystem::drawUI(const GameObjectManager& gameObjects) { glBindTexture(GL_TEXTURE_2D, ao.activeObjectScreenTexturePtr->getTexID()); renderer.DrawVertexRenderStruct(ao.activeObjectScreenMeshMutable); renderer.PopMatrix(); + } else {} } }