一个主要的困境是我的游戏最大的方面似乎无法运行。我正在制作一个 3D 太空侵略者游戏,我试图让玩家(和敌人)互相发射子弹。但似乎两者都没有这样做。
我有一个攻击管理器来处理他们的代码。
/**
* Prepares a bullet for firing if the current time is greater than
* the last fired time + the firing delay. This purpose of this is to
* put a delay between each bullet as it is fired. A bullet is only
* 'readied' for firing if it is available, i.e. it's alive property
* is set to false.
*
*/
void PlayerAttackManager::fireBullet() {
if ( mCurrentTime > ( mLastFiredTime + sFIRE_DELAY ) ) {
// Find the next available bullet (if one exists)
std::vector<Bullet>::iterator end = mBullets.end();
std::vector<Bullet>::iterator curr = mBullets.begin();
for ( ; curr != end && (*curr).alive() == true; ++curr );
// If there is a bullet available.
if ( curr != end ) {
// Move bullet to firing location and set alive.
Vector3 bulletPos = mSceneMgr->getSceneNode( "PlayerParentNode" )->getPosition();
Vector3 bulletDir = mSceneMgr->getSceneNode( "PlayerParentNode" )->getOrientation() * Vector3::UNIT_Z;
// Pass the initial screen position and direction of travel for the bullet.
(*curr).reset( bulletPos, bulletDir );
}
mLastFiredTime = mCurrentTime;
}
}
每次按下空格键时都会调用上述方法,但它会从玩家位置向下发射一颗敌人子弹,从玩家上方向下发射另一颗子弹。
敌人应该攻击(每秒一次),从 main.cpp frameRenderingQueued 方法调用 EnemyAttackManager 来触发 EnemyAttackManager 的更新如下:
void EnemyAttackManager::update(Real const & timeSinceLastFrame, string name)
{
mName = name;
std::vector<Bullet>::iterator end = mBullets.end();
std::vector<Bullet>::iterator curr = mBullets.begin();
for ( ; curr != end && (*curr).alive() == true; ++curr ) {
if ( curr != end ) {
// Move bullet to firing location and set alive.
Vector3 bulletPos = mSceneMgr->getSceneNode( mName + "Node" )->getPosition();
Vector3 bulletDir =mSceneMgr->getSceneNode(mName+ "Node" )->getOrientation() * Vector3::UNIT_Z;
// Pass the initial screen position and direction of travel for the bullet.
(*curr).reset( bulletPos, bulletDir );
(*curr).update(timeSinceLastFrame);
}
}
// Add time since last frame.
mCurrentTime += timeSinceLastFrame;
}
子弹的更新和重置功能分别是:
void Bullet::update( Real const & timeSinceLastFrame ) {
mTtl -= timeSinceLastFrame;
if ( mTtl <= 0 ) {
mNode->setVisible( false );
}
else {
Enemy* enem = new Enemy();
mNode->translate( sMOVE * mDir * timeSinceLastFrame, Node::TS_LOCAL );
// Retrieve a list of possible enemies.
std::vector<Enemy *> enemyVec = enem->getEnemies();
std::vector<Enemy *>::iterator curr = enemyVec.begin();
std::vector<Enemy *>::iterator end = enemyVec.end();
// Check if the ray intersected any of the enemies
for ( ; curr != end && !CollisionManager::instance()->rayIntersects( mNode->getPosition(), 0, (*curr)->meshName(), mDir )
; ++curr );
if ( curr != end ) { // if bullet has collided with an enemy.
//(*curr)->applyDamage();
mTtl = 0;
//JetPack3D::score += 10;
mNode->setVisible( false );
}
//Check the current bullet pos vs. player
}
}
void Bullet::reset( Vector3 const & bulletPos, Vector3 const & bulletDir ) {
mNode->setPosition( bulletPos );
mNode->setVisible( true );
mTtl = sLIFE_TIME;
mDir = bulletDir;
// Need to reverse x and z components so bullet fires forward into the scene.
//now need it to fire right?
mDir.z *= -1;
mDir.x *= -1;
}
所以我的问题是:我如何让敌人和玩家发射子弹(敌人开火而玩家朝着正确的方向)