9 Adding interactive devices and items within the game

 

This chapter covers

  • Programming doors that the player can open
  • Enabling physics simulations that scatter a stack of boxes
  • Building collectible items that players store in their inventory
  • Using code to manage game state, such as inventory data
  • Equipping and using inventory items

Implementing functional items is the next topic we’re going to focus on. Previous chapters covered a number of different elements of a complete game: movement, enemies, the UI, and so forth. But our projects have lacked anything to interact with other than enemies, nor have they had much in the way of game state. In this chapter, you’ll learn how to create functional devices like doors. We’ll also discuss collecting items, which involves both interacting with objects in the level and tracking game state. Games often have to track state like the player’s current stats, progress through objectives, and so on. The player’s inventory is an example of this sort of state, so you’ll build a code architecture that can keep track of items collected by the player. By the end of this chapter, you’ll have built a dynamic space that really feels like a game!

9.1 Creating doors and other devices

9.1.1 Doors that open and close on a keypress

9.1.2 Checking distance and facing before opening the door

9.1.3 Operating a color-changing monitor

9.2 Interacting with objects by bumping into them

9.2.1 Colliding with physics-enabled obstacles

9.2.2 Operating the door with a trigger object

9.2.3 Collecting items scattered around the level

9.3 Managing inventory data and game state

9.3.1 Setting up player and inventory managers

9.3.2 Programming the game managers

9.3.3 Storing inventory in a collection object: List vs. Dictionary

9.4 Inventory UI for using and equipping items

9.4.1 Displaying inventory items in the UI

9.4.2 Equipping a key to use on locked doors

9.4.3 Restoring the player’s health by consuming health packs

Summary