From eb0bae44aa103fc3161f1cfe39fa30df9f5996af Mon Sep 17 00:00:00 2001 From: maka70vv <25.makarovv@gmail.com> Date: Sun, 2 Mar 2025 03:00:50 +0600 Subject: [PATCH 1/2] adding inventory --- ActiveObject.h | 1 + GameObjectManager.cpp | 18 +++++++++--------- Inventory.cpp | 11 ++++++++++- Inventory.h | 3 +++ RenderSystem.h | 2 +- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/ActiveObject.h b/ActiveObject.h index 3d9f915..6385748 100644 --- a/ActiveObject.h +++ b/ActiveObject.h @@ -5,6 +5,7 @@ namespace ZL { struct ActiveObject { + std::string name; std::shared_ptr activeObjectTexturePtr; ZL::VertexDataStruct activeObjectMesh; ZL::VertexRenderStruct activeObjectMeshMutable; diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp index fa7af77..596155b 100644 --- a/GameObjectManager.cpp +++ b/GameObjectManager.cpp @@ -52,6 +52,7 @@ void GameObjectManager::initialize() { // Create active object ActiveObject ao1; + ao1.name = "book"; ao1.activeObjectMesh = ZL::LoadFromTextFile("./book001.txt"); // Add ZL:: namespace ao1.activeObjectMesh.Scale(4); ao1.activeObjectMeshMutable.AssignFrom(ao1.activeObjectMesh); @@ -91,13 +92,6 @@ void GameObjectManager::initialize() { inventoryIconMeshMutable.AssignFrom(inventoryIconMesh); inventoryIconMeshMutable.RefreshVBO(); - - // Add test items to inventory - auto testRoomTexture = std::make_shared(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")); - auto testConeTexture = std::make_shared(CreateTextureDataFromBmp24("./conus.bmp")); - AddItemToInventory("RoomCeramics", testRoomTexture); - AddItemToInventory("Cone", testConeTexture); - roomTexturePtr = rooms[current_room_index].roomTexture; } @@ -127,8 +121,14 @@ void GameObjectManager::handleEvent(const SDL_Event& event) { switch_room(1); } else if (event.type == SDL_MOUSEBUTTONDOWN) { - bx.Interpolate(animationCounter); - animationCounter += 2; + for (auto& ao : activeObjects) { + if (ao.highlighted){ + AddItemToInventory(ao.name, ao.activeObjectTexturePtr); + PrintInventory(); + } + } +// bx.Interpolate(animationCounter); +// animationCounter += 2; } else if (event.type == SDL_MOUSEWHEEL) { diff --git a/Inventory.cpp b/Inventory.cpp index 7a9b42b..0d3df50 100644 --- a/Inventory.cpp +++ b/Inventory.cpp @@ -11,7 +11,7 @@ namespace ZL gInventory.push_back({ name, tex }); } - void RemoveItemFromInventory(const std::string& name) + void RemoveItemFromInventory(const std::string name) { gInventory.erase( std::remove_if(gInventory.begin(), gInventory.end(), @@ -32,6 +32,15 @@ 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 8f771fd..7b06fcc 100644 --- a/Inventory.h +++ b/Inventory.h @@ -24,6 +24,9 @@ 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.h b/RenderSystem.h index 26d12b7..75cc7ed 100644 --- a/RenderSystem.h +++ b/RenderSystem.h @@ -20,7 +20,7 @@ public: private: void drawWorld(const GameObjectManager& gameObjects); void drawUI(const GameObjectManager& gameObjects); - + Renderer renderer; ShaderManager shaderManager; Matrix4f currentProjectionModelView; // Добавлено для хранения матрицы между drawWorld и drawUI 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 2/2] 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 {} } }