Hi i'm make an Inventory System for my personal game project with C++ Composite pattern. What I'm going to do is set a limit so player unable add item once hit the specific limit. but here my problem :
- Did i set the limit & add item function correctly?
-
Way to do the limit checking function.
class Arrow
{
public:
string arrowType;
double weight;Arrow(string type, double w): arrowType(type), weight(w){} //! pure abstract method virtual void getDescription(int level) = 0; virtual double getWeight() = 0;
};
class FireArrow : public Arrow
{
public:
FireArrow(string type, double w ): Arrow(type,w){}void getDescription(int level) { for(int i=0; i<level; i++)cout<<"\t"; { cout<<"Arrow Type: "<<arrowType<<", Weight: "<<weight<<endl; } } double getWeight() { return weight; }
};
class StandardArrow : public Arrow
{
public:
StandardArrow(string type, double w ): Arrow(type,w){}void getDescription(int level) { for(int i=0; i<level; i++)cout<<"\t"; { cout<<"Arrow: "<<arrowType<<", Weight: "<<weight<<endl; } } double getWeight() { return weight; }
};
class ArrowBundle : public Arrow
{
private:
vector<Arrow*> arrowList;
public:
ArrowBundle(string type, double w) : Arrow(type, w){}void Add(Arrow *a) { arrowList.push_back(a); } void getDescription(int level) { cout<<"Arrow: "<<this->arrowType<<", Weight"<<this->weight<<endl; if(!arrowList.empty()) { for(int i=0; i<level; i++)cout<<"\t"; { cout<<"Subordinate of: "<<arrowType<<" : "<<endl; ++level; for(int j=0; j<arrowList.size(); j++) { arrowList[j]->getDescription(level); } } } } double getWeight() { return weight; }
};
int main()
{ArrowBundle arrow("Arrow", 500); ArrowBundle fa("Fire Arrow", 50); ArrowBundle sa("Standard Arrow", 30); reRun: cout<<"Add Item "<<endl; cout<<"===================="<<endl; cout<<"1.Fire Arrow"<<endl; cout<<"2.Standard Arrow"<<endl<<endl; cout<<"Your Choice: "; int choice; cin>>choice; if(choice == 1) { for(int i=0; i<5; i++) { arrow.Add(&fa); } } else if (choice == 2) { for(int i=0; i<5; i++) { arrow.Add(&sa); } } cout<<"The Hierarchy of the Arrow Inventory"<<endl; arrow.getDescription(0); cout<<endl; goto reRun; system("pause"); return 0;
}