diff options
| author | Simon O'Shea <osheas1@wit.edu> | 2023-08-09 09:37:44 -0400 |
|---|---|---|
| committer | Simon O'Shea <osheas1@wit.edu> | 2023-08-09 09:37:44 -0400 |
| commit | cd15c103c6f1113461ead6065e47130276cb61a1 (patch) | |
| tree | 0ed6f9bb68cd940f2a2f596cdd08a59afb93e9c6 /Pathfinding Visualizer | |
| parent | ea904845a7a73c1b8574b41b42897ee521849421 (diff) | |
astar is animated primitively
Diffstat (limited to 'Pathfinding Visualizer')
| -rw-r--r-- | Pathfinding Visualizer/Assets/Scripts/CellMesh.cs | 18 | ||||
| -rw-r--r-- | Pathfinding Visualizer/Assets/Scripts/Main.cs | 12 | ||||
| -rw-r--r-- | Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs | 22 |
3 files changed, 32 insertions, 20 deletions
diff --git a/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs b/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs index 9e62969..aa6acc8 100644 --- a/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs +++ b/Pathfinding Visualizer/Assets/Scripts/CellMesh.cs @@ -78,6 +78,24 @@ public class CellMesh : MonoBehaviour mesh.triangles = triangles; } + public void Animate(Stack<Node> result) + { + int count = result.Count; + for (int i = 0; i < count; i++) + { + Node n = result.Pop(); + Debug.Log("Agent at: " + n.agent.x + ", " + n.agent.y); + Debug.Log(n.lastMove); + + grid.SetValue(n.agent.x, n.agent.y, 3); + //grid.SetValue(n.parent.agent.x, n.parent.agent.y, 0); + + UpdateCellVisual(); + + } + } + + void Start() { diff --git a/Pathfinding Visualizer/Assets/Scripts/Main.cs b/Pathfinding Visualizer/Assets/Scripts/Main.cs index 8cbb5ce..590723b 100644 --- a/Pathfinding Visualizer/Assets/Scripts/Main.cs +++ b/Pathfinding Visualizer/Assets/Scripts/Main.cs @@ -102,14 +102,16 @@ public class Main : MonoBehaviour public void ResetGrid() { world.reset(); - agent = new Vector2Int(-1, -1); + agent = new Vector2Int(); obstacles = new List<Vector2Int>(); samples = new List<Vector2Int>(); } public void StartAlgorithm() { + Vector2Int coord = new Vector2Int(); + bool agentFound = false; // Collect information about world for (int x = 0; x < world.GetWidth(); x++) @@ -138,15 +140,16 @@ public class Main : MonoBehaviour if (!samples.Contains(coord)) samples.Add(coord); } - // Add coordinate to obstacle array + // Add coordinate to agent array if (value == 3) { agent.x = x; agent.y = y; + agentFound = true; } } - if (agent.x == -1 && agent.y == -1) + if (!agentFound) { Debug.Log("NO AGENT!"); return; @@ -157,11 +160,12 @@ public class Main : MonoBehaviour Debug.Log("NO SAMPLES!"); return; } - if ( true ) { Pathfinder path = new Pathfinder(world, agent, obstacles, samples, 0, 0); + Stack<Node> result = path.result; + cellMesh.Animate(result); } } diff --git a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs index 3085868..556abdb 100644 --- a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs +++ b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs @@ -21,12 +21,12 @@ public class Pathfinder public static int height; public static int width; - public static Stack<Node> result; + public Stack<Node> result; public Pathfinder(LogicGrid world, Vector2Int a, List<Vector2Int> o, List<Vector2Int> s, int algo, int heu) { - this.world = world; + this.world = world; height = world.GetHeight(); width = world.GetWidth(); @@ -39,9 +39,6 @@ public class Pathfinder //printInfo(); - - - StartWork(); } @@ -69,21 +66,14 @@ public class Pathfinder if (algorithm == 0) { result = StartAStar(initialState); - Debug.Log(" "); - - int count = result.Count; - for(int i = 0; i < count; i++) - { - Node n = result.Pop(); - Debug.Log("Agent at: " + n.agent.x + ", " + n.agent.y); - Debug.Log(n.lastMove); - } } } public static Stack<Node> StartAStar(Node initialState) { - Stack<Node> solution = new Stack<Node>(); + Debug.Log("GO!"); + + Stack<Node> solution = new Stack<Node>(); List<Node> open = new List<Node>(); @@ -105,6 +95,7 @@ public class Pathfinder if (currentNode.samples.Count() == 0) { Debug.Log("Gottem"); + //Node n = FindLastSample(open); @@ -299,7 +290,6 @@ public class Node onSample = CanSample(); if (onSample.x == 1) { - Debug.Log("SAMPLING"); Node child = new Node(this, this.agent, this.samples, 'S', this.distanceTraveled + 1, this.heuristic); child.samples.RemoveAt(onSample.y); |
