public Point GetNewPositionByBehavior(IAnimal animal, Point nearestAnimal, Point oldPosition, MovingType movingType) { _animalSpeed = _field.Animals[oldPosition.Y, oldPosition.X].Speed; Point newPosition = oldPosition; DistanceHandler distanceHandler = new DistanceHandler(); if ((nearestAnimal.X != -1) && (distanceHandler.GetDistance(oldPosition, nearestAnimal) <= animal.VisionRange)) { if ((distanceHandler.GetDistance(oldPosition, nearestAnimal) <= animal.Speed) && (animal.Type == AnimalType.Carnivore)) { newPosition = nearestAnimal; } else { Point desiredPosition = _pathHandler.GetAnimalNextWaypoint(oldPosition, nearestAnimal, movingType); newPosition = GetSuitableNewPosition(oldPosition, desiredPosition, animal.Type); } } else { newPosition = GetRandomNewPosition(oldPosition); } return(newPosition); }
public Point GetSuitableNewPosition(Point currentPosition, Point desiredPosition, AnimalType animalType) { Point newPosition = new Point(currentPosition.X, currentPosition.Y); if ((desiredPosition.X >= 0) && (desiredPosition.Y >= 0) && (desiredPosition.X < _field.Width) && (desiredPosition.Y < _field.Heigth) && _positionChecker.CheckSuitability(desiredPosition, animalType)) { newPosition = desiredPosition; } else { double minimumDistance = Math.Sqrt(_field.Heigth * _field.Heigth + _field.Width * _field.Width); DistanceHandler distanceHandler = new DistanceHandler(); List <Point> PositionsToMove = GetAllPositionsToMove(currentPosition); foreach (Point position in PositionsToMove) { double distance = distanceHandler.GetDistance(position, desiredPosition); if (distance < minimumDistance) { minimumDistance = distance; newPosition = position; } } } return(newPosition); }