Ejemplo n.º 1
0
            internal HierarchicalNodeToken(string name, HierarchicalNode current, HierarchicalNode parent = null)
            {
                this.Name    = name;
                this.Current = current;

                this.Parent = parent ?? (current != null ? current.InnerParent : null);
            }
Ejemplo n.º 2
0
    public static Graph GenerateGraph0(List <NavMeshNode> nodes, List <NavMeshEdge> edges)
    {
        List <HierarchicalNode> graphNodes = new List <HierarchicalNode>();

        foreach (NavMeshNode node in nodes)
        {
            HierarchicalNode graphNode = new HierarchicalNode(0, node.Centroid, null, true, node.Vertices);
            graphNodes.Add(graphNode);
        }

        foreach (HierarchicalNode node in graphNodes)
        {
            Dictionary <HierarchicalNode, float> connections = new Dictionary <HierarchicalNode, float>();
            foreach (NavMeshEdge edge in edges.FindAll(x => x.StartNode.Centroid == node._graphPos))
            {
                HierarchicalNode toNode = graphNodes.Find(x => x._graphPos == edge.EndNode.Centroid);
                if (!connections.ContainsKey(toNode))
                {
                    connections.Add(graphNodes.Find(x => x._graphPos == edge.EndNode.Centroid), edge.Distance);
                }
            }
            node.SetConnections(connections);
        }

        _currentNavMeshGraph = new Graph(graphNodes);
        return(_currentNavMeshGraph);
    }
Ejemplo n.º 3
0
 public HierarchicalNode(HierarchicalNode copy)
 {
     this.walkable   = copy.walkable;
     this._level     = copy._level;
     this.InnerGraph = copy.InnerGraph;
     this._graphPos  = copy._graphPos;
 }
Ejemplo n.º 4
0
    public void DrawNodeFromPosition(Vector3 pos)
    {
        HierarchicalNode node = levelGraph.GetNode(pos);

        if (node != null)
        {
            Debug.DrawLine(node.Vertices[0], node.Vertices[1], Color.white, 99f, false);
            Debug.DrawLine(node.Vertices[1], node.Vertices[2], Color.white, 99f, false);
            Debug.DrawLine(node.Vertices[2], node.Vertices[0], Color.white, 99f, false);
        }
    }
Ejemplo n.º 5
0
    public HierarchicalPathNode(HierarchicalPathNode parentNode,
                                HierarchicalPathNode endNode,
                                HierarchicalNode node,
                                float cost)
    {
        ParentNode = parentNode;
        EndNode    = endNode;
        Node       = node;
        DirectCost = cost;

        if (endNode != null)
        {
            TotalCost = DirectCost + Distance(endNode);
        }
    }
Ejemplo n.º 6
0
    private bool IsInsideNode(UnityEngine.Vector3 position, HierarchicalNode node)
    {
        UnityEngine.Vector3 v1 = node.Vertices[0];
        UnityEngine.Vector3 v2 = node.Vertices[1];
        UnityEngine.Vector3 v3 = node.Vertices[2];

        float planeV1V2 = (v1.x - position.x) * (v2.z - position.z) - (v2.x - position.x) * (v1.z - position.z);
        float planeV2V3 = (v2.x - position.x) * (v3.z - position.z) - (v3.x - position.x) * (v2.z - position.z);
        float planeV3V1 = (v3.x - position.x) * (v1.z - position.z) - (v1.x - position.x) * (v3.z - position.z);

        int signV1V2 = (int)(Math.Abs(planeV1V2) / planeV1V2);
        int signV2V3 = (int)(Math.Abs(planeV2V3) / planeV2V3);
        int signV3V1 = (int)(Math.Abs(planeV3V1) / planeV3V1);

        return(signV1V2 == signV2V3 && signV2V3 == signV3V1);
    }
Ejemplo n.º 7
0
    public static Graph GenerateGraph0(bool[,] obstructionGrid, Vector3 startPos, Vector2 tileSize, bool useDiagonalMoves)
    {
        int graphWidth  = obstructionGrid.GetLength(0);
        int graphLength = obstructionGrid.GetLength(1);

        HierarchicalNode[,] graph = new HierarchicalNode[graphWidth, graphLength];

        for (int x = 0; x < graphWidth; x++)
        {
            for (int y = 0; y < graphLength; y++)
            {
                //TODO Calculate position of node with size of grid and tiles in mind
                graph[x, y] = new HierarchicalNode(0, startPos + new Vector3(x * tileSize.x, 0, y * tileSize.y) + new Vector3(tileSize.x / 2, 0, tileSize.y / 2), null, !obstructionGrid[x, y]);
            }
        }

        _currentGridGraph = new Graph(graph, startPos, tileSize, useDiagonalMoves);
        return(_currentGridGraph);
    }
Ejemplo n.º 8
0
        protected HierarchicalNode(string name, HierarchicalNode parent, char pathSeparatorChar = DefaultPathSeparatorChar)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (name.Contains(pathSeparatorChar.ToString()))
            {
                throw new ArgumentException("The name contains path separator char.");
            }

            if (Common.StringExtension.ContainsCharacters(name, @"./\*?!@#$%^&"))
            {
                throw new ArgumentException("The name contains invalid character(s).");
            }

            _name   = name.Trim();
            _parent = parent;

            PathSeparatorChar = pathSeparatorChar;
        }
Ejemplo n.º 9
0
        internal protected HierarchicalNode FindNode(string[] paths, Func <HierarchicalNodeToken, HierarchicalNode> onStep = null)
        {
            if (paths == null || paths.Length == 0)
            {
                return(null);
            }

            //确保当前子节点集合已经被加载过
            this.EnsureChildren();

            //当前节点默认为本节点
            var current = this;

            //如果第一个部分是空字符则表示路径是以斜杠(/)打头或第一个部分是以斜杠(/)打头则从根节点开始查找
            if (string.IsNullOrWhiteSpace(paths[0]) || paths[0].Trim()[0] == PathSeparatorChar)
            {
                current = this.FindRoot();
            }

            int pathIndex = 0, partIndex = 0;

            string[] parts = null;

            while (current != null && pathIndex < paths.Length)
            {
                var part = string.Empty;
                HierarchicalNode parent = null;

                if (parts == null && paths[pathIndex].Contains(PathSeparatorChar.ToString()))
                {
                    parts     = paths[pathIndex].Split(PathSeparatorChar);
                    partIndex = 0;
                }

                if (parts == null)
                {
                    part = paths[pathIndex++].Trim();
                }
                else
                {
                    if (partIndex < parts.Length)
                    {
                        part = parts[partIndex++].Trim();
                    }
                    else
                    {
                        parts = null;
                        pathIndex++;
                        continue;
                    }
                }

                switch (part)
                {
                case "":
                case ".":
                    continue;

                case "..":
                    current = current._parent;
                    parent  = current != null ? current._parent : null;
                    break;

                default:
                    parent  = current;
                    current = current.GetChild(part);
                    break;
                }

                if (onStep != null)
                {
                    current = onStep(new HierarchicalNodeToken(part, current, parent));

                    if (current == null)
                    {
                        return(null);
                    }
                }
            }

            return(current);
        }