Skip to content

Commit cbba8cd

Browse files
committed
[Pathfinding] Added check to start pathfinding from the last node
1 parent 3073483 commit cbba8cd

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

Assets/Scripts/Pathfinding/Pathfinding.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using UnityEngine;
45

56
public class Pathfinding
@@ -16,10 +17,25 @@ public class Pathfinding
1617

1718
protected List<PathNode> openList;
1819
protected List<PathNode> closedList = new List<PathNode>();
20+
21+
protected Vector2Int initialPosition = Vector2Int.zero;
22+
protected List<PathNode> path = new List<PathNode>();
1923
public GridBase<PathNode> Grid { get; set; }
20-
public Pathfinding(int width, int height, bool debugGrid = true)
24+
25+
public PathNode LastNode {
26+
get => path.Count > 0 ? path.Last() : null;
27+
protected set { }
28+
}
29+
public Pathfinding(int width, int height, Vector2Int startPosition = default, bool debugGrid = true)
2130
{
2231
Grid = new GridBase<PathNode>(width, height, 10f, Vector3.zero, debugGrid);
32+
initialPosition = startPosition;
33+
}
34+
35+
public virtual List<PathNode> FindPath(Vector2Int endPos, bool startFromLastNode = true)
36+
{
37+
var startPos = startFromLastNode && LastNode != null ? LastNode.Position : initialPosition;
38+
return FindPath(startPos, endPos);
2339
}
2440

2541
public virtual List<PathNode> FindPath(Vector2Int startPos, Vector2Int endPos)
@@ -59,7 +75,7 @@ public virtual List<PathNode> FindPath(Vector2Int startPos, Vector2Int endPos)
5975
if (currentNode == endNode)
6076
{
6177
// Found the final node!
62-
List<PathNode> path = CalculatePath(endNode);
78+
path = CalculatePath(endNode);
6379

6480
OnTakeSnapshot?.Invoke(Grid, currentNode, openList, closedList);
6581
OnTakeFinalSnapshot?.Invoke(Grid, path);

Assets/Scripts/Tests/TestingPathfinding.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ public class TestingPathfinding : MonoBehaviour
88
[SerializeField]
99
protected bool debugGrid = true;
1010

11+
[SerializeField]
12+
protected bool startFromLastNode = false;
13+
1114
[SerializeField]
1215
protected PathfindingDebugVisual pathfindingDebugVisual;
1316

@@ -19,7 +22,7 @@ public class TestingPathfinding : MonoBehaviour
1922
// Start is called before the first frame update
2023
void Start()
2124
{
22-
pathfinding = new Pathfinding(width: 10, height: 10, debugGrid: debugGrid);
25+
pathfinding = new Pathfinding(width: 10, height: 10, startPosition: Vector2Int.zero, debugGrid: debugGrid);
2326

2427
if (pathfindingDebugVisual != null)
2528
{
@@ -40,7 +43,7 @@ void Update()
4043
var worldPosition = UtilsClass.GetMouseWorldPosition();
4144
var cellPosition = pathfinding.Grid.WorldToCell(worldPosition);
4245

43-
List<PathNode> path = pathfinding.FindPath(Vector2Int.zero, cellPosition);
46+
List<PathNode> path = pathfinding.FindPath(cellPosition, startFromLastNode);
4447

4548
if (path != null)
4649
{

0 commit comments

Comments
 (0)