Merge pull request #19 from mephi1984/Albert

working code
This commit is contained in:
Albrut 2025-03-02 17:02:37 +06:00 committed by GitHub
commit 37f0a6aa08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 24 deletions

View File

@ -21,6 +21,11 @@
"string": "cpp", "string": "cpp",
"unordered_map": "cpp", "unordered_map": "cpp",
"unordered_set": "cpp", "unordered_set": "cpp",
"vector": "cpp" "vector": "cpp",
"chrono": "cpp",
"text_encoding": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"*.inc": "cpp"
} }
} }

View File

@ -114,6 +114,12 @@ void GameObjectManager::initialize() {
roomTexturePtr = rooms[current_room_index].roomTexture; roomTexturePtr = rooms[current_room_index].roomTexture;
AddItemToInventory("book1", std::make_shared<Texture>(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")), objects_in_inventory+1);
objects_in_inventory++;
AddItemToInventory("book2", std::make_shared<Texture>(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")), objects_in_inventory+1);
objects_in_inventory++;
//SDL_ShowCursor(SDL_DISABLE); //SDL_ShowCursor(SDL_DISABLE);
SDL_SetRelativeMouseMode(SDL_TRUE); SDL_SetRelativeMouseMode(SDL_TRUE);
} }
@ -243,28 +249,15 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
break; break;
case SDLK_1: case SDLK_1:
{ case SDLK_2:
int hot_key = 1; {
std::string keyStr = std::to_string(hot_key); int hot_key = (event.key.keysym.sym == SDLK_1) ? 1 : 2;
UnselectAllItems();
auto it = gInventory.find(keyStr); if (InventoryItem* item = GetItemByHotkey(hot_key)) {
if (it != gInventory.end()) { item->isSelected = true;
it->second.isSelected = true; }
} }
std::cout << keyStr << std::endl; break;
} break;
case SDLK_2:
{
int hot_key = 2;
std::string keyStr = std::to_string(hot_key);
auto it = gInventory.find(keyStr);
if (it != gInventory.end()) {
it->second.isSelected = true;
}
} break;
// ...handle other keys... // ...handle other keys...
} }
} }

View File

@ -35,6 +35,16 @@ namespace ZL
return nullptr; return nullptr;
} }
InventoryItem* GetItemByHotkey(int hotkey)
{
for (auto& [_, item] : gInventoryMap) {
if (item.hot_key == hotkey) {
return &item;
}
}
return nullptr;
}
void PrintInventory() void PrintInventory()
{ {
std::cout << "Inventory contents:\n"; std::cout << "Inventory contents:\n";
@ -44,6 +54,13 @@ namespace ZL
} }
} }
void UnselectAllItems()
{
for (auto& [_, item] : gInventoryMap) {
item.isSelected = false;
}
}
const std::unordered_map<std::string, InventoryItem>& ReturnInventory() const std::unordered_map<std::string, InventoryItem>& ReturnInventory()
{ {
return gInventoryMap; return gInventoryMap;

View File

@ -17,7 +17,7 @@ namespace ZL
}; };
// Глобальное хранилище предметов // Глобальное хранилище предметов
extern std::unordered_map<std::string, InventoryItem> gInventory; extern std::unordered_map<std::string, InventoryItem> gInventoryMap; // Changed from gInventory
// Добавить предмет в инвентарь // Добавить предмет в инвентарь
void AddItemToInventory(const std::string& name, std::shared_ptr<Texture> tex, int hot_key); void AddItemToInventory(const std::string& name, std::shared_ptr<Texture> tex, int hot_key);
@ -32,4 +32,8 @@ namespace ZL
void PrintInventory(); void PrintInventory();
const std::unordered_map<std::string, InventoryItem>& ReturnInventory(); const std::unordered_map<std::string, InventoryItem>& ReturnInventory();
// Add these new functions
void UnselectAllItems();
InventoryItem* GetItemByHotkey(int hotkey);
} }