In this post I want to share how we approached attributes early on in a “Puzzle and Dragons” clone.
Let’s start with a representation of a character:
This JSON is part of a larger RPG database JSON file that is loaded at runtime. When a character is spawned, either in menus or dungeon, the common PAD formulas are used to determine the current character stats based on the dungeon level (for enemies) or the current experience from the save file (for human player).
Now lets take a look at the character class.
The above code introduces the RpgAttribute class.
The RpgAttribute class is used both by character and player. Player basically holds a list of characters. This is useful for the human player, where the health is a sum of all of the characters health values. In PAD, human characters do not die individually, it is the player that dies when the cumulative health reaches zero.
Attribute usage is pretty simple. To retrieve the current value you would call Health.CurrentValue, to change the health value you would call Health.Plus(x) or Health.Minus(x). The onChange callback is handy in this situation; when you alter the health value, the onChange callback is invoked where you can update the visual representation of health, such as the health bar. Similarly, the onZero callback of the human player’s health attribute signals defeat.
And here is how the human player’s health value gets affected.
The enemy is treated a little differently. Instead of manipulating the enemy player’s health, we manipulate the enemy character’s health directly. That is because enemy characters can be killed one by one, unlike human characters.