コード例 #1
0
 public Node_AStar(Node_AStar copiedNode)
 {
     this.Index                 = copiedNode.Index;
     this.Parent                = copiedNode.Parent;
     this.Passable              = copiedNode.Passable;
     this.DistanceFromStart     = copiedNode.DistanceFromStart;
     this.DistanceToDestination = copiedNode.DistanceToDestination;
     this.CostToDestination     = copiedNode.CostToDestination;
 }
コード例 #2
0
    private bool NodeInClosedList(Node_AStar checkedNode)
    {
        for (int i = 0; i < closedList.Count; i++)
        {
            if (closedList[i] == checkedNode)
            {
                return(true);
            }
        }

        return(false);
    }
コード例 #3
0
    private bool NodeInOpenList(Node_AStar checkedNode)
    {
        for (int i = 0; i < openList.Count; i++)
        {
            if (openList[i] == checkedNode)
            {
                return(true);
            }
        }

        return(false);
    }
コード例 #4
0
    private void Refresh(TileMap_MapData.TileMap_TileData[,] tileData, TileMap_MapData.TileMap_TileData destinationTile)
    {
        openList.Clear();
        closedList.Clear();
        FinalTrack.Clear();
        currentNode = null;

        for (int z = 0; z < column; z++)
        {
            for (int x = 0; x < row; x++)
            {
                node[x, z].Initialize(tileData[x, z], destinationTile);
            }
        }
    }
コード例 #5
0
ファイル: ATest.cs プロジェクト: BlankRip/ExportToFbx
    private void UpdatePathStartNTarget(Transform startPos, Transform targetPos, ref Node_AStar start, ref Node_AStar target)
    {
        float   closestDistance = int.MaxValue;
        float   distance;
        Vector3 direction;
        Vector3 startPosition = startPos.position;
        Vector3 endPosition;

        for (int i = 0; i < RepresentGraphIn2DArray_AStar.instance.allNodes.Length; i++)
        {
            endPosition = RepresentGraphIn2DArray_AStar.instance.allNodes[i].transform.position;
            direction   = (endPosition - startPosition).normalized;
            distance    = Vector3.Distance(startPosition, endPosition);
            if (Physics.Raycast(startPosition, direction, distance, RepresentGraphIn2DArray_AStar.instance.nonWalkableLayers))
            {
                ;
            }
            else
            {
                if (distance < closestDistance)
                {
                    start           = RepresentGraphIn2DArray_AStar.instance.allNodes[i];
                    closestDistance = distance;
                }
            }
        }

        startPosition   = targetPos.position;
        closestDistance = int.MaxValue;
        for (int i = 0; i < RepresentGraphIn2DArray_AStar.instance.allNodes.Length; i++)
        {
            endPosition = RepresentGraphIn2DArray_AStar.instance.allNodes[i].transform.position;
            direction   = (endPosition - startPosition).normalized;
            distance    = Vector3.Distance(startPosition, endPosition);
            if (Physics.Raycast(startPosition, direction, distance, RepresentGraphIn2DArray_AStar.instance.nonWalkableLayers))
            {
                ;
            }
            else
            {
                if (distance < closestDistance)
                {
                    target          = RepresentGraphIn2DArray_AStar.instance.allNodes[i];
                    closestDistance = distance;
                }
            }
        }
    }
コード例 #6
0
    private void Create2DArray(Node_AStar[] nodes, ref float[][] arrayToFill, LayerMask objstacleLayers)
    {
        Vector3 direction;
        float   distanceToNode;

        arrayToFill = new float[nodes.Length][];
        for (int i = 0; i < arrayToFill.Length; i++)
        {
            arrayToFill[i] = new float[nodes.Length];
        }

        Vector3 iPosition;
        Vector3 jPosition;

        for (int i = 0; i < nodes.Length; i++)
        {
            Node_AStar currentNode = nodes[i];
            currentNode.myIndex = i;
            for (int j = 0; j < nodes.Length; j++)
            {
                if (i != j)
                {
                    iPosition      = nodes[i].gameObject.transform.position;
                    jPosition      = nodes[j].gameObject.transform.position;
                    direction      = (jPosition - iPosition).normalized;
                    distanceToNode = Vector3.Distance(iPosition, jPosition);
                    if (Physics.Raycast(iPosition, direction, distanceToNode, objstacleLayers))
                    {
                        arrayToFill[i][j] = -1;
                    }
                    else
                    {
                        arrayToFill[i][j] = distanceToNode;
                        currentNode.connections.Add(nodes[j]);
                        //Debug.DrawLine(iPosition, jPosition, Color.green, Mathf.Infinity);
                    }
                }
                else
                {
                    arrayToFill[i][j] = -1;
                }
            }
        }
    }
コード例 #7
0
    public Calculation_AStar(TileMap_MapData mapData)
    {
        row    = mapData.row;
        column = mapData.column;

        currentNode = null;

        node = new Node_AStar[row, column];

        for (int z = 0; z < column; z++)
        {
            for (int x = 0; x < row; x++)
            {
                node[x, z] = new Node_AStar(mapData.TileData[x, z]);
            }
        }

        openList   = new List <Node_AStar>();
        closedList = new List <Node_AStar>();

        FinalTrack = new List <Node_AStar>();
    }
コード例 #8
0
    public static Stack <int> AStarPath(float[][] graph, Node_AStar[] allNodes, Node_AStar start, Node_AStar target)
    {
        //Setting up things required to do the algorithim
        List <Node_AStar> nodesToProcess = new List <Node_AStar>();
        bool createPath = false;

        nodesToProcess.Add(start);
        foreach (Node_AStar node in allNodes)
        {
            node.visited       = false;
            node.gcost         = float.MaxValue;
            node.fCost         = float.MaxValue;
            node.fromNodeIndex = null;
        }
        start.gcost = 0;
        start.hCost = Vector3.Distance(start.transform.position, target.transform.position);
        start.fCost = start.hCost;

        //Filling in the shortest distances from start and from nodes i.e processing each node
        while (nodesToProcess.Count > 0)
        {
            float      least   = float.MaxValue;
            Node_AStar current = null;
            foreach (Node_AStar node in nodesToProcess)
            {
                if (node.fCost < least)
                {
                    current = node;
                    least   = node.fCost;
                }
            }

            if (current == target)
            {
                createPath = true;
                break;
            }

            nodesToProcess.Remove(current);

            for (int i = 0; i < current.connections.Count; i++)
            {
                if (!current.connections[i].visited)
                {
                    float temp;
                    temp = current.gcost + graph[current.myIndex][current.connections[i].myIndex];
                    if (temp < current.connections[i].gcost)
                    {
                        current.connections[i].gcost         = temp;
                        current.connections[i].fromNodeIndex = current;

                        current.connections[i].hCost = Vector3.Distance(current.connections[i].transform.position, target.transform.position);
                        current.connections[i].fCost = current.connections[i].gcost + current.connections[i].hCost;

                        if (!nodesToProcess.Contains(current.connections[i]))
                        {
                            nodesToProcess.Add(current.connections[i]);
                        }
                    }
                }
            }
            current.visited = true;
        }

        if (createPath)
        {
            //Finding the finalPath
            Stack <int> path         = new Stack <int>();
            Node_AStar  currentNode  = target;
            int         currentIndex = currentNode.myIndex;
            if (currentIndex >= 0 || currentIndex == start.myIndex)
            {
                while (currentNode != null)
                {
                    currentIndex = currentNode.myIndex;
                    path.Push(currentIndex);
                    currentNode = currentNode.fromNodeIndex;
                }
            }

            return(path);
        }
        else
        {
            Debug.Log("<color=red>PATH COULD NOT BE FOUND </color>");
            return(null);
        }
    }
コード例 #9
0
    public bool FindPath(TileMap_MapData.TileMap_TileData[,] tileData, TileMap_MapData.TileMap_TileData startingTile, TileMap_MapData.TileMap_TileData destinationTile)
    {
        Refresh(tileData, destinationTile);

        currentNode = node[startingTile.Index.x, startingTile.Index.z];

        closedList.Add(currentNode);

        int failureCount = row * column;

        while (true)
        {
            for (int z = currentNode.Index.z - 1; z < currentNode.Index.z + 2; z++)
            {
                for (int x = currentNode.Index.x - 1; x < currentNode.Index.x + 2; x++)
                {
                    if (NodeIndexAvailable(x, z) == false)
                    {
                        continue;
                    }

                    if (node[x, z].Passable == false)
                    {
                        continue;
                    }

                    if (NodeInClosedList(node[x, z]) == true)
                    {
                        continue;
                    }

                    if (NodeInOpenList(node[x, z]) == false)
                    {
                        node[x, z].Parent = currentNode;
                        node[x, z].CalculateCostToDestination();
                        openList.Add(node[x, z]);
                    }
                    else
                    {
                        Node_AStar newData = new Node_AStar(node[x, z]);
                        newData.Parent = currentNode;
                        newData.CalculateCostToDestination();

                        if (newData.CostToDestination < node[x, z].CostToDestination)
                        {
                            node[x, z].Parent = currentNode;
                            node[x, z].CalculateCostToDestination();
                        }
                    }
                }
            }

            int lowestCost = 99999999;
            for (int i = 0; i < openList.Count; i++)
            {
                if (openList[i].CostToDestination < lowestCost)
                {
                    lowestCost  = openList[i].CostToDestination;
                    currentNode = openList[i];
                }
            }

            if (currentNode == node[destinationTile.Index.x, destinationTile.Index.z])
            {
                int whileBreaker = row * column;

                while (true)
                {
                    FinalTrack.Add(currentNode);

                    if (currentNode == node[startingTile.Index.x, startingTile.Index.z])
                    {
                        return(true);
                    }

                    currentNode = currentNode.Parent;

                    whileBreaker--;
                    if (whileBreaker < 0)
                    {
                        return(false);
                    }
                }
            }

            openList.Remove(currentNode);
            closedList.Add(currentNode);

            failureCount--;
            if (failureCount < 0)
            {
                return(false);
            }
        }
    }