11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using UnityEngine ;
45
56public 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 ) ;
0 commit comments