Class InventoryComponent
java.lang.Object
com.csse3200.game.components.Component
com.csse3200.game.components.player.InventoryComponent
A generic inventory component that is used to keep track of stored items
for an entity
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addItem
(ItemComponent item) Adds the item to the first empty index of the inventory.void
addItemAt
(ItemComponent item, int index) Adds the item to the Inventory at the given index, if there is capacity.boolean
find
(ItemComponent item) Checks if at least one instance of item is in the Inventory.boolean
Returns true if the itemName is in the inventory, false otherwise.int
Returns the capacity of this inventory component.getItemAt
(int index) Returns the item at index if it exists in the current inventory.Returns the first element of the Inventory if it is not empty.Returns the last element of the Inventory if it is not empty.Returns the names of all items present in the list, in order.getItems()
Returns an ArrayList containing the items stored in this InventoryComponent.int
Returns the index of the currently selected item.Returns the currently selected item of the inventoryint
getSize()
Returns the current number of items in the inventory component.void
increaseCapacity
(int newCapacity) Increases the capacity of the Inventory if the given newSize is larger than the current size.boolean
isEmpty()
Returns true if the Inventory data structure is empty.boolean
isFull()
Returns true if Inventory data structure is full.removeAt
(int index) Removes and returns the item from the Inventory at the given index, if it exists.Removes the item in last non-empty index of the inventory, specifically for meal stationremoveItemName
(String itemName) Removes the first instance of the item with itemName from the Inventory.void
setCapacity
(int newCapacity) Sets the size of this item component to the newSize provided.void
setSelected
(int index) Sets the currently selected item to the index specified.Methods inherited from class com.csse3200.game.components.Component
create, dispose, earlyUpdate, getEntity, setEnabled, setEntity, toString, triggerEarlyUpdate, triggerUpdate, update
-
Constructor Details
-
InventoryComponent
public InventoryComponent(int capacity) Creates inventory component- Parameters:
capacity
- the players inventory size
-
-
Method Details
-
getCapacity
public int getCapacity()Returns the capacity of this inventory component. I.e. the maximum number of items it can hold.- Returns:
- - the set capacity of the inventory.
-
getSize
public int getSize()Returns the current number of items in the inventory component.- Returns:
- - the number of items in the inventory.
-
setCapacity
public void setCapacity(int newCapacity) Sets the size of this item component to the newSize provided.- Parameters:
newCapacity
- - the size to set this item component's capacity to. Must be a positive, non-zero integer.- Throws:
IllegalArgumentException
- - if newSize is less than 1.
-
setSelected
public void setSelected(int index) Sets the currently selected item to the index specified.- Parameters:
index
- - the index to set the currently selected item to- Throws:
IllegalArgumentException
- - if index is not within 0 and this.getSize()
-
getSelectedIndex
public int getSelectedIndex()Returns the index of the currently selected item.- Returns:
- - the index of the currently selected item.
-
getSelectedItem
Returns the currently selected item of the inventory- Returns:
- - the item that is currently selected
-
getItems
Returns an ArrayList containing the items stored in this InventoryComponent.- Returns:
- - an ArrayList containing items currently stored in the inventory.
-
isFull
public boolean isFull()Returns true if Inventory data structure is full.- Returns:
- - true if the ArrayList is full, false otherwise.
-
isEmpty
public boolean isEmpty()Returns true if the Inventory data structure is empty.- Returns:
- - true if the ArrayList is empty, true otherwise.
-
getItemAt
Returns the item at index if it exists in the current inventory.- Parameters:
index
- - the index being queried.- Returns:
- the item at index if it exists in the current inventory, null if there is nothing there.
- Throws:
IllegalArgumentException
- - if the given index is negative or out of bounds.
-
getItemFirst
Returns the first element of the Inventory if it is not empty.- Returns:
- - the item at the first index of the Inventory if it exists, null otherwise.
-
getItemLast
Returns the last element of the Inventory if it is not empty.- Returns:
- - the item at the last index of the Inventory if it exists, null otherwise.
-
increaseCapacity
public void increaseCapacity(int newCapacity) Increases the capacity of the Inventory if the given newSize is larger than the current size.- Parameters:
newCapacity
- the new capacity of the Inventory.
-
find
Checks if at least one instance of item is in the Inventory.- Parameters:
item
- - the item to search the Inventory for.- Returns:
- - true if there is at least one instance of item, false otherwise.
-
addItem
Adds the item to the first empty index of the inventory.- Parameters:
item
- - the item to be added to the Inventory.
-
removeItem
Removes the item in last non-empty index of the inventory, specifically for meal station -
addItemAt
Adds the item to the Inventory at the given index, if there is capacity.- Parameters:
item
- - the item to be added to the Inventory.index
- - the index where the item will be inserted.- Throws:
IllegalArgumentException
- - if the given index is negative or out of bounds.
-
removeAt
Removes and returns the item from the Inventory at the given index, if it exists.- Parameters:
index
- - the index where an item will be removed.- Throws:
IllegalArgumentException
- - if the given index is negative or out of bounds.
-
getItemNames
Returns the names of all items present in the list, in order.- Returns:
- - the list of names of items in the inventory, null if empty.
-
findName
Returns true if the itemName is in the inventory, false otherwise.- Parameters:
itemName
- - the item being checked for in this inventory.- Returns:
- - true if item is in the inventory, false otherwise.
-
removeItemName
Removes the first instance of the item with itemName from the Inventory.- Parameters:
itemName
- - the name of the item to be removed.- Returns:
- - the removed item i, null if not present.
-