Tracking item location in inventory
I'm wanting to know where each object is located in the player inventory. And I'm having troubles deciding on the best way to do this, with my current class structure.
Items are managed by CItem, a base class of an hierarchy that spawns CWeapon, CArmor, CPotion, CKey, CFood and CContainer (bags, chests, ...) . CItem is not abstract. Objects of CItem type are possible. These are usually non magical items that only serve as game plot objects, or simple trinkets.
CInventory manages the inventory. This is a lone class.
A player inventory is the sum of it's bodyparts plus a minimum of 15 objects it can carry around (and that I'm calling pool). Bodyparts are the typical RPG style. Head, Neck, Arms, Waist, Right Hand, Left Hand,...
But the problem is the following. CInventory is meant to have wear() and unwear() member functions. When a player picks an item it gets moved to the pool. When he wears it, that object gets tagged as being worn on a specific bodypart. I don't really feel the need to remove it from the pool... just to tag it is being worn somewhere.
I'm trying to avoid having a CInventory data member for every body part. And I'm also trying to avoid doing this on the CItem by adding a member specifying if the object is being worn anywhere (or nowhere at all). I feel this should be exclusively managed by CInventory. But I'm not sure how to achieve this. Since I'm using weak pointers, a simple vector is not doable.
Code:
class CInventory {
public:
CInventory(boost::shared_ptr<CCreature>);
~CInventory() {};
void add(boost::shared_ptr<CItem>);
void remove(boost::shared_ptr<CItem>);
void equip(boost::shared_ptr<CItem>, BODYPART); // BODYPART is an enum
void unequip(boost::shared_ptr<CItem>);
void swap(boost::shared_ptr<CItem>, boost::shared_ptr<CItem>);
int ContentsWeight() const;
private:
std::set<boost::weak_ptr<CItem> > contents_; // inventory full contents
std::bitset<NUM_BODY_PARTS> use_; // just a a quick way to check usage
boost::weak_ptr<CCreature> owner_; // the owner of the inventory
int maxweight_;
};