shapeOf function
Implementation
HolyShape shapeOf(ActorDefinition a) {
var s = _shapes[a.mask];
if (s != null && s.x == a.x && s.y == a.y) return s;
final m = a.mask;
bool inside(int i, int j) =>
i >= 0 && j >= 0 && i < m.w && j < m.h && m.data[j * m.w + i] > 0;
final edge = <(int, int)>[];
final out = <(int, int)>[];
for (var j = -1; j <= m.h; j++) {
for (var i = -1; i <= m.w; i++) {
final nb = (inside(i - 1, j) ? 1 : 0) +
(inside(i + 1, j) ? 1 : 0) +
(inside(i, j - 1) ? 1 : 0) +
(inside(i, j + 1) ? 1 : 0);
if (inside(i, j)) {
if (nb < 4) edge.add((a.x + i, a.y + j));
} else if (nb != 0) {
out.add((a.x + i, a.y + j));
}
}
}
s = HolyShape(
x: a.x,
y: a.y,
edge: edge,
out: out,
top: a.y,
bottom: a.y + m.h,
mask: m,
);
_shapes[a.mask] = s;
return s;
}