예제 #1
0
 /// <summary>
 /// Clears the grid
 /// </summary>
 public void ClearGrid()
 {
     for (int y = 0; y < GridPoints.GetLength(1); y++)
     {
         for (int x = 0; x < GridPoints.GetLength(0); x++)
         {
             //check if there is an object
             if (GridPoints[x, y].IsOccupied)
             {
                 gameObjects.Remove(GridPoints[x, y].CellObject);
                 GridPoints[x, y].CellObject = null;
             }
         }
     }
 }
예제 #2
0
    /// <summary>
    /// Gets the nearest point in the gird as a Vector3.
    /// Returns null if passed position is too far out of the grid.
    /// </summary>
    /// <param name="position">The position to get the nearest grid cell</param>
    /// <returns>The nearest grid cell if not outside the grid</returns>
    public CustomGridCell GetGridCellInGrid(Vector3 location)
    {
        //round the x and y of the position to test
        int xPosition = Mathf.RoundToInt(location.x);
        int yPosition = Mathf.RoundToInt(location.y);

        //int zPosition = Mathf.RoundToInt(location.z / Constants.LEVEL_EDITOR_SPACING);

        //bounds check
        if (xPosition < 0 || xPosition > GridPoints.GetLength(0) - 1)
        {
            return(null);
        }
        //y bounds
        else if (yPosition < 0 || yPosition > GridPoints.GetLength(1) - 1)
        {
            return(null);
        }

        //in bounds
        return(GridPoints[xPosition, yPosition]);
    }