import 'dart:math' as math;

import '../utils.dart';

// Predator DNA
class PredatorDna {
  PredatorDna({
    required this.preyPerception,
    required this.preyPull,
  });

  double preyPerception;
  double preyPull;

  static const double _mutationThreshold = 0.8;

  /// returns a new BoidDna with random values in preset ranges
  factory PredatorDna.rand() {
    final rng = math.Random();
    return PredatorDna(
      preyPerception: rng.getRandomDoubleInRange(30.0, 100.0),
      preyPull: rng.getRandomDoubleInRange(-1.0, 1.0),
    );
  }

  static double _getRandMutationDelta() {
    final rng = math.Random();
    if (rng.getRandomDoubleInRange(0.0, 1.0) > _mutationThreshold) {
      if (rng.getRandomDoubleInRange(0.0, 1.0) > 0.5) {
        return 0.1;
      } else {
        return -0.1;
      }
    } else {
      return 0.0;
    }
  }

  /// mutate the DNA values
  void mutate() {
    preyPerception += _getRandMutationDelta() * 50.0;
    preyPull += _getRandMutationDelta();
  }
}
