Exemple #1
0
        /// <summary>
        /// Invaders are shooting at player. There can be max Wave+1 shots in play area;
        /// </summary>
        private void ReturnFire()
        {
            if (_invaders.Count <= 0)
            {
                return;
            }

            int possibleShots        = Wave + 1;
            int shotsInvadersCanShot = _invaderShots.Count - possibleShots;

            if (shotsInvadersCanShot > 0 && !(_random.Next(10) < 10 - Wave))
            {
                Point shotLocation;
                Shot  shot;

                // Select invaders from last row  ( only this invaders can shot)

                var groupedByYPosition =
                    from invader in _invaders
                    group invader by invader.Location.Y
                    into invadersGroups
                    orderby invadersGroups.Key
                    select invadersGroups;
                var bottomInvaders = groupedByYPosition.Last();

                for (int i = 0; i < possibleShots; i++)
                {
                    Invader invader = bottomInvaders.ElementAt(_random.Next(0, bottomInvaders.Count()));
                    shotLocation = new Point(invader.Location.X, invader.Location.Y + invader.Size.Height / 2);
                    shot         = new Shot(shotLocation, Direction.Down);
                    _invaderShots.Add(shot);
                    OnShotMoved(_invaderShots.Last(), false);
                }
            }
        }
        private void ReturnFire()
        {
            // EVALUATE later!!

            if (_invaders.Count() == 0)
            {
                return;
            }
            if (_invaderShots.Count >= (Wave + 1))
            {
                return;
            }
            if (_random.Next(10) < 10 - Wave)
            {
                return;
            }

            var invadersGroups =
                from invader in _invaders
                orderby invader.Location.X descending
                group invader by invader.Location.X
                into invaderGroups
                select invaderGroups;

            var     randomGroup = invadersGroups.ElementAt(_random.Next(invadersGroups.Count()));
            Invader shooter     = randomGroup.Last();

            Shot newShot = new Shot(new Point(shooter.Area.X + shooter.Area.Width / 2 - 1, shooter.Area.Bottom), Direction.Down);

            _invaderShots.Add(newShot);
            OnShotMoved(newShot, false);
        }
Exemple #3
0
        private void CheckForInvaderCollisions()
        {
            // Check if any player shots hit an invader
            foreach (Shot shot in _playerShots.ToList())
            {
                var deadInvaders = from invader in _invaders
                                   where invader.Area.Contains(shot.Location)
                                   select invader;
                if (deadInvaders.Count() > 0)
                {
                    Invader deadInvader = deadInvaders.ElementAt(0);
                    Score += deadInvader.Score;
                    _invaders.Remove(deadInvader);
                    OnShipChanged(deadInvader, true);

                    _playerShots.Remove(shot);
                    OnShotMoved(shot, true);
                }
            }

            // Check if any invadors made it to the bottom of the battlefield
            var escapedInvaders = from invader in _invaders
                                  where invader.Area.Bottom >= PlayAreaSize.Height
                                  select invader;

            if (escapedInvaders.Count() > 0)
            {
                EndGame();
            }
        }
Exemple #4
0
        private void NextWave()
        {
            Wave += 1;
            // Scale the speed of the invaders to the wave number, so they get faster at higher levels
            _invaderTimeIntervalLimit = TimeSpan.FromMilliseconds(1000 / Wave);
            _invaders.Clear();

            double xPosition = 0;
            double yPosition = Invader.invaderPixelsPerMove * _invaderRows - 1;

            for (int i = 0; i < _invaderRows; i++)
            {
                for (int j = 0; j < _invadersPerRow; j++)
                {
                    Invader invader;
                    if (i < 2)
                    {
                        invader = new Invader(new Point(xPosition, yPosition), InvaderType.Star, 10);
                    }
                    else
                    {
                        invader = new Invader(new Point(xPosition, yPosition), (InvaderType)(i - 1), 10 * i);
                    }
                    _invaders.Add(invader);

                    // Increment horizontal position
                    xPosition += Invader.invaderPixelsPerMove;
                }
                // Reset horizontal position & increment vertical position
                xPosition  = 0;
                yPosition -= Invader.invaderPixelsPerMove;
            }
        }
Exemple #5
0
        private void InvaderFire(Invader invader)
        {
            // Get a point at the bottom center of the invader
            Point shotLocation = new Point((invader.Area.BottomRight.X + invader.Area.BottomLeft.X) / 2,
                                           invader.Area.Bottom);

            Shot newShot = new Shot(shotLocation, Direction.Down);

            _invaderShots.Add(newShot);

            OnShotMoved(newShot, false);
        }
Exemple #6
0
        public void InvadersFire()
        {
            // makes them fire at random, not all the time:
            int randomNumber = _random.Next(10);

            if (randomNumber < 9 - Wave && 9 - Wave >= 0)
            // the above && condition is insurance for when
            // Wave surpasses 10 and returns a (-) number
            {
                return;
            }
            else
            {
                if (_invaderShots.Count <= 3 + Wave)
                {
                    // use LINQ to group the invaders by their Location.X and sort them descending.
                    // Once it’s got those groups, it can choose a group at random, and use its Last()
                    // method to find the invader at the bottom of the column.

                    double randomIndex = random.Next(12);

                    var invaderColumns =
                        from invader in _invaders
                        group invader by invader.Location.X
                        into columns
                        orderby columns.Key
                        select columns;

                    List <Invader> bottomInvaders = new List <Invader>();

                    foreach (var group in invaderColumns)
                    {
                        Invader bottomInvader = group.Last <Invader>();
                        bottomInvaders.Add(bottomInvader);
                    }

                    Invader firingInvader = bottomInvaders[random.Next(bottomInvaders.Count)];
                    Shot    newShot       = new Shot(new Point(firingInvader.Location.X + firingInvader.Size.Width / 1.3,
                                                               firingInvader.Location.Y + 30), Direction.Down, false);
                    _invaderShots.Add(newShot);
                    OnShotMoved(newShot, false, false);
                }
            }
        }
        private void NextWave()
        {
            Wave++;
            _invaders.Clear();

            InvaderType invaderType;

            for (int row = 0; row < 6; row++)
            {
                switch (row)
                {
                case 0:
                    invaderType = InvaderType.Spaceship;
                    break;

                case 1:
                    invaderType = InvaderType.Bug;
                    break;

                case 2:
                    invaderType = InvaderType.Saucer;
                    break;

                case 3:
                    invaderType = InvaderType.Satellite;
                    break;

                default:
                    invaderType = InvaderType.Star;
                    break;
                }

                for (int column = 0; column < 11; column++)
                {
                    Point   location   = new Point(column * Invader.InvaderSize.Width * 1.4, row * Invader.InvaderSize.Height * 1.4);
                    Invader newInvader = new Invader(invaderType, location, _invaderScores[invaderType]);
                    _invaders.Add(newInvader);
                    OnShipChanged(newInvader, false);

                    //_invaders.Add(new Invader(invaderType, location, _invaderScores[invaderType]));
                    //OnShipChanged(_invaders[_invaders.Count - 1], false);
                }
            }
        }
Exemple #8
0
        private void NextWave()
        {
            Wave++;
            _invaders.Clear();

            for (int row = 0; row <= 5; row++)
            {
                for (int column = 0; column < 11; column++)
                {
                    Point   location = new Point(column * Invader.InvaderSize.Width * 1.4, row * Invader.InvaderSize.Height * 1.4);
                    Invader invader;

                    switch (row)
                    {
                    case 0:
                        invader = new Invader(InvaderType.Spaceship, location, 50);
                        break;

                    case 1:
                        invader = new Invader(InvaderType.Bug, location, 40);
                        break;

                    case 2:
                        invader = new Invader(InvaderType.Saucer, location, 30);
                        break;

                    case 3:
                        invader = new Invader(InvaderType.Satellite, location, 20);
                        break;

                    default:
                        invader = new Invader(InvaderType.Star, location, 10);
                        break;
                    }
                    _invaders.Add(invader);
                    OnShipChanged(invader, false);
                }
            }
        }
Exemple #9
0
        private void ReturnFire()
        {
            if ((_invaderShots.Count() > Wave + 1) ||
                (_random.Next(10) < 10 - Wave))
            {
                return;
            }

            Invader randomInvader = _invaders.GroupBy(q => q.Location.Y)
                                    .OrderBy(d => d.Key)
                                    .Select(g => g.ElementAt(_random.Next(g.Count())))
                                    .Last();

            InvaderFire(randomInvader);

            foreach (Invader invader in _invaders)
            {
                if (TakeAim(_player.Location.X, _player.Size.Width,
                            invader.Location.X, invader.Size.Width))
                {
                    InvaderFire(randomInvader);
                }
            }
        }
Exemple #10
0
 /// <summary>
 /// Checks if shot collides with given invader
 /// </summary>
 /// <param name="playerShot"></param>
 /// <param name="invader"></param>
 /// <returns>True if there is collision</returns>
 private bool CheckForInvaderCollision(Shot playerShot, Invader invader)
 {
     return CollisionHelper.CheckCollision(invader.Area, playerShot.Location);
 }
Exemple #11
0
 /// <summary>
 /// Checks if shot collides with given invader
 /// </summary>
 /// <param name="playerShot"></param>
 /// <param name="invader"></param>
 /// <returns>True if there is collision</returns>
 private bool CheckForInvaderCollision(Shot playerShot, Invader invader)
 {
     return(CollisionHelper.CheckCollision(invader.Area, playerShot.Location));
 }
        private void NextWave()
        {
            Wave += 1;
            // Scale the speed of the invaders to the wave number, so they get faster at higher levels
            _invaderTimeIntervalLimit = TimeSpan.FromMilliseconds(1000 / Wave);
            _invaders.Clear();

            double xPosition = 0;
            double yPosition = Invader.invaderPixelsPerMove * _invaderRows - 1;

            for (int i = 0; i < _invaderRows; i++)
            {
                for (int j = 0; j < _invadersPerRow; j++)
                {
                    Invader invader;
                    if (i < 2)
                    {
                        invader = new Invader(new Point(xPosition, yPosition), InvaderType.Star, 10);
                    }
                    else
                    {
                        invader = new Invader(new Point(xPosition, yPosition), (InvaderType)(i - 1), 10*i);
                    }
                    _invaders.Add(invader);

                    // Increment horizontal position
                    xPosition += Invader.invaderPixelsPerMove;
                }
                // Reset horizontal position & increment vertical position
                xPosition = 0;
                yPosition -= Invader.invaderPixelsPerMove;
            }
        }
        private void InvaderFire(Invader invader)
        {
            // Get a point at the bottom center of the invader
            Point shotLocation = new Point((invader.Area.BottomRight.X + invader.Area.BottomLeft.X) / 2,
                                            invader.Area.Bottom);

            Shot newShot = new Shot(shotLocation, Direction.Down);
            _invaderShots.Add(newShot);

            OnShotMoved(newShot, false);
        }