RPG - Entity

Text RPGs are very good projects to learn on, they include all the mechanics of a game (more than most infact - RPGs are the most complex type of game to create) without spending time learning how to create windows and display graphics and (unless you have a someone to do it for you) spending time making the 2d sprites or 3d models.

This tutorial has been split up into multiple parts to make it easier to understand. After creating the full RPG there will be some other tutorials about adding new features - Saving and Loading, Special Attacks and Loading Contents from Files.

The first step is to plan the RPG, I know to many people this will seem boring, but without a plan you will find the task much harder. The plan need only be for the technical aspects of the game - the story can follow later, however I am going to include the start of the story in my plan. This is the plan for my game - I would suggest that you follow the code that I show you, but change my content for your own, so this should be used as a guideline for your plan.

Wanderer's Quest
The player is passing through a town called Treness, when the town is beseiged by bandits. After helping the towns gaurd repell the attack the player hunts down the bandits, going through Open Fields into Dark Forest before going to the Bandit's Encampment. Over the course of the game the player will have to make some good/bad choices, leading to different endings.
The player can choose from one of three classes:
  • Dragonoid - Melee
  • Lich - Magical
  • Ice Elemental - Ranged
The player can equip a weapon and armour, gain gold through defeating enemies and buy various items that can be kept in his/her inventory. The player can level up, raising their attributes in doing so.
For the first draft of this game the levels will be function based, meaning that they are hardcoded into the game, this will be changed at a later stage. Lastly the player must be able to shop, rest at an inn and fight (multiple enemies, possibly with allies).

Now we can start into coding, the first thing we will code is the Entity class - from which the Player and Enemy class will be derived - this must contain information such as the Entity's health, strength, equipped items, etc. It must be able to fight, and for ease of testing we will include some functions to display it's statistics.

Start up Code::Blocks (or whatever IDE you use) and create a new project, create a file called "Entity.h" and write this:

#ifndef ENTITY_H
#define ENTITY_H

#include <string>
using namespace std;

class Entity{
private:
    string name;		// Player Name

    int health; 		// Player Health
    int maxHealth; 	// Maximum Health
    int mana; 		// Player Mana
    int maxMana; 		// Maximum Mana

    int strength; 		// Affects dealing and taking physical damage
    int intelligence; 	// Affects dealing and taking magical damage
    int dexterity; 	// Addects dealing and taking ranged damage
    int speed; 		// Determines the order of attacks in a fight
};

#endif

There we have all the variables that class Entity needs, now we need to add some accessor functions. You may have learned to use "get" and "set" functions for accessing private variables, which would require us to make "getHealth()", "setHealth(int newHealth)", "getMana()", etc. Which I find boring, but they do serve a purpose, I can put code in "setHealth(int newHealth)" which stops health being set higher than maxHealth, so that I wouldn't need to check every time I changed an Entity's health. Luckily there is a way that still allows us to stop health from being set higher than maxHealth, and saves us some typing - we can return a reference, which is a bit like a "get" and "set" method in one. For a more in depth explanation click here, but looking at my code should explain things sufficiently. All that said, I am only going to provide "get" methods for strength, intelligence, dexterity & speed as these shouldn't be changed apart from leveling up (which we will deal with later).

Add this after the variables:

public:
    int &Health();
    int &Mana();

    int getStr(){return strength;}
    int getInt(){return intelligence;}
    int getDex(){return dexterity;}
    int getSpeed(){return speed;}
    string getName(){return name;}

As the get methods are short I put them in the header file, however the reference methods are a bit longer, so create a new file called "Entity.cpp" and write this:

#include "Entity.h"

#include <string>
using namespace std;

int &Entity::Health(){
    if(health < 0)
        health = 0;
    if(health > maxHealth)
        health = maxHealth;
    return health;
}

int &Entity::Mana(){
    if(mana < 0)
        mana = 0;
    if(mana > maxMana)
        mana = maxMana;
    return mana;
}

That is the basics of a reference method, put the checks for invalid values of the variable at the top and then return it at the bottom, it is really simple and can be used easily.

Now lets create some constructors - in the file "Entity.h", just after the line "public:" add:

    Entity();
    Entity(string nName, int nHealth, int nMana, int nStr, int nInt,
            int nDex, int nSpeed);

And in "Entity.cpp" add this (anywhere):

Entity::Entity(){
    name = "Entity";
    
    health = maxHealth = 20;
    mana = maxMana = 20;
    
    strength = 7;
    intelligence = 7;
    dexterity = 7;
    speed = 7;
}

Entity::Entity(string nName, int nHealth, int nMana, int nStr, int nInt,
      int nDex, int nSpeed){
    name = nName;

    health = maxHealth = nHealth;
    mana = maxMana = nMana;

    strength = nStr;
    intelligence = nInt;
    dexterity = nDex;
    speed = nSpeed;
}

Now we just need to add a function for fighting (which at this point will be empty as fighting will be covered at a later point in this tutorial) and a function to display the player stats. Add this after the functions in "Entity.h":

    void Fight(Entity* enemy){}
    void DisplayStats();

And in "Entity.cpp" write this (again, anywhere you like):

void Entity::DisplayStats(){
    std::cout << "Entity - " << name << std::endl;
    std::cout << "-----------\n";
    std::cout << "Health:\t" << health << "/" << maxHealth << std::endl;
    std::cout << "Mana:\t" << mana << "/" << maxMana << std::endl;
    std::cout << "Str:\t" << strength << std::endl;
    std::cout << "Int:\t" << intelligence << std::endl;
    std::cout << "Dex:\t" << dexterity << std::endl;
    std::cout << "Speed:\t" << speed << std::endl;
}

And there we are, a base Entity class that we can build upon to create out Player and our Enemy class. If you want to, add a "main.cpp" file (in Code::Blocks this should have been automatically created for you), include "Entity.h" and create an Entity, display its stats, and see how the reference functions work, otherwise continue onto the next tutorial:


Next Tutorial --->

If you would like to get in contact, please email me at:
conn_peter@hotmail.com