Inventory and Hotbar - UQcsse3200/2024-studio-2 GitHub Wiki
Inventory
Overview
The Inventory
class and InventoryInterface
manage a player's item collection in the game. They handle adding, removing, and using items, as well as checking inventory status. In future, this InventoryInterface can be generalised to model all containers (e.g. chests etc.) in the game and the current Inventory
class can be abstracted as a base class for all containers (ie useItem
would be moved to a concrete subclass, since a general container would not support this functionality).
Next, the PlayerInventoryDisplay
handles the UI for the items. This is what users can interact with during gameplay. The display can be toggled on or off by pressing E
. Items can be used from the inventory by clicking them (currently just removes item with no effect on game), and items can be picked up from the game via button press P
. Players can use items from either the Hotbar or PlayerInventoryDisplay
by clicking on the item. Dialogue will appear for these items displaying how much usage items have left. However, this display players cannot use certain items such as defence and attack potions outside of combat.
CombatInventoryDisplay
an inventory extending from PlayerInventoryDisplay
is a special inventory that can be toggled on and interactive in `CombatScreen allowing players to use items during combat (which is considered a combat move). the ability to use defence and attack potions will be permitted.
Class locations
Package: com.csse3200.game.inventory
Package: com/csse3200/game/components/player/PlayerInventoryDisplay.java
Package: com/csse3200/game/components/inventory/CombatInInventoryDisplay.java
Inventory Interface
Methods:
int getCapacity()
: Returns the total capacity of the inventory.int numFreeSlots()
: Returns the number of free slots.boolean isFull()
: Checks if the inventory is full.boolean hasItem(int itemCode)
: Checks if an item with the specified code is in the inventory.int getIndex(int itemCode)
: Retrieves the index of an item with the specified code.AbstractItem getAt(int index)
: Retrieves the item at the specified index.void deleteItem(int itemCode)
: Deletes an item with the specified code.void deleteItemAt(int index)
: Deletes the item at the specified index.void clearInventory()
: Clears all items from the inventory.void useItem(int itemCode, ItemUsageContext context)
: Uses an item with the specified code.void useItemAt(int index, ItemUsageContext context)
: Uses the item at the specified index.void sortByName()
: Sorts the inventory by item name.void sortByCode()
: Sorts the inventory by item code.void add(AbstractItem item)
: Adds an item to the inventory.void addAt(int index, AbstractItem item)
: Adds an item to a specific index in the inventory.void swap(int src, int target)
: swaps an items in 2 indexes (adds if null) Package:com.csse3200.game.inventory
The Inventory
class implements InventoryInterface
, providing the core functionality for managing items within the player's inventory.
The test plan for inventory is detailed in Item and Inventory Test Plan
InventoryDisplay is the class for displaying hotbar and inventory. Both can be opened simultaneously by toggling the inventory as the drag and drop will be working between all the slots. the UML for this class
CombatInventoryDisplay
Methods:
create()
: Overrides from PlayerInventoryDisplay and adds a listener foruseItemInCombat
toggleMsg()
: Overrides from PlayerInventoryDisplayenterSlot()
: Overrides from PlayerInventoryDisplayexitSlot()
: Overrides from PlayerInventoryDisplayuseItem()
: Overrides from PlayerInventoryDisplay and ensures certain items can only be used in mapuseItemInCombat()
: Allows users to use items in combat inventory
Class locations
Package: source/core/src/main/com/csse3200/game/components/inventory/InventoryDisplay.java
Sequence Diagram
(https://github.com/user-attachments/assets/d1395457-6fcc-4646-9a00-b68b545d312f)