Esempio n. 1
0
        public Result Run(LevelDescriptionGrid2D levelDescription)
        {
            var result  = new Result();
            var mapping = levelDescription.GetPrefabToRoomTemplateMapping();
            var problematicRoomTemplates = new Dictionary <GameObject, int>();

            foreach (var pair in mapping)
            {
                var roomTemplate = pair.Value;
                var gameObject   = pair.Key;

                var individualResult = RoomTemplateDiagnostics.CheckWrongManualDoors(roomTemplate.Outline, roomTemplate.Doors, out var differentLengthsCount);
                if (individualResult.HasErrors)
                {
                    problematicRoomTemplates.Add(gameObject, differentLengthsCount);
                }
            }

            if (problematicRoomTemplates.Count != 0)
            {
                result.IsPotentialProblem       = true;
                result.ProblematicRoomTemplates = problematicRoomTemplates;
                var sb = new StringBuilder();

                sb.AppendLine($"There are room templates with a high probability of having an incorrect setup of manual doors.");
                sb.AppendLine($"The problematic room templates are:");

                foreach (var pair in problematicRoomTemplates)
                {
                    sb.AppendLine($"- \"{pair.Key.name}\" with {pair.Value} different lengths of doors");
                }

                sb.AppendLine($"Please go through these room templates and check that their setup is correct.");

                result.Summary = sb.ToString();
            }
            else
            {
                result.IsPotentialProblem = false;
            }

            return(result);
        }
        public Result Run(LevelDescriptionGrid2D levelDescription)
        {
            var roomTemplates = levelDescription.GetPrefabToRoomTemplateMapping();
            var doorLengths   = new Dictionary <int, List <GameObject> >();

            foreach (var pair in roomTemplates)
            {
                var gameObject   = pair.Key;
                var roomTemplate = pair.Value;

                foreach (var doorLength in GetDoorLengths(roomTemplate))
                {
                    if (!doorLengths.ContainsKey(doorLength))
                    {
                        doorLengths[doorLength] = new List <GameObject>();
                    }

                    doorLengths[doorLength].Add(gameObject);
                }
            }

            var result = new Result();

            result.DoorLengths = doorLengths;

            if (doorLengths.Count == 1)
            {
                var doorLength = doorLengths.Keys.First();
                result.IsPotentialProblem = false;
                result.Summary            = $"All doors have the same length ({doorLength}). This is the recommended setup.";
            }
            else
            {
                var sb = new StringBuilder();
                sb.AppendLine("There are room templates with different lengths of doors:");

                foreach (var pair in doorLengths.OrderBy(x => x.Key))
                {
                    var doorLength            = pair.Key;
                    var affectedRoomTemplates = pair.Value;
                    var roomTemplatesExample  = string.Join(", ", affectedRoomTemplates.Take(3).Select(x => $"\"{x.name}\""));
                    sb.AppendLine($"- Door length {doorLength} (room template{(affectedRoomTemplates.Count > 1 ? "s" : "")} {roomTemplatesExample}{(affectedRoomTemplates.Count > 3 ? "..." : "")})");
                }

                if (doorLengths.Count == 2)
                {
                    sb.Append($"This can be completely fine if you know what you are doing.");
                }
                else
                {
                    sb.Append($"Having more than 2 different lengths of doors looks very suspicious.");
                }

                sb.AppendLine($"If it was not intentional to have doors of different lengths, go through individual room templates and check their door lengths.");
                sb.Append($"While doing so, give more attention to room templates with the manual door mode.");

                result.IsPotentialProblem = true;
                result.Summary            = sb.ToString();
            }

            return(result);
        }
Esempio n. 3
0
        public static DungeonGeneratorLevelGrid2D TransformLayout(LayoutGrid2D <RoomBase> layout, LevelDescriptionGrid2D levelDescription, GameObject rootGameObject)
        {
            // var layoutCenter = GetLayoutCenter(layout);
            var prefabToRoomTemplateMapping = levelDescription.GetPrefabToRoomTemplateMapping();
            var corridorToConnectionMapping = levelDescription.GetCorridorToConnectionMapping();

            // Prepare an object to hold instantiated room templates
            var roomTemplateInstancesRoot = new GameObject(GeneratorConstantsGrid2D.RoomsRootName);

            roomTemplateInstancesRoot.transform.parent = rootGameObject.transform;

            // Initialize rooms
            var layoutData  = new Dictionary <RoomBase, RoomInstanceGrid2D>();
            var layoutRooms = layout.Rooms.ToDictionary(x => x.Room, x => x);

            foreach (var layoutRoom in layoutRooms.Values)
            {
                var roomTemplatePrefab = prefabToRoomTemplateMapping.GetByValue(layoutRoom.RoomTemplate);

                // Instantiate room template
                var roomTemplateInstance = Object.Instantiate(roomTemplatePrefab);
                roomTemplateInstance.transform.SetParent(roomTemplateInstancesRoot.transform);
                roomTemplateInstance.name = $"{layoutRoom.Room.GetDisplayName()} - {roomTemplatePrefab.name}";

                // Compute correct room position
                var position = layoutRoom.Position.ToUnityIntVector3();
                roomTemplateInstance.transform.position = position;

                // Correct the position based on the grid
                // This is important when there is some cell spacing or when the level is isometric
                var tilemapsHolder = roomTemplateInstance.transform.Find(GeneratorConstantsGrid2D.TilemapsRootName).gameObject;
                if (tilemapsHolder != null)
                {
                    var grid = tilemapsHolder.GetComponent <Grid>();
                    roomTemplateInstance.transform.position = grid.CellToLocal(position);
                }

                // Compute outline polygon
                var polygon = new Polygon2D(layoutRoom.Outline + layoutRoom.Position);

                var connection   = layoutRoom.IsCorridor ? corridorToConnectionMapping[layoutRoom.Room] : null;
                var roomInstance = new RoomInstanceGrid2D(layoutRoom.Room, layoutRoom.IsCorridor, connection, roomTemplatePrefab, roomTemplateInstance, position, polygon);

                // Add room info to the GameObject
                var roomInfo = roomTemplateInstance.GetComponent <RoomInfoGrid2D>();

                if (roomInfo != null)
                {
                    PostProcessUtilsGrid2D.Destroy(roomInfo);
                }

                roomInfo = roomTemplateInstance.AddComponent <RoomInfoGrid2D>();
                roomInfo.RoomInstance = roomInstance;

                layoutData.Add(layoutRoom.Room, roomInstance);
            }

            foreach (var roomInstance in layoutData.Values)
            {
                roomInstance.SetDoors(TransformDoorInfo(layoutRooms[roomInstance.Room].Doors, layoutData));
            }

            // Add level info
            var levelInfo = rootGameObject.GetComponent <LevelInfoGrid2D>();

            if (levelInfo != null)
            {
                PostProcessUtilsGrid2D.Destroy(levelInfo);
            }

            levelInfo = rootGameObject.AddComponent <LevelInfoGrid2D>();
            levelInfo.RoomInstances = layoutData.Values.ToList();

            return(new DungeonGeneratorLevelGrid2D(layoutData, layout, rootGameObject, levelDescription));
        }