aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui.c
diff options
context:
space:
mode:
authornathan <nathansmith@disroot.org>2026-01-19 14:10:40 +0000
committernathan <nathansmith@disroot.org>2026-01-19 14:10:40 +0000
commitf44d154d1372597742642596fe91c619613f81ef (patch)
tree22eb7aaefc5b1100e6294c7aa2fd63a97758a8cb /src/ui.c
parentf339de8c07647ed4c5d659639f0f3e6c271faf8f (diff)
downloadFindThings-f44d154d1372597742642596fe91c619613f81ef.tar.gz
FindThings-f44d154d1372597742642596fe91c619613f81ef.tar.bz2
FindThings-f44d154d1372597742642596fe91c619613f81ef.zip
Using inventory items
Diffstat (limited to 'src/ui.c')
-rw-r--r--src/ui.c56
1 files changed, 52 insertions, 4 deletions
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);
+ }
}