I\'m facing with the following design problem:
TL;TD need to determine if Hero(class) can use specific object while there\'s many heroes implementations
I have
You can use enum
values to define weapon and hero's specific types.
enum class HERO_TYPE{
WARRIOR,
ARCHER,
WIZARD
}
class Hero: public Entity{
public:
Hero( std::string name, Gender gender, double damage, Point2d* location, HERO_TYPE type );
~Hero();
virtual void move(int x, int y);
virtual void damage(Entity* other); // Override
virtual bool use(Potion* _potion);
virtual bool use(Weapon* _weapon) = 0;
virtual bool use(ShieldArmor* _shieldArmor) = 0;
virtual bool use(BodyArmor* _bodyArmor) = 0;
private:
std::string name;
Gender gender;
Weapon* weapon;
ShieldArmor* shield_armor;
BodyArmor* body_armor;
HERO_TYPE type; //define in subclasses.
};
and do same for weapons.
enum class WEAPON_TYPE{
SWORD,
CROSSBOW,
WAND
}
class Weapon: public Item{
public:
Weapon(double damage, Point2d* location, WEAPON_TYPE type);
virtual ~Weapon();
virtual double getDamage() const;
virtual const Point2d* getLocation() const;
virtual const std::string toString() const;
WEAPON_TYPE get_type() { return this->type; }//getter
private:
Point2d* location;
double damage;
WEAPON_TYPE type;
};
Now you can specify weapons for hero classes.
void Hero::use(Weapon *i){
if(!checkWeapon(i->get_type())) return;
//...code...
}
bool Hero::checkWeapon(WEAPON_TYPE t){
switch(this->type){
case HERO_TYPE::WARRIOR:{
if(t == WEAPON_TYPE::SWORD)
return true;
}break;
case HERO_TYPE::ARCHER:{
if(t == WEAPON_TYPE::CROSSBOW)
return true;
}break;
//..all cases..
}
return false;//no hero-weapon matching
}