Archived

This topic is now archived and is closed to further replies.

antness

Text RPG - Player Inventory - Inheritance?

Recommended Posts

antness    122
Hello, I''m working on a text RPG and need to set up the player''s inventory structure. This is what I want. 1) A backpack vector, a vector for what the person is wearing, and variables for what is in the hands. 2) The backpack vector can hold any type of item. Containers (which hold any type of item), weapons, armor, potions, etc. If it is too difficult, I won''t do containers. I have some code. How could I properly implement this? How can I make a vector that will allow any type of item to be in it? Here''s some crap code I have now. The inheritance code I just stuck in there, I was using it to try making items before.
    // items.h

// handles the player''s inventory


#ifndef ITEMS_H
#define ITEMS_H 

#include <iomanip.h>
#include <apvector.h>
#include <apstring.h>
#include <fstream.h>

const int LEN = 80;				// maximum length of item names


class item						// item class

{
private:
	char shortname[LEN];		// short item name

	char name[LEN];				// long item name

	char look[LEN];				// long item description

	unsigned long value;		// item value (0+)
	double weight;				// item weight (>0)
public:
	void getData()
	{
		cin.getline(name, LEN, ''\n'');

		cout << "Enter full item name (ex. a notched scimitar): ";
		cin.getline(name, LEN, ''\n'');

		cout << "Enter short item name (ex. scimitar): ";
		cin.getline(shortname, LEN, ''\n'');

		cout << "Enter long item description: ";
		cin.getline(look, LEN, ''\n'');

		cout << "Enter item appraisal value (0+): ";
		cin >> value;

		cout << "Enter item weight (0+): ";
		cin >> weight;
	}
};

class weapon : public item		// weapon class

{
private:
	int minDamage;				// minimum hit damage

	int maxDamage;				// maximum hit damage

public:
	void getData()
	{
		item::getData();

		cout << "Enter minimum damage: ";
		cin >> minDamage;

		cout << "Enter maximum damage: ";
		cin >> maxDamage;
	}
};

class armor : public item
{
private:
	int defense;				// the maximum defense

	int bodyPart;				// where the armor is worn

public:
	void getData()
	{
		item::getData();

		cout << "Enter defense: ";
		cin >> defense;

		bodyPart = getPosition();
	}
	int getPosition()
	{
		cout << endl << "1	Head" << endl
		 << "2	Chest" << endl
		 << "3	Arms" << endl
		 << "4	Hands" << endl
		 << "5	Legs" << endl;

	cout << "Enter body position (1-5): ";
	cin >> bodyPart;

	return bodyPart;
	}
};

class clothing : public item
{
private:
	int bodyPart;				// where the clothing is worn

public:
	void getData()
	{
		item::getData();

		bodyPart = getPosition();
	}
	int getPosition()
	{
		cout <<endl << "1	Head" << endl
			<< "2	Chest" << endl
			<< "3	Arms" << endl
			<< "4	Hands" << endl
			<< "5	Legs" << endl
			<< "6	Neck" << endl
			<< "7	Waist" << endl
			<< "8	Feet" << endl;
		cout << "Enter body position (1-8): ";
		cin >> bodyPart;

		return bodyPart;
	}
};

/*class container : public item
{
private:
	apvector<weapon, armor>items;
	int maxItems;						// how many items it can hold

	double maxWeight;					// how much weight it can hold

	int bodyPart;						// where it is worn

public:
	void getData()
	{
		item::getData();

		cout << "How many items can it hold? ";
		cin >> maxItems;

		cout << "How much weight can it hold? ";
		cin >> maxWeight;

		bodyPart = getPosition();
	}
	int getPosition()
	{
		cout <<endl << "3	Arms" << endl
			<< "5	Legs" << endl
			<< "7	Waist" << endl
			<< "9	Shoulders" << endl
			<< "10	Back" << endl;
		cout << "Enter body position (3, 5, 7, 9, 10): ";
		cin >> bodyPart;

		return bodyPart;
	}
}; */

class potion : public item
{
private:
	int plusHP;							// how much health it will restore

	int plusMP;							// how much magic power it will restore

	int maxUses;						// how many times it can be used

public:
	void getData()
	{
		item::getData();

		cout << "How much health will it restore? ";
		cin >> plusHP;

		cout << "How much magic power will it restore? ";
		cin >> plusMP;

		cout << "How many usages per piece? ";
		cin >> maxUses;
	}
};

class jewelry : public item
{
private:
	int bodyPart;
public:
	void getData()
	{
		item::getData();

		bodyPart = getPosition();
	}
	int getPosition()
	{
		cout <<endl << "1	Head" << endl
			<< "3	Arms" << endl
			<< "6	Neck" << endl
			<< "7	Waist" << endl
			<< "11	Finger" << endl
			<< "12	Ear" << endl;
		cout << "Enter body position (1, 3, 6, 7, 11, 12): ";
		cin >> bodyPart;

		return bodyPart;
	}
};

///////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////


class inventory
{
public:
	apvector<item>backpack;
	apvector<item>body;

	void dispBodyInv(apvector<item>&body);
	void dispBackpackInv(apvector<item>&backpack);

	void dispItemInv(item);
private:
};

void inventory::dispBodyInv(apvector<item>&body)
{
	body.resize(1);
	body[0].getData();
}

void inventory::dispBackpackInv(apvector<item>&backpack)
{
}

#endif    

Share this post


Link to post
Share on other sites
LilBudyWizer    491
Personally I would just make it a pointer to an item. Then it doesn''t have to handle allocation of the space, but the down side is that you do. Give the item a virtual class that returns the items type. You can make things a bit simplier by having a function that returns a boolean value indicating if a given action is valid for the item. Have each derived class call the same function in it''s parent so you can benefit from inheritance. When an action is performed if the item doesn''t know how to perform that action pass it to the parent class. If it makes it to the root then return invalid action and don''t let the user do what they attempted. A virtual function will take you to the top of the tree, but you can work your way back down manually.

Share this post


Link to post
Share on other sites
rockslave    122
Holy classes! It´s been a long time since I´m out the web. Well, you could maybe make a linked list. But if you can store any kind of item... Are all them the same class, I mean, is a sword the same kind of object a key is? If so, it´s all right for a linked list.

Share this post


Link to post
Share on other sites
LilBudyWizer    491
C/C++ Journal has an article on creating varients with member templates. That will get you around having to manage the space for the items. You still have to figure out what it is when you pull it out though. You could use run-time type identification to do that. If you implement their varient then you will be using RTTI anyway since they use dynamic_cast to throw an exception if you try to assign the varient to an invalid type.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
C/C++ Journal has an article on creating varients with function templates. That will get you around having to manage the space for the items. You still have to figure out what it is when you pull it out though. You could use run-time type identification to do that. If you implement their varient then you will be using RTTI anyway since they use dynamic_cast to throw an exception if you try to assign the varient to an invalid type.

Share this post


Link to post
Share on other sites