hotkesy for items

This commit is contained in:
Альберт Гадиев 2025-03-02 17:33:30 +06:00
parent 37f0a6aa08
commit 1ac4c51a5d
4 changed files with 79 additions and 42 deletions

2
.gitignore vendored
View File

@ -3,6 +3,8 @@
## ##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
.vscode/
# User-specific files # User-specific files
*.rsuser *.rsuser
*.suo *.suo

View File

@ -28,7 +28,8 @@ void GameObjectManager::initialize() {
testObjMeshMutable.data = testObjMesh; testObjMeshMutable.data = testObjMesh;
testObjMeshMutable.RefreshVBO(); testObjMeshMutable.RefreshVBO();
textMesh = ZL::LoadFromTextFile("./textures/mesh_first_room.txt"); // Add ZL:: namespace textMesh = ZL::LoadFromTextFile("./textures/mesh_first_room.txt");
textMesh.NormalData // Add ZL:: namespace
textMesh.Scale(10); textMesh.Scale(10);
textMesh.SwapZandY(); textMesh.SwapZandY();
textMesh.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundX(M_PI * 0.5))); textMesh.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundX(M_PI * 0.5)));
@ -114,11 +115,23 @@ 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); // Добавляем тестовые предметы с правильными hot_key
objects_in_inventory++; if (objects_in_inventory < MAX_INVENTORY_SLOTS) {
AddItemToInventory("book2", std::make_shared<Texture>(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")), objects_in_inventory+1); AddItemToInventory("book1", std::make_shared<Texture>(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")), 1);
objects_in_inventory++; objects_in_inventory++;
std::cout << "Added item with hotkey 1" << std::endl;
}
if (objects_in_inventory < MAX_INVENTORY_SLOTS) {
AddItemToInventory("book2", std::make_shared<Texture>(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")), 2);
objects_in_inventory++;
std::cout << "Added item with hotkey 2" << std::endl;
}
// Test inventory items
AddItemToInventory("book1", std::make_shared<Texture>(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")), 1);
AddItemToInventory("book2", std::make_shared<Texture>(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")), 2);
objects_in_inventory = 2;
//SDL_ShowCursor(SDL_DISABLE); //SDL_ShowCursor(SDL_DISABLE);
SDL_SetRelativeMouseMode(SDL_TRUE); SDL_SetRelativeMouseMode(SDL_TRUE);
@ -250,14 +263,35 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
case SDLK_1: case SDLK_1:
case SDLK_2: case SDLK_2:
case SDLK_3:
case SDLK_4:
case SDLK_5:
case SDLK_6:
case SDLK_7:
case SDLK_8:
case SDLK_9:
{ {
int hot_key = (event.key.keysym.sym == SDLK_1) ? 1 : 2; int slot_index = event.key.keysym.sym - SDLK_1 + 1;
std::cout << "Selected slot: " << slot_index << std::endl;
UnselectAllItems(); UnselectAllItems();
if (InventoryItem* item = GetItemByHotkey(hot_key)) {
if (auto* item = GetItemByIndex(slot_index)) {
std::cout << "Found item in slot " << slot_index << std::endl;
item->isSelected = true; item->isSelected = true;
item->scale = SELECTED_ITEM_SCALE;
inventoryIconMesh = CreateRect2D(
{0.0f, 40.0f},
{INVENTORY_ICON_SIZE/2 * item->scale, INVENTORY_ICON_SIZE/2 * item->scale},
0.5f
);
inventoryIconMeshMutable.AssignFrom(inventoryIconMesh);
inventoryIconMeshMutable.RefreshVBO();
} }
} }
break; break;
// ...handle other keys... // ...handle other keys...
} }
} }
@ -438,12 +472,14 @@ void GameObjectManager::updateScene(size_t ms) {
} }
bool GameObjectManager::isPointInObject(int screenX, int screenY, int objectScreenX, int objectScreenY) const { bool GameObjectManager::isPointInObject(int screenX, int screenY, int objectScreenX, int objectScreenY) const {
// Простая проверка попадания точки в квадрат 64x64 вокруг центра объекта const int baseObjectSize = 32; // Base half-size
const int objectSize = 32; // Половина размера области выделения const float scale = 1.0f; // Get scale from item if needed
return (screenX >= objectScreenX - objectSize && const int scaledObjectSize = static_cast<int>(baseObjectSize * scale);
screenX <= objectScreenX + objectSize &&
screenY >= objectScreenY - objectSize && return (screenX >= objectScreenX - scaledObjectSize &&
screenY <= objectScreenY + objectSize); screenX <= objectScreenX + scaledObjectSize &&
screenY >= objectScreenY - scaledObjectSize &&
screenY <= objectScreenY + scaledObjectSize);
} }
void GameObjectManager::checkMouseIntersection(int mouseX, int mouseY, const Matrix4f& projectionModelView) { void GameObjectManager::checkMouseIntersection(int mouseX, int mouseY, const Matrix4f& projectionModelView) {

View File

@ -2,47 +2,41 @@
namespace ZL 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 hot_key) 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; InventoryItem item;
item.name = name; item.name = name;
item.texture = tex; item.texture = tex;
item.hot_key = hot_key; item.hot_key = slot_index;
item.scale = 1.0f;
// Вставляем или перезаписываем (operator[] так сделает). gInventoryMap[slot_index] = item;
gInventoryMap[name] = item; std::cout << "Added item to slot " << slot_index << std::endl;
} }
void RemoveItemFromInventory(const std::string& name) void RemoveItemFromInventory(int slot_index)
{ {
// erase вернёт количество удалённых элементов, если нужно проверить gInventoryMap.erase(slot_index);
gInventoryMap.erase(name);
} }
InventoryItem* GetItemByName(const std::string& name) InventoryItem* GetItemByIndex(int slot_index)
{ {
// Пытаемся найти элемент по ключу auto it = gInventoryMap.find(slot_index);
auto it = gInventoryMap.find(name); if (it != gInventoryMap.end()) {
if (it != gInventoryMap.end())
{
// Возвращаем адрес найденного InventoryItem
return &it->second; return &it->second;
} }
// Если не нашли nullptr
return nullptr; return nullptr;
} }
InventoryItem* GetItemByHotkey(int hotkey) InventoryItem* GetItemByHotkey(int hotkey)
{ {
for (auto& [_, item] : gInventoryMap) { return GetItemByIndex(hotkey); // Now we can just use the index directly
if (item.hot_key == hotkey) {
return &item;
}
}
return nullptr;
} }
void PrintInventory() void PrintInventory()
@ -58,10 +52,11 @@ namespace ZL
{ {
for (auto& [_, item] : gInventoryMap) { for (auto& [_, item] : gInventoryMap) {
item.isSelected = false; item.isSelected = false;
item.scale = 1.0f; // Reset scale when unselected
} }
} }
const std::unordered_map<std::string, InventoryItem>& ReturnInventory() const std::unordered_map<int, InventoryItem>& ReturnInventory()
{ {
return gInventoryMap; return gInventoryMap;
} }

View File

@ -8,30 +8,34 @@
namespace ZL namespace ZL
{ {
static const int MAX_INVENTORY_SLOTS = 9;
static const float SELECTED_ITEM_SCALE = 1.2f; // Scale factor for selected items
struct InventoryItem struct InventoryItem
{ {
std::string name; std::string name;
std::shared_ptr<Texture> texture; std::shared_ptr<Texture> texture;
bool isSelected = false; bool isSelected = false;
int hot_key; int hot_key;
float scale = 1.0f; // Add scale property
}; };
// Глобальное хранилище предметов // Глобальное хранилище предметов
extern std::unordered_map<std::string, InventoryItem> gInventoryMap; // Changed from gInventory extern std::unordered_map<int, InventoryItem> gInventoryMap; // Changed key type from string to int
// Добавить предмет в инвентарь // Добавить предмет в инвентарь
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 slot_index);
// Удалить предмет из инвентаря // Удалить предмет из инвентаря
void RemoveItemFromInventory(const std::string& name); void RemoveItemFromInventory(int slot_index);
// Поиск предмета по имени (возвращает указатель или nullptr) // Поиск предмета по индексу (возвращает указатель или nullptr)
InventoryItem* GetItemByName(const std::string& name); InventoryItem* GetItemByIndex(int slot_index);
// Вывести весь инвентарь в консоль // Вывести весь инвентарь в консоль
void PrintInventory(); void PrintInventory();
const std::unordered_map<std::string, InventoryItem>& ReturnInventory(); const std::unordered_map<int, InventoryItem>& ReturnInventory();
// Add these new functions // Add these new functions
void UnselectAllItems(); void UnselectAllItems();