Example #1
0
        public bool FindPath(Vector3 _startPosition, Vector3 _targetPosition)
        {
            // rount target position to int
            _targetPosition.x = Mathf.RoundToInt(_targetPosition.x);
            _targetPosition.y = Mathf.RoundToInt(_targetPosition.y);

            // set ship's position offset
            _startPosition  -= ship.transform.position;
            _targetPosition -= ship.transform.position;

            Node startNode  = grid.NodeFromWorldPosition(_startPosition);
            Node targetNode = grid.NodeFromWorldPosition(_targetPosition);

            Heap <Node>    OpenList   = new Heap <Node>(grid.MaxSize);
            HashSet <Node> ClosedList = new HashSet <Node>();

            OpenList.Add(startNode);

            while (OpenList.Count > 0)
            {
                Node currentNode = OpenList.RemoveFirst();
                ClosedList.Add(currentNode);

                if (currentNode == targetNode)
                {
                    GetFinalPath(startNode, targetNode);
                    break;
                }

                foreach (Node neighbourNode in grid.GetNeighbourNodes(currentNode))
                {
                    if (!neighbourNode.isWalkable || ClosedList.Contains(neighbourNode))
                    {
                        continue;
                    }

                    int moveCost = currentNode.gCost + GetManhattanDistance(currentNode, neighbourNode);
                    if (moveCost < neighbourNode.gCost || !OpenList.Contains(neighbourNode))
                    {
                        neighbourNode.gCost  = moveCost;
                        neighbourNode.hCost  = GetManhattanDistance(neighbourNode, targetNode);
                        neighbourNode.Parent = currentNode;

                        if (!OpenList.Contains(neighbourNode))
                        {
                            OpenList.Add(neighbourNode);
                        }
                    }
                }
            }

            return(targetNode.isWalkable ? true : false);
        }
Example #2
0
        private void CalculateImpassable()
        {
            bool    isPreviousWall = false;
            bool    isAfterWall    = false;
            Vector3 wallStartPos   = Vector3.zero;
            Vector3 wallEndPos     = Vector3.zero;
            Vector3 currentPos;

            currentPos = startingPos;

            for (int i = 0; i < grid.GetGridSizeY; i++)
            {
                List <Vector3> rowTiles = new List <Vector3>();

                for (int j = 0; j < grid.GetGridSizeX; j++)
                {
                    if (isPreviousWall && !isAfterWall)
                    {
                        if (grid.NodeFromWorldPosition(currentPos).isWalkable)
                        {
                            wallStartPos = currentPos;
                            rowTiles.Add(currentPos);
                            isAfterWall    = true;
                            isPreviousWall = false;
                        }
                    }
                    else if (isAfterWall)
                    {
                        if (grid.NodeFromWorldPosition(currentPos).isWalkable)
                        {
                            rowTiles.Add(currentPos);
                        }
                        else
                        {
                            // run through the entirety of its X axis' Y axis to check for walls. if it finds walls on both extremeties then the tile is within walls.
                            foreach (Vector3 tilePosition in rowTiles)
                            {
                                for (int k = (int)tilePosition.y; k < (grid.GetGridSizeY - k); k++)
                                {
                                    if (!grid.NodeFromWorldPosition(new Vector3(tilePosition.x, k, tilePosition.z)).isWalkable)
                                    {
                                        for (int l = (int)tilePosition.y; l > (-grid.GetGridSizeY - l); l--)
                                        {
                                            if (!grid.NodeFromWorldPosition(new Vector3(tilePosition.x, l, tilePosition.z)).isWalkable)
                                            {
                                                tilesPos.Add(tilePosition);
                                            }
                                        }
                                    }
                                }
                            }

                            isAfterWall    = false;
                            isPreviousWall = true;
                        }
                    }
                    else
                    {
                        if (grid.NodeFromWorldPosition(currentPos).isWalkable)
                        {
                            isPreviousWall = false;
                        }
                        else
                        {
                            isPreviousWall = true;
                        }
                    }

                    currentPos = new Vector3(
                        currentPos.x + 1f,
                        currentPos.y,
                        currentPos.z
                        );
                }

                isPreviousWall = false;
                isAfterWall    = false;
                wallStartPos   = Vector3.zero;
                wallEndPos     = Vector3.zero;

                currentPos = new Vector3(
                    startingPos.x,
                    currentPos.y - 1f,
                    startingPos.z
                    );
            }
        }