Buyable items - UQcsse3200/2024-studio-1 GitHub Wiki
Overview
A buyable item is an item that can be purchased by the player. A buyable item has a price, that is 10 by default. A price tag of the buyable item price is also shown below it. The player can purchase the item if they have sufficient funds, which is defined by the amount of "coins" that have been accumulated. A UI display will appear if the player cannot purchase a buyable item. Otherwise, the item is immediately "picked up". This means that UsableItems will be immediately added to the inventory, and BuffItems will be immediately applied.
Key Features of buyable items
- Keyboard presses: When the player is in collision with a buyable item, the player can attempt to purchase that item by pressing "P". This then triggers a method in ItemPickupComponent that checks if that item is affordable.
- BuyableComponent: The buyable component is attached to an item if ":buyable" is included in its specification when it is created in the CollectibleFactory. This component adds new methods to the item such as setting the cost and getting the cost. Upon creation, the initial cost of a buyable item is 10. If an item has a BuyableComponent, it cannot be "picked up" like a regular item.
- Checking affordability: The method
checkItemPurchase
in ItemPickupComponent checks for affordability by comparing the player's funds and the cost of the item. The player's funds are obtained by callinggetCoins()
of thecoinsComponent
which is attached to the player. The cost of the buyable item is obtained by callinggetCost()
of theBuyableComponent
which attached to the item.
Relevant Classes
- BuyableComponent.java: Defines an item to be "buyable". Has getter and setter methods. Also handles the creation and removal of the "price tag" label
- CoinsComponent.java: Defines the player funds, and thus, helps determine whether an item is affordable or not
- ItemPickupComponent.java: Handles the main functionality for checking whether an item can be purchased or not. Handles this by getting the cost of the item, the funds of the player and the item that is being attempted to purchase (the item in collision)
Class usage
CollectibleFactory collectibleFactory = new CollectibleFactory()
Entity collectible = collectibleFactory.createCollectibleEntity("item:medkit:buyable")
//Turns a medkit into a buyable item
Testing
Refer to BuyableTest() for testing methods