考虑以下几点:
struct WeaponAttributes {
// Attributes to designate damage and weaknesses to
// specific character classes and armor types.
};
class Weapon {
public:
enum WeaponClassType {
WCT_SWORD,
WCT_AXE,
WCT_DAGGER,
WCT_KNIFE,
WCT_STAFF,
/* WTC_ETC */
};
private:
std::string m_strWeaponClass;
std::string m_strWeaponName;
WeaponClassType m_WeaponClass;
public:
// Constructor, Destructor and members this base class would have
Weapon(const std::string& strWeaponClass, const std::string& strWeaponName, WeaponClassType weaponType);
std::string getWeaponClassName() const;
std::string getWeaponName() const;
WeaponClassType getType() const;
bool isWeaponType(const WeaponClassType& type) const;
bool isWeaponType(const std::string& strClassName) const;
void addAttributes(const WeaponAttributes& attributes) = 0;
};
class Sword : public Weapon {
private:
WeaponAttributes m_attributes;
public:
explicit Weapon(const std::string& strWeaponName);
void addAttributes(const WeaponAttributes& attributes) override;
};
struct CharacterAttributes {
// Similar to WeaponAttributes
};
class Character {
public:
enum CharacterClassType {
CCT_FIGHTER,
CCT_WARRIOR,
CCT_MAGE,
/* CCT_ETC */
};
private:
std::string m_strCharacterClassType;
std::string m_strCharacterClassName;
std::string m_strPlayerOrNPCName;
CharacterClassType m_classType;
public:
Character(const std::string& strName, const std::string& strClassName, const std::string strClassType, CharacterClassType classType);
// Using Three Strings - Example: (Player's or NPC's game name, White, Mage)
// (Player's or NPC's game name, Superior, Warrior)
// (Player's or NPC's game name, Skilled, Fighter)
std::string getName() const;
std::string getClassName() const;
std::string getCharacterName() const;
CharacterClassType getCharacterType() const;
bool isCharacterClassType(const CharacterClassType& type) const;
bool isCharacterClassType(const std::string& strClassType) const;
void addCharacterAttributes(const CharacterAtrributes& attributes) = 0;
bool equipWeapon(Weapon* pWeapon) = 0;
bool equipArmor(Armor* pArmor) = 0;
};
class Character : public Fighter {
private:
CharacterAttributes m_attributes;
std::shared_ptr<Weapon> m_pWeapon;
std::shared_ptr<Armor> m_pArmor;
public:
Fighter(const std::string& strPlayerOrNPCName, const std::string& strClassName);
void addCharacterAttributes(const CharacterAttributes& attributes);
bool equipWeapon(Weapon* pWeapon);
bool equipArmor(Armor* pArmor);
};
对于这种类型的类层次,每个类都有一个枚举类型和字符串描述符与方法来检索类型,字符串和比较,如果他们是特定类型或不一起,您可以轻松遍历不同的列表或向量(容器),以查看是否可以将特定类型的装甲或武器装备到特定的职业角色。这只是一种设计方法,因为有很多方法来实现这样的系统。我并不夸耀这是最好或最差的设计方法,但我觉得它是一个很好的指导或起点。一旦你在没有任何错误的情况下得到你想要的工作稳定状态所需的行为,那么只有这样才可以修改现有代码库进行优化。
这可能更适合于GameDesign.SE – NathanOliver
@NathanOliver不一定,抛开游戏,这是一个如何在相应的类层次结构之间进行耦合的问题。 –
尝试从关系角度来处理问题,而不是面向对象的问题。这个问题很难解决;你会有一个“武器”表,一个“字符”表和一个“weapon_permission”表,外键引用另外两个表。 –