问题描述
我面临以下设计问题:
TL; TD需要确定在有许多英雄实现的情况下Hero(class)是否可以使用特定对象
我有3个Hero类的子类,每个子类都可以使用特定的项.
对于Weapons.hpp,我有Sword,Hammer,CrossBow,Bow,Wand,Staff.
战士可以使用剑或锤子弓箭手可以使用CrossBow或弓箭向导可以使用法杖或魔杖
有英雄基类:
class Hero: public Entity{
public:
Hero(std::string name, Gender gender, double damage, Point2d* location);
~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;
};
这是武器:
class Weapon: public Item{
public:
Weapon(double damage, Point2d* location);
virtual ~Weapon();
virtual double getDamage() const;
virtual const Point2d* getLocation() const;
virtual const std::string toString() const;
private:
Point2d* location;
double damage;
};
在游戏的主界面中,我需要确定Hero * h是否可以使用特定项目而不会降低投放范围.
所以我可以像这样使用它:
Hero *h;
Weapon * i;
// do something assign values
h->use(i);
我通过删除不必要的所有内容并将其通用化为Item
的概念,简化了示例.武器是Item的子类,药水,魔杖,助焊剂电容器也是如此. use
方法执行Item
对target
所做的任何事情.武器将尝试击中并破坏target
.治愈药水会治愈target
.磁通电容器会及时将target
发送回去,或者以1.21吉瓦的电量迅速删除掉其中的删除内容.
但是,所有事物都可以通过Item
的镜头看到.调用类不知道项目对target
的作用,作用或作用. target
甚至不知道在上面使用了什么,他们只是感觉到了效果.没有人知道一个简单的通用接口之外的其他对象.
class Item
{
public:
enum types
{
whole lot of types go here.
They are fairly broad categories, like knife, sword, two handed sword,
healing potion, wand, etc.
};
types getType()
{
return type;
}
virtual bool use(Entity * target) = 0;
private:
types type;
};
class Hero: public Entity{
public:
Hero(std::set<Item::type> & usable): usableItems(usable)
~Hero();
bool use(Item* item,
Entity * target)
{
// this is the magic. If the item's type is in the list of usable items,
// the item is used on the target. They exact type of item or target
// is not known. Polymorphism will take care of everything from here
if (usableItems.find(item->getType()) != usableItems.end())
{
return item->use(target);
}
return false;
}
private:
std::set<Item::type> & usableItems;
};
重点是主要班级都非常愚蠢.它们只是为更详细的对象提供一个框架来完成详细的工作. VorpalSword
使用从Weapon
和Item
继承的通用方法来查看它是否击中了target
,不知道target
实际上是HugeRedDragon
实例,如果击中了则会分配损害,然后进行特定的处理. VorpalSword
确实喜欢检查是否有四肢脱落的情况.
所有Hero看到的都是item->use(target)
.
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 3 subclasses of the class Hero, each one of them can use specific items.
For Weapons.hpp i have Sword,Hammer,CrossBow, Bow ,Wand , Staff.
Warrior can use Sword or hammerArcher can use CrossBow or bowWizard can use staff or wand
There's Hero base class:
class Hero: public Entity{
public:
Hero(std::string name, Gender gender, double damage, Point2d* location);
~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;
};
And this is Weapon:
class Weapon: public Item{
public:
Weapon(double damage, Point2d* location);
virtual ~Weapon();
virtual double getDamage() const;
virtual const Point2d* getLocation() const;
virtual const std::string toString() const;
private:
Point2d* location;
double damage;
};
In the Game's main i need to determine if Hero *h can use specific item without down casting if possible.
so i could use it like:
Hero *h;
Weapon * i;
// do something assign values
h->use(i);
I have simplified the example by removing everything that isn't necessary and generalized to the concept of Item
. A weapon is a subclass of Item, as is a potion, a wand, a flux capacitor, whatever. The use
method does whatever the Item
does to target
. A weapon will attempt to hit and damage target
. A healing potion will heal target
. A flux capacitor will either send target
back in time or zap the expletive deleted out of them with 1.21 gigawatts.
But everything is seen through the lens of Item
. The invoking classes doesn't know what the item is, does, or what it did to target
. target
doesn't even know what was used on it, they just feel the effects. Nobody knows nothin' about the other objects outside of a simple, generic interface.
class Item
{
public:
enum types
{
whole lot of types go here.
They are fairly broad categories, like knife, sword, two handed sword,
healing potion, wand, etc.
};
types getType()
{
return type;
}
virtual bool use(Entity * target) = 0;
private:
types type;
};
class Hero: public Entity{
public:
Hero(std::set<Item::type> & usable): usableItems(usable)
~Hero();
bool use(Item* item,
Entity * target)
{
// this is the magic. If the item's type is in the list of usable items,
// the item is used on the target. They exact type of item or target
// is not known. Polymorphism will take care of everything from here
if (usableItems.find(item->getType()) != usableItems.end())
{
return item->use(target);
}
return false;
}
private:
std::set<Item::type> & usableItems;
};
The point is the main classes are incredibly stupid. They simply provide a framework for much more detailed objects to do the detailed work. VorpalSword
uses generic methods inherited from Weapon
and Item
to see if it hit target
, not knowing that target
is in fact a HugeRedDragon
instance, and if it hit assigns damage and then does the specific things a VorpalSword
does like checking for lopped off limbs.
And all Hero saw was item->use(target)
.
这篇关于C ++确定类是否可以使用对象-文本RPG游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!