In this chapter, we will cover the following topics:
Creating a simple 2D mini-game – SpaceGirl
Displaying single object pickups with carrying and not-carrying text
Displaying single object pickups with carrying and not-carrying icons
Displaying multiple pickups of the same object with text totals
Displaying multiple pickups of the same object with multiple status icons
Revealing icons for multiple object pickups by changing the size of a tiled image
Displaying multiple pickups of different objects as a list of text via a dynamic List<> of PickUp objects
Displaying multiple pickups of different objects as text totals via a dynamic Dictionary<> of PickUp objects and "enum" pickup types
Generalizing multiple icon displays using UI Grid Layout Groups (with scrollbars!)
Introduction
Many games involve the player collecting items or choosing from a selection of items. Examples could be collecting keys to open doors, collecting ammo for weapons, choosing from a collection of spells to cast, and so on.
The recipes in this chapter offer a range of solutions for displaying to the player whether they are carrying an item or not, if they are allowed more than one of an item, and how many they have.
The big picture
The two parts of software design for implementing inventories relate to, first, how we choose to represent the data about inventory items (that is, the data types and structures to store the data) and, secondly, how we choose to display information about inventory items to the player (the UI: User Interface).
Also, whilst not strictly inventory items, player properties such as lives left, health, or time remaining can also be designed around the same concepts that we present in this chapter.
We need to first think about the nature of different inventory items for any particular game:
Single items:
Example(s): the only key for a level, our suit of magic armor
Data type: bool (true/false)
UI: nothing (if not carried) or text/image to show being carried
Or perhaps text saying "no key"/"key", or two images, one showing an empty key outline and the second showing a full color key
If we wish to highlight to the player that there is an option to be carrying this item
Continuous item:
Example(s): time left, health, shield strength
Data type: float (for example, 0.00–1.00) or integer scale (for example, 0% .. 100%)
UI: text number or image progress bar/pie chart
Two or more of same item
Example(s): lives left, or number of arrows or bullets left
Data type: int (whole numbers)
UI: text count or images
Collection of related items
Example(s): keys of different colors to open correspondingly colored doors, potions of different strength with different titles
Data structure: a struct or class for the general item type (for example, class Key (color/cost/doorOpenTagString), stored as an array or List<>
UI: text list or list/grid arrangement of icons
Collection of different items
Example(s): keys, potions, weapons, tools—all in the same inventory system
Data structure: List<> or Dictionary<> or array of objects, which can be instances of different class for each item type
Each of the above representations and UI display methods are illustrated by the recipes in this chapter.