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] 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); }