internal static bool CheckIncludes(Objects.Vector2D pos1, Objects.Vector2D size1, Objects.Vector2D pos2, Objects.Vector2D size2, Objects.Vector2D vec = null) { if (vec == null) { return((pos1.x + size1.x < pos2.x) || (pos1.x > pos2.x + size2.x) || ((pos1.x + size1.x >= pos2.x && pos1.x <= pos2.x + size2.x) && (pos1.y + size1.y < pos2.y || pos1.y > pos2.y + size2.y))); } return((pos1.x + vec.x < pos2.x) || (pos1.x + vec.x > pos2.x) || ((pos1.x + vec.x >= pos2.x && pos1.x + vec.x <= pos2.x) && (pos1.y + vec.y < pos2.y || pos1.y + vec.y > pos2.y))); }
internal static void UpdateCanvas(Image img, Objects.Vector2D pos, int zIdx) { /* * for (int i = 0; i < Global.canvas.Children.Count; i++) * { * }*/ Canvas.SetLeft(img, pos.x); Canvas.SetTop(img, pos.y); Canvas.SetZIndex(img, zIdx); }
public static List <Objects.Vector2D> GetPath(int[,] field, Objects.Vector2D start, Objects.Vector2D goal) { var closedSet = new Collection <Cell>(); var openSet = new Collection <Cell>(); Cell startCell = new Cell() { position = start, cameFrom = null, PathLengthFromStart = 0, HeuristicEstimatePathLength = GetHeuristicPathLength(start, goal) }; openSet.Add(startCell); while (openSet.Count > 0) { var currentCell = openSet.OrderBy(Cell => Cell.EstimateFullPathLength).First(); if (currentCell.position == goal) { return(GetPathForCell(currentCell)); } openSet.Remove(currentCell); closedSet.Add(currentCell); foreach (var neighbourCell in GetNeighbours(currentCell, goal, start, field)) { if (closedSet.Count(cell => cell.position == neighbourCell.position) > 0) { continue; } var openCell = openSet.FirstOrDefault(cell => cell.position == neighbourCell.position); if (openCell == null) { openSet.Add(neighbourCell); } else if (openCell.PathLengthFromStart > neighbourCell.PathLengthFromStart) { openCell.cameFrom = currentCell; openCell.PathLengthFromStart = neighbourCell.PathLengthFromStart; } } } return(null); }
public bool CanMove(Objects.Vector2D pos, int dir) { if (dir == 0) { return(pos.y > 0); } if (dir == 1) { return(pos.x < canvas.Width - 32); } if (dir == 2) { return(pos.y < canvas.Height - 32); } else { return(pos.x > 0); } }
internal static void Print(Objects.Vector2D vec2d) { ShowText("{" + vec2d.x.ToString() + "; " + vec2d.y.ToString() + "}"); }
internal static void OpenMapCell(Objects.Vector2D pos) { var point = MapToCell(GlobalToMap(pos)); Generate.map[Convert.ToInt32(point.x), Convert.ToInt32(point.y)] = 0; }
internal static void LockMapCell(Objects.Vector2D pos, int type) { var point = MapToCell(GlobalToMap(pos)); Generate.map[Convert.ToInt32(point.x), Convert.ToInt32(point.y)] = type; }
internal static int GetCellIndex(Objects.Vector2D point) { return(Convert.ToInt32(point.y * Generate.map.GetLength(0) + point.x)); }
internal static Objects.Vector2D CellToMap(Objects.Vector2D point) { return(new Objects.Vector2D(point.x * 32, point.y * 32)); }
internal static Objects.Vector2D MapToCell(Objects.Vector2D pos) { return(new Objects.Vector2D(pos.x / Generate.ts, pos.y / Generate.ts)); }
internal static Objects.Vector2D GlobalToMap(Objects.Vector2D pos) { return(new Objects.Vector2D(Math.Floor(pos.x), Math.Floor(pos.y))); }
private static int GetHeuristicPathLength(Objects.Vector2D from, Objects.Vector2D to) { return(Convert.ToInt32(Math.Abs(from.x - to.x) + Math.Abs(from.y - to.y))); }
private static Collection <Cell> GetNeighbours(Cell cell, Objects.Vector2D goal, Objects.Vector2D start, int[,] field) { var result = new Collection <Cell>(); // Соседними точками являются соседние по стороне клетки. Objects.Vector2D[] Vectors = new Objects.Vector2D[8]; Vectors[0] = new Objects.Vector2D(cell.position.x + 1, cell.position.y); Vectors[1] = new Objects.Vector2D(cell.position.x - 1, cell.position.y); Vectors[2] = new Objects.Vector2D(cell.position.x, cell.position.y + 1); Vectors[3] = new Objects.Vector2D(cell.position.x, cell.position.y - 1); Vectors[4] = new Objects.Vector2D(cell.position.x + 1, cell.position.y + 1); Vectors[5] = new Objects.Vector2D(cell.position.x + 1, cell.position.y - 1); Vectors[6] = new Objects.Vector2D(cell.position.x - 1, cell.position.y + 1); Vectors[7] = new Objects.Vector2D(cell.position.x - 1, cell.position.y - 1); foreach (var vector in Vectors) { int x = Convert.ToInt32(vector.x); int y = Convert.ToInt32(vector.y); // Проверяем, что не вышли за границы карты. if (x < 0 || x >= field.GetLength(0)) { continue; } if (y < 0 || y >= field.GetLength(1)) { continue; } // Проверяем, что по клетке можно ходить. if ((field[x, y] != 0) && (field[x, y] != 9)) { continue; } // Проверка диагоналей. if (vector == cell.position + new Objects.Vector2D(1, 1) && (field[x - 1, y] != 0 || field[x, y - 1] != 0)) { continue; } if (vector == cell.position + new Objects.Vector2D(-1, 1) && (field[x + 1, y] != 0 || field[x, y - 1] != 0)) { continue; } if (vector == cell.position + new Objects.Vector2D(1, -1) && (field[x - 1, y] != 0 || field[x, y + 1] != 0)) { continue; } if (vector == cell.position + new Objects.Vector2D(-1, -1) && (field[x + 1, y] != 0 || field[x, y + 1] != 0)) { continue; } // Заполняем данные для точки маршрута. var neighbourCell = new Cell() { position = vector, cameFrom = cell, PathLengthFromStart = cell.PathLengthFromStart + GetDistanceBetweenNeighbours(), HeuristicEstimatePathLength = GetHeuristicPathLength(vector, goal) }; result.Add(neighbourCell); } return(result); }