Beispiel #1
0
        /// <summary>
        /// Recostract path base on direction changing
        /// </summary>
        public List <Vector2> RecreatePath(Dictionary <Vector3Int, Node> cameFrom, Node start, Node goal, NavGrid navGrid)
        {
            var     path             = new List <Vector2>();
            var     current          = goal;
            Vector2 currentDirection = Vector2.zero;

            path.Add(navGrid.IndexToPosition(current.position));
            while (current.position != start.position)
            {
                var currentPosition = navGrid.IndexToPosition(current.position);
                if (cameFrom.TryGetValue(new Vector3Int(current.x, current.y, current.jumpCost), out current))
                {
                    var nodeCenterPosition = navGrid.IndexToPosition(current.position);
                    var newDirection       = nodeCenterPosition - currentPosition;
                    if (currentDirection != newDirection)
                    {
                        path.Add(nodeCenterPosition);
                        currentDirection = newDirection;
                    }
                    else
                    {
                        path[path.Count - 1] = nodeCenterPosition;
                    }
                }
                else
                {
                    Debug.LogError("Data for path reconstration was incorrect");
                    return(null);
                }
            }

            path.Reverse();

            return(path);
        }
        public void GenerateMap()
        {
            navGrid.Init(width, height, offset, cellSize);

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    var hit      = Physics2D.BoxCast(navGrid.IndexToPosition(new Vector2Int(x, y)), Vector2.one * navGrid.CellSize * precision, 0, Vector2.zero, 0, collisonLayer);
                    var nodeType = hit.collider ? NavGrid.NodeType.wall : NavGrid.NodeType.free;

                    navGrid.SetNode(x, y, nodeType);
                }
            }
        }
        public void SetSerachTiles(Dictionary <Vector3Int, Node> cameFrom, NavGrid navGrid)
        {
#if UNITY_EDITOR
            serachPoints.Clear();
            if (cameFrom != null)
            {
                serachPoints.AddRange(cameFrom.Values.ToList().ConvertAll(node => node == null ? default : navGrid.IndexToPosition(node.position)));
                cellSize = navGrid.CellSize;
            }
#endif
        }