Inventory System - UQcsse3200/2024-studio-3 GitHub Wiki

Introduction

The inventory system for this game mainly involves the player inventory and implements an InventoryComponent class used to handle storage and retrieval of Items to and from the Inventory (found in components/player/InventoryComponent.java, package: com.csse3200.game.components.player). However, this component has been designed to be a flexible general component that can be used for any entity requiring an inventory or storage component of some sort (i.e. stations). Both the general functionality of the InventoryComponent and how it is used specifically for the player inventory are detailed below.

General Functionality

InventoryComponent provides similar functionality to an ArrayList of ItemComponent, but with additional features, such keeping track of a selected item. Items are stored in an ArrayList in the InventoryComponent, with each element being be initialised to null, which represents empty slots of the inventory.

Initialising the component

To create a new InventoryComponent, use the following: InventoryComponent variable = new InventoryComponent(capacity);

The capacity parameter specifies the maximum number of Items this component can hold. This will initialise the InventoryComponent to have this many slots, with each slot being initialised to null (empty).

Methods

The InventoryComponent has many methods. We do not expect all of them to be used, but provided them all to make the component as flexible as possible. For instance, outside of the player, selection functionality will most likely not be used.

  • getCapacity()/setCapacity(capacity) - get the capacity/set the capacity of the inventory to the given capacity
  • increaseCapacity(capacity) - same as setCapacity() but the given capacity must be greater than the current capacity.
  • getSize() - returns the number of items currently in the inventory
  • setSelected(index) - marks the item at the given index to be the selected item
  • getSelectedIndex()/getSelectedItem() - returns the index of the currently selected item, or the item itself, respectively
  • getItems() - returns a copy of the underlying ArrayList storing the ItemComponent objects
  • isFull()/isEmpty() - returns true if the number of items held is at the maximum capacity, or is equal to 0, respectively
  • getItemAt(index)/getItemFirst()/getItemLast() - returns the item at the given index, first slot, or last slot respectively
  • find(item) - returns true if there is an instance of item in the inventory, false otherwise
  • addItem(item) - adds the item to the first empty slot in the inventory
  • addItemAt(item, index) - adds the item to the given index (slot) in the inventory, replacing the current item if there is one
  • removeAt(index) - Removes and returns the item at the given index, if the slot is not empty
  • getItemNames() - Returns the names of all items in the InventoryComponent, in order.
  • findName(itemName) - Returns true if the itemName is in the inventory, false otherwise.
  • removeItemName(itemName) - Removes the first instance of the item with itemName from the InventoryComponent.

The above methods will throw errors if illegal or out of range inputs are provided. More information is provided in the Javadocs in InventoryComponent.java.

Player Inventory Component

The player uses an InventoryComponent to keep track of the item/s held. Currently, the studio seems to be divided on how many item slots the player's inventory should. The initial plan was to have 2 slots - one for each hand. However, some teams have said they prefer just having a single slot, similar to Overcooked, which is what many of the gameplay mechanics are based off of. Currently, the player inventory is implemented to have just 1 slot. Regardless of what is chosen, the InventoryComponent can handle both options since it was designed to be a flexible size. It also supports increasing the capacity of the inventory if the studio decides to allow upgrading the inventory size.

Displaying the player inventory

UPDATE: InventoryDisplay is not longer intended to be used as a general component (it now should only be used for the player inventory). For an inventory display component usable by station entities, see StationHoverComponent.

In order to display the slots of an inventory and the items being held, an InventoryDisplay component is used, which extends UIComponent. The InventoryDisplay component can be added to any Entity that also has an InventoryComponent, allowing the inventory to be displayed to the user. UPDATE: However, for this game, it is just used to display the player's inventory, using the following code in the PlayerFactory.java file:

Entity player = new Entity().addComponent(new InventoryComponent(config.inventorySize)).addComponent(new InventoryDisplay());

The result is an empty slot image being shown for each slot of the inventory (the player only has 1) located in the bottom left corner of the screen:

image

Any other entity can also use the InventoryDisplay component to display their inventory, as long as it has an InventoryComponent: just simply add the component like shown with the player above. (UPDATE: there should only be ONE InventoryDisplay being used in the game at once (for the player), since it is a UI element, so duplicates will just overlay on top of existing ones. To display the inventories of stations, a subclass of a StationHoverComponent should be used instead) NOTE: do not try to add an InventoryDisplay component to an Entity that does not have a InventoryComponent, as it will not work. The InventoryDisplay component renders and updates itself based on the state and events fired by the InventoryComponent, so it is important that an InventoryComponent exists on the entity.

Once both an InventoryDisplay and InventoryComponent are added to an Entity, you only need to change the InventoryComponent's state (via its methods), and the InventoryDisplay will update itself using events fired by the InventoryComponent.

Currently, all displays will be on the bottom left corner of the screen, but in the future, functionality will be added to be able to change where the display is shown, as well as hide and show the display (i.e. for inventories that should only appear when certain interactions occur). UPDATE: This functionality has been scrapped as the InventoryDisplay is not longer intended to be used as a general component (it now should only be used for the player inventory). For an inventory display component usable by stations, see StationHoverComponent.

Displaying Station Inventories

Station inventories have their inventories displayed differently. See Station Inventory and Visualisation

UML Class Diagram

The UML Class Diagram provides a visual guide to understanding the conceptual design behind the inventory system component. At this current moment in time, the UML Diagram only serve to represent a surface level of accuracy with the real inventory component. This is due to the fact that not all class attributes and operations has been listed on the diagram that is part of the program itself, but rather only a select few as an example. As the studio has not yet reached a general consensus on how the inventory system is expected to operate, the production of a highly accurate UML Diagram is to be delayed since most class attributes and operations have not been finalised.

UML Diagram

Here is a breakdown of the UML Diagram:

  • Player: A class that represents the player character in the game. The player has an InventoryComponent which stores what item the player is currently holding.
  • StationFactory: A class that represents the factory that manages and creates stations. It connects to the StationMealComponent since the stations are where the meals are created.
  • InventoryComponent: The general inventory component that contains all the functionality to store, manage and fetch items from the inventory of an entity. This is required for both the player and the stations in this example.
  • Component: The superclass of all the components used in this game.
  • ItemComponent: The component that stores the generic information required by every item in the game. This component mainly consists of methods to fetch information about the items. Each ItemComponent will have exactly one ItemType.
  • ItemType: An enumeration that stores the different types of items that are present in the game, used by ItemComponent.
  • StationMealComponent: The meal component used by the station to process and combine the given ingredients into a meal (if the ingredients provided can make a valid meal).
  • MealComponent: The component that manages information about the meals, like the quality and price which will be used in the scoring system to evaluate the users rating/score as well as provide revenue when served to customers.
  • IngredientComponent: The component that represents the ingredients in a meal which stores whether it is cookable, choppable, or not. This component essentially handles the preparation of ingredients (ItemComponents) before they are combined on a station.

This UML Diagram provides a grounded structure for a cooking game in the sense that:

  • The player can use items in their inventory to cook/combine items on a station to create a meal.
  • The station handles whether a meal can be made, and if it can then it creates the meal.
  • The cooking stations can store their own items including ingredients and meals.
  • The quality/score of the food will be calculated using the scoring system described here.

InventoryDisplay Component UML

The UML Class Diagram for the InventoryDisplay Component is shown below:

image

The explanation for this UML Diagram is as follows:

  • InventoryComponent: The general inventory component that contains all the functionality to store, manage and fetch items from the inventory of an entity.
  • ItemComponent: The component that stores the generic information required by every item in the game. This component mainly consists of methods to fetch information about the items.
  • IngredientComponent: The component that represents the ingredients in a meal which stores whether it is cookable, choppable, or not. This component essentially handles the preparation of ingredients (ItemComponents) before they are combined on a station.
  • Comparable: This interface defines the standard method to compare Renderable objects, which allows them to be sorted based on their Z-index for proper rendering.
  • Renderable: The component that defines the rules for objects that can be rendered on screen. It ensures all renderable objects are consistent with the methods for rendering.
  • Disposable: The component that defines a way to clean up resources. It ensures that objects release their resources when they are no longer needed.
  • Component: The superclass of all the components used in this game. It provides the foundational structure for all components.
  • RenderComponent: The component that adds rendering capabilities. It serves as a base for components that requires rendering something on the game screen.
  • UIComponent: The component that specialises in UI rendering, serving as the base for all UI-related components.
  • InventoryDisplay: The component that handles the display of the player inventory system.