fixed inventory choosing

This commit is contained in:
maka70vv 2025-03-02 18:37:37 +06:00
parent 512e912471
commit cfc96f645e
3 changed files with 26 additions and 15 deletions

View File

@ -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;
}
}

View File

@ -2,7 +2,7 @@
namespace ZL
{
std::unordered_map<int, InventoryItem> gInventoryMap;
std::unordered_map<std::string, InventoryItem> gInventoryMap;
void AddItemToInventory(const std::string& name, std::shared_ptr<Texture> 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<int, InventoryItem>& ReturnInventory()
const std::unordered_map<std::string, InventoryItem>& ReturnInventory()
{
return gInventoryMap;
}

View File

@ -21,7 +21,7 @@ namespace ZL
};
// Глобальное хранилище предметов
extern std::unordered_map<int, InventoryItem> gInventoryMap; // Changed key type from string to int
extern std::unordered_map<std::string, InventoryItem> gInventoryMap; // Changed key type from string to int
// Добавить предмет в инвентарь
void AddItemToInventory(const std::string& name, std::shared_ptr<Texture> 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<int, InventoryItem>& ReturnInventory();
const std::unordered_map<std::string, InventoryItem>& ReturnInventory();
// Add these new functions
void UnselectAllItems();
InventoryItem* GetItemByHotkey(int hotkey);
InventoryItem* GetItemSelected(bool isSelected);
}