RoomManager.SetupRooms C# (CSharp) Method

SetupRooms() public method

public SetupRooms ( ) : void
return void
    public void SetupRooms()
    {
        float startTime = Time.realtimeSinceStartup;
        this.rooms = new GameObject[roomSide, roomSide];

        TileMapGeneration();
        //print ("Generate tile map " + (Time.realtimeSinceStartup - startTime));

        // Create rooms
        for (int i = 0; i < roomSide; i++) {
            for (int j = 0; j < roomSide; j++) {
                this.rooms [i, j] = RoomSetup (i, j);
            }
        }
        //print ("Create rooms " + (Time.realtimeSinceStartup - startTime));

        // Create outer rock wall
        for (int x = -1; x <= this.roomSide * this.columns; x++) {
            for (int y = -1; y <= this.roomSide * this.rows; y++) {
                if (x == -1 || x == this.roomSide * this.rows || y == -1 || y == this.roomSide * this.columns) {
                    //this.PlaceItem(outerWallTiles[Random.Range(0, outerWallTiles.Length)], x, y);
                    GameObject wall = Instantiate (this.outerWallTiles[0],
                                   									 new Vector3(x - this.columns / 2 + .5f, y - this.rows / 2 + .5f, 0f),
                                   									 Quaternion.identity) as GameObject;
                    wall.transform.SetParent(this.rooms[0,0].transform);
                }
            }
        }

        //print ("Create outer rock wall " + (Time.realtimeSinceStartup - startTime));

        //create game path
        //TODO fix sorting algo for randomPoints
        if (!this.startScreen) {
            List<Vector2> randomPoints = new List<Vector2>();
            foreach (Region region in this.regions) {
                randomPoints.Add(new Vector2(region.focusX, region.focusY));
            }

            List <int[]> pointsDist = new List<int[]>();
            Vector2 start = new Vector2 (16f, 16f);
            Vector2 exit = new Vector2 ((float)rows * roomSide, (float)columns * roomSide);
            randomPoints.Insert (0, start);
            randomPoints.Add (exit);

            for (int i = 0; i < randomPoints.Count; i++) {
                int pointX = (int)randomPoints[i].x;
                int pointY = (int)randomPoints[i].y;
                float dist = Math.Abs(pointX - end[0]) + Math.Abs(pointY - end[1]);
                int[] tuple = new int[2] {i, (int)dist};
                pointsDist.Add(tuple);
            }

            //first item is index of randomPoints the second is the distance
            pointsDist.Sort ((a, b) => a [1].CompareTo (b [1]));

            for (int i = 0; i < pointsDist.Count - 1 ; i++) {
                int index1 = pointsDist[i][0];
                int index2 = pointsDist[i + 1][0];
                float[] current = new float[2] {randomPoints[index1].x, randomPoints[index1].y};
                float[] next = new float[2] {randomPoints[index2].x, randomPoints[index2].y};
                float xDiff = next[0] - current[0];
                float yDiff = next[1] - current[1];
                float moveX;
                float moveY;

                if(Mathf.Abs(xDiff) > Mathf.Abs(yDiff)){
                    moveX = Mathf.Sign(xDiff);
                    moveY = yDiff/Math.Abs(xDiff);
                }else{
                    moveY = Mathf.Sign(yDiff);
                    moveX = xDiff/Math.Abs (yDiff);
                }
                placePath(current, next, moveX, moveY);

            }
            //print ("Create path " + (Time.realtimeSinceStartup - startTime));
        }

        // Create climb points
        /*for (int i = 0; i < this.regions.Count; i++) {
            Region region = this.regions[i];

            if (i < this.roomSide) {
                int x = 1;
                Region rightRegion = this.regions[i + 1];
                while (region.focusX + x < rightRegion.focusX) {
                    Destroy(this.tileMap[region.focusX + x, region.focusY].item);
                    x++;
                }
            }

            if (i < this.regions.Count - this.roomSide) {
                int y = 1;
                Region upperRegion = this.regions[i + this.roomSide];
                while (region.focusY + y < upperRegion.focusY) {
                    Tile tile = this.tileMap[region.focusX, region.focusY + y];
                    if (tile.item != null) {
                        Destroy(tile.item);
                        this.SetGroundTile(this.ElevationTile.tiles[0], region.focusX, region.focusY + y);
                        this.tileMap[region.focusX, region.focusY].blocking = true;
                    }
                    y++;
                }
            }
        }
        print (Time.realtimeSinceStartup - startTime);*/

        // Create blocking tiles
        foreach (Region region in this.regions) {
            region.makeBlocking();
        }
        ElevationTile.ClearWallsHash();
        print ("Create blocking Tiles" + (Time.realtimeSinceStartup - startTime));

        if (!this.startScreen) {
            this.StartTile.PlaceStartTiles();
        }

        //create building
        buildingCount = (int)Mathf.Round(Random.Range (0, 3));
        foreach (Region region in this.regions) {
            if(region.biome.getBiomeNumber() == 0 || region.biome.getBiomeNumber() == 2){
                int buildY = region.focusY;
                int buildX = region.focusX;
                Tile currentTile = this.tileMap[buildX, buildY];
                bool notInThisBiome = false;
                bool isInBounds = false;

                while (currentTile.blocking){
                    if(buildY - 1 > 0){
                        buildY -= 1;
                        currentTile = this.tileMap[buildX, buildY];
                    }else{
                        notInThisBiome = true;
                    }
                }
                //print ("THIS IS THE BUILDINGCOUNT" + buildingCount);

                //make sure y is inbounds
                if(buildY + 1 > 1 && buildY + 1 < columns * roomSide - 1){
                    //print ("y + 1 is inbounds");
                    //check southwest corner for inbounds
                    if(buildX - 1 > 1){
                        //print ("x - 1 is inbounds");
                        //check southeast corner for inbounds
                        if(buildX + 1 < rows * roomSide - 1){
                            //print ("x + 1 is inbounds");
                            //check y + 3 is inbounds
                            if(buildY + 3 < columns * roomSide - 1){
                                //print ("y + 3 is inbounds");
                                isInBounds = true;
                            }
                        }
                    }
                }

                if(notInThisBiome == false && buildings <= buildingCount && isInBounds == true){

                    buildings ++;
                    //print ("ADDING BUILDING");
                    currentTile = this.tileMap[buildX, buildY + 1];

                    Destroy(currentTile.item);
                    this.PlaceItem(buildingTiles[1], buildX, buildY);

                    Tile northTile = this.tileMap[buildX, buildY + 2];
                    Destroy(northTile.item);
                    this.PlaceItem(buildingTiles[3],northTile.x, northTile.y);

                    Tile northEastTile = this.tileMap[buildX + 1, buildY + 2];
                    Destroy(northEastTile.item);
                    this.PlaceItem(buildingTiles[4], northEastTile.x, northEastTile.y);

                    Tile eastTile = this.tileMap[buildX + 1, buildY + 1];
                    Destroy(eastTile.item);
                    this.PlaceItem(buildingTiles[0], eastTile.x, eastTile.y);

                    Tile southEastTile = this.tileMap[buildX + 1, buildY];
                    Destroy(southEastTile.item);
                    this.PlaceItem(buildingTiles[6], southEastTile.x, southEastTile.y);

                    Tile middleTile = this.tileMap[buildX, buildY + 1];
                    Destroy(middleTile.item);
                    this.PlaceItem(buildingTiles[2], middleTile.x, middleTile.y);

                    Tile southWestTile = tileMap[buildX - 1, buildY];
                    Destroy(southWestTile.item);
                    this.PlaceItem(buildingTiles[7], southWestTile.x, southWestTile.y);

                    Tile westTile = tileMap[buildX - 1, buildY + 1];
                    Destroy(westTile.item);
                    this.PlaceItem(buildingTiles[8], westTile.x, westTile.y);

                    Tile northWestTile = tileMap[buildX - 1, buildY + 2];
                    Destroy(northWestTile.item);
                    this.PlaceItem(buildingTiles[5], northWestTile.x, northWestTile.y);
                }
            }
        }

        // Randomly distribute items throughout the game
        if (this.startScreen) {
            blockingCount.minimum = 2;
            blockingCount.maximum = 5;
        }
        LayoutObjectAtRandom (blocks, blockingCount.minimum, blockingCount.maximum);
        //print ("Random Objects layed out" + (Time.realtimeSinceStartup - startTime));

        // Spawn starting enemies
        if (!this.startScreen) {
            for (int i = 0; i < 300; i++) {
                Region region = this.regions[Random.Range(0, this.regions.Count)];
                region.spawnEnemy();
            }
        }
    }

Usage Example

Esempio n. 1
0
 void InitGame()
 {
     roomScript.SetupRooms();
 }