private static int GetHeuristicPathLength(Point from, Point to) { var _from = HexagonalMap.HexToSquare(from); var _to = HexagonalMap.HexToSquare(to); return(Convert.ToInt32(Math.Abs(_from.X - _to.X) + Math.Abs(_from.Y - _to.Y))); }
void DrawHighlightPolygonByPixel(Point p) { var tileCoord = HexagonalMap.PixelToHex(p); DrawClickedPolygon(tileCoord); Console.WriteLine(tileCoord); }
Polygon GetLocatedPolygon(System.Drawing.Point coord) { Polygon p = new Polygon(); var offset = HexagonalMap.HexToPixel(coord); offset.Y += 80; p.Points.Add(new Point(0, 60)); p.Points.Add(new Point(50, 120)); p.Points.Add(new Point(150, 120)); p.Points.Add(new Point(200, 60)); p.Points.Add(new Point(150, 0)); p.Points.Add(new Point(50, 0)); Canvas.SetTop(p, offset.Y); Canvas.SetLeft(p, offset.X); //p.MouseEnter += P_MouseEnter; //p.MouseLeave += P_MouseLeave; //p.Fill = Brushes.Transparent; Panel.SetZIndex(p, 0); p.Stroke = Brushes.Black; return(p); }
public static List <Point> FindPath(HexagonalMap 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, HexagonalMap field, bool straight) { var result = new Collection <PathNode>(); List <Point> neighbourPoints = HexagonalMap.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.isWalkable) { 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); }
void GenerateImages() { foreach (WorldTile item in worldMap) { var textures = item.TextureUris; for (int i = 0; i < textures.Count; i++) { Image img = new Image { Source = new BitmapImage(textures[i]) }; var pixelCoord = HexagonalMap.HexToPixel(item.Coord); Canvas.SetTop(img, pixelCoord.Y); Canvas.SetLeft(img, pixelCoord.X); Panel.SetZIndex(img, i); canvas.Children.Add(img); } } }
private static int GetDistanceBetweenNeighbours(Point to, HexagonalMap field) { return(Convert.ToInt32(field.GetTileFromPoint(to).Terrain.moveCost)); }