void Start()
        {
            player = GameLibOfMethods.player;
            spr    = FindObjectOfType <SpriteControler>();
            anim   = GameLibOfMethods.animator;
            rb     = player.GetComponent <Rigidbody2D>();
            grid   = NodeGridManager.GetGrid(PathFinding.Resolution.High);
            col    = player.GetComponent <Collider2D>();

            contactFilter = new ContactFilter2D();
            contactFilter.SetLayerMask(GameLibOfMethods.Instance.InteractablesLayer);
        }
Exemple #2
0
        void Start()
        {
            spr = FindObjectOfType <SpriteControler>();

            player          = Player.gameObject;
            anim            = Player.anim;
            rb              = Player.rb;
            grid            = NodeGridManager.GetGrid(PathFinding.Resolution.High);
            col             = Player.col;
            playerTransform = Player.transform;

            contactFilter = new ContactFilter2D();
            contactFilter.SetLayerMask(LayerMask.GetMask("MouseOverCollider"));
        }
Exemple #3
0
        void Update()
        {
            if (UpdateFrequency != 0)
            {
                currentT += GameTime.Time.deltaTime;
                if (currentT <= UpdateFrequency)
                {
                    return;
                }
                currentT = 0;
            }

            NodeGridManager.RegisterUnwalkable(col);
        }
Exemple #4
0
 void MakeCurrentNodeOccupied() => NodeGridManager.SetPosUnwalkable(playerTransform.position, col);
Exemple #5
0
        public static void GetPath(Vector2 start, Vector2 target, List <Node> pp, Resolution resolution)
        {
            #region INIT
            var grid     = NodeGridManager.GetGrid(resolution);
            var nodeGrid = grid.nodeGrid;

            Node startNode  = grid.NodeFromWorldPoint(start);
            Node targetNode = grid.NodeFromWorldPoint(target);

            if (!startNode.walkable)
            {
                startNode = NodeHelper.ClosestWalkable(startNode, resolution);
            }
            if (!targetNode.walkable)
            {
                targetNode = NodeHelper.ClosestWalkable(targetNode, resolution);
            }

            Node node, nbr;
            openSet.Clear();
            closedSet.Clear();
            openSet.Add(startNode);
            startNode.hCost = startNode.gCost = 0;
            #endregion

            while (openSet.Count > 0)
            {
                node = openSet.RemoveFirst();
                closedSet.Add(node);

                if (node == targetNode)
                {
                    pp.Clear();
                    Node currentNode = targetNode;
                    while (currentNode != startNode)
                    {
                        pp.Add(currentNode);
                        currentNode = nodeGrid[currentNode.parent.x, currentNode.parent.y];
                    }
                    pp.Reverse();
                    return;
                }

                bool skipD = false;

                var nbsA  = node.neighbours.directionNeighbours;
                int scanA = node.neighbours.DirectionCount;
                for (int i = 0; i < scanA; i++)
                {
                    nbr = nodeGrid[nbsA[i].x, nbsA[i].y];
                    if (!nbr.walkable)
                    {
                        skipD = true; continue;
                    }
                    if (closedSet.Contains(nbr))
                    {
                        continue;
                    }

                    int cost = node.gCost + heuristic(node, nbr);
                    if (!openSet.Contains(nbr))
                    {
                        nbr.gCost  = cost;
                        nbr.hCost  = heuristic(nbr, targetNode);
                        nbr.parent = new BaseNode(node.X, node.Y);
                        openSet.Add(nbr);
                    }
                    else if (cost < nbr.gCost)
                    {
                        nbr.gCost  = cost;
                        nbr.hCost  = heuristic(nbr, targetNode);
                        nbr.parent = new BaseNode(node.X, node.Y);
                    }
                }
                if (skipD)
                {
                    continue;
                }

                var nbsB  = node.neighbours.diagonalNeighbours;
                int scanB = node.neighbours.DiagonalCount;

                for (int i = 0; i < scanB; i++)
                {
                    nbr = nodeGrid[nbsB[i].x, nbsB[i].y];
                    if (!nbr.walkable)
                    {
                        continue;
                    }
                    if (closedSet.Contains(nbr))
                    {
                        continue;
                    }

                    for (int j = 0; j < nbr.neighbours.DiagonalCount; j++)
                    {
                        var nbr_D = nbr.neighbours.diagonalNeighbours[j];
                        var dNode = nodeGrid[nbr_D.x, nbr_D.y];
                        if (!dNode.walkable)
                        {
                            break;
                        }
                    }

                    int cost = node.gCost + heuristic(node, nbr);
                    if (!openSet.Contains(nbr))
                    {
                        nbr.gCost  = cost;
                        nbr.hCost  = heuristic(nbr, targetNode);
                        nbr.parent = new BaseNode(node.X, node.Y);
                        openSet.Add(nbr);
                    }
                    else if (cost < nbr.gCost)
                    {
                        nbr.gCost  = cost;
                        nbr.hCost  = heuristic(nbr, targetNode);
                        nbr.parent = new BaseNode(node.X, node.Y);
                    }
                }
            }

            // If the code escapes the while loop, it means no valid path could be found.
            Debug.LogWarning($"Path not found (Start: {start} -- Target: {target})");
        }
 void Start() => grid = NodeGridManager.GetGrid(resolution);
 void MakeCurrentNodeOccupied()
 {
     NodeGridManager.SetPosUnwalkable(rb.position, col);
 }
Exemple #8
0
 void Awake()
 {
     instance  = this;
     playerCol = GameLibOfMethods.player.GetComponent <Collider2D>();
 }
Exemple #9
0
        public static Node ClosestWalkable(Node closestTo, Resolution resolution)
        {
            grid     = NodeGridManager.GetGrid(resolution);
            nodeGrid = grid.nodeGrid;

            // First, check if any of the neighbors are walkable
            BaseNode[] neighbors = closestTo.neighbours.directionNeighbours;

            X = closestTo.X;
            Y = closestTo.Y;

            maxX = nodeGrid.GetLength(0) - 1;
            maxY = nodeGrid.GetLength(1) - 1;

            helperList.Clear();


            // Find Closest Left
            helperList.Add(ClosestToOrigin(decrease, stay));

            // Find Closest Right
            helperList.Add(ClosestToOrigin(increase, stay));

            // Find Closest Top
            helperList.Add(ClosestToOrigin(stay, increase));

            // Find Closest Bot
            helperList.Add(ClosestToOrigin(stay, decrease));

            // Find Closest Diags
            helperList.Add(ClosestToOrigin(decrease, decrease));
            helperList.Add(ClosestToOrigin(decrease, increase));
            helperList.Add(ClosestToOrigin(increase, decrease));
            helperList.Add(ClosestToOrigin(increase, increase));

            // Find Most efficient Node
            int  minDist     = int.MaxValue;
            Node closestNode = null;

            foreach (Node n in helperList)
            {
                if (n == null)
                {
                    continue;
                }

                int dist = heuristic(n, closestTo);
                if (dist >= minDist)
                {
                    continue;
                }

                minDist     = dist;
                closestNode = n;
            }

            if (closestNode == null)
            {
                Debug.LogError("Couldn't find valid node on " + grid.PosFromNode(closestTo));
                return(closestTo);
            }
            else
            {
                return(closestNode);
            }
        }