handleLoopEdges method

List<LoopNode> handleLoopEdges(
  1. NodeOutput item,
  2. State state
)

Implementation

List<LoopNode> handleLoopEdges(NodeOutput item, State state) {
  var mtx = state.mtx;
  var loops = this.loops(item.id);
  if (loops.length == 0) throw "no loops found for node ${item.id}";
  List<LoopNode> loopNodes = loops.map((String incomeId) {
    if (item.id == incomeId) {
      return LoopNode(
          id: incomeId, node: item, x: state.x, y: state.y, isSelfLoop: true);
    }
    List<int>? coords = mtx.find((NodeOutput n) {
      return n.id == incomeId;
    });
    if (coords?.length != 2) throw "loop target $incomeId not found on matrix";
    NodeOutput? node = mtx.getByCoords(coords![0], coords[1]);
    if (node == null) throw "loop target node $incomeId not found on matrix";
    return LoopNode(
        id: incomeId,
        node: node,
        x: coords[0],
        y: coords[1],
        isSelfLoop: false);
  }).toList();
  bool skip = loopNodes.any((LoopNode income) {
    int checkY = income.y != 0 ? income.y - 1 : 0;
    return mtx.hasVerticalCollision(state.x, checkY);
  });
  return skip ? [] : loopNodes;
}