Sorry, I meant, a class for weapons in which all would fit. Then how would that work when I initialize the goblin, would I create a vector array to store each weapon object and apply that within the constructor?
|
There are so many ways to do this, I suggest going with you feel most comfortable with. If you are trying to learn new things then you can try out an idea that you aren't familiar with. Sure you can init the weapon drop info in a constructor, but you could also create an interface for drops that your Goblin would implement (or any monster type). You could even have a default implementation for drops and only override it when you see fit.
You could have a drop be part of a base class that all monsters inherit from. Drops themselves could be there own object, that could be passed into a constructor for that monster type etc. (as said before).
With each abstraction and or granularity comes its advantages and disadvantages. You could create a mediator object which could then manage an assortment of objects that have different responsibilities. I'll give you an example.
Lets say you have a weapon class, armor class, item class. From these classes you can derive your swords, axes, crossbows, cloth, leather, plate, healing, mana, food etc. Neither of these classes need to know about the other, however, the mediator class (the glue) can bring all of them together and loosely couple. Now this mediator's sole purpose could be handling drops (or any number of things), what this buys you is the goblin need not know anything about weapon, item types etc and vice versa. This decoupling allows you to be flexible. Like I said this is just one way to implement what you are trying to do there are hundreds of ways to accomplish this.
Also, should items like healing potions group together in an all-encompassing class for weapons/armors/items or keep each class unique to its unique type.
|
This is entirely up to how you want to implement. Personally I probably would create an abstract base class 'item' (or whatever suites your naming) that all weapons/armor/misc things would be derived from. Why? Because now I have a common abstraction that I can pass around to different interfaces and let that interface decide on how it is implemented. Here is an example of this.
Item.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
//... includes ...
class Item
{
public:
//For simplicity this will be the only public interface in the base class...
virtual int GetKind()=0;
//Notice virtual deconstructor, it is implemented here
virtual ~Item(){}
protected:
//Derived classes can now call the default constructor
Item(){}
}
|
Yes that base class is very simple, you would probably want to add some more to the interface. Maybe something like
virtual int GetItemLevel()=0;
, you get the idea.
Now you can derive from this class...
Armor.h
1 2 3 4 5 6 7 8 9 10
|
//... includes....
class Armor : public Item
{
public:
//.... implementation details....
virtual int GetKind();
//.... more details ....
}
|
Armor.cpp
1 2 3 4 5
|
int Armor::GetKind()
{
return 42; //Meaning of life right? You will have to decide what the interface does...
//Maybe you don't return an int, maybe it's a const char *, or maybe its an object itself..
}
|
Now what is the point of showing you those? So that I can show you how they can be passed through other interfaces. Imagine now that Armor is a base class for hundreds of different derived armor classes. Suppose you also have weapons and healing potions and random pieces of material... but they all have one thing in common, Item.
You can now pass Item to any interface, figure out what kind of item it is and have it handle it appropriately...
1 2 3 4
|
//You could also pass the item as a pointer.
void BossDrop(const Item &Item);
void MonsterDrop(const Item &Item);
Item MonsterDrop(const Item &Item);
|
Ok how does this help you with the Goblin object? Well you could still have a vector, but now it would be a vector whos elements are a pointer to an Item...
This is probably a lot more than you wanted but since your having fun with this I figured I'd throw out some other concepts.