Exemple #1
0
    private void GenerateMap()
    {
        roughTerrainPercent = Random.Range(0f, 1f);
        wallPercent         = Random.Range(0f, 1f);

        mapSize.x = Random.Range(10, 20);
        mapSize.y = Random.Range(10, 20);


        //Creating List of tiles
        allTileCoords = new List <Utility.Coord> ();

        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                allTileCoords.Add(new Utility.Coord(x, y));
            }
        }

        shuffledTileCoords = new Queue <Utility.Coord>(Utility.ShuffleArray(allTileCoords.ToArray()));


        //Populating map with tiles
        string holderName = "Generated Map";

        if (transform.FindChild(holderName))
        {
            Destroy(transform.FindChild(holderName).gameObject);
        }

        Transform mapHolder = new GameObject(holderName).transform;

        mapHolder.parent = transform;


        map = new GameObject[(int)mapSize.x, (int)mapSize.y];

        playerCoords = GetRandomCoord();

        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                Vector3 tilePosition = new Vector3(-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y / 2 + 0.5f + y);

                //Rough Terrain
                if (Random.value < roughTerrainPercent)
                {
                    Transform newTile = (Transform)Instantiate(DirtPlants1Tile, tilePosition, Quaternion.Euler(Vector3.right * 90));
                    newTile.localScale = new Vector3(5, 5, 5) * (1 - outlinePercent);
                    newTile.parent     = mapHolder;
                    GameObject tile = newTile.gameObject;
                    map[x, y] = tile;
                    tileclass = map[x, y].GetComponent <TileClass>();
                    tileclass.setType(1);
                    tileclass.setPos(tilePosition);
                    //Debug.Log(tileclass.getPos());
                }

                //Basic Terrain
                else
                {
                    Transform newTile = (Transform)Instantiate(Dirt1Tile, tilePosition, Quaternion.Euler(Vector3.right * 90));
                    newTile.localScale = new Vector3(5, 5, 5) * (1 - outlinePercent);
                    newTile.parent     = mapHolder;
                    GameObject tile = newTile.gameObject;
                    map[x, y] = tile;
                    tileclass = map[x, y].GetComponent <TileClass>();
                    tileclass.setType(0);
                    tileclass.setPos(tilePosition);
                    //Debug.Log(tileclass.getType());
                }
            }
        }

        //Walls
        bool[,] wallMap = new bool[(int)mapSize.x, (int)mapSize.y];

        int wallCount        = (int)(mapSize.x * mapSize.y * wallPercent);
        int currentWallCount = 0;

        for (int i = 0; i < wallCount; i++)
        {
            Utility.Coord randomCoord = GetRandomCoord();
            wallMap[randomCoord.x, randomCoord.y] = true;
            currentWallCount++;

            //Wall
            if (MapIsFullyAccessible(wallMap, currentWallCount) && !playerCoords.Equals(randomCoord))
            {
                Vector3 wallPosition = CoordToPosition(randomCoord.x, randomCoord.y);
                wallPosition = wallPosition + new Vector3(0, 0.001f, 0);

                Transform newTile = (Transform)Instantiate(DirtRock1Tile, wallPosition, Quaternion.Euler(Vector3.right * 90));
                newTile.localScale = new Vector3(5, 5, 5) * (1 - outlinePercent);
                newTile.parent     = mapHolder;
                GameObject tile = newTile.gameObject;
                map[randomCoord.x, randomCoord.y] = tile;
                tileclass = tile.GetComponent <TileClass>();
                tileclass.setType(2);
                tileclass.setPos(wallPosition);
                //Debug.Log(tileclass.coverType);
                wallMap[randomCoord.x, randomCoord.y] = true;
            }
            else
            {
                wallMap[randomCoord.x, randomCoord.y] = false;
                currentWallCount--;
            }
        }
    }