Hey guys, so I'm trying to make a simple console (by console i mean cmd) game where you have a character with some attributes and he will fight enemies until he dies.I feel like it would be fun and also a good way for me to learn more about classes and objects.
The question is: How could I find an efficient way to make two classes interact, without having to write all interactions in my main() function? I was reading up on something about
Mediator Pattern in c++ but I didn't really understand much of it: Here is what I have read on it.
http://en.wikipedia.org/wiki/Mediator_pattern
http://sourcemaking.com/design_patterns/mediator/cpp/2
Here is some insight of what I would like to achieve:
Say I have two classes, Character and Weapon
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#ifndef WEAPON_H_INCLUDED
#define WEAPON_H_INCLUDED
class Weapon
{
public:
Weapon(float dmgT, int dur);
virtual float getDmg();
private:
float dmg;
int durability;
};
#endif // WEAPON_H_INCLUDED
|
1 2 3 4 5 6 7 8 9 10 11
|
#include "Weapon.h"
Weapon::Weapon(float dmgT, int dur)
{
dmg = dmgT;
durability = dur;
}
float Weapon::getDmg()
{
return dmg;
}
|
---------------------------------- End of Weapon
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#ifndef CHARACTER_H_INCLUDED
#define CHARACTER_H_INCLUDED
class Character
{
public:
Character(float maxHp,float str,float def);
//return functions
float getHp();
float getMaxHp();
float getStr();
float getDef();
float getLf();
float getCrit();
float getBlock();
float getAgility();
int getMint();
float getDmg();
//editing functions
void healHp(float quan); //used to heal current hit points
void upMaxHp(float quan); //used to increase max hp
void modDmg(float damage); //used to modify dmg
private:
int mints; //currency to buy weapons
float xp; //experience
float dmg; //dmg output
float hp; //current hit points
float maxHp; //max hit points
float str; //strength
float def; //defense
float lf; //lifesteal %
float crit; //crit %
float block; //block %
float agility; //agility %
};
#endif // CHARACTER_H_INCLUDED
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
Character::Character(float maxHpT, float strT, float defT)//constructor
{
mints = 50;//currency
hp = maxHpT;
maxHp = maxHpT;
str = strT;
dmg = 1 + (1.2 * str);
def = defT;
lf = 0.0; crit = 0.0; block = 0.0; agility = 0.0;
}
float Character::getHp(){return hp;}
int Character::getMint(){return mints;}
float Character::getDmg(){return dmg;}
void Character::modDmg(float damage)
{
dmg += damage;
}
|
--------------------------------- End of Character
Now, they are simple classes, however, I would like to find a way for the object of weapon to "stick" itself to my character object
is that possible? Weapons are supposed to modify the dmg of my character object, so what could I do to modify the dmg of Character object without having to really worry about it too much. ie. High maintainability.
Here is what I have for main(){}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include "Character.h"
#include "Weapon.h"
#include "BWeapon.h"
#include "create.h"
int main()
{
Character Hero = create(); //create() returns a character object
int mints = Hero.getMint();//all character objects start with 50 currency
Weapon weapon = BWeapon(mints);
//BWeapon is a function which returns a weapon object
Hero.modDmg(weapon.getDmg());
// am I going to have to do this to modify dmg of my hero?
std::cout.precision(2);
bool gameLoop = true;
while(gameLoop == true)
{
//game stuff
}
return 0;
}
|
Thanks for reading :D