| Player and Enemy Classes | ||||||||
|
||||||||
|
Now we need to create classes for the Player and the Enemy, they will need to have a fight function, the player will also need to have money, inventory and experience while the enemy will have experience given, items dropped and any battle-cries. The extra variables will be easy to create, the fight will be kept simple for now and dealt with later. First though we need to declare the Fight function in the Entity class as virtual so that it can be overloaded.
virtual void Fight(Entity* enemy){}
The reason I put the '{}' there is because as long as the function isn't defined the Entity class will not compile. Now create a file Player.h and enter this code as the base for the class:
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <string>
using namespace std;
#include "Entity.h"
class Player : public Entity{
private:
int level;
int exp;
int gold;
int persuasion; // Persuasion affects item prices and maybe some enemies
public:
int getLevel(){return level;}
int getExp(){return exp;}
int getPer(){return persuasion;}
int &Gold();
bool enoughGold(int amount);
void giveExp(int exp);
void Fight(Entity* enemy){}
};
#endif
Again the player's level, experience and persuasion should not be edited so we only put 'get' methods for them. We give the player some variables and functions to handle how much gold they have. The giveExp function is where the levelling up happens and the player's stats get increased. And finally we overload the Entity's fight function. Now we need the code for the int &Gold() function, create a new file called Player.cpp and add this code to it:
#include <iostream>
#include <string>
using namespace std;
#include "Entity.h"
#include "Player.h"
const int MAX_GOLD = 9999;
int &Player::Gold(){
if(gold < 0)
gold = 0;
if(gold > MAX_GOLD)
gold = MAX_GOLD;
return gold;
}
bool Player::enoughGold(int amount){
// This function will be used to see if the player has enough
// gold to purchase items (or possibly bribe guards)
if(gold >= amount)
return true;
else
return false;
}
That code is pretty self-explanatory. We do the same in the int &Gold function as we did in the int &Health and int &Mana functions, except that this time we used a constant variable for the maximum amount of gold. If you'd like your player to be able to collect as much gold as they can get their hands on you can just remove that if statement (although you might want to make sure that they can't get such a large amount of gold that an integer can't hold it). Now we need to give the player a constructor which allows the user to set the player's statistics up. Add this to the public section of the Player class in Player.h:
Player();
And put this in the Player.cpp file:
Player::Player(){
cout << "Please enter a name for your character.\n->";
cin >> name;
cin.ignore();
int choice = 0;
while(choice < 1 || choice > 3){
cout << "Please choose a character to be:\n";
cout << "1: Dragenoid (melee)\n";
cout << "2: Lich (magic)\n";
cout << "3: Ice Elemental (ranged)\n";
cin >> choice;
cin.ignore();
}
// Do Class Stuff Here
}
First we ask for a name for the character and then use cin >> to get the input from the console and put it into the variable name. Then we use cin.ignore() to make sure that the next time we called cin to get some input from the console that we don't have anything left over (sometimes when the user presses 'Enter', the 'Enter' gets kept in the input, so the next time you call it you get the 'Enter' and then the information you wanted). Now we need to add some code to Player.h so that we can have classes:
// This part goes just before the class definition:
enum plyClass {dragonoid = 1, lich = 2, iceElemental = 3};
// This part goes with the Player's private variables
plyClass playerClass;
And now we can go back to Player.cpp and put this code at the end of the constructor function:
switch(choice){
case 1:
playerClass = dragonoid;
strength = 6;
intelligence = 2;
dexterity = 3;
speed = 4;
persuasion = 2;
break;
case 2:
playerClass = lich;
strength = 2;
intelligence = 6;
dexterity = 2;
speed = 2;
persuasion = 5;
break;
case 3:
playerClass = iceElemental;
strength = 4;
intelligence = 3;
dexterity = 5;
speed = 3;
persuasion = 2;
break;
}
Now of couse this is one of the places where you are free to change this to what you like, in order to fit in with your game. All we've done is set the player's class and then set their attributes in accordance to their class choice. All we need to do is to initialise a few more variables and then we'll be done:
level = 1;
exp = 0;
gold = 30;
The last thing we need to do to make this work is in the Entity.h file we need to change the private variables into protected variables, so that our Player class can access them, this is simple to do, just change:
class Entity{
private:
string name; // Player Name
Into:
class Entity{
protected:
string name; // Player Name
And that will do for our player class. Now we need to create the enemy class, our enemies will have data for the amount of gold and experience the player recieves after killing them. They will also need an attack type (melee, magic or ranged). First create a new file Enemy.h and enter this into it:
#ifndef ENEMY_H
#define ENEMY_H
using namespace std;
#include "Entity.h"
class Enemy : public Entity{
private:
public:
void Fight(Entity* enemy);
}
#endif
Again, we override the Fight function from Entity. Now we just need to add some variables and an enumeration for the variables:
#ifndef ENEMY_H
#define ENEMY_H
using namespace std;
#include "Entity.h"
enum atkType{melee = 1, magic = 2, ranged = 3};
class Enemy : public Entity{
private:
int goldGiven;
int expGiven;
atkType attackType;
public:
Enemy();
Enemy(int nHealth, int nMana, int nStr, int nInt, int nDex, int nSpeed,
int nGold, int nExp, int nAtkType);
void Fight(Entity* enemy){}
};
#endif
Finally we need to create a constructor for our class. Create a file called Enemy.cpp and add the following code to complete this section!
#include <iostream>
#include <string>
using namespace std;
#include "Entity.h"
#include "Enemy.h"
Enemy::Enemy(){
health = maxHealth = 20;
mana = maxMana = 15;
strength = 6;
intelligence = 6;
dexterity = 6;
speed = 6;
goldGiven = 50;
expGiven = 40;
attackType = melee;
}
Enemy::Enemy(int nHealth, int nMana, int nStr, int nInt, int nDex, int nSpeed,
int nGold, int nExp, atkType nAtkType){
health = maxHealth = nHealth;
mana = maxMana = nMana;
strength = nStr;
intelligence = nInt;
dexterity = nDex;
speed = nSpeed;
goldGiven = nGold;
expGiven = nExp;
attackType = nAtkType;
}
And there you have it! A Player and Enemy class from our Entity class. As before you can try out what we've just done, create a player in main.cpp. My next tutorial is coming soon. |
|||||||
|
If you would like to get in contact, please email me at: conn_peter@hotmail.com |