Class InventoryComponent
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds one instance of the given item to the inventory.void
Addsamount
instances of the given item (stacking).Read only view of the inventory for UI renderingint
getItemCount
(String itemId) Gets the current stack count foritemId
, where the stack count is the number of instances of an item in the inventory.int
Gets the count for all instances of items in the inventory.boolean
Returns whether at least one instance ofitemId
exists in the inventory.void
removeItem
(String itemId) Removes all items withitemId
from the inventory.void
Uses one instance ofitemId
.void
Consumes a specified number of items from the inventory.Methods inherited from class com.csse3200.game.components.Component
create, dispose, earlyUpdate, getEntity, setEnabled, setEntity, toString, triggerEarlyUpdate, triggerUpdate, update
-
Constructor Details
-
InventoryComponent
public InventoryComponent() -
InventoryComponent
-
-
Method Details
-
getInventory
Read only view of the inventory for UI rendering -
addItem
Adds one instance of the given item to the inventory.If the item's config has
autoConsume == true
, its effects are applied immediately and the item is not stored. Otherwise the item is added to the inventory stack; if it is not yet present, a new stack is created with count 1.- Parameters:
itemId
- non-null identifier (e.g.,"key:red"
).
-
addItems
Addsamount
instances of the given item (stacking).If
autoConsume == true
, effects are applied once per instance and nothing is stored. Otherwise the inventory count is increased byamount
, creating a new stack if needed.- Parameters:
itemId
- non-null identifier (e.g.,"key:red"
).amount
- number of items to add (must be ≥ 1).- Throws:
IllegalArgumentException
- ifamount < 1
.
-
hasItem
Returns whether at least one instance ofitemId
exists in the inventory.- Parameters:
itemId
- non-null identifier of item.- Returns:
true
if the count is > 0; otherwisefalse
.
-
getItemCount
Gets the current stack count foritemId
, where the stack count is the number of instances of an item in the inventory.- Parameters:
itemId
- non-null identifier of item.- Returns:
- the stored count, or
0
if the item is absent
-
getTotalItemCount
public int getTotalItemCount()Gets the count for all instances of items in the inventory.- Returns:
- the stored count, or
0
if the inventory is empty
-
removeItem
Removes all items withitemId
from the inventory.- Parameters:
itemId
- non-null identifier to remove.
-
useItem
Uses one instance ofitemId
.Decrements inventory item count by 1 if the item is present in the inventory and the number of items is > 0. Also applies its effects.s
- Parameters:
itemId
- non-null identifier of item to decrement.
-
useItems
Consumes a specified number of items from the inventory.This method will decrement the count of the given
itemId
until either the requestedamount
has been used or the available quantity is depleted. If the item does not exist in the inventory or has a count of zero, no changes occur. Looks up the item's config and applies its effects. If no effect can be applied (e.g., using a heart at full HP), the item is not consumed.- Parameters:
itemId
- the identifier of the item to useamount
- the number of items to attempt to consume; if greater than the available count, all available items are consumed
-