public AStarPointData(AStarPoint _AStartPoint, double _g, double _h, AStarPointData _parent) { pointPos = _AStartPoint; g = _g; h = _h; parent = _parent; }
public bool JudgeIsNearPoint(AStarPoint point) { int xDis = Mathf.Abs((pos.x - point.pos.x)); int yDis = Mathf.Abs((pos.y - point.pos.y)); return(xDis <= 1 && yDis <= 1 && (xDis + yDis) <= 1); }
private List <AStarPoint> ShowPath(AStarPoint start, AStarPoint end) { mPathPosList.Clear(); AStarPoint temp = end; while (true) { mPathPosList.Add(temp); Color c = Color.white; if (temp == start) { c = Color.green; } else if (temp == end) { c = Color.red; } CreatePath(temp.mPositionX, temp.mPositionY, c); if (temp.mParentPoint == null) { break; } temp = temp.mParentPoint; } return(mPathPosList); }
private void ShowPath(AStarPoint start, AStarPoint end) { AStarPoint temp = end; while (true) { //Debug.Log(temp.X + "," + temp.Y); Color c = Color.gray; if (temp == start) { c = Color.green; } else if (temp == end) { c = Color.red; } CreateCube(temp.X, temp.Y, c); if (temp.ParentPoint == null) { break; } temp = temp.ParentPoint; } for (int x = 0; x < mapWidth; x++) { for (int y = 0; y < mapHeight; y++) { if (map[x, y].IsWall) { CreateCube(x, y, Color.blue); } } } }
//在网格上设置点的信息 public void InitPoint() { for (int i = 0; i < mGridWidth; i++) { for (int j = 0; j < mGridHeight; j++) { mPointGrid[i, j] = new AStarPoint(i, j); } } //设置障碍物 mPointGrid[4, 2].mIsObstacle = true; mPointGrid[4, 3].mIsObstacle = true; mPointGrid[4, 4].mIsObstacle = true; mPointGrid[4, 5].mIsObstacle = true; mPointGrid[4, 6].mIsObstacle = true; //显示障碍物 for (int x = 0; x < mGridWidth; x++) { for (int y = 0; y < mGridHeight; y++) { if (mPointGrid[x, y].mIsObstacle) { CreatePath(x, y, Color.blue); } } } }
public AStarPoint(int x, int y, AStarPoint point = null, bool isWall = false) { X = x; Y = y; ParentPoint = point; IsWall = isWall; }
/// <summary> /// Start is called on the frame when a script is enabled just before /// any of the Update methods is called the first time. /// </summary> void Start() { // AStarPoint[,] array = new AStarPoint[128,64]; AStarPoint[,] array = new AStarPoint[128, 64]; for (int i = 0; i < array.GetLength(0); ++i) { for (int j = 0; j < array.GetLength(1); ++j) { var obj = GameObject.Instantiate(m_Rect); obj.SetActive(true); obj.transform.SetParent(transform); obj.transform.localScale = Vector3.one; obj.transform.localPosition = new Vector2(i * 0.2f, j * 0.2f); var img = obj.GetComponent <SpriteRenderer>(); array[i, j] = new AStarPoint(i, j); array[i, j].Value = m_Txt.GetPixel(i, j) == Color.white?0:1; array[i, j].Position = obj.transform.localPosition; img.color = array[i, j].Value == 0?Color.white:Color.black; obj.GetComponent <CirclePointSprite>().OnMouseUpHandler = OnMouseClickHandler; } } map = new AStarMap(new AStarMapData(array)); // AStar.GetInstance().FindPathAsyn(map,new AStarNode(10,10),new AStarNode(115,32),true,(e)=> // { // print("1cost time "+(Time.time-t)); // StartCoroutine(showPath(e,map)); // }); // AStar.GetInstance().FindPathAsyn(map.Clone(),new AStarNode(11,9),new AStarNode(115,8),true,(e)=> // { // print("2cost time "+(Time.time-t)); // StartCoroutine(showPath(e,map)); // }); // AStar.GetInstance().FindPathAsyn(map.Clone(),new AStarNode(17,23),new AStarNode(66,8),true,(e)=> // { // print("3cost time "+(Time.time-t)); // StartCoroutine(showPath(e,map)); // }); // AStar.GetInstance().FindPathAsyn(map.Clone(),new AStarNode(66,7),new AStarNode(15,22),true,(e)=> // { // print("4cost time "+(Time.time-t)); // StartCoroutine(showPath(e,map)); // }); // AStar.GetInstance().FindPathAsyn(map.Clone(),new AStarNode(115,31),new AStarNode(11,11),true,(e)=> // { // print("5cost time "+(Time.time-t)); // StartCoroutine(showPath(e,map)); // }); }
/// <summary> /// Start is called on the frame when a script is enabled just before /// any of the Update methods is called the first time. /// </summary> void Start() { AStarPoint[,] array = new AStarPoint[(int)128, (int)64]; for (int i = 0; i < array.GetLength(0); ++i) { for (int j = 0; j < array.GetLength(1); ++j) { var obj = GameObject.Instantiate(m_Rect); obj.SetActive(true); obj.transform.SetParent(transform); obj.transform.localScale = Vector3.one; obj.GetComponent <RectTransform>().anchoredPosition = new Vector2(i * 10, j * 10); var img = obj.GetComponent <Image>(); array[i, j] = new AStarPoint(i, j); array[i, j].Value = m_Txt.GetPixel(i, j) == Color.white?0:1; array[i, j].Position = obj.GetComponent <RectTransform>().anchoredPosition; img.color = array[i, j].Value == 0?Color.white:Color.black; } } AStarMap map = new AStarMap(new AStarMapData(array)); float t = Time.time; //var rst = map.GetPath(new AStarNode(1,1),new AStarNode(6,10),false); AStar.GetInstance().FindPathAsyn(map, new AStarNode(10, 10), new AStarNode(115, 32), true, (e) => { print("1cost time " + (Time.time - t)); StartCoroutine(showPath(e, map)); }); AStar.GetInstance().FindPathAsyn(map.Clone(), new AStarNode(11, 9), new AStarNode(115, 8), true, (e) => { print("2cost time " + (Time.time - t)); StartCoroutine(showPath(e, map)); }); AStar.GetInstance().FindPathAsyn(map.Clone(), new AStarNode(17, 23), new AStarNode(66, 8), true, (e) => { print("3cost time " + (Time.time - t)); StartCoroutine(showPath(e, map)); }); AStar.GetInstance().FindPathAsyn(map.Clone(), new AStarNode(66, 7), new AStarNode(15, 22), true, (e) => { print("4cost time " + (Time.time - t)); StartCoroutine(showPath(e, map)); }); AStar.GetInstance().FindPathAsyn(map.Clone(), new AStarNode(115, 31), new AStarNode(11, 11), true, (e) => { print("5cost time " + (Time.time - t)); StartCoroutine(showPath(e, map)); }); }
//获取位置 public Vector2Int[] GetWays(Vector2Int start, Vector2Int end) { string key = GetDictKey(start, end); if (_wayDict.ContainsKey(key)) { return(_wayDict[key]); } //转换一下 start = start - new Vector2Int(_xMin, _yMin); end = end - new Vector2Int(_xMin, _yMin); //先判断位置是否合法 if (!JudgePosIsRight(start) || !JudgePosIsRight(end)) { return(null); } //开始寻路 List <AStarPoint> ways = new List <AStarPoint>(); AStarPoint nowPoint = new AStarPoint(start, 0, end); SearchAStarPoint(nowPoint, end); int count = 0; while (true) { count++; AStarPoint nextPoint = GetNextPoint(); if (nextPoint != null) { SearchAStarPoint(nextPoint, end); ways.Add(nowPoint); //这个地方看要不要把end加进来 if (nextPoint.pos == end) { break; } nowPoint = nextPoint; } else { return(null); } } //清除缓存 _wayHasSearchDict = new Dictionary <int, bool>(); _openDict = new Dictionary <int, Stack <AStarPoint> >(); //MonoBehaviour.print("搜索节点个数:" + ways.Count); Vector2Int[] shortWays = GetShortWay(ways); if (_wayDict.Count < MAX_SAVE_LENGTH) { _wayDict[key] = shortWays; return(_wayDict[key]); } else { return(shortWays); } }
/// <summary> /// 图块更新自己 /// </summary> public void UpdateSelf() { GameManager _gm = GameObject.FindWithTag("GameManager").GetComponent <GameManager>(); //if (MMapObjId == 0) //{ // return; //} string _dbName = ""; string _tabName = ""; int _id = 0; if (IsWall) { _dbName = Dict.WallDBName; _tabName = "map_" + SaveHeroInfo.CurMapId; _id = MWallId; } else { _dbName = Dict.SqlDBName; _tabName = "map_obj"; _id = MMapObjId; } //value 0 出现 if (Dict.GetInt(_dbName, _tabName, "value", _id) == 0) { //创造 _gm.GetMap().CreateMapObjById(_id); } //value 1 消失 else { AStarPoint _selfPoint = _gm.MPointGrid[MPositionX, MPositionY]; Object.Destroy(_selfPoint.MGameObject); _selfPoint.MGameObject = null; if (_gm.MPointGrid[MPositionX, MPositionY].MConnectPoint == null) { _gm.MPointGrid[MPositionX, MPositionY].Clear(); } else { //针对体型较大如巨龙或章鱼 foreach (AStarPoint _p in _gm.MPointGrid[MPositionX, MPositionY].MConnectPoint) { _gm.MPointGrid[_p.MPositionX, _p.MPositionY].Clear(); } } } if (MType == PointType.TempThrough) { MType = PointType.Obstacle; } }
//寻路 public List <AStarPoint> FindPath(AStarPoint mStartPoint, AStarPoint mEndPoint) { if (mEndPoint.mIsObstacle || mStartPoint.mPosition == mEndPoint.mPosition) { return(null); } //开启列表 List <AStarPoint> openPointList = new List <AStarPoint>(); //关闭列表 List <AStarPoint> closePointList = new List <AStarPoint>(); openPointList.Add(mStartPoint); while (openPointList.Count > 0) { //寻找开启列表中最小预算值的表格 AStarPoint minFPoint = FindPointWithMinF(openPointList); //将当前表格从开启列表移除 在关闭列表添加 openPointList.Remove(minFPoint); closePointList.Add(minFPoint); //找到当前点周围的全部点 List <AStarPoint> surroundPoints = FindSurroundPoint(minFPoint); //在周围的点中,将关闭列表里的点移除掉 SurroundPointsFilter(surroundPoints, closePointList); //寻路逻辑 foreach (var surroundPoint in surroundPoints) { if (openPointList.Contains(surroundPoint)) { //计算下新路径下的G值(H值不变的,比较G相当于比较F值) float newPathG = CalcG(surroundPoint, minFPoint); if (newPathG < surroundPoint.mG) { surroundPoint.mG = newPathG; surroundPoint.mF = surroundPoint.mG + surroundPoint.mH; surroundPoint.mParentPoint = minFPoint; } } else { //将点之间的 surroundPoint.mParentPoint = minFPoint; CalcF(surroundPoint, mEndPoint); openPointList.Add(surroundPoint); } } //如果开始列表中包含了终点,说明找到路径 if (openPointList.IndexOf(mEndPoint) > -1) { break; } } return(ShowPath(mStartPoint, mEndPoint)); }
void Start() { InitMap(); AStarPoint startPoint = map[2, 3]; AStarPoint endPoint = map[6, 3]; FindPath(startPoint, endPoint); ShowPath(startPoint, endPoint); }
public void OnMouseDown() { GameManager _gm = GameObject.FindWithTag("GameManager").GetComponent <GameManager>(); _gm.MPathPosList = null; if (_gm.IsTriggering || _gm.IsAutoMoving || !_gm.MyUI.IsAllUIClosed()) { return; } for (int x = 0; x < Dict.MapWidth; x++) { for (int y = 0; y < Dict.MapHeight; y++) { //将临时可通行的格子重置为不可通行 if (_gm.MPointGrid[x, y].MType == PointType.TempThrough) { _gm.MPointGrid[x, y].MType = PointType.Obstacle; } } } int _posX = System.Convert.ToInt32(transform.position.x); int _posY = System.Convert.ToInt32(transform.position.y); if (FindPath != null) { AStarPoint _astarPoint = _gm.MPointGrid[_posX, _posY]; _gm.ActivateAStarPoint = _astarPoint; //如果是Obj/Item物件 图块置为可通过 if (_astarPoint.IsObj) { _astarPoint.MType = PointType.TempThrough; } FindPath(_posX, _posY); } //23层 MapId24 佛家符号的中心位置处图块 if (SaveHeroInfo.CurMapId == 24) { if (_posX == 5 && _posY == 5) { if (_gm.MStartPos.MPositionX == 4 && _gm.MStartPos.MPositionY == 6 || _gm.MStartPos.MPositionX == 6 && _gm.MStartPos.MPositionY == 6 || _gm.MStartPos.MPositionX == 4 && _gm.MStartPos.MPositionY == 4 || _gm.MStartPos.MPositionX == 6 && _gm.MStartPos.MPositionY == 4 ) { //中心处图块的MapObjId = 811 _gm.TriggerObj(811); } } } }
//计算该点到终点的F值 void CalcF(AStarPoint now, AStarPoint end) { //F = G + H float h = Mathf.Abs(end.MPositionX - now.MPositionX) + Mathf.Abs(end.MPositionY - now.MPositionY); float g = now.MParentPoint == null ? 0 : Vector2.Distance(new Vector2(now.MPositionX, now.MPositionY), new Vector2(now.MParentPoint.MPositionX, now.MParentPoint.MPositionY)) + now.MParentPoint.MG; float f = g + h; now.MF = f; now.MG = g; now.MH = h; }
private void Start() { mCubeParent = GameObject.Find("Plane"); mPointGrid = AStarAlgorithm.GetInsatnce.mPointGrid; InitBG(); mStartPos = mPointGrid[0, 0]; mEndPos = mPointGrid[10, 10]; }
public AStarTriangle(AStarPoint a, AStarPoint b, AStarPoint c) { this.a = a; this.b = b; this.c = c; //计算重心 centroid.x = (a.x + b.x + c.x) / 3; centroid.y = (a.x + b.x + c.x) / 3; centroid.z = (a.x + b.x + c.x) / 3; }
public void InitPoint1(int i, int j, int pointX, int pointY) { //for (int i = 0; i < mGridWidth; i++) //{ // for (int j = 0; j < mGridHeight; j++) // { // mPointGrid[i, j] = new AStarPoint(i, j); // } //} mPointGrid[i, j] = new AStarPoint(pointX, pointY); }
private void InitMap() { for (int i = 0; i < mapWidth; i++) { for (int j = 0; j < mapHeight; j++) { map[i, j] = new AStarPoint(i, j); } } map[4, 2].IsWall = true; map[4, 3].IsWall = true; map[4, 4].IsWall = true; }
private AStarPoint GetNextPoint() { AStarPoint next = null; for (int i = 0; i <= fNumMax; i++) { if (_openDict.ContainsKey(i) && _openDict[i].Count > 0) { next = _openDict[i].Pop(); break; } } return(next); }
private void Walk() { if (mPathPosList != null && mPathPosList.Count > 1) { mStartPos = mPathPosList[mPathPosList.Count - 1]; Color color = mStartPos.mGameObject.GetComponent <Renderer>().material.color; mPathPosList.Remove(mStartPos); Destroy(mStartPos.mGameObject); mStartPos.mGameObject = null; mStartPos = mPathPosList[mPathPosList.Count - 1]; mStartPos.mGameObject.GetComponent <Renderer>().material.color = color; } }
//寻找预计值最小的格子 private AStarPoint FindPointWithMinF(List <AStarPoint> openPointList) { float f = float.MaxValue; AStarPoint temp = null; foreach (AStarPoint p in openPointList) { if (p.mF < f) { temp = p; f = p.mF; } } return(temp); }
private AStarPoint FindMinFOfPoint(List <AStarPoint> openList) { float f = float.MaxValue; AStarPoint temp = null; foreach (AStarPoint point in openList) { if (point.F < f) { temp = point; f = point.F; } } return(temp); }
private static AStarPoint UpdateAStarPoint(AStarPoint value, float oldG, float distance, Vector3 end, AStarPoint parent = null) { if (value == null) { return(value); } float h = (Mathf.Pow((value.vert.Position.x - end.x), 2) + Mathf.Pow((value.vert.Position.y - end.y), 2)); float g = oldG + distance; value.g = g; value.h = h; value.f = h + g; value.parent = parent; return(value); }
//放到Open列表中 private void AddToOpenNumDict(AStarPoint point) { if (point == null) { return; } if (!_openDict.ContainsKey(point.fNum)) { _openDict[point.fNum] = new Stack <AStarPoint>(); } if (point.fNum > fNumMax) { fNumMax = point.fNum; } _openDict[point.fNum].Push(point); }
List <AStarPoint> ShowPath(AStarPoint start, AStarPoint end) { mPathPosList.Clear(); AStarPoint temp = end; while (true) { mPathPosList.Add(temp); //原White Color c = Color.white; //Color c = new Color(0f, 0.2f, 0.3f); if (temp == start) { //Green c = Color.green; } else if (temp == end) { //Red c = Color.red; } if (mPointGrid[temp.MPositionX, temp.MPositionY].MGameObject == null) { CreatePath(temp.MPositionX, temp.MPositionY, c); } if (temp.MParentPoint == null) { break; } //防止死循环 if (temp.MPosition == start.MPosition) { break; } temp = temp.MParentPoint; } return(mPathPosList); }
private static bool GenerateMap(AStarPoint s, AStarPoint e) { if (s.Equals(e)) { return(false); } var main = MainGameManager.Instance; var mainMap = main.MapArray; Max_PNT = new AStarPoint(main.W, main.H); Start_Pnt = s; End_Pnt = e; Map = new char[Max_PNT.x, Max_PNT.y]; for (int y = 0; y < Max_PNT.y; y++) { for (int x = 0; x < Max_PNT.x; x++) { var item = mainMap[x, y]; if (item.ElementContent == ElementContent.Door || item.ElementContent == ElementContent.Enemy || item.ElementContent == ElementContent.BigWall || item.ElementContent == ElementContent.SmallWall || item.ElementState == ElementState.Marked || (item.ElementContent == ElementContent.Trap && item.ElementState == ElementState.UnCovered)) { Map[x, y] = Wall; } else if (item.ElementState == ElementState.UnCovered || (item.ElementContent == ElementContent.Tool && !(item as ToolElement).isHide) || (item.ElementContent == ElementContent.Gold && !(item as GoldElement).isHide)) { Map[x, y] = Space0; } else { Map[x, y] = Space1; } } } if (Map[End_Pnt.x, End_Pnt.y] == Wall) { return(false); } Map[Start_Pnt.x, Start_Pnt.y] = Start; Map[End_Pnt.x, End_Pnt.y] = End; return(true); }
//搜索这个点周围的点 private void SearchAStarPoint(AStarPoint point, Vector2Int end) { if (point.isSearch) { return; } point.isSearch = true; AStarPoint left = GetAStarPointByPos(point.pos.x - 1, point.pos.y, point.gNum, end); AStarPoint up = GetAStarPointByPos(point.pos.x, point.pos.y + 1, point.gNum, end); AStarPoint right = GetAStarPointByPos(point.pos.x + 1, point.pos.y, point.gNum, end); AStarPoint down = GetAStarPointByPos(point.pos.x, point.pos.y - 1, point.gNum, end); AddToOpenNumDict(left); AddToOpenNumDict(up); AddToOpenNumDict(right); AddToOpenNumDict(down); }
private List <NavMeshMovementLine> ReconstructPath(AStarPoint lastPoint, Vector3 end, Vector3 start) { reconstructedPath.Clear(); reconstructedPath.Add(new NavMeshMovementLine { point = lastPoint.vert.Position, associatedVertex = lastPoint.vert }); current = lastPoint.parent; while (current != null) { reconstructedPath.Add(new NavMeshMovementLine { point = current.vert.Position, associatedVertex = lastPoint.vert }); lastPoint = current; current = lastPoint.parent; } reconstructedPath.Reverse(); return(reconstructedPath); }
/// <summary> /// 计算G /// </summary> /// <param name="data"></param> /// <returns></returns> public static double CalG(AStarPointData data, AStarPoint newPos) { double result = data.g + Math.Abs(newPos.x - data.pointPos.x) != Math.Abs(newPos.y - data.pointPos.y) ? StraightLine : SlantLine; double temp = 0; var _type = Map[newPos.x, newPos.y]; switch (_type) { case Space0: temp = 1; break; case Space1: temp = 10; break; } return(result + temp); }
private void CalcF(AStarPoint nowPoint, AStarPoint endPoint) { float h = Mathf.Abs(endPoint.X - nowPoint.X) + Mathf.Abs(endPoint.Y - nowPoint.Y); float g = 0; if (nowPoint.ParentPoint == null) { g = 0; } else { g = Vector2.Distance(new Vector2(nowPoint.X, nowPoint.Y), new Vector2(nowPoint.ParentPoint.X, nowPoint.ParentPoint.Y)); } float f = g + h; nowPoint.G = g; nowPoint.H = h; nowPoint.F = f; }