//Each frame attempts to place a new room
    //calls the checkbounds function, and if successful
    //runs the AddRoom script
    IEnumerator PlaceRooms()
    {
        //While we can fit more rooms, and want more rooms
        while (unusedDoors.Count > 0 && roomCount < dS.maxRooms)
        {
            DoorConnection doorConnection  = null;
            Vector3        newRoomLocation = Vector3.zero;
            Vector3        newRoomRotation = Vector3.zero;
            int            linkIndex       = rng.Next(0, unusedDoors.Count);
            Transform      linkToAttach    = unusedDoors[linkIndex].door;                   //pick a random doorway
            GameObject     newRoom         = dS.roomTypes[rng.Next(0, dS.roomTypes.Count)]; //picks a random room prefab

            if (CheckBounds(newRoom, linkToAttach, out newRoomLocation, out newRoomRotation, out doorConnection))
            {
                yield return(AddRoom(unusedDoors[linkIndex], doorConnection, newRoomLocation, Quaternion.Euler(newRoomRotation)));
            }


            yield return(null);
        }
        //seal off the remaining doorways
        //before this need to check if rooms are connected but their doorways are considered not connected
        yield return(null);

        StartCoroutine(PlaceDeadEnd());
        yield return(null);
    }
    //newRoom is the prefab of the room to try to place, previousDoor is the global position of the door it is attaching to
    bool CheckBounds(GameObject newRoom, Transform previousDoor, out Vector3 roomLocation, out Vector3 roomRotation, out DoorConnection doorConnection)
    {
        //make it null so no errors (yay)
        doorConnection = null;

        //get the room script corresponding to the prefab (gives the doorways)
        Room newRoomscript = newRoom.GetComponent(typeof(Room)) as Room;

        roomLocation = previousDoor.position;

        roomRotation = previousDoor.rotation.eulerAngles + new Vector3(0, 180, 0);


        List <Transform> Doors = new List <Transform>(newRoomscript.Doorways);

        //create a list of indices
        List <int> indexList = new List <int>();

        for (int i = 0; i < Doors.Count; i++)
        {
            indexList.Add(i);
        }
        while (indexList.Count > 0)
        {
            int       index = rng.Next(0, indexList.Count);
            Transform door  = Doors[indexList[index]];
            roomRotation -= door.localRotation.eulerAngles;

            Vector3 doorEffectiveLocalPosition = Quaternion.Euler(roomRotation) * door.localPosition;

            roomLocation -= doorEffectiveLocalPosition;
            //If the room does not collide with another room
            if (newRoomscript.NoCollisionAbstract(roomLocation, roomRotation, dS.collisionTolerance))
            {
                doorConnection = new DoorConnection(door, newRoom, indexList[index]);
                return(true);
            }

            roomLocation += doorEffectiveLocalPosition;

            roomRotation += door.localRotation.eulerAngles;


            indexList.RemoveAt(index);
        }
        return(false);
    }
    IEnumerator AddRoom(DoorConnection prevDoorConnection, DoorConnection doorConnection, Vector3 position, Quaternion rotation)
    {
        //prevDoorConnection - previous (room + door used + index)
        //doorConnection - this (room + door used + index)
        GameObject newRoom        = Instantiate(doorConnection.room, position, rotation, dungeonParent.transform);
        Room       newRoomScript  = newRoom.GetComponent <Room>();
        Room       prevRoomScript = prevDoorConnection.room.GetComponent <Room>();

        newRoomScript.connectedRooms.Add(prevDoorConnection.room);
        prevRoomScript.connectedRooms.Add(doorConnection.room);
        unusedDoors.Remove(prevDoorConnection);
        //give the doorconnection the real values rather than prefabs
        doorConnection.door = newRoomScript.Doorways[doorConnection.doorIndex];
        doorConnection.room = newRoom;
        instantiatedRoomObjects.Add(newRoom);

        //instance the room
        //get references
        //add doorways to list
        for (int i = 0; i < newRoomScript.Doorways.Length; ++i)
        {
            //if this is the door we used
            if (i == doorConnection.doorIndex)
            {
                GameObject newDoor = Instantiate(dS.door, doorConnection.door);
                newDoor.layer = LayerMask.NameToLayer("Doors");
            }
            else
            {
                unusedDoors.Add(new DoorConnection(newRoomScript.Doorways[i], newRoom, i));
            }
            yield return(null);
        }
        //increment roomcount


        ++roomCount;
        yield return(null);
    }
Beispiel #4
0
        void read(Field f)
        {
            BinaryReader br = f.getData();

            uint unknown = br.ReadUInt32();    // unknown

            uint loc_marker = br.ReadUInt32(); // loc-marker

            world_formid = br.ReadUInt32();

            if (world_formid != 0)
            {
                x = br.ReadInt16();
                y = br.ReadInt16();
            }
            else
            {
                cell_formid = br.ReadUInt32();
            }

            uint nverts = br.ReadUInt32();

            for (int i = 0; i < nverts; i++)
            {
                Vertex v = new Vertex(br.ReadSingle(), br.ReadSingle(), br.ReadSingle());
                vertices.Add(v);
            }

            uint ntris = br.ReadUInt32();

            for (int i = 0; i < ntris; i++)
            {
                Triangle t = new Triangle();
                t.vert1 = br.ReadUInt16();
                t.vert2 = br.ReadUInt16();
                t.vert3 = br.ReadUInt16();

                t.neigh1 = br.ReadUInt16();
                t.neigh2 = br.ReadUInt16();
                t.neigh3 = br.ReadUInt16();

                t.cover_marker = br.ReadUInt16();
                t.cover_flag   = br.ReadUInt16();

                triangles.Add(t);
            }

            uint nconns = br.ReadUInt32();

            for (int i = 0; i < nconns; i++)
            {
                ExternalConnection ec = new ExternalConnection();
                ec.unknown = br.ReadUInt32();
                ec.formid  = br.ReadUInt32();
                ec.tri     = br.ReadUInt16();
            }

            uint ndoors = br.ReadUInt32();

            for (int i = 0; i < ndoors; i++)
            {
                DoorConnection d = new DoorConnection();

                d.tri     = br.ReadUInt16();
                d.unknown = br.ReadUInt32();
                d.door    = br.ReadUInt32();
            }
            uint nctris = br.ReadUInt32();

            for (int i = 0; i < nctris; i++)
            {
                cover_tris.Add(br.ReadUInt16());
            }

            divisor = br.ReadUInt32();

            maxXD = br.ReadSingle();
            maxYD = br.ReadSingle();

            minx = br.ReadSingle();
            miny = br.ReadSingle();
            minz = br.ReadSingle();

            maxx = br.ReadSingle();
            maxy = br.ReadSingle();
            maxz = br.ReadSingle();

            uint num_segments = divisor * divisor;

            for (int i = 0; i < num_segments; i++)
            {
                Segment sg = new Segment();
                sg.length = br.ReadUInt32();

                for (int j = 0; j < sg.length; j++)
                {
                    sg.tris.Add(br.ReadUInt16());
                }

                segments.Add(sg);
            }


            int remaining_data = (int)(br.BaseStream.Length - br.BaseStream.Position);

            if (remaining_data != 0)
            {
                Log.error("NVNM ending error.");
            }
        }
    //Display Doors on Room
    void DisplayDoors(int room)
    {
        //Room that doors are drawing in
        Room thisRoom = rooms[room];

        //Position Values of Door
        float middleX = 0, middleY = 0;       //Middle of Room
        float halfSizeX = 0, halfSizeY = 0;   //HalfSize of Room
        float multiDoorX = 0, multiDoorY = 0; //Position if there are multiple doors on a side

        //Middle positions in half room size
        halfSizeX = (defaultRoomX * zVal / 2) - (defaultDoorX * zVal / 2);
        halfSizeY = (defaultRoomY * zVal / 2) - (defaultDoorY * zVal / 2);

        //Count Vars for doors on sides
        int leftDoorCount = 0, rightDoorCount = 0, downDoorCount = 0, upDoorCount = 0;

        //For Each door in the room
        for (int i = 0; i < thisRoom.doors.Count; i++)
        {
            //Middle position plus the position of the room
            middleX = halfSizeX + (thisRoom.pos.x * zVal) + xVal + mapOrigin.x;
            middleY = halfSizeY + (thisRoom.pos.y * zVal) + yVal + mapOrigin.y;

            //new Var For door
            Rect door = new Rect();

            //Which Side is the Door on
            switch (thisRoom.doors[i].doorSide)
            {
            case Door.side.LEFT:
            {
                //Increment Count
                leftDoorCount++;
                //Check for All left side doors and move according to the position of them
                multiDoorY = (defaultRoomY * zVal * ((float)leftDoorCount / ((float)thisRoom.leftDoors + 1))) - (defaultDoorY * zVal / 2);
                //If door side count is 1 then the position will be in middle
                middleY = multiDoorY + (thisRoom.pos.y * zVal) + yVal + mapOrigin.y;

                //Set Door Var
                door = new Rect(middleX - halfSizeX, middleY, defaultDoorX * zVal, defaultDoorY * zVal);

                break;
            }

            case Door.side.RIGHT:
            {
                //Increment Count
                rightDoorCount++;
                //Check for All right side doors and move according to the position of them
                multiDoorY = (defaultRoomY * zVal * ((float)rightDoorCount / ((float)thisRoom.rightDoors + 1))) - (defaultDoorY * zVal / 2);
                //If door side count is 1 then the position will be in middle
                middleY = multiDoorY + (thisRoom.pos.y * zVal) + yVal + mapOrigin.y;

                //Set Door Var
                door = new Rect(middleX + halfSizeX, middleY, defaultDoorX * zVal, defaultDoorY * zVal);

                break;
            }

            case Door.side.DOWN:

            {
                //Increment Count
                downDoorCount++;
                //Check for All down side doors and move according to the position of them
                multiDoorX = (defaultRoomX * zVal * ((float)downDoorCount / ((float)thisRoom.downDoors + 1))) - (defaultDoorX * zVal / 2);
                //If door side count is 1 then the position will be in middle
                middleX = multiDoorX + (thisRoom.pos.x * zVal) + xVal + mapOrigin.x;


                //Set Door Var
                door = new Rect(middleX, middleY + halfSizeY, defaultDoorX * zVal, defaultDoorY * zVal);

                break;
            }

            case Door.side.UP:
            {
                //Increment Count
                upDoorCount++;
                //Check for All down side doors and move according to the position of them
                multiDoorX = (defaultRoomX * zVal * ((float)upDoorCount / ((float)thisRoom.upDoors + 1))) - (defaultDoorX * zVal / 2);
                //If door side count is 1 then the position will be in middle
                middleX = multiDoorX + (thisRoom.pos.x * zVal) + xVal + mapOrigin.x;


                //Set Door Var
                door = new Rect(middleX, middleY - halfSizeY, defaultDoorX * zVal, defaultDoorY * zVal);
                break;
            }

            default:
                break;
            }

            //Draw new Room
            EditorGUI.DrawRect(door, doorColor);

            //Set Door position
            thisRoom.doors[i].SetPos(door.center);

            Event current = Event.current;

            //Check if the user is in the Create conncection state and trying to make a connection with the door with left click
            if (door.Contains(current.mousePosition) && connectingDoors && current.button == 0 && current.isMouse && firstDoorSelected != null && thisRoom.doors[i] != firstDoorSelected && thisRoom.doors[i].connection == null && thisRoom != rooms[firstDoorSelected.roomNum])
            {
                //Create Connection
                DoorConnection nConnection = new DoorConnection();
                //Set This door to the current door selected
                nConnection.thisDoor = firstDoorSelected;
                //Set connected door to the room and door at [i]
                nConnection.connection = thisRoom.doors[i];

                //Create the reverse connection
                DoorConnection nConnectionReverse = new DoorConnection();
                nConnectionReverse.connection = firstDoorSelected;
                nConnectionReverse.thisDoor   = thisRoom.doors[i];

                //Set connections to rooms
                thisRoom.doors[i].connection = nConnectionReverse;
                firstDoorSelected.connection = nConnection;

                //Add connection to draw list
                connections.Add(nConnection);

                //remove current values selected
                connectingDoors   = false;
                firstDoorSelected = null;
            }

            //Right click on Door
            if (door.Contains(current.mousePosition) && current.type == EventType.ContextClick)
            {
                //Set mouse position
                clickPos = current.mousePosition;
                //Current door Selected
                doorRightClicked = i;
                //Create Menu
                GenericMenu menu = new GenericMenu();

                //What is selected
                menu.AddDisabledItem(new GUIContent("Door"));

                //Delete Door
                menu.AddItem(new GUIContent("Remove Door"), false, RemoveDoor, thisRoom.doors[i]);

                //Check if there isn't connection to display an option to create one
                if (thisRoom.doors[i].connection == null)
                {
                    menu.AddItem(new GUIContent("Connect Room"), false, ConnectDoor, thisRoom.doors[i]);
                }

                //Check if there is a connection to display an option to delete one
                if (thisRoom.doors[i].connection != null)
                {
                    menu.AddItem(new GUIContent("Remove Connection"), false, RemoveConnection, thisRoom.doors[i]);
                }
                menu.ShowAsContext();
            }
        }
    }
    //Load previously created rooms on start
    void LoadOnStart()
    {
        //Find all asset paths that already exist
        string[] levelNames = AssetDatabase.FindAssets("l:LevelObj", new[] { "Assets/Levels/Level Objects" });

        //Temporary list of asset objects
        List <LevelObject> tempList = new List <LevelObject>();

        //loop through found assets
        for (int i = 0; i < levelNames.Length; i++)
        {
            //Create new Level object with found path
            LevelObject newObject = (LevelObject)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(levelNames[i]), typeof(LevelObject));

            if (newObject == null) //check if newObject was a levelObject
            {
                continue;
            }

            //Add Object to temp list
            tempList.Add(newObject);

            //Create new room to hold data from asset
            Room newRoom = new Room();
            //set name
            newRoom.name = newObject.name;
            //set position from saved editor position
            newRoom.pos = new Vector2(newObject.worldEditorX, newObject.worldEditorY);
            //Create empty list of doorways
            newRoom.doors = new List <Door>();
            //Set Dictionary ID
            newRoom.roomDictionaryID = currentNewKeyIndex;
            //Reset door values to default
            newRoom.ResetDoors();
            //Add to room dictionary
            rooms.Add(currentNewKeyIndex, newRoom);
            //increment key


            //add doorways
            for (int d = 0; d < newObject.doorways.Count; d++)
            {
                //set room and add door
                roomRightClicked = currentNewKeyIndex;
                //Add door to list
                AddDoor(newObject.doorways[d].side);
                //Create new connection
                DoorConnection newConnection = new DoorConnection();
                //Set this door to connection

                newConnection.thisDoor = rooms[currentNewKeyIndex].doors[d];
                //Set connection to door in list
                rooms[currentNewKeyIndex].doors[d].connection = newConnection;
                //Add door to room foor list
            }
            currentNewKeyIndex++;
        }

        //Create Connections in for loop
        //Increment for templist should be same size as rooms for every asset
        int r = 0;

        //for each room in the newly created dictionary
        foreach (Room room in rooms.Values)
        {
            //if there are doors in the asset add new connection
            if (tempList[r].doorways.Count >= 1)
            {
                Debug.Log("Room " + room.name + " has " + room.doors.Count + " doors");
                //For Every door in room
                for (int d = 0; d < room.doors.Count; d++)
                {
                    if (room.doors[d].connection.connection != null)
                    {
                        continue;
                    }


                    //Find other door with connection
                    Door otherDoor = FindDoorWithConnection(room, tempList[r]);


                    //Continue if other didn't find the other door
                    if (otherDoor == null)
                    {
                        continue;
                    }

                    //Set connected door to other door
                    room.doors[d].connection.connection = otherDoor;
                    //Set other door connection to the current door looped through
                    otherDoor.connection.connection = room.doors[d];
                    //Add connection to list of connections
                    if (!connections.Contains(room.doors[d].connection) || !connections.Contains(otherDoor.connection))
                    {
                        connections.Add(room.doors[d].connection);
                    }
                    //  connections.Add(otherDoor.connection);
                }
            }
            //Increment to next tempList location
            r++;
        }
    }