我有两节课:

class CEnemy : CObject
{
protected:
    int hitPoints;
};
class COgro : public CEnemy
{
    COgro::COgro() {hitPoints = 100}
};


在其他文件中,我有类“ CRocket”,可以与COgro发生冲突,它具有以下功能:

void CRocket::OnCollision(CObject *collisionObject)
{
    if (typeid(*collisionObject) == typeid(COgro))
    {
        //collisionObject->hitPoints -= 10; ?? or what?
    }
}


我想在死之前先拍10次。这该怎么做?
我已经尝试过:

     collisionObject->hitPoints -= 10;
     (CEnemy)collisionObject->hitPoints -= 10;


但我无法编译...如何编辑此hitPoints值,但不更改'(CObject * collisionObject)'?
谢谢

编辑:

//===============================================================

//------------------------------------CLASS CRocket-----------------------
class CRocket : public CObject
{
protected:
    void OnAnimate(scalar_t deltaTime);
    void OnCollision(CObject *collisionObject);
    void OnDraw(CCamera *camera);

public:

    float pitch;
    float distanceTravel;
    CVector forward;
    bool isExplosion;

    CTexture *explosionTex;
    CExplosion *explosion;

    CRocket();
    ~CRocket();

    void Load();
    void Unload();

};
void CRocket::OnCollision(CObject *collisionObject)
{
        if (typeid(*collisionObject) == typeid(COgroEnemy))
        {
            isExplosion = true;
            velocity = CVector(0.0, 0.0, 0.0);
            explosion = new CExplosion(500, position, 8.0, explosionTex->texID);
            PlaySound();
        }
}

//-----------------------------------------class CObject
class CObject : public CNode
{
protected:
    virtual void OnAnimate(scalar_t deltaTime)
    {
        position += velocity * deltaTime;
        velocity += acceleration * deltaTime;
    }
    virtual void OnDraw(CCamera *camera) {}
    virtual void OnCollision(CObject *collisionObject) {}

    virtual void OnPrepare()
    {
        ProcessCollisions(FindRoot());
    }

public:
    CVector position;
    CVector velocity;
    CVector acceleration;
    scalar_t size;

    bool isDead;

    CObject() {isDead = false;}
    ~CObject() {}
...
...
...
}

//---------------------------------------class CEnemy
class CEnemy : public CObject
{
public:
    int hitPoints;
protected:

    float distFromPlayer;
    float runSpeed;
    AIState_t aiState;

    virtual void OnProcessAI() {}
    void OnCollision(CObject *collisionObject)
    {
          // if this enemy collides with another enemy
          if (typeid(*collisionObject) == typeid(CEnemy))
          {
               modelState = MODEL_IDLE;
               velocity = CVector(0.0, 0.0, 0.0);
          }
          // if this enemy collides with the terrain (always)
          else if (typeid(*collisionObject) == typeid(CTerrain))
          {
               position.y = ((CTerrain*)collisionObject)->GetHeight(position.x, position.z) + size;
          }
          else
          {
          }
     }

public:
    CPlayer *player;
...
...

//----------------------------------class COgro-------------------------
class COgroEnemy : public CEnemy
{
protected:
    void OnProcessAI();
    void OnCollision(CObject *collisionObject);
    void OnPrepare();

public:
    COgroEnemy() { Load(); }
    COgroEnemy(float x, float z) { position.x = x; position.z = z; Load(); }
    ~COgroEnemy() {}

    void Load();
};

最佳答案

您需要将指针强制转换为指针类型CEnemy*(或子类),或者将取消引用的指针强制转换为引用类型CEnemy&。为了最大程度的安全,我建议您使用dynamic_cast,而不要使用邪恶的C型强制转换;尽管这有点偏执,因为您要在投放前检查类型。

// no checks, undefined behaviour if type is wrong
((CEnemy*)collisionObject)->hitPoints -= 10;
static_cast<CEnemy*>(collisionObject)->hitPoints -= 10;

// throws if type is wrong
dynamic_cast<CEnemy&>(*collisionObject).hitPoints -= 10;

// does nothing if type is wrong
if (CEnemy* enemy = dynamic_cast<CEnemy*>(collisionObject)) {
    enemy->hitPoints -= 10;
}


您可以将其与类型检查结合使用,而不是使用typeid

if (COgro * ogro = dynamic_cast<COgro*>(collisionObject)) {
    ogro->hitPoints -= 10;
}


请注意,这与您的测试并不完全相同:如果对象是COgro的子类型,则它将通过,而您的测试将检查完全匹配。

10-08 06:53