Exemple #1
0
        public override void Update(long elapsedMilliseconds)
        {
            var deers  = MapObjects.GetAll <Deer>();
            var wolves = MapObjects.GetAll <Wolf>();

            foreach (var deer in deers)
            {
                //проверяю чтобы олени не вышли из полигона
                if (deer.Coordinate.X < _leftX - _barier || deer.Coordinate.Y < _downY - _barier)
                {
                    deer._speed = _deerSeed;
                }
                else if (deer.Coordinate.X > _rightX - _barier || deer.Coordinate.Y > _upY - _barier)
                {
                    deer._speed = -_deerSeed;
                }

                deer.MoveUpRight();
            }

            foreach (var wolf in wolves)
            {
                var nearestDeer = deers.Aggregate((deer1, deer2) =>
                                                  PointExtension.Distance((Point)wolf, (Point)deer1) < PointExtension.Distance((Point)wolf, (Point)deer2) ? deer1 : deer2);
                wolf.MoveUpRight(new Coordinate(nearestDeer.X, nearestDeer.Y));

                //Ищем ближайшего волка
                var minDistance = double.MaxValue;
                foreach (var _wolf in wolves.Where(_wolf =>
                                                   (PointExtension.Distance((Point)wolf, (Point)_wolf) < minDistance) &&
                                                   (PointExtension.Distance((Point)wolf, (Point)_wolf) > 0)))
                {
                    minDistance = (PointExtension.Distance((Point)wolf, (Point)_wolf));
                }

                if (minDistance < 250)
                {
                    if (wolf._flagPack == false)
                    {
                        Console.WriteLine(minDistance);
                        wolf.WolfInThePack();
                        wolf._flagPack = true;
                    }
                }
                else
                {
                    wolf._speed    = _wolfSpeed;
                    wolf._flagPack = false;
                }

                if (!wolf.CanEat(nearestDeer))
                {
                    continue;
                }
                MapObjects.Remove(nearestDeer);
                deers.Remove(nearestDeer);
                var newDeerCoordinate = GenerateCoordinatesAnimal(1)[0];
                MapObjects.Add(new Deer(newDeerCoordinate, 10));
            }
        }
Exemple #2
0
 public bool CanEat(Deer deer)
 {
     return(PointExtension.Distance(this, deer) < _speed);
 }