Exemplo n.º 1
0
        private void Reset()
        {
            _nodes.Clear();

            RootNode = ScriptableObject.CreateInstance <NavMeshGridNode>().Initialize(new Index(0, 0));
            _nodes.Add(RootNode);
        }
Exemplo n.º 2
0
        private bool DrawNewNodesButtons(NavMeshGridNode node)
        {
            foreach (var side in node.AllEmptyNeighboringNodesSides)
            {
                if (side.IsDiagonal())
                {
                    continue;
                }

                Handles.BeginGUI();

                var buttonSize = new Vector2(50f, 20f);

                var addNewNodeButtonClicked = GUI.Button(
                    position: new Rect(HandleUtility.WorldToGUIPoint(node.PositionWithoutOffset + Grid.GetOffsetBySide(side)) - buttonSize * 0.5f, buttonSize),
                    text: $"+{side}");

                if (addNewNodeButtonClicked)
                {
                    Grid.AddNewNode(node.Index.IndexBySide(side));

                    Debug.Log($"Node added to {side}");

                    Grid.RefreshNodesPositions();
                    return(true);
                }

                Handles.EndGUI();

                SceneView.RepaintAll();
            }

            return(false);
        }
Exemplo n.º 3
0
        public void RemoveNode(NavMeshGridNode selectedNode)
        {
            foreach (var neighboringNode in selectedNode.AllNeighboringNodes)
            {
                neighboringNode.RemoveNeighboringNode(selectedNode);
            }

            _nodes.Remove(selectedNode);
        }
Exemplo n.º 4
0
        public void LinkToGridNode(NavMeshGridNode node, bool snap = false)
        {
            _currentNode = node;

            if (snap)
            {
                transform.position = node.Position;
            }
        }
Exemplo n.º 5
0
        private void DrawNodesSelectButtons()
        {
            Handles.color = Color.green;

            foreach (var node in Grid.Nodes)
            {
                if (node == Grid.RootNode)
                {
                    continue;
                }

                var nodeButtonClicked = Handles.Button(node.Position, Quaternion.identity, 0.2f, 0.2f, Handles.RectangleHandleCap);

                if (nodeButtonClicked)
                {
                    _selectedNode = node;
                }
            }


            if (_selectedNode)
            {
                DrawNewNodesButtons(_selectedNode);
                DrawNodeCustomOffsetHandle(_selectedNode);

                Handles.BeginGUI();

                var buttonSize = new Vector2(60f, 20f);

                var removeNodeButtonClicked = GUI.Button(
                    position: new Rect(HandleUtility.WorldToGUIPoint(_selectedNode.Position + Vector2.up * 0.3f) - buttonSize * 0.5f, buttonSize),
                    text: $"-remove");

                if (removeNodeButtonClicked)
                {
                    Grid.RemoveNode(_selectedNode);

                    Debug.Log($"Node {_selectedNode.Index.Column} {_selectedNode.Index.Row} removed");

                    Grid.RefreshNodesPositions();

                    _selectedNode = null;
                }

                Handles.EndGUI();

                SceneView.RepaintAll();
            }

            else
            {
                DrawNewNodesButtonsForAll();
            }
        }
Exemplo n.º 6
0
 public void RemoveNeighboringNode(NavMeshGridNode nodeToRemove)
 {
     for (int i = 0; i < _neighboringNodes.Length; i++)
     {
         if (_neighboringNodes[i] == nodeToRemove)
         {
             _neighboringNodes[i] = null;
             break;
         }
     }
 }
Exemplo n.º 7
0
        public NavMeshGridNode AddNeighboringNode(Side neighboringNodeSide, NavMeshGridNode newNode)
        {
            if (_neighboringNodes[(int)neighboringNodeSide] != null)
            {
                return(null);
            }

            _neighboringNodes[(int)neighboringNodeSide] = newNode;

            newNode.AddNeighboringNode(neighboringNodeSide.OppositeSide(), this);

            return(newNode);
        }
Exemplo n.º 8
0
        public void Find(NavMeshGridNode from, NavMeshGridNode to)
        {
            var startNode  = from;
            var targetNode = to;

            var totalCost = 0;

            var reachable = new List <PathNode>()
            {
                new PathNode(from, totalCost)
            };
            var explored = new List <NavMeshGridNode>();

            while (reachable.Count > 0)
            {
                var currentNode = ChooseNode(reachable, targetNode);

                if (currentNode.Node == targetNode)
                {
                    ResultPathNodes = BuildPath(new PathNode(targetNode, 0)
                    {
                        PreviousNode = currentNode
                    });
                }

                reachable.Remove(currentNode);
                explored.Add(currentNode.Node);

                ++totalCost;

                var newReachable = currentNode.Node.AllNeighboringNodes.Select(node => new PathNode(node, totalCost));

                foreach (var newReachableNode in newReachable)
                {
                    if (newReachableNode.Node && !explored.Contains(newReachableNode.Node) && !reachable.Contains(reachable.Find((x) => x.Node == newReachableNode.Node)))
                    {
                        //if (newReachableNode.Node.Data.IsEmpty() == false)
                        //    continue;

                        newReachableNode.PreviousNode = currentNode;
                        reachable.Add(newReachableNode);
                    }
                }
            }
        }
Exemplo n.º 9
0
        private void DrawNodeCustomOffsetHandle(NavMeshGridNode node)
        {
            if (!_showNodesCustomOffsetsHandles)
            {
                return;
            }

            EditorGUI.BeginChangeCheck();

            Handles.color = Color.white;

            var buttonOffset = Vector2.down * 0.4f;
            var customOffset = Handles.FreeMoveHandle(node.Position + buttonOffset, Quaternion.identity, 0.1f, Vector3.zero, Handles.DotHandleCap);

            if (EditorGUI.EndChangeCheck())
            {
                node.SetCustomOffset((Vector2)customOffset - node.PositionWithoutOffset - buttonOffset, (Grid.NodesHorizontalOffset + Grid.NodesVerticalOffset).magnitude / 4f);
                Debug.Log(node.PositionWithoutOffset);
            }
        }
Exemplo n.º 10
0
        private PathNode ChooseNode(IEnumerable <PathNode> pathNodes, NavMeshGridNode endNode)
        {
            var      minCost  = Mathf.Infinity;
            PathNode bestNode = null;

            foreach (var node in pathNodes)
            {
                var nodeCost = node.Cost;

                var costToEndNode = Vector2Int.Distance(
                    a: new Vector2Int(node.Node.Index.Row, node.Node.Index.Column),
                    b: new Vector2Int(endNode.Index.Row, endNode.Index.Column));

                var totalCost = nodeCost + costToEndNode;

                if (totalCost < minCost)
                {
                    minCost  = totalCost;
                    bestNode = node;
                }
            }

            return(bestNode);
        }
Exemplo n.º 11
0
 public bool GetNodeByIndex(Index index, out NavMeshGridNode node)
 {
     node = _nodes.Find(node => node.Index == index);
     return(node != null);
 }
Exemplo n.º 12
0
 protected void BuildPath(NavMeshGridNode targetNode)
 {
     _currentPath.Find(_currentNode, targetNode);
 }
Exemplo n.º 13
0
 public bool TryGetNeighboringNodeBySide(Side side, out NavMeshGridNode resultNode)
 {
     resultNode = _neighboringNodes[(int)side];
     return(resultNode != null);
 }