Player Bullets going the wrong way and Enemy bullets not firing

239 Views Asked by At

Bit of a major predicament as the largest aspect of my game seems to be non-functioning. I'm making a 3D space invaders game and I'm trying to get the player(and enemies) to fire bullets at each other. But neither seem to be doing so.

I have an attack manager for both that handles their code.

/**
* 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;
    }
}

The above method is called every time the space bar is pressed but it sends one enemy bullet from the player's position moving downwards and another from above the player moving downwards.

The enemies should attack(one every second) which is called for the EnemyAttackManager to fire from the main.cpp frameRenderingQueued method The EnemyAttackManager's update is as follows:

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;
}

And the bullet's update and reset functions are these respectively:

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;
}

So my question is: How do I make the enemies and the player fire bullets(enemies firing at all and player in the right direction)

0

There are 0 best solutions below