Esempio n. 1
0
    //Generates a wall starting at the coordinates given, going in a random direction
    private void GenerateWall(int x, int y, Grid <Tile> grid, GridGenerationProfile gridProfile)
    {
        int length = UE.Random.Range(gridProfile.MinWallLength, gridProfile.MaxWallLength + 1);
        int dir    = UE.Random.Range(0, 4);

        for (int i = 0; i <= length; i++)
        {
            switch (dir)
            {
            case 0:
                x += 1;
                break;

            case 1:
                x -= 1;
                break;

            case 2:
                y += 1;
                break;

            case 3:
                y -= 1;
                break;
            }

            var tile = new Tile(x, y, grid);
            tile.Type = TileType.Obstacle;
            grid.SetValue(x, y, tile);
        }
    }
Esempio n. 2
0
    public Grid <Tile> Generate(GridGenerationProfile gridProfile)
    {
        var width    = UE.Random.Range(gridProfile.MinGridWidth, gridProfile.MaxGridWidth);
        var height   = UE.Random.Range(gridProfile.MinGridHeight, gridProfile.MaxGridHeight);
        var cellSize = gridProfile.GridCellSize;
        var origin   = gridProfile.GridOrigin;

        var resultGrid = new Grid <Tile>(
            width,
            height,
            cellSize,
            origin,
            (int x, int y, Grid <Tile> grid) => InitializeTile(x, y, grid, gridProfile));

        PlaceObjective(resultGrid);
        CreatePathsToAllGreenTiles(resultGrid);

        return(resultGrid);
    }
Esempio n. 3
0
    private Tile InitializeTile(int x, int y, Grid <Tile> grid, GridGenerationProfile gridProfile)
    {
        var tile                = new Tile(x, y, grid);
        var randomValue         = UE.Random.Range(0f, 1f);
        var newWaterSpawnChance = gridProfile.WaterSpawnChance;

        int adjacentWater = NextToWater(tile, grid);

        if (adjacentWater != 0)
        {
            newWaterSpawnChance += gridProfile.WaterGrouping * adjacentWater;
        }

        //This is to check if the tile has already been generated previously i.e. part of a wall.
        //Without this check, the type of the tile will be determined again, overwriting the previous type
        try
        {
            tile.Type = grid.GetValue(x, y).Type;
            return(tile);
        }
        catch (S.NullReferenceException e) { }

        if (randomValue < gridProfile.ObstacleSpawnChance)
        {
            tile.Type = TileType.Obstacle;
            GenerateWall(x, y, grid, gridProfile);
        }
        else if (randomValue < gridProfile.ObstacleSpawnChance + newWaterSpawnChance)
        {
            tile.Type = TileType.Water;
        }
        else
        {
            tile.Type = TileType.Grass;
        }
        return(tile);
    }