Inventory - UQcsse3200/2026-studio-1 GitHub Wiki
Overview
The Inventory system stores, manages, and lets players interact with items in the game through a visual UI grid.
Core Workflow
- Storage & Management: Stores items in an indexed collection for fast slot access. Automatically manages quantities, merges duplicate items into stacks up to a limit, and handles full-capacity checks.
- World Interaction: Allows the player to pick up items found in the world into the inventory, and drop one unit of a stored item back onto the map. If spawning the dropped item fails, that unit is restored to the inventory.
- UI Display: Shows stored items and their quantities on an interactive grid, allowing the player to select slots and trigger actions.
- Item Actions & Effects: Triggers specific effects when an item is consumed or activated (e.g., restoring player health).
Team Dependencies
- Loot Generation: Provides base item data (mock items used during initial development).
- Health / Combat: Receives usage events from consumable items for testing effects.
Behind The Scenes:
Storage & Management:
The Inventory class manages the items currently stored by the player. It extends Component and maintains a collection representing the available inventory slots.
Inventory Storage
Inventory slots are represented using a Java Map, where:
- Key: The inventory slot number.
- Value: The
Itemobject currently stored in that slot.
This allows the system to quickly access an item using its slot number.
The Inventory class also maintains two variables for tracking inventory capacity:
quantity: Represents the number of inventory slots currently occupied. It is updated whenever an item is added to or completely removed from a slot.maxQuantity: Represents the maximum number of slots available in the inventory. It is used to determine whether another item can be placed into a new slot.
These values allow the inventory to determine whether it has available space before adding an item and whether it currently contains any items before performing removal operations.
Adding Items
The Inventory class provides an add method for placing an item into the inventory.
When adding an item, the system will:
- Check the item's type or supported behaviour.
- Determine whether the item can be stacked with an existing item.
- Check the item's maximum quantity allowed within a slot.
- If the item can be stacked, add to an existing compatible stack without exceeding its maximum quantity.
- If a new slot is required, check whether
quantityhas reachedmaxQuantity. - Search for an empty inventory slot.
- Store the item in the available slot.
- Update
quantitywhen a previously empty slot becomes occupied. - Reject or leave the remaining item quantity if there is insufficient inventory capacity.
Items that cannot be stacked, such as weapons, occupy their own slot.
Getting / Selecting Items
The Inventory class provides a get method for retrieving an item from a particular slot.
Slot selection input is handled by the player input system. Once a slot number is provided, the inventory will:
- Receive the selected slot number.
- Check whether the slot contains an item.
- Return the stored
Itemobject if the slot is occupied. - Allow the appropriate item-use system to perform the item's action.
The Inventory class is responsible for retrieving the item, while keyboard input and the item's actual behaviour are handled by their respective systems.
Removing Items
The Inventory class provides a remove method for removing an item from a particular inventory slot.
When removing an item, the system will:
- Receive the selected slot number.
- Check whether the inventory contains any items using
quantity. - Check whether the selected slot contains an item.
- Remove or reduce the stored item's quantity as required.
- Clear the slot when no item remains.
- Update
quantitywhen a slot becomes empty.
When the player drops an item, only one unit of the item in the selected slot is removed. If the stack contains more than one unit, its quantity decreases by one and the slot remains occupied. If it contains exactly one unit, the slot is cleared and quantity decreases by one.
Any world rendering or creation of a dropped item is handled outside the inventory storage logic. The drop action spawns one unit of the selected item on the map. If spawning fails, the removed unit is restored to the inventory.