We’ve almost got a playable game, I promise! I think that jumping backwards and forwards is quite a confusing way to learn, hence why we’re building all the individual components separately and then joining them all together at the end. Anyway, in this section we’ll begin to think about how fighting will work in this game, and we’ll be creating a Creature class accordingly.

The battle system in this game will be very simple; every fight will involve two sides—the player and the enemies—who will take turns attacking each other. Unlike in some RPGs a turn will not consist of all the enemies attacking and then the player attacking, but rather every creature involved will attack at a separate time independent of their side. To determine the order in which each creature attacks, we will use an agility value; the higher their agility, the earlier they attack. Once all creatures have attacked once, the turn starts again.

When a creature attacks, they will not be guaranteed to hit and will instead have a chance to do damage that decreases as the defender’s evasion increases. If the attacker hits the defender the amount of damage done will be determined by what weapon the attacker has equipped, how much strength the attacker has, and what armor the defender has equipped. It will also be modified by a small random factor to add a little bit of excitement into the mix! The amount of damage the attacker does will be subtracted from the defender’s hit points, and when their hit points are reduced to zero the defender will die.

With that in mind, the Creature class should have hp, strength, agility, evasion, equippedWeapon, and equippedArmor member variables. To allow for healing and restoration of hit points we should also have a maxHp value, and of course each Creature should also have a name. Finally, no RPG is complete without experience points and loot drops, so we’ll also give each Creature an xp value and an inventory. It’ll also be important to know which Area the Creature is in, and we should add a function to allow Creatures to move from one Area to another.

Note that we’ve forward-declared each class that we’re only storing a pointer to, but we’ve had to #include "inventory.hpp" because we are storing an actual instance of Inventory instead of a pointer. The usual constructors and JSON-related functions are present, but we’ve got a few more functions as well to make dealing with the Creature a little easier, most notably the getAreaPtr function. So far we’ve almost exclusively stored pointers to entities instead of their ids, but here we’ve stored the id instead, and have added a function to convert it to a pointer when necessary. Honestly there isn’t much of a reason for this besides making things a little bit simpler later on, so feel free to change it back to the usual method.

Most of the functions here are just variations on ones we’ve seen before (though there’s a lot more data involved here), but traverse is more interesting. The traverse function requires a Door that the Creature is attempting to travel through. First it checks the locked property of the door. If the door is closed but unlocked or locked but the Creature has the key then it is opened. Once the door is opened the Creature is moved to the other Area linked to by the door. It makes sense to make traverse a member function of Creature and not Door because the Creature is the one doing the action (though it’s perfectly possible to do it the other way around).

Before moving on, don’t forget to load the creatures from a JSON file similarly to the other entities we’ve created, and also add the template specialisations and instantiations to entity_manager.cpp. Since the Creature class makes use of Weapons and Armor, load creatures.json after those.

### Creatures and Areas

We’ve said that we want a battle system, but how is the player going to encounter all those other Creatures in the first place? Since they’re entities that appear in the game world, it makes sense to add them to the Area class like everything else. This isn’t very hard to do so I’ll leave it mostly to you, but there is something you have to take into account that’s easy to miss.

Note that Creatures are the only entities that are both modifiable (Areas also are) and multi-instance (Items also are). Modifying an Area was fine because each Area was only ever used once, and using the same Item multiple times was fine because they were never modified. Creatures however will be reused many times (imagine having to create a separate JSON entry every time a Rat appeared) but will also take damage, and so will be modified. If we store pointers to their positions in the Entity Manager, every Rat will be identical and when one Rat takes damage, they all will! Obviously this would be disastrous, so instead of storing pointers to Creatures in the Area we should store actual Creatures. The Creature constructor can still use pointers though, because we can leave the duplication up to the Area class. Hence

To build the member creatures list, iterate over the argument creatures list and add use * to dereference the creature and add it to the list. Now for the JSON changes. These aren’t much harder, you just have to account for the presence of a "creatures" key, e.g. for load