public override bool TryMove(Field <IAnimal> field, out Vector nextMove) { nextMove = new Vector(0, 0); if (!TryDecreaseHealth()) { return(false); } _targetAntelope = FindClosestAntelope(field); if (_targetAntelope == null) { nextMove = _randomMoveCalculator.GetFreePositionsAndCalculate(Position, field); } else { nextMove = CalculateMoveToFollowTarget(field); var nextPos = Position + nextMove; if (field[nextPos.X, nextPos.Y] == _targetAntelope) { _health += _healthForAntelope; _targetAntelope.Die(EventArgs.Empty); _targetAntelope = null; } } return(true); }
private void PerformMating(Field <IAnimal> field) { if (_roundsLeftUntilBirth <= 0) { var cellBetweenParents = _vectorMath.DivideByNumber(Position + _matingTarget.Position, 2); GibBirth(new BirthEventArgs(cellBetweenParents)); _roundsLeftUntilBirth = ROUNDS_TO_REPRODUCE; } if (_matingTarget != null && MateStillAround(field)) { _roundsLeftUntilBirth--; } else { _matingTarget = FindNewMate(field); _roundsLeftUntilBirth = ROUNDS_TO_REPRODUCE; } }
public Antelope FindClosestAntelope(Field <IAnimal> field) { Antelope targetAntelope = null; for (int i = -_visionRange; i <= _visionRange; i++) { for (int j = -_visionRange; j <= _visionRange; j++) { var currentCellAsAntelope = field[Position.X + i, Position.Y + j] as Antelope; var currentIsCloser = targetAntelope == null || _vectorMath.ToEightWayDistance(new Vector(i, j)) < _vectorMath.ToEightWayDistance(Position - targetAntelope.Position); if (currentCellAsAntelope != null && currentIsCloser) { targetAntelope = currentCellAsAntelope; } } } return(targetAntelope); }
public Antelope FindNewMate(Field <IAnimal> field) { var closestMateDistance = int.MaxValue; Antelope newMate = null; for (int i = -_matingRange; i <= _matingRange; i++) { for (int j = -_matingRange; j <= _matingRange; j++) { if (i == 0 && j == 0) { continue; } var checkedCellAsAntelope = field[Position.X + i, Position.Y + j] as Antelope; if (checkedCellAsAntelope != null && _vectorMath.ToEightWayDistance(i, j) < closestMateDistance) { newMate = checkedCellAsAntelope; closestMateDistance = _vectorMath.ToEightWayDistance(Position - newMate.Position); } } } return(newMate); }