• 2D shoot 'em up
SDL2 Santa game tutorial 🎅
SDL2 Shooter 3 tutorial
The Legend of Edgar 1.36
SDL2 map editor tutorial [UPDATED]
TBFTSS: The Pandoran War - Amiga OS4 Port
— Simple 2D adventure game —
In the last tutorial, we worked on simple inventory management. With that done, we can expand our gameplay somewhat. In this tutorial, we'll see how we can use an inventory item (a rusty key) to interact with a dungeon element (a chest), to receive the object contained within.
Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./adventure06 to run the code. You will see a window open like the one above, showing the prisoner on a tiled background. Use WASD to move around. The two chests are locked and require a key to open (the key can be found nearby). Pick up the key and then walk into one of the chests to open it. The key will disappear from your inventory and be replaced by the item in the chest - either an eyeball or a red potion. Only one key exists in the dungeon, so choose wisely! Close the window to exit.
Inspecting the code
Compared to the last tutorial, this one is thankfully much shorter. We'll start by looking at structs.h, where we've introduced a new struct:
The Chest struct is used to detail a chest, in the same way as the Prisoner struct details the prisoner. It holds the open state of the chest (isOpen) and also a pointer to the entity that will be the item within.
We've made updates to chest.c, to use the new Chest struct, assigning an instance to the chest entity's data pointer:
The initChest function has also been updated to take an entity as the item contained within. We've made some assumptions here (and throughout the tutorial) as to this item. Most importantly, it will only ever be one item and the item itself is assumed not to be NULL.
The first thing we do in the function now is malloc a Chest object. We then assign the item we passed into the initChest function to the Chest. With that done, we remove the item from the dungeon, with a call to removeEntityFromDungeon. The reason for this is because own spawnEntity function always adds entities to the dungeon when they are created. We could've made a new function to not do this or tweak the spawnEntity function to made the adding optional, but there's no harm in leaving it as is, since most entities will be added to the dungeon anyway.
The final change is to assign the created Object to the chest entity's data field.
We've made some big changes to the touch function, and this is where the bulk of our interaction logic will lie. Previously, the chest would simply respond to a touch call with a message saying it is locked. Now, the chest will interrogate the player's inventory to see if they have a key. Take a look at the function below, and then we'll run through it:
As before, the first thing we do is test if the touching entity is the player. If so, we grab the Chest object from the chest's data field. We then test to see if the chest is already open (isOpen). If it is, we don't do anything else. However, if it isn't open, we made a call to a new function called hasInventoryItem, to see if the player has a "Rusty key". We'll see more on this function when we come to inventory.c. For now, know that this function will return 0 (false) or 1 (true) if the named entity exists in the player's inventory.
If we don't have a Rusty key, the old "It's locked ..." message will be displayed. If we do have the key, we move onto the phase of fetching the item from the chest. The first thing we do is set the Chest's isOpen flag to 1, and then update the chest's texture. As the chest is now open, we'll draw it using a different texture to when its closed. This is just a case of calling getAtlasImage. We've decided in our game that keys may only be used once, and so we remove the Rusty key from the player's inventory. The removeInventoryItem will return the removed entity, allowing us to manipulate it further. In this case, we want to destroy it. We do this by setting its alive state to ALIVE_DEAD and adding it back into the dungeon, so that the doEntities loop can destroy it correctly (the doEntities loop ensure that all the data is freed as expected).
With the chest open and the key removed, the only thing left to do is fetch the item from the chest. For this, we call addToInventory, passing over the Chest's item. We don't need to perform a check if it can be added to the inventory, since we already know there is a free slot, having removed the key we were holding. We then set an information message to say that the item has been retrieved, and finally set the chest's item to NULL, effectively marking the Chest as empty.
That's our updates to chest.c done. We can now look at the updates that we have done with inventory.c, to support the interaction with the chest. As we already saw, we added in two new functions. We'll start by looking at hasInventoryItem:
It should be fairly clear what's going on here. We're grabbing the Prisoner data from the player entity, and then looping through the inventory slots to look for an entity with the name of the argument we've passed in. Should we find a match, we'll return 1 (true). If we reach the end of our inventory list and find nothing, we'll return 0 (false).
The other function we added is removeInventoryItem. There won't be too many surprises here:
Again, we're looping through the player's inventory, searching for the named item. When we find it, we're grabbing a reference to it, NULLing the inventory slot to effectively mark it as unused, and then returning the entity that formerly occupied the slot. If we find nothing, we're returning NULL. Under different circumstance, we'd need to be careful that the item we removed isn't NULL. However, in our case we already checked to see if it existed before removing it, so we're safe.
We're almost done with our update. Let's take a quick look at how we're making use of the new initChest function, done in the initDungeon function:
We're creating our Rusty key as normal, placing it away from the chests (to give the prisoner some exercise!). We're then calling initItem, to create an eyeball, and grabbing the returned reference to the entity (this is a very minor change that we're not covering here). With item reference in hand, we're calling initChest and passing the eyeball over. We're doing the same thing with the red potion. In effect, we're creating the eyeball and red potion, and placing them inside a chest each.
The final tweak comes in our drawEntities loop, in entities.c:
We're checking that the entity isn't dead before drawing it. This is because used items, such as the rusty key, are returned to the dungeon for clean up. However, they might be briefly drawn before being removed, depending on the order in which entities are processed. This is just a precaution to prevent undesirable graphics glitches.
So, there we have it - the ability to manage an inventory and use it to interact with other entities in the dungeon. Our game is starting to take shape. A few more updates and tweaks, and we'll be ready to implement the game proper. At this point, something that would make the dungeon more interesting is a fog of war effect, meaning that the map will be hidden in darkness until we start to explore it. This will grant a lot of mystery to the proceedings. We'll take that in the next part.
The source code for all parts of this tutorial (including assets) is available for purchase:
It is also available as part of the SDL2 tutorial bundle: