update method

void update({
  1. required List<AquaticCreature> creatures,
  2. required Size bounds,
  3. required double dt,
})

Implementation

void update({
  required List<AquaticCreature> creatures,
  required Size bounds,
  required double dt,
}) {
  if (bounds.width <= 0 || bounds.height <= 0) return;

  for (var creature in creatures) {
    switch (creature.type) {
      case CreatureType.jellyfish:
        _updateJellyfishPhysics(creature, bounds, dt);
        break;
      case CreatureType.seaTurtle:
        _updateSeaTurtlePhysics(creature, bounds, dt);
        break;
      case CreatureType.mantaRay:
        _updateMantaRayPhysics(creature, bounds, dt);
        break;
      case CreatureType.seahorse:
        _updateSeahorsePhysics(creature, bounds, dt);
        break;
      case CreatureType.starfish:
        _updateStarfishPhysics(creature, bounds, dt);
        break;
      case CreatureType.hermitCrab:
        _updateHermitCrabPhysics(creature, bounds, dt);
        break;
    }
  }

  // Gentle separation check to prevent same-species creatures (like jellyfish) from overlapping/glitching
  for (int i = 0; i < creatures.length; i++) {
    for (int j = i + 1; j < creatures.length; j++) {
      final c1 = creatures[i];
      final c2 = creatures[j];
      if (c1.type == c2.type) {
        final double dist = (c1.position - c2.position).distance;
        final double minDist = (c1.config.size + c2.config.size) * 0.7;
        if (dist < minDist) {
          Offset push = c1.position - c2.position;
          if (push.distance > 0.01) {
            push = push / push.distance;
          } else {
            push = Offset(_random.nextDouble() - 0.5, _random.nextDouble() - 0.5);
            push = push / push.distance;
          }
          double force = (minDist - dist) * 0.4;
          c1.position += push * force;
          c2.position -= push * force;

          // Nudge angles to steer away
          c1.angle = _normalizeAngle(c1.angle + 0.12);
          c2.angle = _normalizeAngle(c2.angle - 0.12);
        }
      }
    }
  }
}