private static int GetHeuristicPathLength(Point from, Point to) { var _from = HexMap.HexToSquare(from); var _to = HexMap.HexToSquare(to); return(Convert.ToInt32(Math.Abs(_from.X - _to.X) + Math.Abs(_from.Y - _to.Y))); }
public Path(BattleTile start, BattleTile target, HexMap map, BattleWindow window) { this.window = window; tiles = map.GetTilesFromPoints(AStar.FindPath(map, start.coord, target.coord, false)); window.DrawPath(this); }
private void Character_Moved(object sender, Character.MoveEventArgs e) { var point = HexMap.HexToPixel(e.target.coord); Canvas.SetTop(this, point.Y); Canvas.SetLeft(this, point.X); ShowData(); }
public static List <Point> FindPath(HexMap field, Point start, Point goal, bool straight) { // Шаг 1. var closedSet = new Collection <PathNode>(); var openSet = new Collection <PathNode>(); // Шаг 2. PathNode startNode = new PathNode() { Position = start, CameFrom = null, PathLengthFromStart = 0, HeuristicEstimatePathLength = GetHeuristicPathLength(start, goal) }; openSet.Add(startNode); while (openSet.Count > 0) { // Шаг 3. var currentNode = openSet.OrderBy(node => node.EstimateFullPathLength).First(); // Шаг 4. if (currentNode.Position == goal) { return(GetPathForNode(currentNode)); } // Шаг 5. openSet.Remove(currentNode); closedSet.Add(currentNode); // Шаг 6. foreach (var neighbourNode in GetNeighbours(currentNode, goal, field, straight)) { // Шаг 7. if (closedSet.Count(node => node.Position == neighbourNode.Position) > 0) { continue; } var openNode = openSet.FirstOrDefault(node => node.Position == neighbourNode.Position); // Шаг 8. if (openNode == null) { openSet.Add(neighbourNode); } else if (openNode.PathLengthFromStart > neighbourNode.PathLengthFromStart) { // Шаг 9. openNode.CameFrom = currentNode; openNode.PathLengthFromStart = neighbourNode.PathLengthFromStart; } } } // Шаг 10. return(null); }
private static Collection <PathNode> GetNeighbours(PathNode currentNode, Point goal, HexMap field, bool straight) { var result = new Collection <PathNode>(); List <Point> neighbourPoints = HexMap.GetNeighbourPoints(currentNode.Position); foreach (var point in neighbourPoints) { // Проверяем, что не вышли за границы карты. if (point.X < 0 || point.X >= field.Tiles.GetLength(0)) { continue; } if (point.Y < 0 || point.Y >= field.Tiles.GetLength(1)) { continue; } // Проверяем, что по клетке можно ходить. if (!field[point.X, point.Y].terrain.walkable) { continue; } // Заполняем данные для точки маршрута. var neighbourNode = new PathNode() { Position = point, CameFrom = currentNode, PathLengthFromStart = currentNode.PathLengthFromStart + GetDistanceBetweenNeighbours(point, field), HeuristicEstimatePathLength = GetHeuristicPathLength(point, goal) }; if (straight) { neighbourNode.PathLengthFromStart = currentNode.PathLengthFromStart; } result.Add(neighbourNode); } return(result); }
private static int GetDistanceBetweenNeighbours(Point to, HexMap field) { return(field.GetTileFromPoint(to).terrain.moveCost); }