The Legend of the Tomato Quest Tutorial - Player Stats

The Players Stats
First we need to decide how to structure our Attributes and Stats for our players.

For attributes I've decided on the following:
 * 1) Strength
 * 2) Vitality
 * 3) Agility
 * 4) Intelligence
 * 5) Luck

For stats I've decided on these:
 * 1) Health
 * 2) Attack
 * 3) Magic Attack
 * 4) Defense
 * 5) Evasion
 * 6) Attack Speed
 * 7) Critical Rate

With this we can now start making our structures for the character to use.

Building the Character Attribute and Stat Structures
The first thing we need to build is the Attribute's and Stat's in our character class.

Add the following fields at the top of the Character class in Character.cs

Now we have fields that will store all of our Attribute and Stat data for each character we create.

You will notice that, apart from the stats we decided on earlier, we have included other stats as well. We added a MaxHealth stat, because we need to know the maximum amount of health a player can health, the Health stat is merely how much health the player has at any current time, same goes for mana

We've also included a StatPoints stat, this will be used for when the player wants to add some stats to his attributes. Everytime the player grows a level, he will be given some stats, to allocate on either Strength, Vitality or Agility.

The last 3 stats, are Level, Experience, and ExperienceNextLevel. These stats are for the player's level, how much experience the player currently has, and how much experience is needed for the player to level up.

Refreshing and Adding Stats
Before we go ahead and add these stats to our player, we need a way to.
 * 1) Add a stat to a player when he has a stat point
 * 2) A way to refresh stats every frame, so that the stats are updated when the player adds a stat to one of his attributes.

Add the following method inside the Character class in Character.cs:

What this new routine does, is take in a a String called attributeToAdd. The String is basically just either "Strength", "Agility", "Vitality", "Intelligence" or "Luck", and we use this to determine which stat to add.

In each case, we first subtract a stat point from the characters stat point, since he is using that stat point to increase an attribute. Next we increase the stat that we want to increase by 1.

There is one more routine we need to make in order to get our stats working. We need a routine that we can use that will update all the stats, such as Health, Attack, Defense, every loop, so that it keeps up to date when we grow a level and add stats.

The following routine handles this. Add this method inside the Character class inside Character.cs:

This routine is fairly straight forward. It goes through all the stats and updates them accordingly. There are comments in the code which tells you what the algorithm is for calculating each stat, and can each be easily modified.

You will also notice in the above code, there is a segment on leveling up. What happens here is once the characters experience is equal to or beyond the experience next level, the character grow's a level, is given some stat points to use on his character, and any left over experience is carried over to the next level. We also increase how much experience is needed to grow to the next level by multiplying it by 1.5, this makes it more challenging to get a level next time.

Adding the Stats to our Character
Finally, we can go ahead and edit our characters stats. To do so, we'll need to modify the Character constructor method in the Character class, inside Character.cs, change it to look like the following.

The lines that have been added have comments above them with "Add these lines" or something similar. We've also changed the declaration of the method to include 3 new parameters, Strength, Vitality, and Agility. This is so when we create a new character, we can specify how much of each attribute the character starts with.

The rest of the code simply sets the default values for things like Health, StatPoints, Level, Experience, and ExperienceNextLevel.

The Final Touches
There are some final things we need to add to make sure our stats are updated correctly.

Modify the creating of the _Player Character with the single line of code as indicated in the below code to include the 5 new attributes, to the Game constructor method in the Game class in Game.cs

Also, add the following line of the code to the Run method in the Game class in Game.cs

And now our stats will update every frame.

There isn't much to look at, since we cant see our stats yet. But the next tutorial will be focusing on User Interface's so we can see our stats, health and mana, and also add some stats.

Project so far
The source code for this tutorial can be found [[Media:RPGTutorial4.zip|Here]].

I strongly advise that as you go through each tutorial, you look through the source code, it will help you understand where everything should go, and how things are structured, and help to avoid confusion.