好了,我已经看遍了这个网站和其他一些网站(但如果有类似问题的链接,请告诉我),我得到了库存系统应该如何工作的所有细节。我的问题是从一个非常宽泛的角度提出的。这只是一个控制台文本RPG。我应该如何构造我的items类?到目前为止,我的想法是这样的:
// Item.h
class Item {
public:
int Id;
std::string Name;
bool CanUse, CanEquip;
virtual void Use();
}
class Weapon: public Item {
public:
int Attack, Defense, Health; // stat changes when equipped
void Use() { std::cout << "Can't use\n"; }
}
class Sword: public Weapon {
// constructor defining stat changes for a sword
// and to define Name = "Sword"
}那么,我是不是应该多次继承,或者我可以做一些更容易的事情呢?最终,我将拥有其他各种类型的武器,但在我看来,从Weapon继承它们中的每一种都比我可以做到的其他方式麻烦得多?
也许我可以在Weapon中有一个枚举或结构来定义每种类型的武器?如果我像上面的代码那样做,我可能真的会将virtual void Use();函数移到一个类class Consumables: public Item中,因为这些应该是唯一“可用”的东西。
提前感谢任何愿意提供帮助的人。我通过书本和在线教程自学了我所知道的一切,所以我相当缺乏经验:)
发布于 2017-02-02 04:16:27
所以我最终决定这样做:
enum class ItemType {
Sword,
Axe,
Health_potion,
Helmet,
Random
}
Item CreateItem(ItemType Names) {
Item item;
if (Names == ItemType::Random)
{
Names = (ItemType)Rand0toN1(4); // just a function spitting
} // out a rand number 0 to N-1
switch (Names)
{
case ItemType::Sword:
item = Item("Sword", 5, 0, 0, 0, false, true, 6);
break; // constructor Item(Name, attack, defense, health
case ItemType::Axe: // maxhealth, canUse, canEquip, slot)
item = Item("Axe", 3, 1, 0, 0, false, true, 6);
break;
case ItemType::Helmet:
item = Item("Helmet", 0, 3, 0, 1, false, true, 4);
break;
case ItemType::Health_potion:
item = Item("Health Potion", 0, 7, 0, 0, true, false, 0);
break;
default:
break;
}
return item;
}做这样的事情有什么错吗?我现在只有Item类,而不是所有这些继承的类。
https://stackoverflow.com/questions/41965593
复制相似问题