public static Vector3 BranchRoom(LayoutStructure.Room from, int direction, float distance) { Vector3 offset = Vector3.zero; Vector3 position = Vector3.zero; switch (direction) { case 1: offset.z = from.bounds.extents.z + distance; position = (from.anchorPoint + offset); break; case 2: offset.x = from.bounds.extents.x + distance; position = (from.anchorPoint + offset); break; case 3: offset.z = -from.bounds.extents.z - distance; position = (from.anchorPoint + offset); break; case 4: offset.x = -from.bounds.extents.x - distance; position = (from.anchorPoint + offset); break; } return(position); }
private void GenerateRooms(LayoutStructure layout) { int quantity = Random.Range(roomParameters.quantity.min, roomParameters.quantity.max + 1); int generationAttempts = quantity; Vector3 initialSize = LayoutUtility.RandomRoomSize(roomParameters.width, roomParameters.length); LayoutStructure.Room previousRoom = layout.AddRoom(Vector3.zero, initialSize); int direction = 0; for (int i = 1; i < generationAttempts; i++) { bool generated = false; direction = Random.Range(1, 5); for (int attempts = 0; attempts < 4; attempts++) { int distance = layoutParameters.roomToRoomDistance.RandomInclusive(); Vector3 position = LayoutUtility.BranchRoom(previousRoom, direction, distance); Vector3 size = LayoutUtility.RandomRoomSize(roomParameters.width, roomParameters.length); Bounds bounds = new Bounds(position, size); if (!layout.DoesOverlapAnyRoom(bounds)) { previousRoom = layout.AddRoom(bounds); generated = true; break; } else if (++direction > 4) { direction = 1; } } if (!generated) { generationAttempts++; if (i >= 2) { previousRoom = layout.rooms[Random.Range(0, layout.rooms.Count - 2)]; } else { break; } } } Debug.Log(layout.rooms.Count); }
public override void Preview(LayoutStructure layout, Parameters parameters) { Bounds totalBounds = layout.CalculateTotalBounds(); int textureSize = Mathf.CeilToInt(Mathf.Max(totalBounds.size.x, totalBounds.size.z)); Texture2D texture = new Texture2D(textureSize, textureSize); texture.wrapMode = TextureWrapMode.Clamp; texture.filterMode = FilterMode.Point; for (int i = 0; i < layout.rooms.Count; i++) { LayoutStructure.Room room = layout.rooms[i]; Bounds innerBounds = new Bounds(room.bounds.center, room.bounds.size); innerBounds.Expand(new Vector3(parameters.wallThickness * -2.0f, 0, parameters.wallThickness * -2.0f)); for (float pointX = room.bounds.min.x; pointX <= room.bounds.max.x; pointX += 1.0f) { for (float pointZ = room.bounds.min.z; pointZ <= room.bounds.max.z; pointZ += 1.0f) { if (!innerBounds.Contains(new Vector3(pointX, 0, pointZ))) { int x = Convert3DPointTo2DPixel(pointX, totalBounds.center.x, textureSize); int y = Convert3DPointTo2DPixel(pointZ, totalBounds.center.z, textureSize); texture.SetPixel(x, y, Color.red); } } } int centerX = Convert3DPointTo2DPixel(room.anchorPoint.x, totalBounds.center.x, textureSize); int centerY = Convert3DPointTo2DPixel(room.anchorPoint.z, totalBounds.center.z, textureSize); texture.SetPixel(centerX, centerY, Color.blue); } texture.Apply(); parameters.previewRenderer.material.mainTexture = texture; }