gpt4 book ai didi

C++类继承

转载 作者:行者123 更新时间:2023-11-28 00:20:20 25 4
gpt4 key购买 nike

我有两个类:

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 的子类型,它将通过,而您的测试会检查是否完全匹配。

关于C++类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27804480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com