actorShape function

ActorShape actorShape(
  1. ActorDefinition a
)

Implementation

ActorShape actorShape(ActorDefinition a) {
  final m = a.mask;
  var s = _shapes[m];
  if (s != null) return s;

  final w = m.w;
  final h = m.h;
  final data = m.data;
  bool on(int x, int y) =>
      x >= 0 && y >= 0 && x < w && y < h && data[y * w + x] > 0;

  final body = <(int, int)>[];
  final up = <(int, int)>[];
  final side = <(int, int, int)>[];
  final outline = <(int, int, double, double)>[];
  final top = <int>[];
  final rows = <(int, int)>[];

  for (var x = 0; x < w; x++) {
    var y = 0;
    while (y < h && !on(x, y)) {
      y++;
    }
    top.add(y < h ? y : -1);
  }
  for (var y = 0; y < h; y++) {
    var l = -1;
    var r = -1;
    for (var x = 0; x < w; x++) {
      if (!on(x, y)) continue;
      if (l < 0) l = x;
      r = x;
      body.add((x, y));
      if (!on(x, y - 1)) up.add((x, y));
      if (!on(x - 1, y)) side.add((x, y, -1));
      if (!on(x + 1, y)) side.add((x, y, 1));
    }
    rows.add((l, r));
  }
  for (var y = -1; y <= h; y++) {
    for (var x = -1; x <= w; x++) {
      if (on(x, y)) continue;
      var nx = 0.0;
      var ny = 0.0;
      for (var dy = -1; dy <= 1; dy++) {
        for (var dx = -1; dx <= 1; dx++) {
          if (on(x + dx, y + dy)) {
            nx -= dx;
            ny -= dy;
          }
        }
      }
      if (on(x - 1, y) || on(x + 1, y) || on(x, y - 1) || on(x, y + 1)) {
        final rawL = hypot(nx, ny);
        final l = rawL == 0.0 ? 1.0 : rawL;
        outline.add((x, y, nx / l, ny / l));
      }
    }
  }

  s = ActorShape._(
    w: w,
    h: h,
    data: data,
    body: body,
    up: up,
    side: side,
    outline: outline,
    top: top,
    rows: rows,
  );
  _shapes[m] = s;
  return s;
}