Example #1
0
    private Vector2 SurroundingRooms(int x, int z)
    {
        nSizeRoom currentRoom = grid[new Vector2(x, z)];
        //Check West
        Vector2 comp = new Vector2(x - 1, z);

        if (grid.ContainsKey(comp))
        {
            if (grid[comp] != null)
            {
                if (grid[comp].roomId != currentRoom.roomId)
                {
                    return(comp);
                }
            }
        }
        //Check East
        comp = new Vector2(x + 1, z);
        if (grid.ContainsKey(comp))
        {
            if (grid[comp] != null)
            {
                if (grid[comp].roomId != currentRoom.roomId)
                {
                    return(comp);
                }
            }
        }
        //Check North
        comp = new Vector2(x, z + 1);
        if (grid.ContainsKey(comp))
        {
            if (grid[comp] != null)
            {
                if (grid[comp].roomId != currentRoom.roomId)
                {
                    return(comp);
                }
            }
        }
        //Check South
        comp = new Vector2(x, z - 1);
        if (grid.ContainsKey(comp))
        {
            if (grid[comp] != null)
            {
                if (grid[comp].roomId != currentRoom.roomId)
                {
                    return(comp);
                }
            }
        }

        return(Vector2.zero);
    }
Example #2
0
    private nSizeRoom createAtGrid(int x, int z, int sizeX, int sizeZ, GameObject prefab)
    {
        GameObject g = Instantiate(prefab, new Vector3(x * GridSize, 0, z * GridSize), Quaternion.identity, BoHObject.transform);

        g.transform.localScale = new Vector3(GridSize, GridSize, GridSize);
        nSizeRoom roomScript = g.GetComponent <nSizeRoom>();

        roomScript.xCoord = x;
        roomScript.zCoord = z;
        roomScript.roomId = currentRoomNumber;
        roomScript.xSize  = sizeX;
        roomScript.zSize  = sizeZ;
        for (int xInd = 0; xInd < sizeX; xInd++)
        {
            for (int zInd = 0; zInd < sizeZ; zInd++)
            {
                grid.Add(new Vector2((int)x + xInd, (int)z - zInd), roomScript);
            }
        }
        currentRoomNumber++;
        return(roomScript);
    }
Example #3
0
    // Start is called before the first frame update
    void Start()
    {
        BoHObject      = new GameObject();
        BoHObject.name = "BackOfHouse";
        grid           = new Dictionary <Vector2, nSizeRoom>();
        List <nSizeRoom> rooms = new List <nSizeRoom>();

        //for all the defined room prefabs
        foreach (room prefab in roomPrefabs)
        {
            //randomly add rooms until no possible spot exists
            int     failsafe     = 0;
            Vector2 nextPosition = pickRandomEmptySpot(prefab.xLen, prefab.zLen);
            while (nextPosition != new Vector2(-1, -1) && failsafe < 100)
            {
                failsafe++;
                rooms.Add(createAtGrid((int)nextPosition.x, (int)nextPosition.y, prefab.xLen, prefab.zLen, prefab.prefab));
                nextPosition = pickRandomEmptySpot(prefab.xLen, prefab.zLen);
            }
        }

        //fill the remaining empty spaces

        for (int x = 0; x <= xGrid; x++)
        {
            for (int z = 0; z <= zGrid; z++)
            {
                if (!grid.ContainsKey(new Vector2(x, z)))
                {
                    rooms.Add(createAtGrid(x, z, 1, 1, oneByOnePrefab));
                }
            }
        }
        //merge duplicate rooms
        foreach (nSizeRoom room in rooms)
        {
            mergeRooms(room);
        }

        //set room ids to reflect merged rooms
        int newID = 0;

        foreach (Transform child in BoHObject.transform)
        {
            nSizeRoom oldRoom = child.GetComponent <nSizeRoom>();
            if (oldRoom == null)
            {
                nSizeRoom[] childRooms = child.GetComponentsInChildren <nSizeRoom>();
                foreach (nSizeRoom childroom in childRooms)
                {
                    childroom.roomId = newID;
                }
            }
            else
            {
                oldRoom.roomId = newID;
            }
            newID++;
        }

        //make doorways
        for (int x = 0; x <= xGrid; x++)
        {
            for (int z = 0; z <= zGrid; z++)
            {
                if (grid[new Vector2(x, z)] != null)
                {
                    Vector2 roomsDir = SurroundingRooms(x, z);
                    if (roomsDir != Vector2.zero)
                    {
                        nSizeRoom origRoom = grid[new Vector2(x, z)];
                        origRoom.makeDoorwayTo(roomsDir);
                        grid[roomsDir].makeDoorwayTo(new Vector2(x, z));
                        changeRoomIds(grid[roomsDir].roomId, origRoom.roomId);
                    }
                }
            }
        }
    }
Example #4
0
    private void mergeRooms(nSizeRoom room)
    {
        int  x = room.xCoord;
        int  z = room.zCoord;
        bool hasSurrounding = false;
        //Check West
        Vector2 comp = new Vector2(x - 1, z);

        //if there's something in that spot
        if (grid.ContainsKey(comp))
        {
            //and it's not the same room
            if (room.roomId != grid[comp].roomId)
            {
                //and it's the same size as the compared room
                if (room.xSize == grid[comp].xSize && room.zSize == grid[comp].zSize)
                {
                    //and it is directly next to the other room
                    if (x == grid[comp].xCoord || z == grid[comp].zCoord)
                    {
                        //delete the walls and make one big room
                        hasSurrounding = true;
                        room.deleteWest();
                        grid[comp].deleteEast();
                        if (room.gameObject.transform.parent == BoHObject.transform)
                        {
                            GameObject parent = new GameObject();
                            parent.name = "Room " + room.roomId;
                            parent.tag  = "room";
                            room.gameObject.transform.parent = parent.transform;
                            parent.transform.parent          = BoHObject.transform;
                            grid[comp].transform.parent      = parent.transform;
                        }
                        else
                        {
                            grid[comp].transform.parent = room.transform.parent;
                        }
                    }
                }
            }
        }
        //Check East
        comp = new Vector2(x + 1, z);
        if (grid.ContainsKey(comp))
        {
            if (room.roomId != grid[comp].roomId)
            {
                if (room.xSize == grid[comp].xSize && room.zSize == grid[comp].zSize)
                {
                    if (x == grid[comp].xCoord || z == grid[comp].zCoord)
                    {
                        hasSurrounding = true;
                        room.deleteEast();
                        grid[comp].deleteWest();
                        if (room.gameObject.transform.parent == BoHObject.transform)
                        {
                            GameObject parent = new GameObject();
                            parent.name = "Room " + room.roomId;
                            parent.tag  = "room";
                            room.gameObject.transform.parent = parent.transform;
                            parent.transform.parent          = BoHObject.transform;
                            grid[comp].transform.parent      = parent.transform;
                        }
                        else
                        {
                            grid[comp].transform.parent = room.transform.parent;
                        }
                    }
                }
            }
        }
        //Check North
        comp = new Vector2(x, z + 1);
        if (grid.ContainsKey(comp))
        {
            if (room.roomId != grid[comp].roomId)
            {
                if (room.xSize == grid[comp].xSize && room.zSize == grid[comp].zSize)
                {
                    if (room.xCoord == grid[comp].xCoord || room.zCoord == grid[comp].zCoord)
                    {
                        hasSurrounding = true;
                        room.deleteNorth();
                        grid[comp].deleteSouth();
                        if (room.gameObject.transform.parent == BoHObject.transform)
                        {
                            GameObject parent = new GameObject();
                            parent.name = "Room " + room.roomId;
                            parent.tag  = "room";
                            room.gameObject.transform.parent = parent.transform;
                            parent.transform.parent          = BoHObject.transform;
                            grid[comp].transform.parent      = parent.transform;
                        }
                        else
                        {
                            grid[comp].transform.parent = room.transform.parent;
                        }
                    }
                }
            }
        }
        //Check South
        comp = new Vector2(x, z - 1);
        if (grid.ContainsKey(comp))
        {
            if (room.roomId != grid[comp].roomId)
            {
                if (room.xSize == grid[comp].xSize && room.zSize == grid[comp].zSize)
                {
                    if (room.xCoord == grid[comp].xCoord || room.zCoord == grid[comp].zCoord)
                    {
                        hasSurrounding = true;
                        room.deleteSouth();
                        grid[comp].deleteNorth();
                        if (room.gameObject.transform.parent == BoHObject.transform)
                        {
                            GameObject parent = new GameObject();
                            parent.name = "Room " + room.roomId;
                            parent.tag  = "room";
                            room.gameObject.transform.parent = parent.transform;
                            parent.transform.parent          = BoHObject.transform;
                            grid[comp].transform.parent      = parent.transform;
                        }
                        else
                        {
                            grid[comp].transform.parent = room.transform.parent;
                        }
                    }
                }
            }
        }

        //if it has no surroundong rooms, make it it's own room
        if (!hasSurrounding)
        {
            room.gameObject.name  = "Room " + room.roomId;
            room.tag              = "room";
            room.transform.parent = BoHObject.transform;
        }
    }