1
\$\begingroup\$

So I'm making a game where you fire bullets at enemy ships which are flying past you. The enemy ships are also firing at a straight line in front of them. The bullets are made into a list of another class and the enemy ships are as well. Both the player and the enemy ships fire from the same class of bullets but just from different initialized lists.

My issue here is that when a bullet fired from the player's ship destroys an enemy ship, every bullet which in already in play after being fired by the same ship is also removed and I don't know how to only remove the ship while leaving the bullets in play. Sorry if this is confusing.

Here is my code which detects collisions between created rectangles on my player bullets and the enemies.

public void HitEnemy()
    {
        foreach (Bullets bullet in bullets)
        {
            bulletbb = new Rectangle(
                (int)bullet.position.X,
                (int)bullet.position.Y,
                texWeaponBullet.Width,
                texWeaponBullet.Height);

            foreach (Enemies enemy in enemies)
            {
                enemybb = new Rectangle(
                    (int)enemy.position.X,
                    (int)enemy.position.Y,
                    texEnemy1.Width,
                    texEnemy1.Height);

                if (bulletbb.Intersects(enemybb))
                {
                    bullet.isVisible = false;
                    enemy.isVisible = false;
                }
            }                
        }
    }

If isVisible = false then the object is removed from play.

Here is my Enemies class:

namespace MainProject
{    
class Enemies
    {
    public Texture2D texture;

    public Vector2 position;
    public Vector2 velocity;

    public bool isVisible = true;

    Random random = new Random();
    int randY;
    int randX;

    //Bullets
    List<Bullets> eBullets = new List<Bullets>();
    Texture2D bulletTexture;
    float shoot = 0;

    public Enemies(Texture2D newTexture, Vector2 newPosition, Texture2D newBulletTexture)
    {
        texture = newTexture;
        position = newPosition;
        bulletTexture = newBulletTexture;

        randY = random.Next(-4, 4);
        randX = random.Next(-4, -1);

        velocity = new Vector2(-4, 0);
    }

    public void Update(GraphicsDevice graphics, GameTime gameTime)
    {
        position += velocity;

        if (position.Y <= 0 || position.Y >= 600 - texture.Height)
        {
            velocity.Y = -velocity.Y;
        }

        if (position.X < 0 - texture.Width)
        {
            isVisible = false;
        }

        shoot += (float)gameTime.ElapsedGameTime.TotalSeconds;
        if (shoot > 1)
        {
            shoot = 0;
            ShootBullets();
        }

        UpdateBullets();
    }

    public void UpdateBullets()
    {
        foreach(Bullets bullet in eBullets)
        {
            bullet.position += bullet.velocity;
            if (bullet.position.X < 0)
            {
                bullet.isVisible = false;
            }
        }
        for(int i = 0; i < eBullets.Count; i++)
        {
            if (!eBullets[i].isVisible)
            {
                eBullets.RemoveAt(i);
                i--;
            }
        }
    }

    public void ShootBullets()
    {
        Bullets newBullet = new Bullets(bulletTexture);
        newBullet.velocity.X = velocity.X - 1.5f;
        newBullet.position = new Vector2(position.X + newBullet.velocity.X, 
            position.Y + (texture.Height / 2f) - (bulletTexture.Height / 2));

        newBullet.isVisible = true;
        if (eBullets.Count < 3)
        {
            eBullets.Add(newBullet);
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        foreach (Bullets bullet in eBullets)
        {
            bullet.Draw(spriteBatch);
        }

        spriteBatch.Draw(texture, position, Color.White);
    }
}
}

I am at a bit of a loss for what to do. Any help would be appreciated and tell me if you need any more information because there might be something I missed.

\$\endgroup\$
3
\$\begingroup\$

As you're probably aware, since the ship is responsible for drawing and moving the bullets it created, the bullets it created will no longer be drawn or updated when their creator is destroyed.

Instead, consider having another object that manages bullets -- let's call it BulletManager. BulletManager has UpdateBullets, which it calls each frame. It also has ShootBullets, taking position as an argument. When an enemy shoots, it calls BulletManager.ShootBullets(position).

\$\endgroup\$
  • \$\begingroup\$ I am really confused. I am unsure how to initialize the BulletManager object. Does BulletManager also store the Bullets objects or does it just update all the bullet logic? \$\endgroup\$ – TheCal May 3 '18 at 12:47
  • \$\begingroup\$ It should have the list of bullet objects, yes. eBullets, UpdateBullets, and ShootBullets will all be in the BulletManager now (the latter modified to take a position argument that'll be set by the enemy that calls the function) \$\endgroup\$ – Jibb Smart May 3 '18 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.