space-game001/Inventory.cpp
Vladislav Khorev da7805ce0c Final version
2025-03-03 07:58:27 +03:00

91 lines
2.2 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 clear()
{
gInventoryMap.clear();
}
void AddItemToInventory(const std::string& name, std::shared_ptr<Texture> tex, int slot_index)
{
if (slot_index > MAX_INVENTORY_SLOTS || slot_index < 1) {
return;
}
InventoryItem item;
item.name = name;
item.texture = tex;
item.hot_key = slot_index;
item.scale = 1.0f;
gInventoryMap[name] = item;
std::cout << "Added item to slot " << slot_index << std::endl;
}
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;
}
InventoryItem* GetItemByHotkey(int hotkey)
{
for (auto& [_, item] : gInventoryMap) {
if (item.hot_key == hotkey) {
return &item;
}
}
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";
for (auto& [itemName, item] : gInventoryMap)
{
std::cout << " - " << itemName << "\n";
}
}
void UnselectAllItems()
{
for (auto& [_, item] : gInventoryMap) {
item.isSelected = false;
item.scale = 1.0f; // Reset scale when unselected
}
}
const std::unordered_map<std::string, InventoryItem>& ReturnInventory()
{
return gInventoryMap;
}
}