From f44d154d1372597742642596fe91c619613f81ef Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 19 Jan 2026 07:10:40 -0700 Subject: Using inventory items --- src/ui.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) (limited to 'src/ui.c') diff --git a/src/ui.c b/src/ui.c index e071036..cca77cf 100644 --- a/src/ui.c +++ b/src/ui.c @@ -280,6 +280,36 @@ void addItemToInventory(Inventory* inventory, InventoryItem item) } } +void removeInventoryItem(Inventory* inventory, int itemIndex) +{ + // Out of bounds. + if (itemIndex < 0 || itemIndex >= inventory->itemCount) + { + return; + } + + // Decrease count. + InventoryItem* item = &inventory->items[itemIndex]; + --item->count; + + // Remove item if no more. + if (item->count <= 0) + { + for (int index = itemIndex; index < inventory->itemCount - 1; ++index) + { + inventory->items[index] = inventory->items[index + 1]; + } + + --inventory->itemCount; + + // Might prevent a future bug or something :D + if (inventory->itemCount < 0) + { + inventory->itemCount = 0; + } + } +} + void updateInventory(Inventory* inventory, Game* game) { if (IsWindowResized()) @@ -346,6 +376,8 @@ void updateInventory(Inventory* inventory, Game* game) int row = 0; int column = 0; + bool killItem = false; + int killIndex = 0; // Draw items. for (int index = 0; index < inventory->itemCount; ++index) @@ -353,11 +385,22 @@ void updateInventory(Inventory* inventory, Game* game) InventoryItem* item = &inventory->items[index]; Color color = WHITE; - // Make it blink when selected. - if (mouseIsSelecting && row == mouseRow && column == mouseColumn && - (int)(GetTime() / game->settings.inventoryItemBlinkSpeed) % 2 == 0) + // Item Is selected. + if (mouseIsSelecting && row == mouseRow && column == mouseColumn) { - color = item->blinkColor; + // Make it blink when selected. + if ((int)(GetTime() / game->settings.inventoryItemBlinkSpeed) % 2 == 0) + { + color = item->blinkColor; + } + + // Use item. + if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) + { + killItem = interactWithEntity(item->parent, game, SELECTION_USE) == + INTERACTION_KILL_ITEM; + killIndex = index; + } } DrawTextureEx(game->assets.textures[item->textureId], (Vector2){x, y}, 0.0, @@ -380,4 +423,9 @@ void updateInventory(Inventory* inventory, Game* game) column = 0; } } + + if (killItem) + { + removeInventoryItem(inventory, killIndex); + } } -- cgit v1.2.3