Example #1
0
        public void Update(GameTime gameTime)
        {
            bool[] bulletsToKeepAround = new bool[Bullets.Count];
            Parallel.For(0, Bullets.Count, i =>
            {
                Bullet currentBullet = Bullets[i];
                if (currentBullet.ShouldDispose(GameTime.Now))
                {
                    currentBullet.Dispose();
                }
                if (currentBullet.Disposed)
                {
                    bulletsToKeepAround[i] = false; // don't keep me around
                }
                else
                {
                    currentBullet.Update(gameTime);
                    bulletsToKeepAround[i] = true; // keep me around
                }
            });

            lock (_locker)
            {
                for (int i = bulletsToKeepAround.Length - 1; i >= 0; i--)
                {
                    if (!bulletsToKeepAround[i])
                    {
                        Bullets[i] = Bullets[Bullets.Count - 1];
                        Bullets.RemoveAt(Bullets.Count - 1);
                    }
                }
            }
        }