space-game001/Inventory.cpp
2025-03-02 09:55:56 +06:00

52 lines
1.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Inventory.h"
namespace ZL
{
// Определяем глобальную переменную
std::unordered_map<std::string, InventoryItem> gInventoryMap;
void AddItemToInventory(const std::string& name, std::shared_ptr<Texture> tex)
{
InventoryItem item;
item.name = name;
item.texture = tex;
// Вставляем или перезаписываем (operator[] так сделает).
gInventoryMap[name] = item;
}
void RemoveItemFromInventory(const std::string& name)
{
// erase вернёт количество удалённых элементов, если нужно проверить
gInventoryMap.erase(name);
}
InventoryItem* GetItemByName(const std::string& name)
{
// Пытаемся найти элемент по ключу
auto it = gInventoryMap.find(name);
if (it != gInventoryMap.end())
{
// Возвращаем адрес найденного InventoryItem
return &it->second;
}
// Если не нашли nullptr
return nullptr;
}
void PrintInventory()
{
std::cout << "Inventory contents:\n";
for (auto& [itemName, item] : gInventoryMap)
{
std::cout << " - " << itemName << "\n";
}
}
const std::unordered_map<std::string, InventoryItem>& ReturnInventory()
{
return gInventoryMap;
}
}