Item's interaction System with Key bindings - UQcsse3200/2024-studio-1 GitHub Wiki
Objective
Implement a system that allows the player to interact with items in the inventory using key bindings. The system enables players to use specific items from their inventory by pressing designated keys (e.g., '1' for the first item, '2' for the second item). The inventory's UI updates in real-time to reflect the count of each item, ensuring players can see the effects of item usage and the availability of items even when their count is zero. An item is usable as long as the count is more than zero, otherwise the keypress is disabled
Code Structure
The implementation involves several key components and classes that work together to achieve the desired functionality. Below is a detailed breakdown of each component and its role in the system.
1. KeyboardPlayerInputComponent
This class handles player input from the keyboard and triggers corresponding actions based on key presses.
-
Constructor
KeyboardPlayerInputComponent(KeyMapping keyMapping)
: Initializes the component with a given key mapping.KeyboardPlayerInputComponent()
: Initializes with default key bindings.
-
Methods
useItem(Integer num)
: Triggers the use of the item based on the number (e.g.,use1
,use2
).getDownActions()
,getUpActions()
: Define the actions associated with key presses and releases.
2. KeyMapping
This class defines the mapping of keyboard keys to player actions.
-
Fields
keyMap
: A map of key codes to actions.
-
Constructor
KeyMapping(Map<Integer, KeyBinding> keyMap)
: Initializes with a custom key map.KeyMapping()
: Initializes with a default key map.
3. PlayerActions
This class manages player actions such as walking, attacking, and using items.
- Methods
use(UsableItem item)
: If that item exists in the inventory, applies the effects of an item and delete it from inventory after use.
4. InventoryComponent
This class manages the player's inventory, including item pickup and dropping.
-
Fields
inventory
: The underlying inventory model that contains user's inventory .
-
Methods
pickup(Collectible item)
: Adds an item to the inventory so that the count can be incemented for it to be usable.drop(Collectible item)
: Removes an item from the inventory and decrements t.
Usage
Key Bindings
-
Default Bindings
1
: Use the first item in the inventory2
: Use the second item in the inventory3
: Use the third item in the inventory
Example Scenario
-
Item Pickup
- The player picks up a MedKit. The count of MedKit is updated in the UI and the player's inventory.
-
Using an Item
- The player presses '1' to use the MedKit. The
useItem(1)
method is triggered, which calls theapply()
method of the MedKit to apply its effects and updates the inventory count in the UI.
- The player presses '1' to use the MedKit. The
-
Inventory Update
- If the MedKit was the last one, its count is updated to zero in the UI and the key press is disabled, disabling users from using the item if it doesn't exists
Example Usage
In order to add more items:
- Create a KeyBinding enum:
KeyMapping.KeyBinding Press_1;
setKeyBinding(Input.Keys.NUM_1, Press_1);
- Map it to an action:
private Map<KeyMapping.KeyBinding, Action> getUpActions(){
actionMap.put(PRESS_1, (i) -> useItem(1));
Example
When 2
is pressed on keyboard, shield is activated and is displayed on UI: