Ejemplo n.º 1
0
    private void Start()
    {
        //Determine size of the grid, based on the cell size and the playable area size
        int xSize = (int)Mathf.Ceil(2 * halfExtents[0] / playableGridCellSize);
        int zSize = (int)Mathf.Ceil(2 * halfExtents[2] / playableGridCellSize);

        playableAreaGrid = new PlayableGridCell[xSize, zSize];

        //Create an Axis Aligned Bounding Box for each cell in the grid, with the proper center and extents
        Vector3 cellExtents  = new Vector3(playableGridCellSize, 0.1f, playableGridCellSize);
        float   halfCellSize = playableGridCellSize * 0.5f;

        for (int i = 0; i < xSize; ++i)
        {
            for (int j = 0; j < zSize; ++j)
            {
                Vector3 cellOffset = new Vector3(i, 0, j) * playableGridCellSize + new Vector3(halfCellSize, halfExtents.y, halfCellSize);
                Vector3 cellCenter = -halfExtents + cellOffset;

                playableAreaGrid[i, j] = new PlayableGridCell
                {
                    cellBounds = new Bounds(cellCenter, cellExtents),
                    isOccupied = false
                };
            }
        }
    }
Ejemplo n.º 2
0
    private void RandomizeAsteroidTransform(GameObject asteroid)
    {
        //Create a list of all guaranteedly unoccupied cells
        List <PlayableGridCell> freeCells = new List <PlayableGridCell>();

        for (int i = 0; i < playableAreaGrid.GetLength(0); ++i)
        {
            for (int j = 0; j < playableAreaGrid.GetLength(1); ++j)
            {
                if (playableAreaGrid[i, j].isOccupied == false)
                {
                    freeCells.Add(playableAreaGrid[i, j]);
                }
            }
        }

        //If there is at least one free cell
        //Teleport the asteroid in its center
        //We can't apply a random offset, because neighbouring cells might not be free
        if (freeCells.Count > 0)
        {
            int chosenCellIndex          = Random.Range(0, freeCells.Count);
            PlayableGridCell chosenCell  = freeCells[chosenCellIndex];
            Vector3          newPosition = chosenCell.cellBounds.center;
            asteroid.GetComponent <AsteroidController>().RadomizeDirection(newPosition);
        }
    }
Ejemplo n.º 3
0
    private void CreatePlayableGrid()
    {
        Vector3 playingFieldExtents = GetPlayingFieldExtents();
        Vector3 cellExtents         = new Vector3(PlayableGridCellSize, 0.1f, PlayableGridCellSize);
        float   halfCellSize        = PlayableGridCellSize * 0.5f;

        //Determine size of the grid, based on the cell size and the playable area size
        int xSize = (int)Mathf.Floor(playingFieldExtents.x / PlayableGridCellSize);
        int zSize = (int)Mathf.Floor(playingFieldExtents.z / PlayableGridCellSize);

        PlayableAreaGrid = new PlayableGridCell[xSize, zSize];

        for (int i = 0; i < xSize; ++i)
        {
            for (int j = 0; j < zSize; ++j)
            {
                Vector3 cellOffset = new Vector3(i, 0, j) * PlayableGridCellSize + new Vector3(halfCellSize, playingFieldExtents.y, halfCellSize);
                Vector3 cellCenter = -playingFieldExtents * 0.5f + cellOffset;
                PlayableAreaGrid[i, j] = new PlayableGridCell
                {
                    cellBounds = new Bounds(cellCenter, cellExtents),
                    isOccupied = false
                };
            }
        }
    }
Ejemplo n.º 4
0
    private List <Vector3> FindFreePositions(uint requestedPositionsCnt)
    {
        //Create a list of all guaranteedly unoccupied cells
        List <PlayableGridCell> freeCells = new List <PlayableGridCell>();

        for (int i = 0; i < PlayableAreaGrid.GetLength(0); ++i)
        {
            for (int j = 0; j < PlayableAreaGrid.GetLength(1); ++j)
            {
                if (PlayableAreaGrid[i, j].isOccupied == false)
                {
                    freeCells.Add(PlayableAreaGrid[i, j]);
                }
            }
        }

        var result = new List <Vector3>();

        for (uint i = 0; i < requestedPositionsCnt; ++i)
        {
            if (freeCells.Count > 0)
            {
                int chosenCellIndex         = Random.Range(0, freeCells.Count);
                PlayableGridCell chosenCell = freeCells[chosenCellIndex];
                result.Add(chosenCell.cellBounds.center);
                freeCells.RemoveAt(chosenCellIndex);
            }
        }

        return(result);
    }
Ejemplo n.º 5
0
    private void Start()
    {
        //Determine size of the grid, based on the cell size and the playable area size
        int xSize = (int)Mathf.Ceil(2 * halfExtents[0] / playableGridCellSize);
        int zSize = (int)Mathf.Ceil(2 * halfExtents[2] / playableGridCellSize);

        playableAreaGrid = new PlayableGridCell[xSize, zSize];

        //Create an Axis Aligned Bounding Box for each cell in the grid, with the proper center and extents
        Vector3 cellExtents  = new Vector3(playableGridCellSize, 0.1f, playableGridCellSize);
        float   halfCellSize = playableGridCellSize * 0.5f;

        for (int i = 0; i < xSize; ++i)
        {
            for (int j = 0; j < zSize; ++j)
            {
                Vector3 cellOffset = new Vector3(i, 0, j) * playableGridCellSize + new Vector3(halfCellSize, halfExtents.y, halfCellSize);
                Vector3 cellCenter = -halfExtents + cellOffset;

                playableAreaGrid[i, j] = new PlayableGridCell
                {
                    cellBounds = new Bounds(cellCenter, cellExtents),
                    isOccupied = false
                };
            }
        }

        for (int i = 0; i < asteroidsCount; i++)
        {
            GameObject createdAsteroid = Instantiate(asteroidModels[Random.Range(0, 3)]);
            RegisterAsteroid(createdAsteroid);
            RandomizeAsteroidTransform(createdAsteroid);
        }
    }
Ejemplo n.º 6
0
 void OnDrawGizmos()
 {
     //Used to draw some deubg display, so we can visualize what is on the grid, and the player safe area
     if (!ShowDebugDraw || PlayableAreaGrid == null)
     {
         return;
     }
     for (int i = 0; i < PlayableAreaGrid.GetLength(0); ++i)
     {
         for (int j = 0; j < PlayableAreaGrid.GetLength(1); ++j)
         {
             PlayableGridCell cell       = PlayableAreaGrid[i, j];
             Bounds           cellBounds = cell.cellBounds;
             Gizmos.color = cell.isOccupied ? Color.red : Color.green;
             Gizmos.DrawCube(cellBounds.center, cellBounds.size - new Vector3(0.05f, 0, 0.05f));
         }
     }
 }