public void AddConnection(Cell cell) { _connections.Add(cell); }
private float CheckToCellDistance(Cell cell, float currentMinDist, Vector3 position, ref Cell rezult) { var minDist = currentMinDist; if (cell._type == CellType.USABLE) { var dist = Vector3.Distance(cell._position, position); if (minDist == -1) { minDist = dist; rezult = cell; } if (minDist > dist) { minDist = dist; rezult = cell; } } return minDist; }
private void InitializeCells() { for (int i = 0; i < _rowCount; i++) { for (int j = 0; j < _colCount; j++) { float cellPosX = cellSize / 2 + j * cellSize; float cellPosZ = (_rowCount * cellSize) - (cellSize / 2 + i * cellSize); var cellPos = new Vector3(cellPosX, 0, cellPosZ); switch (Config.cells[i, j]) { case 0: _cells[i, j] = new Cell(CellType.UNUSABLE, cellPos); break; case 2: _cells[i, j] = new Cell(CellType.USABLE, cellPos); break; } } } }
private float CalculateBetweenCellDistance(Cell cell_1, Cell cell_2) { return Vector3.Distance(cell_1._position, cell_2._position); }
public Vertex(Cell cell) { _cell = cell; }
private bool IsPassable(Cell cell) { return cell._type == CellType.USABLE ? true : false; }
private Vertex GetVertex(Cell cell) { return vertices.Find(x => x._cell == cell); }
private Stack<Vector3> GetPath(Cell startCell, Cell targetCell) { SetVertices(); opened = new List<Vertex>(); closed = new List<Vertex>(); opened.Add(GetVertex(startCell)); var counter = 0; while (opened.Count > 0 || counter > 20) { counter++; var currentVertex = GetMinDistVertexFromOpened(); opened.Remove(currentVertex); closed.Add(currentVertex); if (currentVertex._cell._position == targetCell._position) return BuildPath(currentVertex); foreach (Cell connectionCell in currentVertex._cell.GetConnections()) { var checkVertex = GetVertex(connectionCell); if (!IsPassable(checkVertex._cell)) closed.Add(checkVertex); if (closed.Contains(checkVertex) || opened.Contains(checkVertex)) continue; checkVertex.g = 1; checkVertex.h = CalculateBetweenCellDistance(checkVertex._cell, targetCell); checkVertex.f = checkVertex.g + checkVertex.h; checkVertex.parent = currentVertex; opened.Add(checkVertex); } } var targetVertex = GetMinDistVertexFromClosed(); return BuildPath(targetVertex); return null; }