diff options
| author | nathan <nathansmith@disroot.org> | 2026-01-19 14:10:40 +0000 |
|---|---|---|
| committer | nathan <nathansmith@disroot.org> | 2026-01-19 14:10:40 +0000 |
| commit | f44d154d1372597742642596fe91c619613f81ef (patch) | |
| tree | 22eb7aaefc5b1100e6294c7aa2fd63a97758a8cb | |
| parent | f339de8c07647ed4c5d659639f0f3e6c271faf8f (diff) | |
| download | FindThings-f44d154d1372597742642596fe91c619613f81ef.tar.gz FindThings-f44d154d1372597742642596fe91c619613f81ef.tar.bz2 FindThings-f44d154d1372597742642596fe91c619613f81ef.zip | |
Using inventory items
| -rw-r--r-- | src/entities/oldMint.c | 4 | ||||
| -rw-r--r-- | src/entities/stickyNickel.c | 4 | ||||
| -rw-r--r-- | src/entity.c | 3 | ||||
| -rw-r--r-- | src/entity.h | 1 | ||||
| -rw-r--r-- | src/ui.c | 56 | ||||
| -rw-r--r-- | src/ui.h | 1 | ||||
| -rw-r--r-- | src/world.c | 4 |
7 files changed, 66 insertions, 7 deletions
diff --git a/src/entities/oldMint.c b/src/entities/oldMint.c index 445f34a..9e43fd9 100644 --- a/src/entities/oldMint.c +++ b/src/entities/oldMint.c @@ -30,8 +30,10 @@ InteractionCommand interactWithOldMint(Entity* entity, Game* game, "Oh goodie it's an old mint!\nThough I advice you don't taste it\n...(assuming you want to live)"); addItemToInventory(&game->inventory, item); return INTERACTION_TALK; + case SELECTION_USE: + return INTERACTION_KILL_ITEM; default: - entity->id = ENTITY_NONE; + entity->visable = false; return INTERACTION_END; } } diff --git a/src/entities/stickyNickel.c b/src/entities/stickyNickel.c index 3738089..49a3fc7 100644 --- a/src/entities/stickyNickel.c +++ b/src/entities/stickyNickel.c @@ -32,8 +32,10 @@ InteractionCommand interactWithStickyNickel(Entity* entity, Game* game, "Luck you, its a sticky nickel :D\nBetter wash your hands..."); addItemToInventory(&game->inventory, item); return INTERACTION_TALK; + case SELECTION_USE: + return INTERACTION_KILL_ITEM; default: - entity->id = ENTITY_NONE; + entity->visable = false; return INTERACTION_END; } } diff --git a/src/entity.c b/src/entity.c index 2565d3b..4cd13d4 100644 --- a/src/entity.c +++ b/src/entity.c @@ -162,6 +162,7 @@ Entity createEntity(EntityId id, Vector3 position) { Entity entity; entity.id = id; + entity.visable = true; entity.data = NULL; // Run init callback. @@ -187,7 +188,7 @@ void updateEntity(Entity* entity, Game* game) UpdateEntityCallback updateCallback = entityEntries[entity->id].updateCallback; - if (updateCallback != NULL) + if (entity->visable && updateCallback != NULL) { updateCallback(entity, game); } diff --git a/src/entity.h b/src/entity.h index 41ca8c0..19fc9be 100644 --- a/src/entity.h +++ b/src/entity.h @@ -71,6 +71,7 @@ struct Entity { EntityId id; Vector3 position; // Shouldnt be changed directly. BoundingBox box; + bool visable; void* data; }; @@ -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); + } } @@ -75,6 +75,7 @@ void initInventory(Inventory* inventory, const Settings* settings); void showInventory(Inventory* inventory, Game* game); void hideInventory(Inventory* inventory, Game* game); void addItemToInventory(Inventory* inventory, InventoryItem item); +void removeInventoryItem(Inventory* inventory, int itemIndex); void updateInventory(Inventory* inventory, Game* game); #endif diff --git a/src/world.c b/src/world.c index b53fb57..5872688 100644 --- a/src/world.c +++ b/src/world.c @@ -1080,6 +1080,10 @@ void castRayBVH(const World* world, BVHNode node, Ray ray, bool allowAll, { continue; } + if (!world->entities[node.entities[index]].visable) + { + continue; + } else if (world->entities[node.entities[index]].id == ENTITY_NONE) { continue; |
