Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Here's a bit of Unity3D code on how to make/use Dijkstra's Algorithm.
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- [RequireComponent(typeof(LineRenderer))]
- public class Dijkstra : Pathfinding{
- protected override void Start()
- {
- SetUpDirections();
- }
- public override List<Node> Begin (Node[,] _fullGrid, Node _startNode, Node _endNode) {
- Display (); //Resets all the colors on the tiles.
- sx = _fullGrid.GetLength(0);
- sz = _fullGrid.GetLength(1);
- openList.Clear();
- unopenList.Clear();
- fullGrid = _fullGrid;
- startNode = _startNode;
- endNode = _endNode;
- BeginCalculation();
- return openList;
- }
- private List<Node> unopenList = new List<Node>();
- protected override void SetUpDirections()
- {
- directions.Add(new V2(-1, 0));
- directions.Add(new V2( 0, -1));
- directions.Add(new V2( 0, 1));
- directions.Add(new V2( 1, 0));
- }
- /// <summary>
- /// Sets up information so Dijkstra's can begin.
- /// </summary>
- /// <param name='v2'>
- /// A Vector2. The place to begin Dijkstra's from.
- /// </param>
- protected override void BeginCalculation()
- {
- //This check ensures that we don't allow the algorithm
- // to be running more than once at the same time.
- finished = false;
- //Reset all the values in the fullGrid.
- // Their distances all are at the max value, so any value
- // Dijkstra's finds will replace the current info.
- foreach(Node n in fullGrid)
- {
- n.distance = int.MaxValue;
- n.parent = null;
- unopenList.Add(n);
- }
- maxDistance = 1;
- StartCoroutine(Calculate(startNode.v2, 0));
- distance = endNode.distance;
- }
- protected override IEnumerator Calculate(V2 v2, int d)
- {
- Node v2Node = fullGrid[v2.x,v2.z];
- v2Node.distance = 0;
- do
- {
- Node smallestDistanceNode = null;
- unopenList.Sort(compareByDistance);
- smallestDistanceNode = unopenList[unopenList.Count-1];
- if(smallestDistanceNode.distance == int.MaxValue)
- {
- break;
- }
- Node n = smallestDistanceNode;
- unopenList.Remove(n);
- yield return StartCoroutine(UpdateCell(n));
- if(!openList.Contains(n))
- openList.Add(n);
- //For every node adjacent to the node we're looking at.
- foreach(V2 adj in getAdjacents(n.v2))
- {
- Node adjNode = fullGrid[adj.x, adj.z];
- if(adjNode.distance > n.distance+adjNode.weight)
- {
- //Here's the path update.
- adjNode.distance = n.distance+adjNode.weight;
- adjNode.parent = n;
- }
- }
- //if(unopenList.Count > 0)
- // if(unopenList[unopenList.Count-1].distance > endNode.distance)
- // break;
- }while(unopenList.Count != 0);
- Display();
- openList.Sort(compareByDistance);
- yield return StartCoroutine(FindPathBack(startNode, endNode));
- distance = endNode.distance;
- startNode = endNode = null;
- finished = true;
- yield return null;
- }
- public List<Node> pathBack;
- protected override IEnumerator FindPathBack(Node start, Node end)
- {
- pathBack = new List<Node>();
- Node n = end;
- do
- {
- pathBack.Add(n);
- yield return StartCoroutine(UpdateCell(n, Color.red, Color.magenta));
- n = n.parent;
- }while(n != null);
- }
- protected int compareByDistance(Node b, Node a)
- {
- int result = (a.distance).CompareTo(b.distance);
- return result;
- }
- protected override IEnumerator UpdateCell(Node n)
- {
- yield return StartCoroutine(UpdateCell(n, Color.blue, Color.green));
- }
- protected IEnumerator UpdateCell(Node n, Color c1, Color c2)
- {
- //Updates the cell/node's text to show the [distance] variable.
- string s = n.distance+"";
- if(n.distance/100f >= 1)
- s = "";
- if(maxDistance < n.distance && n.distance <= .9f*int.MaxValue)
- {
- maxDistance = n.distance;
- UpdateAllCells();
- }
- //n.cell.GetComponentInChildren<TextMesh>().text = s;
- n.cell.renderer.material.color = Color.Lerp(c1, Color.Lerp(c2, Color.black, .5f),
- (float)(n.distance/(float)maxDistance));
- yield return new WaitForSeconds(0.001f);
- }
- protected override void UpdateAllCells()
- {
- foreach(Node n in openList)
- {
- n.cell.renderer.material.color = Color.Lerp(Color.blue, Color.Lerp(Color.green, Color.black, .5f),
- (float)(n.distance/(float)maxDistance));
- }
- }
- List<V2> directions = new List<V2>();
- protected override List<V2> getAdjacents(V2 v2)
- {
- //Gets all nodes around the node with position [v2].
- List<V2> result = new List<V2>();
- foreach(V2 dir in directions)
- {
- int x = v2.x+dir.x;
- int z = v2.z+dir.z;
- //If it's one of these, it's invalid. Like position (-1,0) is not on the grid.
- if( x < 0 ||
- x>=sx ||
- z < 0 ||
- z >=sz)
- {
- }
- else
- {
- result.Add(new V2(x,z));
- }
- }
- return result;
- }
- protected override void Display()
- {
- for(int ix = 0; ix < sx; ix++)
- {
- for(int iz = 0; iz < sz; iz++)
- {
- Node m = fullGrid[ix, iz];
- if(m != null)
- {
- m.cell.renderer.material.color = Color.Lerp(Color.black, Color.grey, m.distance/10f);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement