/// <summary> /// 搜索行走路线,找到则返回列表,未找到返回null。 /// </summary> /// <param name="iBgnX"></param> /// <param name="iBgnY"></param> /// <param name="iEndX"></param> /// <param name="iEndY"></param> /// <param name="entity">本次寻路相关联的对象</param> /// <param name="whilteList">白名单列表,不阻挡通行。</param> /// <returns></returns> public LinkedList <IMoveGrid> SearchRoutes(int iBgnX, int iBgnY, int iEndX, int iEndY, TileEntity entity = null, List <TileEntity> whilteList = null) { Dictionary <TileEntity, bool> whiteHash = null; if (whilteList != null) { whiteHash = new Dictionary <TileEntity, bool>(); foreach (var tar in whilteList) { whiteHash.Add(tar, true); } } LinkedList <IMoveGrid> path = m_aStar.Search(new TilePoint(iBgnX, iBgnY), new TilePoint(iEndX, iEndY), new AStarUserContext(this, entity, whiteHash)); // DEBUG if (path != null) { m_dbgLastRoute = new LinkedList <IMoveGrid>(); foreach (var v in path) { m_dbgLastRoute.AddFirst(v); } } else { m_dbgLastRoute = null; } return(path); }
/// <summary> /// 更新paths,并取下一个node位置 /// </summary> /// <param name="currentX"></param> /// <param name="currentY"></param> public void findUpdatedPath(int currentX, int currentY) { PathSolver <MyPathNode, System.Object> aStar = new PathSolver <MyPathNode, System.Object>(Game.grid); IEnumerable <MyPathNode> path = aStar.Search(new Vector2(currentX, currentY), new Vector2(endGridPosition.x, endGridPosition.y), null); int x = 0; if (path != null) { foreach (MyPathNode node in path) { if (x == 1) { nextNode = node; break; } x++; } foreach (GameObject g in GameObject.FindGameObjectsWithTag("GridBox")) { if (g.GetComponent <Renderer>().material.color != Color.red && g.GetComponent <Renderer>().material.color == myColor) { g.GetComponent <Renderer>().material.color = Color.white; } } foreach (MyPathNode node in path) { GameObject.Find(node.X + "," + node.Y).GetComponent <Renderer>().material.color = myColor; } } }