The Legend of the Tomato Quest Tutorial - Player Stats

From SwinGame

Contents

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

//Attributes
public int Strength;
public int Vitality;
public int Agility;
public int Intelligence;
public int Luck;
 
//Stats
public int Attack;
public int MagicAttack;
public int Defense;
public int Evasion;
public int AttackSpeed;
public Double CriticalRate;
public int Health;
public int MaxHealth;
public int Mana;
public int MaxMana;
public int StatPoints;
public int SkillPoints;
public int Level;
public int Experience;
public int ExperienceNextLevel;

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:

public void AddAttribute(String attributeToAdd)
{
    //If the Character has Stat Points
    if (StatPoints > 0)
    {
        switch (attributeToAdd)
        {
            case "Strength":
                //Subract a stat point, Add Strength
                StatPoints--;
                Strength++;
                break;
 
            case "Agility":
                //Subract a stat point, Add Agility
                StatPoints--;
                Agility++;
                break;
 
            case "Vitality":
                //Subract a stat point, Add Vitality
                StatPoints--;
                Vitality++;
                break;
 
            case "Intelligence":
                //Subract a stat point, Add Intelligence
                StatPoints--; 
                Intelligence++;
                break;
 
            case "Luck":
                //Subract a stat point, Add Luck
                StatPoints--; 
                Luck++;
                break;
        }
    }
}

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:

public void RefreshCharacterStats()
{
    //Health = Base(20) + Vitality * 10
    MaxHealth = 20 + (Vitality * 10);
 
    //If characters current health is over the new max health, reduce character health
    if (Health > MaxHealth)
    {
        Health = MaxHealth;
    }
 
    //Mana = Base(10) + Intelligence * 5
    MaxMana = 10 + (Intelligence * 5);
 
    //If characters current mana is over the new max mana, reduce character mana
    if (Mana > MaxMana)
    {
        Mana = MaxMana;
    }
 
    //if the players experience is equal or greater then the next experience level, character gains a new level.
    if (Experience >= ExperienceNextLevel)
    {
        //Increase level by 1
        Level++;
 
        //Find the remaining experience to carry over to the next level, and set characters experience to it.
        Experience = Experience - ExperienceNextLevel;
 
        //Increase the amount of experience needed to get the next level
        ExperienceNextLevel = (int)(ExperienceNextLevel * 1.5);
 
        //Give the Character some stat Points
        StatPoints = StatPoints + 5;
 
        //Give the Character a skill point
        SkillPoints++;
    }
 
    //Attack = Base(5) + Stength
    Attack = 5 + Strength;
 
    //Magic Attack = Base(5) + Intelligence
    MagicAttack = 5 + Intelligence;
 
    //Defense = Base(1) + Vitality
    Defense = 1 + Vitality;
 
    //Evasion = Base(1%) + (Agility / 2)
    Evasion = (int)(1 + (Agility / 2));
 
    //The lower the Attack Speed, the faster the attack
    //Attack Speed = Base(60) - (Agility)
    AttackSpeed = 60 - Agility;
 
    //Attack Speed must be at least 1.
    if (AttackSpeed <= 0)
    {
        AttackSpeed = 1;
    }
 
    //Critical Rate = Base(1%) + (Luck)
    CriticalRate = 1 + Luck;
}

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.

Warning: Be aware, that the Constructor declaration has changed, be sure to change it in your project as well
//Character Constructor
 
//MODIFY THE CONSTRUCTOR DECLARATION
 
public Character(String name, int SpawnX, int SpawnY, int strength, int vitality, int agility, int intelligence, int luck)
{
    //Load the Character Sprite
    _Sprite = Graphics.CreateSprite(Resources.GameImage(name), 3, 12, 24, 32);
 
    //Position the Character
    _Sprite.xPos = SpawnX;
    _Sprite.yPos = SpawnY;
 
    _Sprite.EndingAction = SpriteEndingAction.ReverseLoop;
    _Sprite.Movement.SetTo(Physics.CreateVector(0, 0));
    _Anim = CharacterAnim.None;
 
    //ADD THESE LINES OF CODE
    Strength = strength;
    Vitality = vitality;
    Agility = agility;
    Intelligence = intelligence;
    Luck = luck;
 
    //ADD THIS LINE OF CODE
    this.RefreshCharacterStats();
 
    //ADD THESE LINES OF CODE
    Health = MaxHealth;
    Mana = MaxMana;
    StatPoints = 8;
    SkillPoints = 0;
    Level = 1;
    Experience = 0;
    ExperienceNextLevel = 100;
}

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

public Game()
{
    _Player = new Character("Hero", _Map.EventPositionX(PLAYERSPAWN, 0), _Map.EventPositionY(PLAYERSPAWN, 0), 10, 10, 10, 10, 10);
}

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

public void Run()
{
    //Draw the Map
    _Map.DrawLevel();
 
    //Update Player with Controls
    _Controller.UpdateInput(_Player, _Map);
 
    //Draw Player
    _Player.DrawCharacter();
 
    //ADD THIS LINE OF CODE
    //Refresh Player Stats
    _Player.RefreshCharacterStats();
 
    //Make the Camera follow the Player
    Camera.FollowSprite(_Player.Sprite, 0, 0);
 
    //Draw the FrameRate
    Text.DrawFramerate(200, 0, Resources.GameFont("Courier"));  
}

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 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.

Tutorial Table of Contents

  1. The Legend of the Tomato Quest Tutorial
  2. Loading the level
  3. The Player
    1. Loading and Drawing the Player
    2. Moving the Player
  4. Player Stats
  5. User Interface
  6. Attacking Animation
  7. AI
    1. The Healer
    2. The Healer II
    3. Enemies
    4. Enemies II
  8. Interaction
  9. Combat
  10. Items and End Game Conditions
  11. Additions and Improvements