GetCellAt() public static method

public static GetCellAt ( Vector3 pos ) : Cell,
pos Vector3
return Cell,
示例#1
0
    // Instantiate walls?
    void BuildWalls()
    {
        int cellsPerSide = CellMaster.CellsPerSide;

        var verticalWalls = new Cell[cellsPerSide, cellsPerSide];

        for (int x = 0; x < CellMaster.CellsPerSide - 1; x++)
        {
            int wallType = Random.Range(0, wallPrefabs.Length);
            for (int y = 0; y < CellMaster.CellsPerSide; y++)
            {
                if (!CellMaster.GetCellAt(x, y).canGoEast)
                {
                    CreateWall(CellMaster.GetCellAt(x, y), Direction.East, wallType);
                    verticalWalls[x, y] = CellMaster.GetCellAt(x, y);

                    if (y > 0 && verticalWalls[x, y - 1] == null)
                    {
                        CreateWallCap(CellMaster.GetCellAt(x, y), true);
                    }
                }
                else
                {
                    wallType = Random.Range(0, wallPrefabs.Length);
                    if (y > 0 && verticalWalls[x, y - 1] != null)
                    {
                        CreateWallCap(CellMaster.GetCellAt(x, y), true);
                    }
                }
            }
        }

        var horizontalWalls = new Cell[cellsPerSide, cellsPerSide];

        for (int y = 0; y < cellsPerSide - 1; y++)
        {
            int wallType = Random.Range(0, wallPrefabs.Length);
            for (int x = 0; x < cellsPerSide; x++)
            {
                if (!CellMaster.GetCellAt(x, y).canGoNorth)
                {
                    CreateWall(CellMaster.GetCellAt(x, y), Direction.North, wallType);
                    horizontalWalls[x, y] = CellMaster.GetCellAt(x, y);

                    if (x > 0 && horizontalWalls[x - 1, y] == null)
                    {
                        CreateWallCap(CellMaster.GetCellAt(x, y), false);
                    }
                }
                else
                {
                    wallType = Random.Range(0, wallPrefabs.Length);
                    if (x > 0 && horizontalWalls[x - 1, y] != null)
                    {
                        CreateWallCap(CellMaster.GetCellAt(x, y), false);
                    }
                }
            }
        }
    }
示例#2
0
    public Cell GetCellInDirection(Direction dir)
    {
        switch (dir)
        {
        case Direction.North:
            if (y < CellMaster.CellsPerSide - 1)
            {
                return(CellMaster.GetCellAt(x, y + 1));
            }
            break;

        case Direction.South:
            if (y > 0)
            {
                return(CellMaster.GetCellAt(x, y - 1));
            }
            break;

        case Direction.East:
            if (x < CellMaster.CellsPerSide - 1)
            {
                return(CellMaster.GetCellAt(x + 1, y));
            }
            break;

        case Direction.West:
            if (x > 0)
            {
                return(CellMaster.GetCellAt(x - 1, y));
            }
            break;

        default:
            Debug.LogWarning("Invalid direction " + dir);
            break;
        }
        return(null);
    }
示例#3
0
    public static Vector3[] PathTo(Vector3 fromPoint, Vector3 toPoint)
    {
        fromPoint.y = toPoint.y = 0;

        Cell startCell = CellMaster.GetCellAt(fromPoint);
        Cell endCell   = CellMaster.GetCellAt(toPoint);

        if (startCell.x < 0 || endCell.y < 0)
        {
            Debug.Log("Invalid path start and/or end");
            return(null);
        }

        // A* setup
        var evaluated  = new List <Cell>();
        var toEvaluate = new List <Cell>();
        var cameFrom   = new Dictionary <Cell, Cell>();

        var costTo   = new Dictionary <Cell, int>();
        var costFrom = new Dictionary <Cell, float>();

        var totalCost = new Dictionary <Cell, float>();

        toEvaluate.Add(startCell);
        costTo.Add(startCell, 0);
        costFrom.Add(startCell, HeuristicDist(startCell, endCell));
        totalCost.Add(startCell, costFrom[startCell]);

        // A* loop
        while (toEvaluate.Count > 0)
        {
            Cell  evalPoint  = null;
            float lowestCost = float.MaxValue;
            foreach (var cell in toEvaluate)    // perhaps ove toeval to a heap or something
            {
                float newCost = totalCost[cell];
                if (newCost < lowestCost)
                {
                    lowestCost = newCost;
                    evalPoint  = cell;
                }
            }

            // if reached dest
            if (evalPoint == endCell)
            {
                var cellPath = WalkPath(cameFrom, evalPoint);

                var path = new List <Vector3>();
                path.Add(fromPoint);

                for (int i = 1; i < cellPath.Count - 1; i++)
                {
                    path.Add(cellPath[i].centrePosition);
                }

                path.Add(toPoint);

                path = SmoothPath(path);
                return(path.ToArray());
            }

            toEvaluate.Remove(evalPoint);
            evaluated.Add(evalPoint);

            var neighbours = new List <Cell>();
            if (evalPoint.canGoNorth && evalPoint.GetCellInDirection(Direction.North).room != -1)
            {
                neighbours.Add(evalPoint.GetCellInDirection(Direction.North));
            }
            if (evalPoint.canGoSouth && evalPoint.GetCellInDirection(Direction.South).room != -1)
            {
                neighbours.Add(evalPoint.GetCellInDirection(Direction.South));
            }
            if (evalPoint.canGoEast && evalPoint.GetCellInDirection(Direction.East).room != -1)
            {
                neighbours.Add(evalPoint.GetCellInDirection(Direction.East));
            }
            if (evalPoint.canGoWest && evalPoint.GetCellInDirection(Direction.West).room != -1)
            {
                neighbours.Add(evalPoint.GetCellInDirection(Direction.West));
            }

            foreach (Cell newPoint in neighbours)
            {
                if (evaluated.Contains(newPoint))
                {
                    continue;
                }

                int tentCostTo = costTo[evalPoint] + 1;

                if (!toEvaluate.Contains(newPoint))
                {
                    toEvaluate.Add(newPoint);
                    costTo.Add(newPoint, tentCostTo);
                    costFrom.Add(newPoint, HeuristicDist(newPoint, endCell));
                    totalCost.Add(newPoint, tentCostTo + costFrom[newPoint]);
                    cameFrom.Add(newPoint, evalPoint);
                }
                else if (tentCostTo < costTo[newPoint])
                {
                    costTo[newPoint]    = tentCostTo;
                    cameFrom[newPoint]  = evalPoint;
                    totalCost[newPoint] = tentCostTo + costFrom[newPoint];
                }
            }
        }

        Debug.Log("No path found from " + fromPoint + " to " + toPoint);
        return(null);
    }