1

在我基于 netback 的类 rogue-like 游戏中,我有一个基实体,它带有一个名为Creature 的派生类。我还有另一个Creature派生类,称为Player。

由于Player基本上是一个Creature但添加/修改了方法,因此我的攻击方法(两者)需要 2 个指向Creature对象的指针。一个用于被攻击的生物,一个用于玩家。

在玩家版本的虚拟方法,攻击中,我需要设置玩家的XP和Score成员变量来体现战斗。我不断收到错误消息:

Player.cpp:34:11: error: ‘class Creature’ has no member named ‘setScore’
Player.cpp:34:29: error: ‘class Creature’ has no member named ‘getScore’

我假设是因为它是 Creature 类型的。但我无法将其投射出来,因此我实际上可以在播放器上使用这些方法。

我还注意到我在Creature 中定义了“setXp”和“getXp”。但是生物没有xp ..我可以/应该将分数方法添加到生物并在播放器中定义它们吗?

我怎样才能让它做到这一点?谢谢!这是一些代码。如果您需要更多信息,请告诉我:

播放器.h

#ifndef _Player_included_
#define _Player_included_

#include "Creature.h"
#include "Weapon.h"
#include "Armor.h"

class Player : public Creature {

public:
    Player(void);
    virtual ~Player(void);

    void dumpStats();
    virtual bool move(char dir, DungeonLevel & dl, std::mt19937 & randomGen$
    virtual bool canAttack(DungeonLevel & dl);
    int getHit(std::mt19937 & randomGen);
    bool fightOrFlight(DungeonLevel & dl, std::mt19937 & randomGen);
    virtual void attack(Creature * monster, Creature * player, std::mt19937$

    void generateHP();
    void addXp(int xpToAdd);
    void addScore(int xpToMultiply); //score is just 2x the xp.
    virtual int getScore();
    virtual void setScore(int scoreToSet);
    virtual int getXp();
    virtual void setXp(int xpToSet);

    void setXPToLevel(int xpToLevelToSet);
    int getXPToLevel();
private:
    //Creature provides level, HP, and maxHP
    int score;
    int xp;
    int xpToLevel;
    Weapon * playerWeapon;
    Armor * playerArmor;
};

#endif

Player.cpp的攻击方法

void Player::attack(Creature * monster, Creature * player, std::mt19937 & randomGen, Du$
    int monsterHit = monster->getHit(randomGen);
    int playerHit = getHit(randomGen);

    player = static_cast<Player*>(player);

    if ((monster->getHP() - playerHit) <= 0){
            playerHit = monster->getHP();
            player->addXp(playerHit);
            player->setScore((player->getScore()) + (2 * playerHit)));
            cout << "Monster name: " << monster->getName() << endl;
            cout << "monster killed: " << monster << endl;
            monster->removeMonster(dl);
    }
    else if (monster != NULL){
            cout << "Monsters Hit: " << monsterHit << endl;
            player->setHP((player->getHP()) - monsterHit);
            player->addXp(playerHit);
            player->setScore((player->getScore()) + (2 * playerHit)));
            monster->setHP((monster->getHP() - playerHit));
            cout << "Your HP: [" << player->getHP() << "]/[" << player->getMaxHP() $
            cout << "Monsters HP: [" << monster->getHP() << "]/[" << monster->getMa$
    }
    else if ((player->getHP() - monsterHit) <= 0){
            monsterHit = player->getHP();
            //game over
    }

    cout << "You hit: " << playerHit << endl;
}

生物.h

#ifndef _Creature_included_
#define _Creature_included_

#include "Entity.h"
#include "DungeonLevel.h"

#include <random>

class Creature : public Entity {

public:
    Creature(void);
    virtual ~Creature(void);

    virtual void dumpObject();
    virtual void dumpObjectData();
    virtual void writeFragment(std::ostream & output);
    virtual void writeDataAsFragment(std::ostream & output);
    virtual void setElementData(std::string elementName, std::string elementValue);

    virtual bool move( DungeonLevel & dl, Creature & player, std::mt19937 & randomGen);
    virtual void attack(Creature * monster, Creature & player, std::mt19937 & randomGen, DungeonLevel & dl);
    virtual int getHit(std::mt19937 & randomGen);

    virtual bool canAttack();
    virtual void removeMonster(DungeonLevel & dl);

    virtual void setXLoc(int xToSet);
    virtual int getXLoc();
    virtual void setYLoc(int yToSet);
    virtual int getYLoc();

    virtual void setXp(int xpToSet);
    virtual int getXp();
    virtual void addXp(int xpToAdd);

    virtual int getLevel();
    virtual void setLevel(int levelToSet);
    virtual int getHP();
    virtual void setHP(int HPToSet);
    virtual int getMaxHP();
    virtual void setMaxHP(int maxHPToSet);

private:
    int xLoc;
    int yLoc;
    int level;
    int HP;
};

#endif

Creature.cpp中的攻击方法

void Creature::attack(Creature * monster, Creature & player, std::mt19937 & randomGen,  DungeonLevel & dl){
    int monsterHit = monster->getHit(randomGen);
    int playerHit = player.getHit(randomGen);

    if ((monster->getHP() - playerHit) <= 0){
            playerHit = monster->getHP();
            cout << "Monster name: " << monster->getName() << endl;
            this->removeMonster(dl);
            cout << "back to creature attack with monster removed.."<<endl; 
            cout << "delete monster here." << endl;
    }
    else if ((player.getHP() - monsterHit) <= 0){
            cout << "You died. Game Over." << endl;
            //make a function to end the game
    }
}
4

1 回答 1

1

线

player = static_cast<Player*>(player);

实际上什么都不做。您仍然拥有player相同类型的相同变量Creature*(因为它是用这种类型声明的)。相反,你可以写

Player* player_ref = reinterpret_cast<Player*>(player);然后使用player_ref -> setScore();.

不过,多态解决方案会更整洁。

于 2013-05-04T21:57:22.167 回答