/*
     * Generates all nodes according to the length and height of the map
     * Then, for each node, add their neighbours as successors
     */
    private void GenerateNodes()
    {
        for (int j = StartY; j < MapLength; j++)
        {
            for (int i = StartX; i < MapHeight; i++)
            {
                GameObject newPos =
                    (GameObject)Instantiate(Node, new Vector2(j * (NodeDistance), i * (NodeDistance)), Node.transform.rotation);
                newPos.transform.parent = gameObject.transform;
                newPos.layer            = 12;

                newPos.AddComponent <Node>();
                newPos.GetComponent <Node>().SetUpNode(ref _graph);
            }
        }

        for (int j = 0; j < _graph.ReturnGraph().Count; j++)
        {
            Node node = _graph.ReturnGraph().ElementAt(j);

            if (node.GetY() < NodeDistance)
            {
                node.AddNeighbour(-NodeDistance, ref _graph);
                node.AddNeighbour(NodeDistance, ref _graph);
            }
        }

        StartCoroutine(WaitForDeletion());
    }
    /**
     * Adds this node horizontal neighbour as it successors
     */
    public void AddNeighbour(float direction, ref GraphOfMap graph)
    {
        for (int i = 0; i < graph.ReturnGraph().Count; i++)
        {
            Node node = graph.ReturnGraph().ElementAt(i);

            if (node.GetX() == GetX() - direction && node.GetY() == GetY())
            {
                AddSuccessor(node, ref graph);
                //Debug.Log("successor " + node.GetX() +", " + node.GetY());
            }
        }
    }
    /*
     * Creates a jumping route
     * Neighbours of the current node become sucessor of the passed node (which should have higher Y coordinates)
     * Also add successors the other way
     */
    private void AddSuccessorForRising(Node platformNode, ref GraphOfMap graph, float nodeDistance)
    {
        for (int j = 0; j < graph.ReturnGraph().Count; j++)
        {
            Node node = graph.ReturnGraph().ElementAt(j);

            if ((node.GetX() == this.GetX() - nodeDistance || node.GetX() == this.GetX() + nodeDistance) && node.GetY() == this.GetY() && !node.gameObject.GetComponent <Collider2D>().IsTouchingLayers(_enviLayer))
            {
                node.AddSuccessor(platformNode, ref graph);
                platformNode.AddSuccessor(node, ref graph);

                DeleteSuccessor(node, ref graph);
                node.DeleteSuccessor(this, ref graph);
            }
        }
    }
    /**
     * Finds the node that is above the current node
     */
    private Node FindPlatformNode(float nodeDistance, ref GraphOfMap graph)
    {
        Node platformNode = null;

        for (int i = 0; i < graph.ReturnGraph().Count; i++)
        {
            Node node = graph.ReturnGraph().ElementAt(i);

            if (node.GetX() == this.GetX() &&
                node.GetY() == this.GetY() + nodeDistance)
            {
                platformNode = node;
                return(platformNode);
            }
        }

        return(platformNode);
    }