Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class main : MonoBehaviour {
  6.  
  7. public Transform prefab;
  8. public Transform prefab2;
  9. List<Vertex> graph = new List<Vertex>();
  10. Vertex target;
  11.  
  12. // Use this for initialization
  13. void Start () {
  14. for (int j= -2;j<=2;j+=2) {
  15. for (int i = -6;i<=6;i+=2) {
  16. graph.Add (new Vertex(i,0,j));
  17. }
  18. }
  19.  
  20. for (int j= 0;j<21;j++) {
  21. if(j<14) addEdge (graph[j],graph[j+7]);
  22. }
  23.  
  24. addEdge (graph[7],graph[8]);
  25. addEdge (graph[15],graph[16]);
  26. addEdge (graph[9],graph[10]);
  27. addEdge (graph[10],graph[11]);
  28. addEdge (graph[4],graph[5]);
  29. addEdge (graph[19],graph[20]);
  30. addEdge (graph[1],graph[9]);
  31. addEdge (graph[18],graph[12]);
  32.  
  33. foreach (Vertex vert in graph) {
  34. Instantiate (prefab,vert.loc,Quaternion.identity);
  35. }
  36.  
  37. target = graph[20];
  38. List<Node> result = aStar (new Node(graph[0],0,null));
  39. if(result != null) {
  40. foreach (Node n in result) {
  41. Instantiate (prefab2,n.vert.loc+Vector3.up,Quaternion.identity);
  42. }
  43. }
  44. else Debug.LogError ("FAILED!");
  45.  
  46. }
  47.  
  48. // Update is called once per frame
  49. void Update () {
  50. foreach (Vertex vert in graph) {
  51. foreach (Vertex vert2 in vert.adjacent) {
  52. Debug.DrawLine (vert.loc,vert2.loc);
  53. }
  54. }
  55. }
  56.  
  57. List<Node> aStar(Node init) {
  58. List<Node> openList = new List<Node>();
  59. List<Vertex> closedList = new List<Vertex>();
  60. openList.Add (init);
  61. while (openList.Count != 0) {
  62. openList.Sort (compareNodes);
  63. Node current = openList[0];
  64. if (current.vert == target) return generatePath(current);
  65. openList.RemoveAt (0);
  66. closedList.Add (current.vert);
  67. List<Node> newNodes = current.expand ();
  68. foreach (Node newNode in newNodes) {
  69. if(!(closedList.Contains (newNode.vert))) {
  70. //Insert Replacement code here
  71. openList.Add (newNode);
  72. }
  73. }
  74. }
  75. return null;
  76. }
  77.  
  78. List<Node> generatePath(Node final) {
  79. List<Node> res = new List<Node>();
  80. Node cur = final;
  81. res.Add (cur);
  82. while(cur.parent != null) {
  83. res.Add (cur.parent);
  84. cur=cur.parent;
  85. }
  86. res.Reverse ();
  87. return res;
  88. }
  89.  
  90. void addEdge (Vertex v1, Vertex v2) {
  91. v1.adjacent.Add(v2);
  92. v2.adjacent.Add(v1);
  93. }
  94.  
  95. class Vertex {
  96.  
  97. public Vector3 loc;
  98. public List<Vertex> adjacent = new List<Vertex>();
  99.  
  100. public Vertex(int x, int y, int z) {
  101. loc = new Vector3(x,y,z);
  102. }
  103.  
  104. }
  105.  
  106. class Node {
  107. public Vertex vert;
  108. public Node parent;
  109. public float g = 0;
  110. public Node(Vertex v, float cost, Node p) {
  111. vert = v; g = cost; parent = p;
  112. }
  113. public List<Node> expand() {
  114. List<Node> ret = new List<Node>();
  115. foreach( Vertex v in vert.adjacent) {
  116. ret.Add (new Node(v,g+Vector3.Distance(vert.loc,v.loc),this));
  117. }
  118. return ret;
  119. }
  120. }
  121.  
  122. float h (Node a,Vertex b) {
  123. return Vector3.Distance (a.vert.loc,b.loc);
  124. }
  125.  
  126. private int compareNodes(Node x, Node y) {
  127. if (x == null) {
  128. if (y == null) return 0;
  129. else return -1;
  130. }
  131. else {
  132. if (y == null) return 1;
  133. else {
  134. float fx = x.g + h (x,target);
  135. float fy = y.g + h (y,target);
  136. return fx.CompareTo (fy);
  137. }
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement