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
.vscode/
# User-specific files
*.rsuser
*.suo

View File

@ -28,7 +28,8 @@ void GameObjectManager::initialize() {
testObjMeshMutable.data = testObjMesh;
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.SwapZandY();
textMesh.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundX(M_PI * 0.5)));
@ -114,11 +115,23 @@ void GameObjectManager::initialize() {
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++;
// Добавляем тестовые предметы с правильными hot_key
if (objects_in_inventory < MAX_INVENTORY_SLOTS) {
AddItemToInventory("book1", std::make_shared<Texture>(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp")), 1);
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_SetRelativeMouseMode(SDL_TRUE);
@ -250,14 +263,35 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
case SDLK_1:
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();
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->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;
// ...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 {
// Простая проверка попадания точки в квадрат 64x64 вокруг центра объекта
const int objectSize = 32; // Половина размера области выделения
return (screenX >= objectScreenX - objectSize &&
screenX <= objectScreenX + objectSize &&
screenY >= objectScreenY - objectSize &&
screenY <= objectScreenY + objectSize);
const int baseObjectSize = 32; // Base half-size
const float scale = 1.0f; // Get scale from item if needed
const int scaledObjectSize = static_cast<int>(baseObjectSize * scale);
return (screenX >= objectScreenX - scaledObjectSize &&
screenX <= objectScreenX + scaledObjectSize &&
screenY >= objectScreenY - scaledObjectSize &&
screenY <= objectScreenY + scaledObjectSize);
}
void GameObjectManager::checkMouseIntersection(int mouseX, int mouseY, const Matrix4f& projectionModelView) {

View File

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

View File

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