-
In Trouble
I'm trying to create a small RPG and I'm having trouble figurig out how to attach an inventory to a being when it's being created.
Please take a look at my code
Being.h
Code:
class Being
{
public:
void makeInv(Being& target);
void atk(Being& target, Being& attacker);
void takeDmg(Being& attaker, Being& target);
void setHp(int HP){hp = HP;}
void setSp(int SP){sp = SP;}
void setFlee(int FLEE){flee = FLEE;}
void setHit(int HIT){hit = HIT;}
void setAtkPower(int ATKPOWER){atkPower = ATKPOWER;}
void setMultip(int MULTIP){multip = MULTIP;}
void setInvNumSet(int INVNUMSET){invNumSet = INVNUMSET;}
int getHp(){return hp;}
int getSp(){return sp;}
int getFlee(){return flee;}
int getHit(){return hit;}
int getAtkPower(){return atkPower;}
int getMultip(){return multip;}
int getInvNum(){return invNumSet;}
protected:
int hp;
int sp;
int flee;
int hit;
int atkPower;
int multip;
int invNumSet;
};
#endif
Being.cpp
Code:
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
#include "Enemy.h"
#include "Hero.h"
void Being::makeInv(Being& target)
{
string *pInvNum;
pInvNum = new string[target.getInvNum()];
for(int i = 0; target.getInvNum() > i; i++)
{
pInvNum[i] = "\nEmpty Slot !!";
cout << pInvNum[i] << endl;
}
}
void Being::atk(Being& target, Being& attacker)
{
int fleeRoll, fleeRoll2;
fleeRoll = (target.getFlee() - attacker.getHit() );
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
fleeRoll2 = rand() % 100;
if(fleeRoll < fleeRoll2)
{
cout << "\nHit !!";
takeDmg(attacker, target);
}
else
cout << "\nMiss !!";
}
void Being::takeDmg(Being& attacker, Being& target)
{
int temp;
temp = target.getHp();
temp -= (attacker.getAtkPower() * attacker.getMultip() );
target.setHp(temp);
}
Enemies are the same as this class, so I won't waste space posting it twice.
Hero.h
Code:
#ifndef HERO_H
#define HERO_H
#include <iostream>
#include "Being.h"
class Warrior : public Being
{
public:
Warrior()
{hp = 100, sp = 10, flee = 120, hit = 100,
invNumSet = 10, atkPower = 10, multip = 2; };
void warriorAtk();
};
Anyone got any tips on how to attach the inventory to the instance when the constructor is called?
-
Assuming you have an item class. You could make a vector of item pointers in your being class.
Code:
#include <vector>
using namespace std;
class item;
class being {
public:
void addtoInv(item*);
item getfromInv(string); // The name of the item perhaps
private:
vector<item*> Inv;
};
-
Could you explain exactly what you are doing there. Still really unsure about pointers.
-
Sure. Let's pretend you have a higharchy of item classes. Then, all you have to do is declare an instance of each item. Then all rooms and beings will have an inventory that's simply a vector of pointers to an item. In theory, though, there is only one of each item object, but of course it could be pointed to multiple times. Then to use it, you simply call the items action and change the pointer to it to NULL (assuming it's a one use item). I found this easier than dynamically allocating and deleting objects every time you get or use them.
If you're having trouble understanding this, then you should put off the RPG and try and learn pointers better. Once you get a good concept of that, jump back into the RPG if you wish.