Guest User

Untitled

a guest
Jul 2nd, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.98 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class DrawMap : MonoBehaviour {
  6.     //draw map
  7.     public Transform Prefab;
  8.     Vector3 startVector = Vector3.zero, actualVector;
  9.     public int W = 30, H = 20;
  10.     //A*
  11.     public int Ax, Ay, Bx, By;
  12.     public List<Field> open = new List<Field>(), closed = new List<Field>();
  13.     public Field StartField, GoalField;
  14.     bool IFindPath;
  15.     Field BufforField;
  16.     //vizualization
  17.     public Material PathMaterial, NormalMaterial, StartFieldMaterial, GoalFieldMaterial;
  18.  
  19.     void Start()
  20.     {
  21.         //draw whole map
  22.         actualVector = startVector;
  23.         for (int i = H; i > 0; i--)
  24.         {
  25.             for (int a = 1; a < W + 1; a++)
  26.             {
  27.                 actualVector.x += 1.5f;
  28.                 Instantiate(Prefab, actualVector, Quaternion.identity);
  29.                 GameObject SpawnedField = GameObject.Find("Plane(Clone)");
  30.                 SpawnedField.name = a + "," + i;
  31.                 Field SpawnedFieldScript = SpawnedField.GetComponent<Field>();
  32.                 SpawnedFieldScript.x = a;
  33.                 SpawnedFieldScript.y = i;
  34.                 if (a == Ax && i == Ay)
  35.                 {
  36.                     closed.Add(SpawnedFieldScript);
  37.                     StartField = SpawnedFieldScript;
  38.                     Renderer rend = StartField.gameObject.GetComponent<Renderer>();
  39.                     rend.material = StartFieldMaterial;
  40.                 }
  41.                 else if (a == Bx && i == By)
  42.                 {
  43.                     GoalField = SpawnedFieldScript;
  44.                     Renderer rend = GoalField.gameObject.GetComponent<Renderer>();
  45.                     rend.material = GoalFieldMaterial;
  46.                 }
  47.             }
  48.             actualVector.z -= 1.5f;
  49.             actualVector.x = startVector.x;
  50.         }
  51.         //end of drawing
  52.         FindNeightbours(StartField); //find neightbours of start field (A)
  53.         while (open != null && !IFindPath) //find path until the open list is not null or the path was not found
  54.         {
  55.             open = open.OrderBy(f => f.FCost).ToList();
  56.             for(int i=0; i<open.Count; i++) //check that some elements don't have the same FCost value.
  57.             {
  58.                 for (int a = 0; a < open.Count; a++)//comparison all elements
  59.                 {
  60.                     if(open[i].FCost == open[a].FCost && a != i)//check that script is not checking the same elements
  61.                     {
  62.                         if(i > a)
  63.                         {
  64.                             if(open[a].HCost < open[i].HCost) //check what field is closer to goal
  65.                             {
  66.                                Field buffor = open[i]; //replace this fields in open list
  67.                                open[i] = open[a];
  68.                                open[a] = buffor;
  69.                             }
  70.                         }
  71.                         else
  72.                         {
  73.                             if (open[i].HCost < open[a].HCost)
  74.                             {
  75.                                 Field buffor = open[a]; //replace this fields in open list
  76.                                 open[a] = open[i];
  77.                                 open[i] = buffor;
  78.                             }
  79.                         }
  80.                     }
  81.                 }
  82.             }
  83.             open = open.OrderBy(f => f.FCost).ToList();
  84.             BufforField = open[0];
  85.             if (open.Count > 0) open[0].FieldBehindMe = open[1];
  86.             else open[0].FieldBehindMe = StartField;
  87.  
  88.             Debug.Log("The lowest cost: " + open[0].FCost + " Have: " + open[0].x + "," + open[0].y + " field.");
  89.             Renderer rend = open[0].gameObject.GetComponent<Renderer>();
  90.             rend.material = PathMaterial;
  91.             FindNeightbours(open[0]);
  92.         }
  93.         if (!IFindPath) Debug.Log("The path was not found.");
  94.         else
  95.         {
  96.             Debug.Log(BufforField.gameObject.name);
  97.             Debug.Log("I found the path.");
  98.  
  99.         }
  100.     }
  101.  
  102.     void FindNeightbours(Field CenterField) //function finding neighboring fields
  103.     {
  104.         for (int y = 1; y >= -1; y--)
  105.         {
  106.             for (int x = -1; x <= 1; x++)
  107.             {
  108.                 if (GameObject.Find((CenterField.x + x) + "," + (CenterField.y + y)))
  109.                 {
  110.                     Field SearchedField = GameObject.Find((CenterField.x + x) + "," + (CenterField.y + y)).GetComponent<Field>();
  111.                     if (SearchedField == GoalField) IFindPath = true;
  112.                     if (!closed.Contains(SearchedField) && !open.Contains(SearchedField))
  113.                     {
  114.                         SearchedField.HCost = CalcDist(SearchedField, GoalField);
  115.                         SearchedField.GCost = CalcDist(SearchedField, StartField);
  116.                         SearchedField.FCost = SearchedField.HCost + SearchedField.GCost;
  117.                         open.Add(SearchedField);
  118.                     }
  119.                 }
  120.                 else Debug.Log("The field with paramters: " + (CenterField.x + x) + " and " + (CenterField.y + y) + " cannot be find.");
  121.             }
  122.         }
  123.         if (!closed.Contains(CenterField)) closed.Add(CenterField);
  124.         open.Remove(CenterField);
  125.     }
  126.  
  127.     int CalcDist(Field StartField, Field GoalField) //function calculating the distance
  128.     {
  129.         int x=StartField.x, y=StartField.y;
  130.         int DocelX=GoalField.x, DocelY=GoalField.y;
  131.         int dist=0;
  132.  
  133.         while(x != DocelX || y != DocelY)
  134.         {
  135.             if (x != DocelX && y != DocelY) dist += 14; //if the path moves diagonally add 14 cost
  136.             else if (x != DocelX && y == DocelY || x == DocelX && y != DocelY) dist += 10; //if the path moves straight add 10 cost
  137.             //calcutating the direction of path
  138.  
  139.             if (x < DocelX) x++;
  140.             if (x > DocelX) x--;
  141.             if (y < DocelY) y++;
  142.             if (y > DocelY) y--;
  143.         }
  144.         return dist;
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment