Example #1
0
    private void createDoor(ref int x, ref int y, int rndX, int rndY, WorldRoom r)
    {
        x = Random.Range(rndX, rndX + r.RoomSizeX + 1);
        y = Random.Range(rndY, rndY + r.RoomSizeY + 1);

        int rnd = Random.Range(0, 4);

        if (rnd == 0)
        {
            x = rndX;
        }
        else if (rnd == 1)
        {
            x = (rndX + r.RoomSizeX) + 1;
        }
        else if (rnd == 2)
        {
            y = rndY;
        }
        else
        {
            y = (rndY + r.RoomSizeY) + 1;
        }

        if ((x == rndX && y == rndY) || (x == rndX && y == (rndY + r.RoomSizeY) + 1) ||
            (x == (rndX + r.RoomSizeX) + 1 && y == rndY) || (x == (rndX + r.RoomSizeX) + 1 && y == (rndY + r.RoomSizeY) + 1))
        {
            if (rnd == 0)
            {
                x++;
            }
            else if (rnd == 1)
            {
                x--;
            }
            else if (rnd == 2)
            {
                y++;
            }
            else
            {
                y--;
            }
        }
    }
Example #2
0
    private bool checkIfCanPlaceRoom(WorldRoom r, int x, int y)
    {
        bool canConstruct = true;

        for (int i = -1; i < r.RoomSizeX + 1; i++)
        {
            for (int j = -1; j < r.RoomSizeY + 1; j++)
            {
                if (x + i > matrixX - 2 || y + j > matrixY - 2 || x + i <= 0 || y + j <= 0 || table[x + i, y + j].occupied)
                {
                    canConstruct = false;
                    break;
                }
            }
        }

        return(canConstruct);
    }
Example #3
0
    private void createCorridors(bool toCenter)
    {
        foreach (Room r in roomManager.GetRooms())
        {
            WorldRoom rRoom = r.GetComponent <WorldRoom>();

            WorldRoom closestOne;

            if (toCenter)
            {
                closestOne = FindClosestRoom(rRoom);
            }
            else
            {
                closestOne = rooms[0];
            }

            if (closestOne)
            {
                var condition = AStar.Instance.Begin(0, closestOne.roomDoorX, closestOne.roomDoorY, rRoom.roomDoorX, rRoom.roomDoorY, table);

                if (condition)
                {
                    List <Node> path = AStar.Instance.getPath();

                    foreach (Node n in path)
                    {
                        //if (!(n.i_ == closestOne.roomDoorX && n.j_ == closestOne.roomDoorY) &&
                        //    !(n.i_ == r.roomDoorX && n.j_ == r.roomDoorY))
                        //{
                        table[n.i_, n.j_].occupied = true;
                        table[n.i_, n.j_].cell     = Cells.Corridor;
                        if (CORRIDORMATERIAL)
                        {
                            table[n.i_, n.j_].mapCell.transform.GetChild(0).GetComponent <Renderer>().material = CORRIDORMATERIAL;
                        }

                        //}
                    }
                }
            }
        }
    }
Example #4
0
    private WorldRoom FindClosestRoom(WorldRoom room)
    {
        float     closestDistance = float.MaxValue;
        WorldRoom closestRoom     = null;

        foreach (WorldRoom r in rooms)
        {
            if (r != room)
            {
                float distance = Vector3.Distance(r.gameObject.transform.position, room.gameObject.transform.position);
                if (distance < closestDistance)
                {
                    closestDistance = distance;
                    closestRoom     = r;
                }
            }
        }

        return(closestRoom);
    }
Example #5
0
    private void createRoom(WorldRoom r)
    {
        int rndX = 0, rndY = 0;

        int maxTries = 50;

        while (maxTries-- >= 0 && !checkIfCanPlaceRoom(r, rndX, rndY))
        {
            randomPosition(ref rndX, ref rndY);
        }

        if (maxTries >= 0)
        {
            for (int i = 0; i < r.RoomSizeX; i++)
            {
                for (int j = 0; j < r.RoomSizeY; j++)
                {
                    table[rndX + i, rndY + j].occupied = true;
                    table[rndX + i, rndY + j].cell     = Cells.Room;
                    if (ROOMMATERIAL)
                    {
                        if (!r.MainRoom)
                        {
                            table[rndX + i, rndY + j].mapCell.transform.GetChild(0).GetComponent <Renderer>().material = ROOMMATERIAL;
                        }
                        else
                        {
                            table[rndX + i, rndY + j].mapCell.transform.GetChild(0).GetComponent <Renderer>().material = DOORMATERIAL;
                        }
                    }
                }
            }

            r.matrixStartX = rndX;
            r.matrixStartY = rndY;

            int x = 0, y = 0;

            createDoor(ref x, ref y, rndX - 1, rndY - 1, r);

            r.roomDoorX = x;
            r.roomDoorY = y;
            table[r.roomDoorX, r.roomDoorY].cell = Cells.Door;

            if (DOORMATERIAL)
            {
                table[r.roomDoorX, r.roomDoorY].mapCell.transform.GetChild(0).GetComponent <Renderer>().material = DOORMATERIAL;
            }

            GameObject roomGo = Instantiate(r.gameObject, new Vector3(rndX, 0, rndY), Quaternion.identity);

            GameObject doorObject = createPhysicalDoor(roomGo, table[r.roomDoorX, r.roomDoorY].mapCell.transform.GetChild(0).gameObject);

            GameObject d = new GameObject();
            d.AddComponent <Door>();
            d.transform.position = doorObject.transform.position;
            d.transform.parent   = roomGo.transform;

            Room roomComponent = roomGo.GetComponent <Room>();
            roomComponent.setDoor(d.GetComponent <Door>());

            if (doorObject.transform.childCount > 0 && doorObject.transform.GetChild(0).GetComponent <HiddingPlace>())
            {
                roomGo.GetComponent <Room>().RemoveHiddingPlace(doorObject.transform.GetChild(0).GetComponent <HiddingPlace>());
            }

            //doorObject.GetComponent<Renderer>().material = DOORMATERIAL;

            DestroyImmediate(doorObject);

            roomGo.transform.parent = roomManager.transform;
            roomComponent.RegisterRoom();
        }

        else
        {
            Debug.Log("Cannot place this room, max tries reached");
        }
    }