示例#1
0
文件: CellManager.cs 项目: Sundem/TD
 public void AddConnection(Cell cell)
 {
     _connections.Add(cell);
 }
示例#2
0
文件: CellManager.cs 项目: Sundem/TD
        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;
        }
示例#3
0
文件: CellManager.cs 项目: Sundem/TD
        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;
                    }
                }
            }
        }
示例#4
0
 private float CalculateBetweenCellDistance(Cell cell_1, Cell cell_2)
 {
     return Vector3.Distance(cell_1._position, cell_2._position);
 }
示例#5
0
 public Vertex(Cell cell)
 {
     _cell = cell;
 }
示例#6
0
 private bool IsPassable(Cell cell)
 {
     return cell._type == CellType.USABLE ? true : false;
 }
示例#7
0
 private Vertex GetVertex(Cell cell)
 {
     return vertices.Find(x => x._cell == cell);
 }
示例#8
0
        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;
        }