public void Cache(NodeGrid2D grid) { neighbours = new Neighbours(); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { // We don't want to cache the node itself as neighbor. if (x == 0 && y == 0) { continue; } int checkX = X + x; int checkY = Y + y; bool canAdd = (checkX >= 0 && checkX < grid.gridX) && (checkY >= 0 && checkY < grid.gridY); if (!canAdd) { continue; } bool isDiagonal = (x != 0 && y != 0); // Comment out this line to enable diagonals // if (isDiagonal) { continue; } neighbours.Add(new BaseNode(checkX, checkY), isDiagonal); } } }
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); }
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")); }
void Start() => grid = NodeGridManager.GetGrid(resolution);
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); } }