cracks function

List<CrackLine> cracks(
  1. Rng r,
  2. double x0,
  3. int n,
  4. double reach, [
  5. double depth = 7.0,
  6. double bias = 0.3,
])

Jagged cracks splitting the floor from x0.

Implementation

List<CrackLine> cracks(
  Rng r,
  double x0,
  int n,
  double reach, [
  double depth = 7.0,
  double bias = 0.3,
]) {
  final out = <CrackLine>[];
  for (var i = 0; i < n; i++) {
    final side = r.chance(0.5 + bias * 0.5) ? 1.0 : -1.0;
    final l = reach * r.range(0.45, 1.0) * (i < 2 ? 1.0 : 0.7);
    final pts = <Point2>[(x0 + side * r.range(0.0, 1.5), 0.0)];
    var x = pts[0].$1;
    var y = 0.0;
    while ((x - x0).abs() < l && y < depth) {
      x += side * r.range(1.5, 3.5);
      y = math.min(
        depth,
        math.max(0.0, y + r.range(-0.6, 1.6) * ((x - x0).abs() > 3.0 ? 1.0 : 0.3)),
      );
      pts.add(((x + 0.5).floorToDouble(), (y + 0.5).floorToDouble()));
      if (r.chance(0.18) && pts.length > 3 && y < depth - 2.0) {
        final f = <Point2>[((x + 0.5).floorToDouble(), (y + 0.5).floorToDouble())];
        var fx = x;
        var fy = y;
        for (var j = r.intRange(1, 3); j > 0; j--) {
          fx += side * r.range(1.0, 3.0);
          fy += r.range(0.8, 2.0);
          f.add((
            (fx + 0.5).floorToDouble(),
            (math.min(depth + 1.0, fy) + 0.5).floorToDouble(),
          ));
        }
        out.add(CrackLine(pts: f, at: (x - x0).abs() / reach, fork: true));
      }
    }
    out.add(CrackLine(pts: pts, at: 0.0, fork: false));
  }
  return out;
}