A*寻路-2(忘了哪个是最终版的)

using UnityEngine;
using System.Collections.Generic;

public class TestAStar : MonoBehaviour
{

//以点为方格
//A点 [1,2] B点[6,7]
private List<float[,]> parentPosList = new List<float[,]>();
List<Son> sonList = new List<Son>();
List<Son> parentList = new List<Son>();
List<Son> oldList = new List<Son>();

List<float[,]> barrierPosList = new List<float[,]>();

//防止测试死机
private int n = 0;
private float[,] start = new float[1, 2] { { 1, 2 } };
private float[,] end = new float[1, 2] { { 6, 7 } };


void Start()
{
barrierPosList.Add(new float[,] { { 2, 4 } });
barrierPosList.Add(new float[,] { { 3, 4 } });
barrierPosList.Add(new float[,] { { 4, 4 } });

XunLu(start, end);
}

public void XunLu(float[,] start, float[,] end)
{
//找到起始点A 周围8个点 F=G+H G值由父点决定(决定周围8点) H值由终点B点决定
//需要一个存 父点 的list parent
//首先可以知道start点 周围8点坐标 存到list son
//存父节点
//List<Son> parent = new List<Son>();
parentPosList.Add(start);
//===
sonList.Clear();
//拿到周围8个点坐标
Son one = new Son(new float[1, 2] { { start[0, 0], start[0, 1] + 1 } });
Son two = new Son(new float[1, 2] { { start[0, 0] + 1, start[0, 1] + 1 } });
Son three = new Son(new float[1, 2] { { start[0, 0] + 1, start[0, 1] } });
Son four = new Son(new float[1, 2] { { start[0, 0] + 1, start[0, 1] - 1 } });
Son five = new Son(new float[1, 2] { { start[0, 0], start[0, 1] - 1 } });
Son six = new Son(new float[1, 2] { { start[0, 0] - 1, start[0, 1] - 1 } });
Son seven = new Son(new float[1, 2] { { start[0, 0] - 1, start[0, 1] } });
Son eight = new Son(new float[1, 2] { { start[0, 0] - 1, start[0, 1] + 1 } });
sonList.Add(one); sonList.Add(two); sonList.Add(three); sonList.Add(four); sonList.Add(five); sonList.Add(six); sonList.Add(seven); sonList.Add(eight);
//sonDict.Add(0, one); sonDict.Add(1, two); sonDict.Add(2, three); sonDict.Add(3, four); sonDict.Add(4, five); sonDict.Add(5, six); sonDict.Add(6, seven); sonDict.Add(7, eight);
//oldoldDict = sonDict;
//遍历sondict 如果有终点坐标 return
n++;
for (int i = 0; i < sonList.Count; i++)
{
if (sonList[i].pos[0, 0] == end[0, 0] && sonList[i].pos[0, 1] == end[0, 1])
{
Debug.Log("路径已经找到!!");
Debug.Log("输出路径:");
for (int j = 0; j < parentPosList.Count; j++)
{
Debug.Log("路径坐标:" + parentPosList[j][0, 0] + " , " + parentPosList[j][0, 1]);
}
return;

}
}
if (n > 100)
{
Debug.Log("n>100 return!!!");
Debug.Log(parentPosList.Count);
return;
}

//清除之前的 新加son
//oldDict.Clear();
//oldDict = oldoldDict;
//oldoldDict.Clear();
//周围8点的G H值 还需要存 //sonDict里面只剩下 需要求G H值的son点 知道son的pos和end点 求 G H值 父节点就是start
//for (int i = 0; i < sonDict.Count - 1; i++)
//{
// if (sonDict.ContainsKey(i))
// {
// sonDict[i].H = Mathf.Abs(end[0, 0] - sonDict[i].pos[0, 0]) + Mathf.Abs(end[0, 1] - sonDict[i].pos[0, 1]);//H
// if (sonDict[i].pos[0, 0] == start[0, 0] || sonDict[i].pos[0, 1] == start[0, 1])//G
// {
// sonDict[i].G = 10;
// }
// else
// {
// sonDict[i].G = 14;
// }
// sonDict[i].F = sonDict[i].G + sonDict[i].H;
// }
//}

for (int i = 0; i < sonList.Count; i++)
{
sonList[i].H = (Mathf.Abs(end[0, 0] - sonList[i].pos[0, 0]) + Mathf.Abs(end[0, 1] - sonList[i].pos[0, 1])) * 10;//H
if (sonList[i].pos[0, 0] == start[0, 0] || sonList[i].pos[0, 1] == start[0, 1])
{
sonList[i].G = 10;
}
else
{
sonList[i].G = 14;
}
sonList[i].F = sonList[i].G + sonList[i].H;
}
for (int i = 0; i < sonList.Count; i++)
{
for (int j = 0; j < barrierPosList.Count; j++)
{
if (sonList[i].pos[0, 0] == barrierPosList[j][0, 0] && sonList[i].pos[0, 1] == barrierPosList[j][0, 1])
{
sonList[i].F = 1000;
}
}
}

for (int i = 0; i < sonList.Count - 1; i++)
{
if (sonList[i].F < sonList[i + 1].F)
{
sonList[i + 1] = sonList[i];
}
}

float[,] pos = sonList[7].pos;
XunLu(pos, end);


//判断son中 那个点F值最小

//for (int i = 0; i < listI.Count - 1; i++)
//{
// if (sonDict[listI[i]].F < sonDict[listI[i + 1]].F)
// {
// Debug.Log(sonDict[listI[i]].F); Debug.Log(sonDict[listI[i+1]].F);
// sonDict[listI[i]] = sonDict[listI[i + 1]];
// Debug.Log(sonDict[listI[i]].F); Debug.Log(sonDict[listI[i + 1]].F);
// }
//}
//List<Son> listSon = new List<Son>();
//foreach (var item in sonDict.Values)
//{
// listSon.Add(item);
// Debug.Log("item.add = " + listSon.Count);
//}
//for (int i = 0; i < listSon.Count - 1; i++)
//{
// if (listSon[i].F < listSon[i + 1].F)
// {
// listSon[i + 1] = listSon[i];
// }
//}
//最小的F sonDict[listI[listI.count-1]]
//Debug.Log(listSon.Count);
//parentList.Add(listSon[listSon.Count - 1]);
//dict第8个的F值最小 设置他为父节点
//float[,] parent = sonList[index];
//parentList.Add(parent);
//清除sonlist oldlist做下次判断
//sonList.Clear();
//sonDict.Clear();
//
//XunLu( sonDict[listI[listI.Count-1]].pos, end);]
//int len = parentList.Count - 1;
//XunLu(parentList[len].pos, end);
//Dictionary<int, List<float>> dictSon = new Dictionary<int, List<float>>();
//计算8点的的G H值
//8点的G值 存到dict
//dictSon[0][0]= 10; dictSon[1][0] = 14; dictSon[2][0] = 10; dictSon[3][0] = 14;
//dictSon[4][0] = 10; dictSon[5][0] = 14; dictSon[6][0] = 10; dictSon[7][0] = 14;
//8点的H值 //应该是遍历sonlist里面有几个点,拿到这几个点的G H值
//float oneH = Mathf.Abs((end[0, 0] - one[0, 0]) + (end[0, 1] - one[0, 1])) * 10; //H值
//float twoH = Mathf.Abs((end[0, 0] - two[0, 0]) + (end[0, 1] - two[0, 1])) * 10;
//float threeH = Mathf.Abs((end[0, 0] - three[0, 0]) + (end[0, 1] - three[0, 1])) * 10;
//float fourH = Mathf.Abs((end[0, 0] - four[0, 0]) + (end[0, 1] - four[0, 1])) * 10;
//float fiveH = Mathf.Abs((end[0, 0] - five[0, 0]) + (end[0, 1] - five[0, 1])) * 10;
//float sixH = Mathf.Abs((end[0, 0] - six[0, 0]) + (end[0, 1] - six[0, 1])) * 10;
//float sevenH = Mathf.Abs((end[0, 0] - seven[0, 0]) + (end[0, 1] - seven[0, 1])) * 10;
//float eightH = Mathf.Abs((end[0, 0] - eight[0, 0]) + (end[0, 1] - eight[0, 1])) * 10;
//8点H值 存到dict
//dictSon[0][1] = oneH; dictSon[1][1] = twoH; dictSon[2][1] = threeH; dictSon[3][1] = fourH;
//dictSon[4][1] = fiveH; dictSon[5][1] = sixH; dictSon[6][1] = sevenH; dictSon[7][1] = eightH;
//存
//dictSon.Add(1, new List<float> { 10, oneH }); dictSon.Add(2, new List<float> { 14, twoH });
//dictSon.Add(3, new List<float> { 10, threeH }); dictSon.Add(4, new List<float> { 14, fourH });
//dictSon.Add(5, new List<float> { 10, fiveH }); dictSon.Add(6, new List<float> { 14, sixH });
//dictSon.Add(7, new List<float> { 10, sevenH }); dictSon.Add(8, new List<float> { 14, eightH });
//比较8点之中 F=G+H 的最小值
//int index = 0;
//for (int i = 0; i < dictSon.Count - 1; i++)
//{
// if ((dictSon[i][0] + dictSon[i][1]) - (dictSon[i + 1][0] + dictSon[i + 1][1]) < 0)
// {
// List<float> list = new List<float> { dictSon[i + 1][0], dictSon[i + 1][1] };
// dictSon.Remove(i + 1);
// dictSon.Add(i + 1, list);
// index = i;
// }
//}


}


public class Son
{
public float F;
public float G;
public float H;
public float[,] pos;
public float[,] parentPos;
public Son(float[,] pos)
{
this.pos = pos;
}

}

 

}

posted on 2016-08-26 10:48  tianjiuwan  阅读(184)  评论(0编辑  收藏  举报