Ejemplo n.º 1
0
    // Use this for initialization
    private void Start()
    {
        //initialize multidimensional array

        // grid cords for starting room & new initialized rooms
        int x = 5;
        int y = 5;

        // How many rooms there should be.
        int totalRooms = 10;

        // initialize the list of rooms with a single parent room
        rooms = new List <RoomClass> ();
        rooms.Add(new RoomClass(0, null, x, y));        // ###################### initializing first room with overloaded method

        // randomly add 10 connected rooms to the floor. Is increased by one when a room overlaps so there will be (10) rooms
        for (int i = 1; i < totalRooms; i++)
        {
            bool overlap = false;

            // get the old node and the new node we will be linking to it
            RoomClass newParent = GetRandomRoomWithFreeNeighbors();
            RoomClass newRoom   = new RoomClass(i, newParent);

            // get the directions so we can bind the rooms both ways
            Direction direction = newParent.GetRandomFreeDirection();
            Direction oppositeDirection;
            if (direction == Direction.North)
            {
                oppositeDirection = Direction.South;
            }
            else if (direction == Direction.South)
            {
                oppositeDirection = Direction.North;
            }
            else if (direction == Direction.West)
            {
                oppositeDirection = Direction.East;
            }
            else
            {
                oppositeDirection = Direction.West;
            }

            // Sets newRoom's x & y to parents
            newRoom.x = newParent.x;
            newRoom.y = newParent.y;

            // Checks which direction the newRoom is compared to its parent and changes x or y accordingly.
            if (oppositeDirection == Direction.South)
            {
                newRoom.x++;
            }
            else if (oppositeDirection == Direction.North)
            {
                newRoom.x--;
            }
            else if (oppositeDirection == Direction.East)
            {
                newRoom.y++;
            }
            else if (oppositeDirection == Direction.West)
            {
                newRoom.y--;
            }

            //print("PARENT ID: " +newParent.number + "  x: " + newParent.x + "  y: " + newParent.y);
            //print("ROOM ID: " + newRoom.number + "   x: " + newRoom.x + "  y: " + newRoom.y + "  # of Rooms: " + rooms.Count);

            // Loops through current rooms in the list and checks if newRoom x&y match any other rooms
            for (int j = 1; j < rooms.Count; j++)
            {
                // If there is a match break out of loop and set overlap to true
                if (newRoom.x == rooms[j].x && newRoom.y == rooms[j].y)
                {
                    overlap = true;
                    print("OVERLAP: ID: " + newRoom.number + " newRoom.x: " + newRoom.x + "  newRoom.y: " + newRoom.y + " rooms[" + j + "] ID: " + rooms[j].number + " room[" + j + "].x: " + rooms[j].x + "  room[" + j + "].y: " + rooms[j].y);
                    totalRooms++;
                    break;
                }
            }


            // If not true then the newRoom is not overlaping another room so its ok to use
            if (!overlap)
            {
                // link the two nodes both ways
                newParent.neighbors[direction]       = newRoom;
                newRoom.neighbors[oppositeDirection] = newParent;


                // add the new room to the list so we can search again
                rooms.Add(newRoom);
            }
        }

        // test that this worked
        foreach (RoomClass r in rooms)
        {
            Debug.Log(r.ToString());
        }
    }