Package com.csse3200.game.inventory
Interface InventoryInterface
- All Known Implementing Classes:
Inventory
public interface InventoryInterface
Represents player's inventory. Can store a certain number of distinct items (determined by
limit), some items can be stacked (ie, stackable items).
Note: this interface in future can possibly be abstracted further and used as a container in multiple contexts, e.g. loot boxes or chests in game etc.
-
Method Summary
Modifier and TypeMethodDescriptionint
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, replacing any existing item at that index.void
Clears all items from the inventory.void
deleteItem
(int itemCode) Deletes an instance of an item with the specified code from the inventory.deleteItemAt
(int index) Deletes the item at the specified index in the inventory.getAt
(int index) Retrieves the item at the specified index in the inventory.int
Retrieves the total capacity of the inventory.int
getIndex
(int itemCode) Retrieves the index of an item with the specified code.int
Retrieves the index of the first occurrence of an item with the given name.boolean
hasItem
(int itemCode) Checks if the inventory contains an item with the specified code.boolean
Checks if an item with the given name exists in the inventory.boolean
isFull()
Checks if the inventory is full.int
Returns the number of free slots available in the inventory.void
Sorts the inventory by item code.void
useItem
(int itemCode, ItemUsageContext context) Uses an item with the specified code from the inventory.void
useItemAt
(int index, ItemUsageContext context) Uses the item at the specified index in the inventory.
-
Method Details
-
getCapacity
int getCapacity()Retrieves the total capacity of the inventory.- Returns:
- the number of distinct item slots available in the inventory
-
numFreeSlots
int numFreeSlots()Returns the number of free slots available in the inventory.- Returns:
- the number of available slots in the inventory
-
isFull
boolean isFull()Checks if the inventory is full.- Returns:
true
if the inventory has no free slots,false
otherwise
-
hasItem
boolean hasItem(int itemCode) Checks if the inventory contains an item with the specified code.- Parameters:
itemCode
- the code of the item to check for- Returns:
true
if the inventory contains the item,false
otherwise
-
hasItem
Checks if an item with the given name exists in the inventory.- Parameters:
name
- the name of the item.- Returns:
true
if the item is present,false
otherwise.
-
getIndex
int getIndex(int itemCode) Retrieves the index of an item with the specified code.- Parameters:
itemCode
- the code of the item to retrieve the index for- Returns:
- the index of the item in the inventory, or -1 if the item is not found
-
getIndex
Retrieves the index of the first occurrence of an item with the given name.- Parameters:
name
- the name of the item.- Returns:
- the index of the item, or -1 if the item is not found.
-
getAt
Retrieves the item at the specified index in the inventory.- Parameters:
index
- the index of the item in the inventory- Returns:
- the item at the specified index, or
null
if there is no item at that index - Throws:
ArrayIndexOutOfBoundsException
- if the index is out of range, i.e,(index < 0 || index >= capacity)
-
deleteItem
void deleteItem(int itemCode) Deletes an instance of an item with the specified code from the inventory.- Parameters:
itemCode
- the code of the item to delete
-
deleteItemAt
Deletes the item at the specified index in the inventory.- Parameters:
index
- the index of the item to delete- Returns:
- the item that was removed (or null if there was no item at that index)
-
clearInventory
void clearInventory()Clears all items from the inventory.Warning: This action is irreversible and will remove all items from the inventory.
-
useItem
Uses an item with the specified code from the inventory.- Parameters:
itemCode
- the code of the item to usecontext
- the context in which the item is used (seeItemUsageContext
)
-
useItemAt
Uses the item at the specified index in the inventory.- Parameters:
index
- the index of the item to usecontext
- the context in which the item is used (seeItemUsageContext
)
-
add
Adds an item to the inventory. If the item can be stacked, it may be added to an existing stack.- Parameters:
item
- the item to add to the inventory- Returns:
- the index at which the item was added (-1 if could not be added)
-
addAt
Adds an item to a specific index in the inventory, replacing any existing item at that index.- Parameters:
index
- the index at which to add the itemitem
- the item to add to the inventory
-
sortByCode
void sortByCode()Sorts the inventory by item code.
-