diff options
| author | nathan <nathansmith@disroot.org> | 2026-01-20 13:39:03 +0000 |
|---|---|---|
| committer | nathan <nathansmith@disroot.org> | 2026-01-20 13:39:03 +0000 |
| commit | 602bfee943ac7810d0ca961851883c8909f5d7ff (patch) | |
| tree | 7f790cdadd55a4e6766c6599415fddf7b4006840 /src/ui.c | |
| parent | f44d154d1372597742642596fe91c619613f81ef (diff) | |
| download | FindThings-602bfee943ac7810d0ca961851883c8909f5d7ff.tar.gz FindThings-602bfee943ac7810d0ca961851883c8909f5d7ff.tar.bz2 FindThings-602bfee943ac7810d0ca961851883c8909f5d7ff.zip | |
Intractable inventory items
Diffstat (limited to 'src/ui.c')
| -rw-r--r-- | src/ui.c | 140 |
1 files changed, 137 insertions, 3 deletions
@@ -228,6 +228,25 @@ void updateInteractionMenu(InteractionMenu* menu, Game* game) } } +void startInteraction(Game* game, EntityId entityId) +{ + InteractionChat* chat = &game->interactionChat; + InteractionMenu* menu = &game->interactionMenu; + + clearInteractionChat(chat); + resetInteractionMenu(menu); + chat->entityId = entityId; + menu->entityId = entityId; +} + +void endInteraction(Game* game) +{ + hideInteractionChat(&game->interactionChat); + hideInteractionMenu(&game->interactionMenu); + game->interactionChat.entityId = ENTITY_NONE; + game->interactionMenu.entityId = ENTITY_NONE; +} + void resizeInventory(Inventory* inventory, const Settings* settings) { float outlineThickness = settings->uiOutlineSize; @@ -237,11 +256,14 @@ void resizeInventory(Inventory* inventory, const Settings* settings) inventory->rect.x = GetRenderWidth() / 2.0 - inventory->rect.width / 2.0; inventory->rect.y = GetRenderHeight() / 2.0 - inventory->rect.height / 2.0; } + void initInventory(Inventory* inventory, const Settings* settings) { resizeInventory(inventory, settings); inventory->visable = false; inventory->itemCount = 0; + inventory->interactingEntity = NULL; + inventory->interactingIndex = 0; } void showInventory(Inventory* inventory, Game* game) @@ -310,6 +332,103 @@ void removeInventoryItem(Inventory* inventory, int itemIndex) } } +void inventoryEndInteraction(Inventory* inventory, Game* game) +{ + inventory->interactingEntity = NULL; + endInteraction(game); +} + +// Yup, pretty much a rewrite of player interaction code. + +// Returns true if item should be killed. +bool inventoryInteractWithEntity(Inventory* inventory, Game* game, + Entity* entity, Selection selection) +{ + InteractionChat* chat = &game->interactionChat; + InteractionMenu* menu = &game->interactionMenu; + + // Handle selection. + switch (selection) + { + case SELECTION_USE: + startInteraction(game, entity->id); + inventory->interactingEntity = entity; + break; + case SELECTION_LEAVE: + inventoryEndInteraction(inventory, game); + break; + default: + break; + } + + // Interact with it. + switch (interactWithEntity(entity, game, selection)) + { + case INTERACTION_TALK: + hideInteractionMenu(menu); + showInteractionChat(chat); + break; + case INTERACTION_SHOW_MENU: + hideInteractionChat(chat); + showInteractionMenu(menu); + break; + case INTERACTION_TALK_AND_SHOW_MENU: + showInteractionChat(chat); + showInteractionMenu(menu); + break; + case INTERACTION_END: + inventoryEndInteraction(inventory, game); + break; + case INTERACTION_KILL_ITEM: + inventoryEndInteraction(inventory, game); + return true; + default: + break; + } + + return false; +} + +void inventoryHandleInteraction(Inventory* inventory, Game* game) +{ + InteractionChat* chat = &game->interactionChat; + InteractionMenu* menu = &game->interactionMenu; + int keyPressed = GetKeyPressed(); + + bool shouldKill = false; + + if (IsKeyPressed(game->settings.leaveKey)) + { + shouldKill = inventoryInteractWithEntity(inventory, game, + inventory->interactingEntity, + SELECTION_LEAVE); + } + else if (IsKeyPressed(game->settings.nextMessageKey)) + { + if (isInteractionChatAnimationDone(chat)) + { + shouldKill = inventoryInteractWithEntity(inventory, game, + inventory->interactingEntity, + SELECTION_LEAVE); + } + else + { + endInteractionChatAnimation(chat); + } + } + else if (keyPressed >= KEY_ONE && keyPressed <= KEY_NINE) + { + shouldKill = inventoryInteractWithEntity( + inventory, game, inventory->interactingEntity, + SELECTION_MENU_ITEM + (keyPressed - KEY_ONE)); + } + + if (shouldKill) + { + removeInventoryItem(inventory, inventory->interactingIndex); + } +} + void updateInventory(Inventory* inventory, Game* game) { if (IsWindowResized()) @@ -317,6 +436,19 @@ void updateInventory(Inventory* inventory, Game* game) resizeInventory(inventory, &game->settings); } + // Is interacting. + if (inventory->interactingEntity != NULL) + { + inventoryHandleInteraction(inventory, game); + return; + } + + // Hide with interaction chat/menu. + if (game->interactionChat.visible || game->interactionMenu.visible) + { + return; + } + // Handle toggle key. if (IsKeyPressed(game->settings.toggleInventoryKey)) { @@ -329,7 +461,8 @@ void updateInventory(Inventory* inventory, Game* game) showInventory(inventory, game); } } - + + // Not visable. if (!inventory->visable) { return; @@ -397,8 +530,9 @@ void updateInventory(Inventory* inventory, Game* game) // Use item. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { - killItem = interactWithEntity(item->parent, game, SELECTION_USE) == - INTERACTION_KILL_ITEM; + killItem = inventoryInteractWithEntity(inventory, game, item->parent, + SELECTION_USE); + inventory->interactingIndex = index; killIndex = index; } } |
