Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using System.Linq;
- public class DrawMap : MonoBehaviour {
- //draw map
- public Transform Prefab;
- Vector3 startVector = Vector3.zero, actualVector;
- public int W = 30, H = 20;
- //A*
- public int Ax, Ay, Bx, By;
- public List<Field> open = new List<Field>(), closed = new List<Field>();
- public Field StartField, GoalField;
- bool IFindPath;
- Field BufforField;
- //vizualization
- public Material PathMaterial, NormalMaterial, StartFieldMaterial, GoalFieldMaterial;
- void Start()
- {
- //draw whole map
- actualVector = startVector;
- for (int i = H; i > 0; i--)
- {
- for (int a = 1; a < W + 1; a++)
- {
- actualVector.x += 1.5f;
- Instantiate(Prefab, actualVector, Quaternion.identity);
- GameObject SpawnedField = GameObject.Find("Plane(Clone)");
- SpawnedField.name = a + "," + i;
- Field SpawnedFieldScript = SpawnedField.GetComponent<Field>();
- SpawnedFieldScript.x = a;
- SpawnedFieldScript.y = i;
- if (a == Ax && i == Ay)
- {
- closed.Add(SpawnedFieldScript);
- StartField = SpawnedFieldScript;
- Renderer rend = StartField.gameObject.GetComponent<Renderer>();
- rend.material = StartFieldMaterial;
- }
- else if (a == Bx && i == By)
- {
- GoalField = SpawnedFieldScript;
- Renderer rend = GoalField.gameObject.GetComponent<Renderer>();
- rend.material = GoalFieldMaterial;
- }
- }
- actualVector.z -= 1.5f;
- actualVector.x = startVector.x;
- }
- //end of drawing
- FindNeightbours(StartField); //find neightbours of start field (A)
- while (open != null && !IFindPath) //find path until the open list is not null or the path was not found
- {
- open = open.OrderBy(f => f.FCost).ToList();
- for(int i=0; i<open.Count; i++) //check that some elements don't have the same FCost value.
- {
- for (int a = 0; a < open.Count; a++)//comparison all elements
- {
- if(open[i].FCost == open[a].FCost && a != i)//check that script is not checking the same elements
- {
- if(i > a)
- {
- if(open[a].HCost < open[i].HCost) //check what field is closer to goal
- {
- Field buffor = open[i]; //replace this fields in open list
- open[i] = open[a];
- open[a] = buffor;
- }
- }
- else
- {
- if (open[i].HCost < open[a].HCost)
- {
- Field buffor = open[a]; //replace this fields in open list
- open[a] = open[i];
- open[i] = buffor;
- }
- }
- }
- }
- }
- open = open.OrderBy(f => f.FCost).ToList();
- BufforField = open[0];
- if (open.Count > 0) open[0].FieldBehindMe = open[1];
- else open[0].FieldBehindMe = StartField;
- Debug.Log("The lowest cost: " + open[0].FCost + " Have: " + open[0].x + "," + open[0].y + " field.");
- Renderer rend = open[0].gameObject.GetComponent<Renderer>();
- rend.material = PathMaterial;
- FindNeightbours(open[0]);
- }
- if (!IFindPath) Debug.Log("The path was not found.");
- else
- {
- Debug.Log(BufforField.gameObject.name);
- Debug.Log("I found the path.");
- }
- }
- void FindNeightbours(Field CenterField) //function finding neighboring fields
- {
- for (int y = 1; y >= -1; y--)
- {
- for (int x = -1; x <= 1; x++)
- {
- if (GameObject.Find((CenterField.x + x) + "," + (CenterField.y + y)))
- {
- Field SearchedField = GameObject.Find((CenterField.x + x) + "," + (CenterField.y + y)).GetComponent<Field>();
- if (SearchedField == GoalField) IFindPath = true;
- if (!closed.Contains(SearchedField) && !open.Contains(SearchedField))
- {
- SearchedField.HCost = CalcDist(SearchedField, GoalField);
- SearchedField.GCost = CalcDist(SearchedField, StartField);
- SearchedField.FCost = SearchedField.HCost + SearchedField.GCost;
- open.Add(SearchedField);
- }
- }
- else Debug.Log("The field with paramters: " + (CenterField.x + x) + " and " + (CenterField.y + y) + " cannot be find.");
- }
- }
- if (!closed.Contains(CenterField)) closed.Add(CenterField);
- open.Remove(CenterField);
- }
- int CalcDist(Field StartField, Field GoalField) //function calculating the distance
- {
- int x=StartField.x, y=StartField.y;
- int DocelX=GoalField.x, DocelY=GoalField.y;
- int dist=0;
- while(x != DocelX || y != DocelY)
- {
- if (x != DocelX && y != DocelY) dist += 14; //if the path moves diagonally add 14 cost
- else if (x != DocelX && y == DocelY || x == DocelX && y != DocelY) dist += 10; //if the path moves straight add 10 cost
- //calcutating the direction of path
- if (x < DocelX) x++;
- if (x > DocelX) x--;
- if (y < DocelY) y++;
- if (y > DocelY) y--;
- }
- return dist;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment