/// <summary> /// A*寻路移动 /// useLinearFinder 使用直线寻路,对于夺宝奇兵类型的怪物,使用直线寻路,寻找近的点 /// </summary> private bool AStarMove(Monster sprite, Point p, int action) { //首先置空路径 //sprite.PathString = ""; Point srcPoint = sprite.Coordinate; //***************************************** //srcPoint = new Point(710, 3450); //p = new Point(619, 2191); //**************************************** //进行单元格缩小 Point start = new Point() { X = srcPoint.X / 20, Y = srcPoint.Y / 20 }, end = new Point() { X = p.X / 20, Y = p.Y / 20 }; //如果起止一样,就不移动 if (start.X == end.X && start.Y == end.Y) { return(true); } GameMap gameMap = GameManager.MapMgr.DictMaps[sprite.MonsterZoneNode.MapCode]; //System.Diagnostics.Debug.WriteLine( //string.Format("开始AStar怪物寻路, ExtenstionID={0}, Start=({1},{2}), End=({3},{4}), fixedObstruction=({5},{6}), MapCode={7}", // sprite.MonsterInfo.ExtensionID, (int)start.X, (int)start.Y, (int)end.X, (int)end.Y, gameMap.MyNodeGrid.numCols, gameMap.MyNodeGrid.numRows, // sprite.MonsterZoneNode.MapCode) // ); if (start != end) { List <ANode> path = null; gameMap.MyNodeGrid.setStartNode((int)start.X, (int)start.Y); gameMap.MyNodeGrid.setEndNode((int)end.X, (int)end.Y); try { path = gameMap.MyAStarFinder.find(gameMap.MyNodeGrid); } catch (Exception) { sprite.DestPoint = new Point(-1, -1); LogManager.WriteLog(LogTypes.Error, string.Format("AStar怪物寻路失败, ExtenstionID={0}, Start=({1},{2}), End=({3},{4}), fixedObstruction=({5},{6})", sprite.MonsterInfo.ExtensionID, (int)start.X, (int)start.Y, (int)end.X, (int)end.Y, gameMap.MyNodeGrid.numCols, gameMap.MyNodeGrid.numRows)); return(false); } if (path == null || path.Count <= 1) { // 寻找一个直线的两点间的从开始点出发的最大无障碍点 Point maxPoint; if (FindLinearNoObsMaxPoint(gameMap, sprite, p, out maxPoint)) { path = null; end = new Point() { X = maxPoint.X / gameMap.MapGridWidth, Y = maxPoint.Y / gameMap.MapGridHeight, }; p = maxPoint; gameMap.MyNodeGrid.setStartNode((int)start.X, (int)start.Y); gameMap.MyNodeGrid.setEndNode((int)end.X, (int)end.Y); path = gameMap.MyAStarFinder.find(gameMap.MyNodeGrid); } } if (path == null || path.Count <= 1) { //路径不存在 //sprite.MoveToPos = new Point(-1, -1); //防止重入 sprite.DestPoint = new Point(-1, -1); sprite.Action = GActions.Stand; //不需要通知,统一会执行的动作 Global.RemoveStoryboard(sprite.Name); return(false); } else { //找到路径 设置路径 //sprite.PathString = Global.TransPathToString(path); //System.Diagnostics.Debug.WriteLine(String.Format("monster_{0} 路径:{1} ", sprite.RoleID, sprite.PathString)); //System.Diagnostics.Debug.WriteLine(String.Format("start:{0}, {1} end {2}, {3}", start.X, start.Y, end.X, end.Y)); //System.Diagnostics.Debug.WriteLine(String.Format("srcPoint:{0}, {1} P {2}, {3}", srcPoint.X, srcPoint.Y, p.X, p.Y)); sprite.Destination = p; double UnitCost = 0; //if (action == (int)GActions.Walk) //{ // UnitCost = Data.WalkUnitCost; //} //else if (action == (int)GActions.Run) //{ // UnitCost = Data.RunUnitCost; //} UnitCost = Data.RunUnitCost; //怪物使用跑步的移动速度 UnitCost = UnitCost / sprite.MoveSpeed; UnitCost = 20.0 / UnitCost * Global.MovingFrameRate; UnitCost = UnitCost * 0.5; StoryBoardEx.RemoveStoryBoard(sprite.Name); StoryBoardEx sb = new StoryBoardEx(sprite.Name); sb.Completed = Move_Completed; //path.Reverse(); Point firstPoint = new Point(path[0].x * gameMap.MapGridWidth, path[0].y * gameMap.MapGridHeight); sprite.Direction = this.CalcDirection(sprite.Coordinate, firstPoint); sprite.Action = (GActions)action; sb.Binding(); sprite.FirstStoryMove = true; sb.Start(sprite, path, UnitCost, 20); } } return(true); }
private bool AStarMove(Monster sprite, Point p, int action) { Point srcPoint = sprite.Coordinate; Point start = new Point { X = srcPoint.X / 20.0, Y = srcPoint.Y / 20.0 }; Point end = new Point { X = p.X / 20.0, Y = p.Y / 20.0 }; bool result; if (start.X == end.X && start.Y == end.Y) { result = true; } else { GameMap gameMap = GameManager.MapMgr.DictMaps[sprite.MonsterZoneNode.MapCode]; if (start != end) { List <ANode> path = null; gameMap.MyNodeGrid.setStartNode((int)start.X, (int)start.Y); gameMap.MyNodeGrid.setEndNode((int)end.X, (int)end.Y); try { path = gameMap.MyAStarFinder.find(gameMap.MyNodeGrid); } catch (Exception) { sprite.DestPoint = new Point(-1.0, -1.0); LogManager.WriteLog(LogTypes.Error, string.Format("AStar怪物寻路失败, ExtenstionID={0}, Start=({1},{2}), End=({3},{4}), fixedObstruction=({5},{6})", new object[] { sprite.MonsterInfo.ExtensionID, (int)start.X, (int)start.Y, (int)end.X, (int)end.Y, gameMap.MyNodeGrid.numCols, gameMap.MyNodeGrid.numRows }), null, true); return(false); } if (path == null || path.Count <= 1) { Point maxPoint; if (this.FindLinearNoObsMaxPoint(gameMap, sprite, p, out maxPoint)) { path = null; end = new Point { X = maxPoint.X / (double)gameMap.MapGridWidth, Y = maxPoint.Y / (double)gameMap.MapGridHeight }; p = maxPoint; gameMap.MyNodeGrid.setStartNode((int)start.X, (int)start.Y); gameMap.MyNodeGrid.setEndNode((int)end.X, (int)end.Y); path = gameMap.MyAStarFinder.find(gameMap.MyNodeGrid); } } if (path == null || path.Count <= 1) { sprite.DestPoint = new Point(-1.0, -1.0); sprite.Action = GActions.Stand; Global.RemoveStoryboard(sprite.Name); return(false); } sprite.Destination = p; double UnitCost = (double)Data.RunUnitCost; UnitCost /= sprite.MoveSpeed; UnitCost = 20.0 / UnitCost * (double)Global.MovingFrameRate; UnitCost *= 0.5; StoryBoardEx.RemoveStoryBoard(sprite.Name); StoryBoardEx sb = new StoryBoardEx(sprite.Name); sb.Completed = new StoryBoardEx.CompletedDelegateHandle(this.Move_Completed); Point firstPoint = new Point((double)(path[0].x * gameMap.MapGridWidth), (double)(path[0].y * gameMap.MapGridHeight)); sprite.Direction = this.CalcDirection(sprite.Coordinate, firstPoint); sprite.Action = (GActions)action; sb.Binding(); sprite.FirstStoryMove = true; sb.Start(sprite, path, UnitCost, 20); } result = true; } return(result); }