private RoomPositionInfo GetSpawnLocationFromConnection(Room lastRoom, Room.RoomConnection connection, Room nextRoom) { Vector2 result = lastRoom.transform.position; Vector2 mapPos = lastRoom.mapPosition; RoomPositionInfo roomPosInfo = new RoomPositionInfo(); switch (connection) { case Room.RoomConnection.Top: result += new Vector2(0f, lastRoom.roomSize.y) + nextRoom.roomOffset - lastRoom.roomOffset; mapPos += new Vector2(0f, 60f); break; case Room.RoomConnection.Bottom: result -= new Vector2(0f, lastRoom.roomSize.y) - nextRoom.roomOffset + lastRoom.roomOffset; mapPos -= new Vector2(0f, 60f); break; case Room.RoomConnection.Left: result -= new Vector2(lastRoom.roomSize.x, 0f) - nextRoom.roomOffset + lastRoom.roomOffset; mapPos -= new Vector2(60f, 0f); break; case Room.RoomConnection.Right: result += new Vector2(lastRoom.roomSize.x, 0f) + nextRoom.roomOffset - lastRoom.roomOffset; mapPos += new Vector2(60f, 0f); break; } for (int i = 0; i < activeRooms.Count; i++) { if (activeRooms[i].IsPointInRoom(result) || activeRooms[i].DoesRoomOverlap(nextRoom.roomSize, result)) { //Debug.Log(lastRoom.gameObject.name + " is inside " + activeRooms[i].gameObject.name); roomPosInfo.ResetPos(); //return Vector2.zero; return(roomPosInfo); } } roomPosInfo.spawnPosition = result; roomPosInfo.mapPosition = mapPos; return(roomPosInfo); }
private void AddRoom(IZone zone, IRoom room, int x, int y, int z) { roomStack.Push(new RoomPositionInfo(room, x, y, z)); while (roomStack.Count != 0) { RoomPositionInfo roomPositionInfo = roomStack.Pop(); room = roomPositionInfo.Room; x = roomPositionInfo.X; y = roomPositionInfo.Y; z = roomPositionInfo.Z; if (Grid.ContainsKey(room)) { //return; } else { MapRoom mapRoom = new MapRoom(zone, room, new Position(x, y, z)); Grid.Add(room, mapRoom); hashPositions.Add(mapRoom.Position.ToString()); } if (room.North != null) { IExit exit = room.North; if (exit.Zone == room.Zone) { IRoom newRoom = zone.Rooms[exit.Room]; if (!Grid.ContainsKey(newRoom)) { Position currentPosition = Grid[room].Position; while (CheckPosition(currentPosition.X, currentPosition.Y + 1, currentPosition.Z)) { MigrateSouth(currentPosition.Y); } roomStack.Push(new RoomPositionInfo(newRoom, currentPosition.X, currentPosition.Y + 1, currentPosition.Z)); } } } if (room.East != null) { IExit exit = room.East; if (exit.Zone == room.Zone) { IRoom newRoom = zone.Rooms[exit.Room]; if (!Grid.ContainsKey(newRoom)) { Position currentPosition = Grid[room].Position; while (CheckPosition(currentPosition.X + 1, currentPosition.Y, currentPosition.Z)) { MigrateWest(currentPosition.X); } roomStack.Push(new RoomPositionInfo(newRoom, currentPosition.X + 1, currentPosition.Y, currentPosition.Z)); } } } if (room.South != null) { IExit exit = room.South; if (exit.Zone == room.Zone) { IRoom newRoom = zone.Rooms[exit.Room]; if (!Grid.ContainsKey(newRoom)) { Position currentPosition = Grid[room].Position; while (CheckPosition(currentPosition.X, currentPosition.Y - 1, currentPosition.Z)) { MigrateNorth(currentPosition.Y); } roomStack.Push(new RoomPositionInfo(newRoom, currentPosition.X, currentPosition.Y - 1, currentPosition.Z)); } } } if (room.West != null) { IExit exit = room.West; if (exit.Zone == room.Zone) { IRoom newRoom = zone.Rooms[exit.Room]; if (!Grid.ContainsKey(newRoom)) { Position currentPosition = Grid[room].Position; while (CheckPosition(currentPosition.X - 1, currentPosition.Y, currentPosition.Z)) { MigrateEast(currentPosition.X); } roomStack.Push(new RoomPositionInfo(newRoom, currentPosition.X - 1, currentPosition.Y, currentPosition.Z)); } } } if (room.Up != null) { IExit exit = room.Up; if (exit.Zone == room.Zone) { IRoom newRoom = zone.Rooms[exit.Room]; if (!Grid.ContainsKey(newRoom)) { Position currentPosition = Grid[room].Position; while (CheckPosition(currentPosition.X, currentPosition.Y, currentPosition.Z + 1)) { MigrateDown(currentPosition.Z); } roomStack.Push(new RoomPositionInfo(newRoom, currentPosition.X, currentPosition.Y, currentPosition.Z + 1)); } } } if (room.Down != null) { IExit exit = room.Down; if (exit.Zone == room.Zone) { IRoom newRoom = zone.Rooms[exit.Room]; if (!Grid.ContainsKey(newRoom)) { Position currentPosition = Grid[room].Position; while (CheckPosition(currentPosition.X, currentPosition.Y, currentPosition.Z - 1)) { MigrateUp(currentPosition.Z); } roomStack.Push(new RoomPositionInfo(newRoom, currentPosition.X, currentPosition.Y, currentPosition.Z - 1)); } } } } }
public void CreateRoom(Room lastRoom = null, int roomsToMake = 10) { Vector2 spawnLocation = Vector2.zero; Vector2 mapLocation = Vector2.zero; GameObject newRoomGameObject; Room newRoom = null; //Debug.Log("Making " + roomsToMake + " rooms"); if (lastRoom == null) { spawnLocation = Vector2.zero; newRoomGameObject = Instantiate(roomCollection.startRooms[GetRandomRoomIndex(roomCollection.startRooms)].gameObject, spawnLocation, Quaternion.identity) as GameObject; string numberName1 = newRoomGameObject.name + " " + activeRooms.Count; newRoomGameObject.name = numberName1; newRoom = newRoomGameObject.GetComponent <Room>(); newRoom.mapPosition = mapLocation; newRoom.roomType = Room.RoomType.Start; newRoom.Initialize(); activeRooms.Add(newRoom); if (roomsToMake > 0) { CreateRoom(newRoom, roomsToMake - 1); } return; } Room.RoomConnection targetConnection = lastRoom.GetRandomFreeConnection(); Room.RoomConnection oppositeConnection = GetOppositeConnection(targetConnection); if (targetConnection == Room.RoomConnection.None) { //Debug.Log(lastRoom.gameObject.name + " has no free connections"); //Debug.Log("Restarting with " + roomsToMake + " ropms"); RestartRoomCreation(GetRandomRoomWithFreeConnections(), roomsToMake); return; } Room nextRoom = GetRandomRoom(oppositeConnection); RoomPositionInfo roomPosInfo = GetSpawnLocationFromConnection(lastRoom, targetConnection, nextRoom); spawnLocation = roomPosInfo.spawnPosition; mapLocation = roomPosInfo.mapPosition; if (spawnLocation == Vector2.zero) { //Debug.Log("Map ran into itself"); //Debug.Log("Restarting with " + roomsToMake + " ropms"); RestartRoomCreation(GetRandomRoomWithFreeConnections(), roomsToMake); return; } newRoomGameObject = Instantiate(nextRoom.gameObject, spawnLocation, Quaternion.identity) as GameObject; string numberName = newRoomGameObject.name + " " + activeRooms.Count; newRoomGameObject.name = numberName; newRoom = newRoomGameObject.GetComponent <Room>(); newRoom.mapPosition = mapLocation; newRoom.occupiedConnections.Add(oppositeConnection); lastRoom.occupiedConnections.Add(targetConnection); newRoom.Initialize(); activeRooms.Add(newRoom); if (roomsToMake > 0) { //Debug.Log(roomsToMake + " more rooms to make"); CreateRoom(newRoom, roomsToMake - 1); } }