考慮以下幾點:
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」表,外鍵引用另外兩個表。 –