Class AbstractItem
java.lang.Object
com.csse3200.game.inventory.items.AbstractItem
- Direct Known Subclasses:
ConsumableItem
Abstract base class for items that can be used by a player.
This class provides a common implementation for an item including default behavior for common
properties like name and limit. Subclasses must provide their own implementation for the
useItem
method, which defines how the item can be used and must define what
ItemUsageContext
should be input to useItem
.
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractItem
(String name, int itemCode) Constructs an AbstractItem with the specified name and item code.protected
AbstractItem
(String name, int itemCode, int limit, int quantity) Constructs an AbstractItem with the specified name, item code, limit and initial quantity. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(int n) Increases the current quantity of the item by n.Returns a description of how the current item can be used and what it doesint
Gets the item code of the item.int
getLimit()
Gets the maximum number of this item that can be held in a `stack`.getName()
Gets the name of the item.int
Gets the quantity of the item currently stacked.Retrieves the texture of the object.boolean
isEmpty()
Returns whether the quantity of this item has reached 0 (ie is consumed).int
Checks the number of this item that can be further stacked.boolean
Whether an item can only be used in combatboolean
Whether an item can only be used on the mapvoid
setDescription
(String description) If what item can do, update the description of current itemprotected void
setTexturePath
(String texturePath) Sets the path to the texture of the object using the provided file path.abstract void
useItem
(ItemUsageContext context) Uses the item with the providedItemUsageContext
and returns a result.
-
Field Details
-
name
-
itemCode
protected final int itemCode -
limit
protected final int limit -
quantity
protected int quantity -
description
-
onlyCombatItem
protected boolean onlyCombatItem -
onlyMapItem
protected boolean onlyMapItem
-
-
Constructor Details
-
AbstractItem
Constructs an AbstractItem with the specified name and item code. Defaults to single non-stackable item.- Parameters:
name
- name of the itemitemCode
- unique item code (up to name)Note - all concrete subclasses must provide a unique itemCode
-
AbstractItem
Constructs an AbstractItem with the specified name, item code, limit and initial quantity.- Parameters:
name
- name of the itemitemCode
- unique item code (up to name)limit
- the maximum size of the item stackquantity
- the initial starting quantity of the item- Throws:
IllegalArgumentException
- if limit or quantity is negative, or quantity > limitNote - all concrete subclasses must provide a unique itemCode
-
-
Method Details
-
getName
Gets the name of the item.- Returns:
- the name of the item
-
onlyCombatItem
public boolean onlyCombatItem()Whether an item can only be used in combat -
onlyMapItem
public boolean onlyMapItem()Whether an item can only be used on the map -
getItemCode
public int getItemCode()Gets the item code of the item.- Returns:
- the code of the item
-
getLimit
public int getLimit()Gets the maximum number of this item that can be held in a `stack`.- Returns:
- the stack limit of the item
-
getQuantity
public int getQuantity()Gets the quantity of the item currently stacked.- Returns:
- the quantity of the item
-
getDescription
Returns a description of how the current item can be used and what it does- Returns:
- description of item
-
setDescription
If what item can do, update the description of current item- Parameters:
description
- description of item
-
numAddable
public int numAddable()Checks the number of this item that can be further stacked.- Returns:
- how much the quantity of this item can increase by
-
add
public void add(int n) Increases the current quantity of the item by n.- Parameters:
n
- - number of item to add- Throws:
IllegalArgumentException
- if the number of items cannot be increased by n
-
useItem
Uses the item with the providedItemUsageContext
and returns a result.This method must be implemented by subclasses to define the specific behavior of the item when used. The result type is defined by the generic parameters of the subclass. The input type
- Parameters:
context
- the context for this item usage
-
isEmpty
public boolean isEmpty()Returns whether the quantity of this item has reached 0 (ie is consumed).- Returns:
- whether there are none of this item remaining
-
setTexturePath
Sets the path to the texture of the object using the provided file path.- Parameters:
texturePath
- The file path of the texture to set.
-
getTexturePath
Retrieves the texture of the object.- Returns:
- The path to the texture associated with this object.
- Throws:
IllegalAccessError
- if the texture has not been set prior to calling this method.
-