예제 #1
0
    /*
     * Replace this room with puzzle1 room
     */
    private void ReplaceWithPuzzle1()
    {
        // Remove all childs
        for (int i = transform.childCount - 1; i >= 0; i--)
        {
            Destroy(transform.GetChild(i).gameObject);
        }

        // Add shop room
        Transform trans = Instantiate(myGenerator.puzzle1, transform).transform;

        walls = trans.GetChild(0);
        doors = trans.GetChild(1);
        floor = trans.GetChild(2);
        traps = trans.GetChild(3);

        size = new Vector2(9, 7);

        // Fix camera
        SmoothCamera camera = Camera.main.GetComponent <SmoothCamera>();

        camera.offset = new Vector2(Mathf.Floor(size.x / 2), Mathf.Floor(size.y / 2));
        camera.GetComponent <Camera>().orthographicSize = size.y / 2 + 2;
    }
예제 #2
0
    /*
     * Generate a room
     */
    private IEnumerator GenerateRoom(Vector2 virtualLoc)
    {
        // Default variables
        bool lastRoom  = false;
        bool mainRoom  = false;
        bool northDoor = false;
        bool eastDoor  = false;
        bool southDoor = false;
        bool westDoor  = false;

        if (rooms.Count == maxRooms)
        {
            lastRoom = true;
        }
        else if (rooms.Count > maxRooms)
        {
            yield break;
        }

        // Generate sizes
        int sizeX = MakeOdd(Random.Range(minSize, maxSize));

        yield return(new WaitForEndOfFrame());

        int sizeY = MakeOdd(Random.Range(minSize, sizeX));

        // Create base gameobject
        GameObject roomGo = new GameObject("Room #" + (rooms.Count + 1));

        Room room = roomGo.AddComponent <Room>();

        room.myGenerator = this;
        room.virtualLoc  = virtualLoc;
        room.size        = new Vector2(sizeX, sizeY);
        room.Init();

        rooms.Add(virtualLoc, room);

        // Main room check
        if (rooms.Count == 1)
        {
            mainRoom = true;
        }

        // Check rooms on my sides
        if (room.northRoom)
        {
            northDoor = true;
        }

        if (room.eastRoom)
        {
            eastDoor = true;
        }

        if (room.southRoom)
        {
            southDoor = true;
        }

        if (room.westRoom)
        {
            westDoor = true;
        }

        // Datermine locations of doors
        // Top door check
        yield return(new WaitForEndOfFrame());

        if (!northDoor && !lastRoom && Random.Range(1, 100) % 2 == 0)
        {
            // Yes we want a top door
            northDoor = true;
        }

        // Right door check
        yield return(new WaitForEndOfFrame());

        if (!eastDoor && !lastRoom && Random.Range(1, 100) % 2 == 0)
        {
            // Yes we want a top door
            eastDoor = true;
        }

        // Bottom door check
        yield return(new WaitForEndOfFrame());

        if (!southDoor && !lastRoom && Random.Range(1, 100) % 2 == 0)
        {
            // Yes we want a top door
            southDoor = true;
        }

        // Left door check
        yield return(new WaitForEndOfFrame());

        if (!westDoor && !lastRoom && Random.Range(1, 100) % 2 == 0)
        {
            // Yes we want a top door
            westDoor = true;
        }

        // Make sure we get the minimum amount of rooms
        if (!northDoor && !eastDoor && !southDoor && !westDoor && rooms.Count < minRooms)
        {
            switch (Random.Range(1, 4))
            {
            case 1:
                northDoor = true;
                break;

            case 2:
                eastDoor = true;
                break;

            case 3:
                southDoor = true;
                break;

            case 4:
                westDoor = true;
                break;
            }
        }

        // Generate room
        for (int x = 0; x < sizeX; x++)
        {
            for (int y = 0; y < sizeY; y++)
            {
                // Create left wall
                if (x == 0 && leftWalls.Length > 0)
                {
                    GameObject wall = leftWalls[Random.Range(0, leftWalls.Length)];
                    Instantiate(wall, new Vector2(x - 1, y), Quaternion.identity, room.walls.transform);
                }

                // Create right wall
                if (x == (sizeX - 1) && rightWalls.Length > 0)
                {
                    GameObject wall = rightWalls[Random.Range(0, rightWalls.Length)];
                    Instantiate(wall, new Vector2(x + 1, y), Quaternion.identity, room.walls.transform);
                }

                // Create bottom wall
                if (y == 0 && bottomWalls.Length > 0)
                {
                    GameObject wall = bottomWalls[Random.Range(0, bottomWalls.Length)];
                    Instantiate(wall, new Vector2(x, y - 1), Quaternion.identity, room.walls.transform);
                }

                // Create top wall
                if (y == (sizeY - 1) && topWalls.Length > 0)
                {
                    GameObject wall = topWalls[Random.Range(0, topWalls.Length)];
                    Instantiate(wall, new Vector2(x, y + 1), Quaternion.identity, room.walls.transform);
                }

                // Create bottom left corner
                if (x == 0 && y == 0 && cornerBottomLeft)
                {
                    Instantiate(cornerBottomLeft, new Vector2(x - 1, y - 1), Quaternion.identity, room.walls.transform);
                }

                // Create bottom right corner
                if (x == (sizeX - 1) && y == 0 && cornerBottomRight)
                {
                    Instantiate(cornerBottomRight, new Vector2(x + 1, y - 1), Quaternion.identity, room.walls.transform);
                }

                // Create top left corner
                if (x == 0 && y == (sizeY - 1) && cornerTopLeft)
                {
                    Instantiate(cornerTopLeft, new Vector2(x - 1, y + 1), Quaternion.identity, room.walls.transform);
                }

                // Create top right corner
                if (x == (sizeX - 1) && y == (sizeY - 1) && cornerTopRight)
                {
                    Instantiate(cornerTopRight, new Vector2(x + 1, y + 1), Quaternion.identity, room.walls.transform);
                }

                // Pick random tile
                GameObject tile = floorTiles[Random.Range(0, floorTiles.Length)];

                // Create floor tile
                Instantiate(tile, new Vector2(x, y), Quaternion.identity, room.floor.transform);
            }
        }

        // Check if it is the start room
        if (mainRoom)
        {
            // Set camera focus on this room
            camera.target = room.transform;

            // Set offset to focus on the middle of the room
            camera.offset = new Vector2(sizeX / 2, sizeY / 2);

            camera.GetComponent <Camera>().orthographicSize = sizeY / 2 + 2;

            // Set current room
            currentRoom = room;
        }
        else
        {
            // Firstly disable room since we'll start in the main room
            roomGo.SetActive(false);
        }

        // Generate sub rooms
        if (!lastRoom)
        {
            if (northDoor)
            {
                Vector2 loc = new Vector2(virtualLoc.x, virtualLoc.y + 1);

                if (!rooms.ContainsKey(loc))
                {
                    yield return(StartCoroutine(GenerateRoom(loc)));
                }
            }

            if (eastDoor)
            {
                Vector2 loc = new Vector2(virtualLoc.x + 1, virtualLoc.y);

                if (!rooms.ContainsKey(loc))
                {
                    yield return(StartCoroutine(GenerateRoom(loc)));
                }
            }

            if (southDoor)
            {
                Vector2 loc = new Vector2(virtualLoc.x, virtualLoc.y - 1);

                if (!rooms.ContainsKey(loc))
                {
                    yield return(StartCoroutine(GenerateRoom(loc)));
                }
            }

            if (westDoor)
            {
                Vector2 loc = new Vector2(virtualLoc.x - 1, virtualLoc.y);

                if (!rooms.ContainsKey(loc))
                {
                    yield return(StartCoroutine(GenerateRoom(loc)));
                }
            }
        }
    }