Exemplo n.º 1
0
        /// <summary>
        /// Обновление всех входящих элементов
        /// </summary>
        public static void Update()
        {
            foreach (BaseObject obj in _objs)
            {
                obj.Update();
            }
            foreach (Bullet b in _bullets)
            {
                b.Update();
            }

            for (int i = 0; i < _asteroids.Count; i++)
            {
                if (_asteroids[i] == null || (_asteroids[i] is Aid_kit && _asteroids[i].Rect.X < 0))
                {
                    _asteroids.RemoveAt(i--);
                    continue;
                }
                _asteroids[i].Update();

                for (int j = 0; j < _bullets.Count; j++)
                {
                    if (_bullets[j].Rect.X > Game.Width)
                    {
                        //_log($"Было: {_bullets.Count} Стало: {_bullets.Count - 1}.\n");
                        _bullets.RemoveAt(j--);
                        continue;
                    }

                    if (_bullets[j].Collision(_asteroids[i]))
                    {
                        System.Media.SystemSounds.Hand.Play();
                        if (!(_asteroids[i] is Aid_kit))
                        {
                            _score += _asteroids[i].Power;
                            _log($"Получено {_asteroids[i].Power} очков за сбитый астероид.\n");
                        }
                        _asteroids.RemoveAt(i--);
                        _bullets.RemoveAt(j--);
                        break;
                        //_asteroid[i] = null;
                        //_bullet = null;
                    }
                }

                if (i < 0 || !_ship.Collision(_asteroids[i]))
                {
                    continue;
                }
                var rnd = new Random();
                _ship?.EnergyLow(_asteroids[i].Power * rnd.Next(1, 10));

                if (_asteroids[i] is Aid_kit)
                {
                    _log($"Очки здоровья востановлены.\n");
                }
                else if (_asteroids[i] is Asteroid)
                {
                    _log($"Получен урон {_asteroids[i].Power} от астероида.\n");
                }

                System.Media.SystemSounds.Asterisk.Play();
                if (_ship.Energy <= 0)
                {
                    _ship?.Die();

                    _log($"Ваш корабль уничтожен.\nВы проиграли.\n");
                }
            }

            if (_asteroids.Count <= 0)
            {
                AddAsteroids();
            }

            #region Old Asteroids
            //foreach (Asteroid astr in _asteroid)
            //{
            //if (astr == null)
            //  continue;
            //astr.Update();

            //if (astr.Collision(_bullet))
            //{
            //  System.Media.SystemSounds.Hand.Play();
            //  //astr.Death();
            //  //_bullet.Death();
            //  astr = null;

            //}
            //}
            #endregion
        }