public bool CheckTerminationCondition(MapSegmentTraverseResult lastResult, MapSegmentTraverseResult startNode, List <Vector2> points)
    {
        if (points.Count > MAX_POINTS_BEFORE_ERROR)
        {
            Debug.LogError("Error when generating mapsegment colliders. Points in mesh exceeds " + MAX_POINTS_BEFORE_ERROR);

            return(true);
        }

        // These null-guards works against dveelopment error and step-by-step implementation of this algorithm
        if (lastResult == null || startNode == null)
        {
            return(true);
        }

        if (lastResult.PathTile == null || startNode.PathTile == null)
        {
            return(true);
        }

        if (lastResult.PathTile == startNode.PathTile && lastResult.Direction == startNode.Direction)
        {
            // Even when no loops of the while-loop has been executed, a single, startnode will be present in this list.
            if (points.Count > 1)
            {
                return(true);
            }
        }

        return(false);
    }
    public List <Vector2> GetGroupPoints(ICollection <MapSegmentPathTile> tiles, MapSegmentPathTile startTile, MapSegmentDirection startDirection)
    {
        var result    = new List <Vector2>();
        var startNode = new MapSegmentTraverseResult {
            PathTile = startTile, Direction = startDirection
        };

        var currentNode = startNode;
        var point       = startNode.PathTile.GetStartPoint(startDirection).ToVector2();

        result.Add(point);

        while (!CheckTerminationCondition(currentNode, startNode, result))
        {
            point = currentNode.PathTile.GetEndPoint(currentNode.Direction).ToVector2();
            result.Add(point);
            currentNode = GetNextNode(currentNode);
        }

        return(result);
    }
    public MapSegmentTraverseResult GetNextNode(MapSegmentTraverseResult lastStep)
    {
        if (lastStep.Direction == MapSegmentDirection.Up)
        {
            var nextTile = lastStep.PathTile.NeighbourRight;
            if (MapSegmentPathTile.IsFree(nextTile))
            {
                return(new MapSegmentTraverseResult {
                    PathTile = lastStep.PathTile, Direction = MapSegmentDirection.Right
                });
            }
            else
            {
                if (MapSegmentPathTile.IsFree(nextTile.NeighbourUp))
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile, Direction = lastStep.Direction
                    });
                }
                else
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile.NeighbourUp, Direction = MapSegmentDirection.Left
                    });
                }
            }
        }
        else if (lastStep.Direction == MapSegmentDirection.Right)
        {
            var nextTile = lastStep.PathTile.NeighbourDown;
            if (MapSegmentPathTile.IsFree(nextTile))
            {
                return(new MapSegmentTraverseResult {
                    PathTile = lastStep.PathTile, Direction = MapSegmentDirection.Down
                });
            }
            else
            {
                if (MapSegmentPathTile.IsFree(nextTile.NeighbourRight))
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile, Direction = lastStep.Direction
                    });
                }
                else
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile.NeighbourRight, Direction = MapSegmentDirection.Up
                    });
                }
            }
        }
        else if (lastStep.Direction == MapSegmentDirection.Down)
        {
            var nextTile = lastStep.PathTile.NeighbourLeft;
            if (MapSegmentPathTile.IsFree(nextTile))
            {
                return(new MapSegmentTraverseResult {
                    PathTile = lastStep.PathTile, Direction = MapSegmentDirection.Left
                });
            }
            else
            {
                if (MapSegmentPathTile.IsFree(nextTile.NeighbourDown))
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile, Direction = lastStep.Direction
                    });
                }
                else
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile.NeighbourDown, Direction = MapSegmentDirection.Right
                    });
                }
            }
        }
        else if (lastStep.Direction == MapSegmentDirection.Left)
        {
            var nextTile = lastStep.PathTile.NeighbourUp;
            if (MapSegmentPathTile.IsFree(nextTile))
            {
                return(new MapSegmentTraverseResult {
                    PathTile = lastStep.PathTile, Direction = MapSegmentDirection.Up
                });
            }
            else
            {
                if (MapSegmentPathTile.IsFree(nextTile.NeighbourLeft))
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile, Direction = lastStep.Direction
                    });
                }
                else
                {
                    return(new MapSegmentTraverseResult {
                        PathTile = nextTile.NeighbourLeft, Direction = MapSegmentDirection.Down
                    });
                }
            }
        }

        // Theorically impossible but catches and handles errors
        // TODO: Implement exceptions and exception handling for this kind of errors
        return(null);
    }