From 4ff416757f8394e1084d48bf5b3d6ff5967ee882 Mon Sep 17 00:00:00 2001 From: maka70vv <25.makarovv@gmail.com> Date: Sun, 2 Mar 2025 17:16:22 +0600 Subject: [PATCH 1/4] getting selected inventory items --- GameObjectManager.cpp | 6 ++++++ Inventory.cpp | 10 ++++++++++ Inventory.h | 1 + 3 files changed, 17 insertions(+) diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp index b6d8120..1887ea1 100644 --- a/GameObjectManager.cpp +++ b/GameObjectManager.cpp @@ -150,6 +150,10 @@ void GameObjectManager::handleEvent(const SDL_Event& event) { switch_room(1); } else if (event.type == SDL_MOUSEBUTTONDOWN) { + if (InventoryItem* item = GetItemSelected(true)) { + + } + else { const auto highlightedObjects = aoMgr.findByHighlighted(true); for (auto* ao : highlightedObjects) { @@ -162,6 +166,7 @@ void GameObjectManager::handleEvent(const SDL_Event& event) { aoMgr.removeByName(ao->name); } + } // bx.Interpolate(animationCounter); // animationCounter += 2; } @@ -255,6 +260,7 @@ void GameObjectManager::handleEvent(const SDL_Event& event) { UnselectAllItems(); if (InventoryItem* item = GetItemByHotkey(hot_key)) { item->isSelected = true; + std:: cout << item->name << std::endl; } } break; diff --git a/Inventory.cpp b/Inventory.cpp index 292cfb0..4bb7a39 100644 --- a/Inventory.cpp +++ b/Inventory.cpp @@ -45,6 +45,16 @@ namespace ZL return nullptr; } + InventoryItem* GetItemSelected(bool selected) + { + for (auto& [_, item] : gInventoryMap) { + if (item.isSelected == selected) { + return &item; + } + } + return nullptr; + } + void PrintInventory() { std::cout << "Inventory contents:\n"; diff --git a/Inventory.h b/Inventory.h index 655bcc7..ad5daf3 100644 --- a/Inventory.h +++ b/Inventory.h @@ -36,4 +36,5 @@ namespace ZL // Add these new functions void UnselectAllItems(); InventoryItem* GetItemByHotkey(int hotkey); + InventoryItem* GetItemSelected(bool isSelected); } From a6a275a93450afb8e5dd0618dcf1d708d6a5077c Mon Sep 17 00:00:00 2001 From: maka70vv <25.makarovv@gmail.com> Date: Sun, 2 Mar 2025 17:30:55 +0600 Subject: [PATCH 2/4] moving active inventory object --- RenderSystem.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/RenderSystem.cpp b/RenderSystem.cpp index 0c67204..4ca7cfb 100644 --- a/RenderSystem.cpp +++ b/RenderSystem.cpp @@ -214,15 +214,29 @@ void RenderSystem::drawUI(const GameObjectManager& gameObjects) { for (const auto& [name, item] : inventoryMap) { renderer.PushMatrix(); + if (item.isSelected) { + float xPos = Environment::width + - gameObjects.INVENTORY_MARGIN + - gameObjects.INVENTORY_ICON_SIZE+25; + float yPos = gameObjects.INVENTORY_MARGIN + + i * (gameObjects.INVENTORY_ICON_SIZE+25 + + gameObjects.INVENTORY_MARGIN); + renderer.TranslateMatrix(Vector3f{xPos, yPos, 0.0f}); + glBindTexture(GL_TEXTURE_2D, item.texture->getTexID()); + } + else { + float xPos = Environment::width - gameObjects.INVENTORY_MARGIN - gameObjects.INVENTORY_ICON_SIZE; float yPos = gameObjects.INVENTORY_MARGIN + i * (gameObjects.INVENTORY_ICON_SIZE + gameObjects.INVENTORY_MARGIN); - renderer.TranslateMatrix(Vector3f{xPos, yPos, 0.0f}); glBindTexture(GL_TEXTURE_2D, item.texture->getTexID()); + } + + renderer.DrawVertexRenderStruct(gameObjects.inventoryIconMeshMutable); renderer.PopMatrix(); From 103c43eba1b274871b7962398d2e356c8198fc8e Mon Sep 17 00:00:00 2001 From: maka70vv <25.makarovv@gmail.com> Date: Sun, 2 Mar 2025 18:19:25 +0600 Subject: [PATCH 3/4] select inventory 1-9 --- GameObjectManager.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp index c9efcd5..54e2f3e 100644 --- a/GameObjectManager.cpp +++ b/GameObjectManager.cpp @@ -259,14 +259,22 @@ void GameObjectManager::handleEvent(const SDL_Event& event) { case SDLK_1: case SDLK_2: - { - int hot_key = (event.key.keysym.sym == SDLK_1) ? 1 : 2; + case SDLK_3: + case SDLK_4: + case SDLK_5: + case SDLK_6: + case SDLK_7: + case SDLK_8: + case SDLK_9: + { + UnselectAllItems(); - if (InventoryItem* item = GetItemByHotkey(hot_key)) { + if (InventoryItem* item = GetItemByHotkey(event.key.keysym.sym - SDLK_1 + 1)) { item->isSelected = true; std:: cout << item->name << std::endl; } } + break; // ...handle other keys... } From cfc96f645ee13689d232d86dd7d96cacef4ba356 Mon Sep 17 00:00:00 2001 From: maka70vv <25.makarovv@gmail.com> Date: Sun, 2 Mar 2025 18:37:37 +0600 Subject: [PATCH 4/4] fixed inventory choosing --- GameObjectManager.cpp | 2 +- Inventory.cpp | 29 ++++++++++++++++++++--------- Inventory.h | 10 +++++----- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/GameObjectManager.cpp b/GameObjectManager.cpp index 2310599..3a089c6 100644 --- a/GameObjectManager.cpp +++ b/GameObjectManager.cpp @@ -269,7 +269,7 @@ void GameObjectManager::handleEvent(const SDL_Event& event) { { UnselectAllItems(); - if (InventoryItem* item = GetItemByHotkey(hot_key)) { + if (InventoryItem* item = GetItemByHotkey(event.key.keysym.sym - SDLK_1 + 1)) { item->isSelected = true; } } diff --git a/Inventory.cpp b/Inventory.cpp index ba86887..71af814 100644 --- a/Inventory.cpp +++ b/Inventory.cpp @@ -2,7 +2,7 @@ namespace ZL { - std::unordered_map gInventoryMap; + std::unordered_map gInventoryMap; void AddItemToInventory(const std::string& name, std::shared_ptr tex, int slot_index) { @@ -16,27 +16,38 @@ namespace ZL item.hot_key = slot_index; item.scale = 1.0f; - gInventoryMap[slot_index] = item; + gInventoryMap[name] = item; std::cout << "Added item to slot " << slot_index << std::endl; } - void RemoveItemFromInventory(int slot_index) + void RemoveItemFromInventory(const std::string& name) { - gInventoryMap.erase(slot_index); + // erase вернёт количество удалённых элементов, если нужно проверить + gInventoryMap.erase(name); } - InventoryItem* GetItemByIndex(int slot_index) + + InventoryItem* GetItemByName(const std::string name) { - auto it = gInventoryMap.find(slot_index); - if (it != gInventoryMap.end()) { + // Пытаемся найти элемент по ключу + auto it = gInventoryMap.find(name); + if (it != gInventoryMap.end()) + { + // Возвращаем адрес найденного InventoryItem return &it->second; } + // Если не нашли – nullptr return nullptr; } InventoryItem* GetItemByHotkey(int hotkey) { - return GetItemByIndex(hotkey); // Now we can just use the index directly + for (auto& [_, item] : gInventoryMap) { + if (item.hot_key == hotkey) { + return &item; + } + } + return nullptr; } InventoryItem* GetItemSelected(bool selected) @@ -66,7 +77,7 @@ namespace ZL } } - const std::unordered_map& ReturnInventory() + const std::unordered_map& ReturnInventory() { return gInventoryMap; } diff --git a/Inventory.h b/Inventory.h index 77d73cf..adea97e 100644 --- a/Inventory.h +++ b/Inventory.h @@ -21,7 +21,7 @@ namespace ZL }; // Глобальное хранилище предметов - extern std::unordered_map gInventoryMap; // Changed key type from string to int + extern std::unordered_map gInventoryMap; // Changed key type from string to int // Добавить предмет в инвентарь void AddItemToInventory(const std::string& name, std::shared_ptr tex, int slot_index); @@ -30,15 +30,15 @@ namespace ZL void RemoveItemFromInventory(int slot_index); // Поиск предмета по индексу (возвращает указатель или nullptr) - InventoryItem* GetItemByIndex(int slot_index); + InventoryItem* GetItemByHotkey(int hot_key); + InventoryItem* GetItemSelected(bool isSelected); + InventoryItem* GetItemByName(std::string name); // Вывести весь инвентарь в консоль void PrintInventory(); - const std::unordered_map& ReturnInventory(); + const std::unordered_map& ReturnInventory(); // Add these new functions void UnselectAllItems(); - InventoryItem* GetItemByHotkey(int hotkey); - InventoryItem* GetItemSelected(bool isSelected); }